1affae2bfSwdenk /* 2affae2bfSwdenk * (C) Copyright 2001 3affae2bfSwdenk * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4affae2bfSwdenk * 51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 6affae2bfSwdenk */ 7affae2bfSwdenk 8affae2bfSwdenk #include <common.h> 9affae2bfSwdenk #include <command.h> 10affae2bfSwdenk #include <ide.h> 1110a37fd7SStephen Warren #include <malloc.h> 12735dd97bSGrant Likely #include <part.h> 13affae2bfSwdenk 14affae2bfSwdenk #undef PART_DEBUG 15affae2bfSwdenk 16affae2bfSwdenk #ifdef PART_DEBUG 17affae2bfSwdenk #define PRINTF(fmt,args...) printf (fmt ,##args) 18affae2bfSwdenk #else 19affae2bfSwdenk #define PRINTF(fmt,args...) 20affae2bfSwdenk #endif 21affae2bfSwdenk 22735dd97bSGrant Likely struct block_drvr { 23735dd97bSGrant Likely char *name; 24735dd97bSGrant Likely block_dev_desc_t* (*get_dev)(int dev); 25336b6f90SStephen Warren int (*select_hwpart)(int dev_num, int hwpart); 26735dd97bSGrant Likely }; 27735dd97bSGrant Likely 28735dd97bSGrant Likely static const struct block_drvr block_drvr[] = { 29cde5c64dSJon Loeliger #if defined(CONFIG_CMD_IDE) 30735dd97bSGrant Likely { .name = "ide", .get_dev = ide_get_dev, }, 31735dd97bSGrant Likely #endif 32c7057b52SDave Liu #if defined(CONFIG_CMD_SATA) 33c7057b52SDave Liu {.name = "sata", .get_dev = sata_get_dev, }, 34c7057b52SDave Liu #endif 35cde5c64dSJon Loeliger #if defined(CONFIG_CMD_SCSI) 36735dd97bSGrant Likely { .name = "scsi", .get_dev = scsi_get_dev, }, 37735dd97bSGrant Likely #endif 38cde5c64dSJon Loeliger #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) 39735dd97bSGrant Likely { .name = "usb", .get_dev = usb_stor_get_dev, }, 40735dd97bSGrant Likely #endif 41735dd97bSGrant Likely #if defined(CONFIG_MMC) 42d2356284SStephen Warren { 43d2356284SStephen Warren .name = "mmc", 44d2356284SStephen Warren .get_dev = mmc_get_dev, 45d2356284SStephen Warren .select_hwpart = mmc_select_hwpart, 46d2356284SStephen Warren }, 47735dd97bSGrant Likely #endif 48735dd97bSGrant Likely #if defined(CONFIG_SYSTEMACE) 49735dd97bSGrant Likely { .name = "ace", .get_dev = systemace_get_dev, }, 50735dd97bSGrant Likely #endif 51f4d8de48SHenrik Nordström #if defined(CONFIG_SANDBOX) 52f4d8de48SHenrik Nordström { .name = "host", .get_dev = host_get_dev, }, 53f4d8de48SHenrik Nordström #endif 54735dd97bSGrant Likely { }, 55735dd97bSGrant Likely }; 56735dd97bSGrant Likely 57751bb571SStefan Roese DECLARE_GLOBAL_DATA_PTR; 58751bb571SStefan Roese 590c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 60336b6f90SStephen Warren block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart) 61735dd97bSGrant Likely { 62735dd97bSGrant Likely const struct block_drvr *drvr = block_drvr; 63751bb571SStefan Roese block_dev_desc_t* (*reloc_get_dev)(int dev); 64336b6f90SStephen Warren int (*select_hwpart)(int dev_num, int hwpart); 6523090dacSHeiko Schocher char *name; 66336b6f90SStephen Warren int ret; 67751bb571SStefan Roese 687e71dc68STim Kientzle if (!ifname) 697e71dc68STim Kientzle return NULL; 707e71dc68STim Kientzle 7123090dacSHeiko Schocher name = drvr->name; 722e5167ccSWolfgang Denk #ifdef CONFIG_NEEDS_MANUAL_RELOC 7323090dacSHeiko Schocher name += gd->reloc_off; 7423090dacSHeiko Schocher #endif 75b16aadf4SLei Wen while (drvr->name) { 7623090dacSHeiko Schocher name = drvr->name; 77521af04dSPeter Tyser reloc_get_dev = drvr->get_dev; 78336b6f90SStephen Warren select_hwpart = drvr->select_hwpart; 792e5167ccSWolfgang Denk #ifdef CONFIG_NEEDS_MANUAL_RELOC 8023090dacSHeiko Schocher name += gd->reloc_off; 81521af04dSPeter Tyser reloc_get_dev += gd->reloc_off; 82336b6f90SStephen Warren if (select_hwpart) 83336b6f90SStephen Warren select_hwpart += gd->reloc_off; 84521af04dSPeter Tyser #endif 85336b6f90SStephen Warren if (strncmp(ifname, name, strlen(name)) == 0) { 86336b6f90SStephen Warren block_dev_desc_t *dev_desc = reloc_get_dev(dev); 87336b6f90SStephen Warren if (!dev_desc) 88336b6f90SStephen Warren return NULL; 89ecdd57e2SStephen Warren if (hwpart == 0 && !select_hwpart) 90336b6f90SStephen Warren return dev_desc; 91336b6f90SStephen Warren if (!select_hwpart) 92336b6f90SStephen Warren return NULL; 93336b6f90SStephen Warren ret = select_hwpart(dev_desc->dev, hwpart); 94336b6f90SStephen Warren if (ret < 0) 95336b6f90SStephen Warren return NULL; 96336b6f90SStephen Warren return dev_desc; 97336b6f90SStephen Warren } 98735dd97bSGrant Likely drvr++; 99735dd97bSGrant Likely } 100735dd97bSGrant Likely return NULL; 101735dd97bSGrant Likely } 102336b6f90SStephen Warren 103336b6f90SStephen Warren block_dev_desc_t *get_dev(const char *ifname, int dev) 104336b6f90SStephen Warren { 105ecdd57e2SStephen Warren return get_dev_hwpart(ifname, dev, 0); 106336b6f90SStephen Warren } 107735dd97bSGrant Likely #else 108336b6f90SStephen Warren block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart) 109336b6f90SStephen Warren { 110336b6f90SStephen Warren return NULL; 111336b6f90SStephen Warren } 112336b6f90SStephen Warren 11399d2c205SRob Herring block_dev_desc_t *get_dev(const char *ifname, int dev) 114735dd97bSGrant Likely { 115735dd97bSGrant Likely return NULL; 116735dd97bSGrant Likely } 117735dd97bSGrant Likely #endif 118735dd97bSGrant Likely 1190c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 120735dd97bSGrant Likely 121affae2bfSwdenk /* ------------------------------------------------------------------------- */ 122affae2bfSwdenk /* 123affae2bfSwdenk * reports device info to the user 124affae2bfSwdenk */ 12569a2a4d9SSergei Trofimovich 12669a2a4d9SSergei Trofimovich #ifdef CONFIG_LBA48 12769a2a4d9SSergei Trofimovich typedef uint64_t lba512_t; 12869a2a4d9SSergei Trofimovich #else 12969a2a4d9SSergei Trofimovich typedef lbaint_t lba512_t; 13069a2a4d9SSergei Trofimovich #endif 13169a2a4d9SSergei Trofimovich 13269a2a4d9SSergei Trofimovich /* 13369a2a4d9SSergei Trofimovich * Overflowless variant of (block_count * mul_by / div_by) 13469a2a4d9SSergei Trofimovich * when div_by > mul_by 13569a2a4d9SSergei Trofimovich */ 13669a2a4d9SSergei Trofimovich static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by) 13769a2a4d9SSergei Trofimovich { 13869a2a4d9SSergei Trofimovich lba512_t bc_quot, bc_rem; 13969a2a4d9SSergei Trofimovich 14069a2a4d9SSergei Trofimovich /* x * m / d == x / d * m + (x % d) * m / d */ 14169a2a4d9SSergei Trofimovich bc_quot = block_count / div_by; 14269a2a4d9SSergei Trofimovich bc_rem = block_count - div_by * bc_quot; 14369a2a4d9SSergei Trofimovich return bc_quot * mul_by + (bc_rem * mul_by) / div_by; 14469a2a4d9SSergei Trofimovich } 14569a2a4d9SSergei Trofimovich 146affae2bfSwdenk void dev_print (block_dev_desc_t *dev_desc) 147affae2bfSwdenk { 14869a2a4d9SSergei Trofimovich lba512_t lba512; /* number of blocks if 512bytes block size */ 149affae2bfSwdenk 150af75a45dSWolfgang Denk if (dev_desc->type == DEV_TYPE_UNKNOWN) { 151af75a45dSWolfgang Denk puts ("not available\n"); 152af75a45dSWolfgang Denk return; 153af75a45dSWolfgang Denk } 154af75a45dSWolfgang Denk 1558ec6e332STor Krill switch (dev_desc->if_type) { 156574b3195SDetlev Zundel case IF_TYPE_SCSI: 157574b3195SDetlev Zundel printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", 158574b3195SDetlev Zundel dev_desc->target,dev_desc->lun, 159affae2bfSwdenk dev_desc->vendor, 160affae2bfSwdenk dev_desc->product, 161affae2bfSwdenk dev_desc->revision); 162574b3195SDetlev Zundel break; 1636e24a1ebSRemy Bohmer case IF_TYPE_ATAPI: 164574b3195SDetlev Zundel case IF_TYPE_IDE: 165574b3195SDetlev Zundel case IF_TYPE_SATA: 166574b3195SDetlev Zundel printf ("Model: %s Firm: %s Ser#: %s\n", 167574b3195SDetlev Zundel dev_desc->vendor, 168574b3195SDetlev Zundel dev_desc->revision, 169574b3195SDetlev Zundel dev_desc->product); 170574b3195SDetlev Zundel break; 1716e24a1ebSRemy Bohmer case IF_TYPE_SD: 1726e24a1ebSRemy Bohmer case IF_TYPE_MMC: 17347bebe34SNícolas Carneiro Lebedenco case IF_TYPE_USB: 17447bebe34SNícolas Carneiro Lebedenco printf ("Vendor: %s Rev: %s Prod: %s\n", 17547bebe34SNícolas Carneiro Lebedenco dev_desc->vendor, 17647bebe34SNícolas Carneiro Lebedenco dev_desc->revision, 17747bebe34SNícolas Carneiro Lebedenco dev_desc->product); 17847bebe34SNícolas Carneiro Lebedenco break; 1796e24a1ebSRemy Bohmer case IF_TYPE_DOC: 1806e24a1ebSRemy Bohmer puts("device type DOC\n"); 1816e24a1ebSRemy Bohmer return; 1828ec6e332STor Krill case IF_TYPE_UNKNOWN: 1836e24a1ebSRemy Bohmer puts("device type unknown\n"); 1846e24a1ebSRemy Bohmer return; 185574b3195SDetlev Zundel default: 1866e24a1ebSRemy Bohmer printf("Unhandled device type: %i\n", dev_desc->if_type); 187574b3195SDetlev Zundel return; 188affae2bfSwdenk } 189affae2bfSwdenk puts (" Type: "); 190affae2bfSwdenk if (dev_desc->removable) 191affae2bfSwdenk puts ("Removable "); 192affae2bfSwdenk switch (dev_desc->type & 0x1F) { 193726c0f1eSDetlev Zundel case DEV_TYPE_HARDDISK: 194726c0f1eSDetlev Zundel puts ("Hard Disk"); 195affae2bfSwdenk break; 196726c0f1eSDetlev Zundel case DEV_TYPE_CDROM: 197726c0f1eSDetlev Zundel puts ("CD ROM"); 198affae2bfSwdenk break; 199726c0f1eSDetlev Zundel case DEV_TYPE_OPDISK: 200726c0f1eSDetlev Zundel puts ("Optical Device"); 201affae2bfSwdenk break; 202726c0f1eSDetlev Zundel case DEV_TYPE_TAPE: 203726c0f1eSDetlev Zundel puts ("Tape"); 204affae2bfSwdenk break; 205726c0f1eSDetlev Zundel default: 206726c0f1eSDetlev Zundel printf ("# %02X #", dev_desc->type & 0x1F); 207affae2bfSwdenk break; 208affae2bfSwdenk } 209affae2bfSwdenk puts ("\n"); 21033699df1SJerry Huang if (dev_desc->lba > 0L && dev_desc->blksz > 0L) { 211affae2bfSwdenk ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem; 212c40b2956Swdenk lbaint_t lba; 2136e592385Swdenk 214c40b2956Swdenk lba = dev_desc->lba; 215affae2bfSwdenk 216c40b2956Swdenk lba512 = (lba * (dev_desc->blksz/512)); 217affae2bfSwdenk /* round to 1 digit */ 218*214b3f31SPavel Machek /* 2048 = (1024 * 1024) / 512 MB */ 219*214b3f31SPavel Machek mb = lba512_muldiv(lba512, 10, 2048); 22069a2a4d9SSergei Trofimovich 221affae2bfSwdenk mb_quot = mb / 10; 222affae2bfSwdenk mb_rem = mb - (10 * mb_quot); 223affae2bfSwdenk 224affae2bfSwdenk gb = mb / 1024; 225affae2bfSwdenk gb_quot = gb / 10; 226affae2bfSwdenk gb_rem = gb - (10 * gb_quot); 22742dfe7a1Swdenk #ifdef CONFIG_LBA48 2286e592385Swdenk if (dev_desc->lba48) 229c40b2956Swdenk printf (" Supports 48-bit addressing\n"); 230c40b2956Swdenk #endif 2314b142febSHeiko Schocher #if defined(CONFIG_SYS_64BIT_LBA) 2326c6166f5SMike Frysinger printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n", 233c40b2956Swdenk mb_quot, mb_rem, 234c40b2956Swdenk gb_quot, gb_rem, 235c40b2956Swdenk lba, 236c40b2956Swdenk dev_desc->blksz); 237c40b2956Swdenk #else 238affae2bfSwdenk printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n", 239affae2bfSwdenk mb_quot, mb_rem, 240affae2bfSwdenk gb_quot, gb_rem, 241c40b2956Swdenk (ulong)lba, 242affae2bfSwdenk dev_desc->blksz); 243c40b2956Swdenk #endif 244affae2bfSwdenk } else { 245affae2bfSwdenk puts (" Capacity: not available\n"); 246affae2bfSwdenk } 247affae2bfSwdenk } 248b3aff0cbSJon Loeliger #endif 249affae2bfSwdenk 2500c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 251affae2bfSwdenk 252affae2bfSwdenk void init_part(block_dev_desc_t *dev_desc) 253affae2bfSwdenk { 254affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION 255affae2bfSwdenk if (test_part_iso(dev_desc) == 0) { 256affae2bfSwdenk dev_desc->part_type = PART_TYPE_ISO; 257affae2bfSwdenk return; 258affae2bfSwdenk } 259affae2bfSwdenk #endif 260affae2bfSwdenk 261affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION 262affae2bfSwdenk if (test_part_mac(dev_desc) == 0) { 263affae2bfSwdenk dev_desc->part_type = PART_TYPE_MAC; 264affae2bfSwdenk return; 265affae2bfSwdenk } 266affae2bfSwdenk #endif 267affae2bfSwdenk 26807f3d789Srichardretanubun /* must be placed before DOS partition detection */ 26907f3d789Srichardretanubun #ifdef CONFIG_EFI_PARTITION 27007f3d789Srichardretanubun if (test_part_efi(dev_desc) == 0) { 27107f3d789Srichardretanubun dev_desc->part_type = PART_TYPE_EFI; 27207f3d789Srichardretanubun return; 27307f3d789Srichardretanubun } 27407f3d789Srichardretanubun #endif 27507f3d789Srichardretanubun 276affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION 277affae2bfSwdenk if (test_part_dos(dev_desc) == 0) { 278affae2bfSwdenk dev_desc->part_type = PART_TYPE_DOS; 279affae2bfSwdenk return; 280affae2bfSwdenk } 281affae2bfSwdenk #endif 282c7de829cSwdenk 283c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION 284c7de829cSwdenk if (test_part_amiga(dev_desc) == 0) { 285c7de829cSwdenk dev_desc->part_type = PART_TYPE_AMIGA; 286c7de829cSwdenk return; 287c7de829cSwdenk } 288c7de829cSwdenk #endif 28999d2c205SRob Herring dev_desc->part_type = PART_TYPE_UNKNOWN; 290affae2bfSwdenk } 291affae2bfSwdenk 292affae2bfSwdenk 2930c9c8fb5SGabe Black #if defined(CONFIG_MAC_PARTITION) || \ 2940c9c8fb5SGabe Black defined(CONFIG_DOS_PARTITION) || \ 2950c9c8fb5SGabe Black defined(CONFIG_ISO_PARTITION) || \ 2960c9c8fb5SGabe Black defined(CONFIG_AMIGA_PARTITION) || \ 2970c9c8fb5SGabe Black defined(CONFIG_EFI_PARTITION) 2980c9c8fb5SGabe Black 299affae2bfSwdenk static void print_part_header(const char *type, block_dev_desc_t *dev_desc) 300affae2bfSwdenk { 301affae2bfSwdenk puts ("\nPartition Map for "); 302affae2bfSwdenk switch (dev_desc->if_type) { 303726c0f1eSDetlev Zundel case IF_TYPE_IDE: 304726c0f1eSDetlev Zundel puts ("IDE"); 305affae2bfSwdenk break; 306726c0f1eSDetlev Zundel case IF_TYPE_SATA: 307726c0f1eSDetlev Zundel puts ("SATA"); 308c7057b52SDave Liu break; 309726c0f1eSDetlev Zundel case IF_TYPE_SCSI: 310726c0f1eSDetlev Zundel puts ("SCSI"); 311affae2bfSwdenk break; 312726c0f1eSDetlev Zundel case IF_TYPE_ATAPI: 313726c0f1eSDetlev Zundel puts ("ATAPI"); 314affae2bfSwdenk break; 315726c0f1eSDetlev Zundel case IF_TYPE_USB: 316726c0f1eSDetlev Zundel puts ("USB"); 317affae2bfSwdenk break; 318726c0f1eSDetlev Zundel case IF_TYPE_DOC: 319726c0f1eSDetlev Zundel puts ("DOC"); 320affae2bfSwdenk break; 3218f3b9642SLei Wen case IF_TYPE_MMC: 3228f3b9642SLei Wen puts ("MMC"); 3238f3b9642SLei Wen break; 324f4d8de48SHenrik Nordström case IF_TYPE_HOST: 325f4d8de48SHenrik Nordström puts("HOST"); 326f4d8de48SHenrik Nordström break; 327726c0f1eSDetlev Zundel default: 328726c0f1eSDetlev Zundel puts ("UNKNOWN"); 329affae2bfSwdenk break; 330affae2bfSwdenk } 331affae2bfSwdenk printf (" device %d -- Partition Type: %s\n\n", 332affae2bfSwdenk dev_desc->dev, type); 333affae2bfSwdenk } 334affae2bfSwdenk 3350c9c8fb5SGabe Black #endif /* any CONFIG_..._PARTITION */ 3360c9c8fb5SGabe Black 337affae2bfSwdenk void print_part(block_dev_desc_t * dev_desc) 338affae2bfSwdenk { 339affae2bfSwdenk 340affae2bfSwdenk switch (dev_desc->part_type) { 341affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION 342affae2bfSwdenk case PART_TYPE_MAC: 343affae2bfSwdenk PRINTF ("## Testing for valid MAC partition ##\n"); 344affae2bfSwdenk print_part_header ("MAC", dev_desc); 345affae2bfSwdenk print_part_mac (dev_desc); 346affae2bfSwdenk return; 347affae2bfSwdenk #endif 348affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION 349affae2bfSwdenk case PART_TYPE_DOS: 350affae2bfSwdenk PRINTF ("## Testing for valid DOS partition ##\n"); 351affae2bfSwdenk print_part_header ("DOS", dev_desc); 352affae2bfSwdenk print_part_dos (dev_desc); 353affae2bfSwdenk return; 354affae2bfSwdenk #endif 355affae2bfSwdenk 356affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION 357affae2bfSwdenk case PART_TYPE_ISO: 358affae2bfSwdenk PRINTF ("## Testing for valid ISO Boot partition ##\n"); 359affae2bfSwdenk print_part_header ("ISO", dev_desc); 360affae2bfSwdenk print_part_iso (dev_desc); 361affae2bfSwdenk return; 362affae2bfSwdenk #endif 363c7de829cSwdenk 364c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION 365c7de829cSwdenk case PART_TYPE_AMIGA: 366c7de829cSwdenk PRINTF ("## Testing for a valid Amiga partition ##\n"); 367c7de829cSwdenk print_part_header ("AMIGA", dev_desc); 368c7de829cSwdenk print_part_amiga (dev_desc); 369c7de829cSwdenk return; 370c7de829cSwdenk #endif 37107f3d789Srichardretanubun 37207f3d789Srichardretanubun #ifdef CONFIG_EFI_PARTITION 37307f3d789Srichardretanubun case PART_TYPE_EFI: 37407f3d789Srichardretanubun PRINTF ("## Testing for valid EFI partition ##\n"); 37507f3d789Srichardretanubun print_part_header ("EFI", dev_desc); 37607f3d789Srichardretanubun print_part_efi (dev_desc); 37707f3d789Srichardretanubun return; 37807f3d789Srichardretanubun #endif 379affae2bfSwdenk } 380affae2bfSwdenk puts ("## Unknown partition table\n"); 381affae2bfSwdenk } 382affae2bfSwdenk 3830c9c8fb5SGabe Black #endif /* HAVE_BLOCK_DEVICE */ 3842f501646SStephen Warren 3853f9eb6e1SPavel Machek int get_partition_info(block_dev_desc_t *dev_desc, int part, 3863f9eb6e1SPavel Machek disk_partition_t *info) 3872f501646SStephen Warren { 3880c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 3892f501646SStephen Warren 390894bfbbfSStephen Warren #ifdef CONFIG_PARTITION_UUIDS 391894bfbbfSStephen Warren /* The common case is no UUID support */ 392894bfbbfSStephen Warren info->uuid[0] = 0; 393894bfbbfSStephen Warren #endif 394894bfbbfSStephen Warren 3952f501646SStephen Warren switch (dev_desc->part_type) { 3962f501646SStephen Warren #ifdef CONFIG_MAC_PARTITION 3972f501646SStephen Warren case PART_TYPE_MAC: 3982f501646SStephen Warren if (get_partition_info_mac(dev_desc, part, info) == 0) { 3992f501646SStephen Warren PRINTF("## Valid MAC partition found ##\n"); 4002f501646SStephen Warren return 0; 4012f501646SStephen Warren } 4022f501646SStephen Warren break; 4032f501646SStephen Warren #endif 4042f501646SStephen Warren 4052f501646SStephen Warren #ifdef CONFIG_DOS_PARTITION 4062f501646SStephen Warren case PART_TYPE_DOS: 4072f501646SStephen Warren if (get_partition_info_dos(dev_desc, part, info) == 0) { 4082f501646SStephen Warren PRINTF("## Valid DOS partition found ##\n"); 4092f501646SStephen Warren return 0; 4102f501646SStephen Warren } 4112f501646SStephen Warren break; 4122f501646SStephen Warren #endif 4132f501646SStephen Warren 4142f501646SStephen Warren #ifdef CONFIG_ISO_PARTITION 4152f501646SStephen Warren case PART_TYPE_ISO: 4162f501646SStephen Warren if (get_partition_info_iso(dev_desc, part, info) == 0) { 4172f501646SStephen Warren PRINTF("## Valid ISO boot partition found ##\n"); 4182f501646SStephen Warren return 0; 4192f501646SStephen Warren } 4202f501646SStephen Warren break; 4212f501646SStephen Warren #endif 4222f501646SStephen Warren 4232f501646SStephen Warren #ifdef CONFIG_AMIGA_PARTITION 4242f501646SStephen Warren case PART_TYPE_AMIGA: 4252f501646SStephen Warren if (get_partition_info_amiga(dev_desc, part, info) == 0) { 4262f501646SStephen Warren PRINTF("## Valid Amiga partition found ##\n"); 4272f501646SStephen Warren return 0; 4282f501646SStephen Warren } 4292f501646SStephen Warren break; 4302f501646SStephen Warren #endif 4312f501646SStephen Warren 4322f501646SStephen Warren #ifdef CONFIG_EFI_PARTITION 4332f501646SStephen Warren case PART_TYPE_EFI: 4342f501646SStephen Warren if (get_partition_info_efi(dev_desc, part, info) == 0) { 4352f501646SStephen Warren PRINTF("## Valid EFI partition found ##\n"); 4362f501646SStephen Warren return 0; 4372f501646SStephen Warren } 4382f501646SStephen Warren break; 4392f501646SStephen Warren #endif 4402f501646SStephen Warren default: 4412f501646SStephen Warren break; 4422f501646SStephen Warren } 4430c9c8fb5SGabe Black #endif /* HAVE_BLOCK_DEVICE */ 4442f501646SStephen Warren 4452f501646SStephen Warren return -1; 4462f501646SStephen Warren } 44799d2c205SRob Herring 448336b6f90SStephen Warren int get_device(const char *ifname, const char *dev_hwpart_str, 4492023e608SStephen Warren block_dev_desc_t **dev_desc) 4502023e608SStephen Warren { 4512023e608SStephen Warren char *ep; 452336b6f90SStephen Warren char *dup_str = NULL; 453336b6f90SStephen Warren const char *dev_str, *hwpart_str; 454336b6f90SStephen Warren int dev, hwpart; 455336b6f90SStephen Warren 456336b6f90SStephen Warren hwpart_str = strchr(dev_hwpart_str, '.'); 457336b6f90SStephen Warren if (hwpart_str) { 458336b6f90SStephen Warren dup_str = strdup(dev_hwpart_str); 459336b6f90SStephen Warren dup_str[hwpart_str - dev_hwpart_str] = 0; 460336b6f90SStephen Warren dev_str = dup_str; 461336b6f90SStephen Warren hwpart_str++; 462336b6f90SStephen Warren } else { 463336b6f90SStephen Warren dev_str = dev_hwpart_str; 464ecdd57e2SStephen Warren hwpart = 0; 465336b6f90SStephen Warren } 4662023e608SStephen Warren 4672023e608SStephen Warren dev = simple_strtoul(dev_str, &ep, 16); 4682023e608SStephen Warren if (*ep) { 4692023e608SStephen Warren printf("** Bad device specification %s %s **\n", 4702023e608SStephen Warren ifname, dev_str); 471336b6f90SStephen Warren dev = -1; 472336b6f90SStephen Warren goto cleanup; 4732023e608SStephen Warren } 4742023e608SStephen Warren 475336b6f90SStephen Warren if (hwpart_str) { 476336b6f90SStephen Warren hwpart = simple_strtoul(hwpart_str, &ep, 16); 477336b6f90SStephen Warren if (*ep) { 478336b6f90SStephen Warren printf("** Bad HW partition specification %s %s **\n", 479336b6f90SStephen Warren ifname, hwpart_str); 480336b6f90SStephen Warren dev = -1; 481336b6f90SStephen Warren goto cleanup; 482336b6f90SStephen Warren } 483336b6f90SStephen Warren } 484336b6f90SStephen Warren 485336b6f90SStephen Warren *dev_desc = get_dev_hwpart(ifname, dev, hwpart); 4862023e608SStephen Warren if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) { 487336b6f90SStephen Warren printf("** Bad device %s %s **\n", ifname, dev_hwpart_str); 488336b6f90SStephen Warren dev = -1; 489336b6f90SStephen Warren goto cleanup; 4902023e608SStephen Warren } 4912023e608SStephen Warren 492336b6f90SStephen Warren cleanup: 493336b6f90SStephen Warren free(dup_str); 4942023e608SStephen Warren return dev; 4952023e608SStephen Warren } 4962023e608SStephen Warren 49710a37fd7SStephen Warren #define PART_UNSPECIFIED -2 49810a37fd7SStephen Warren #define PART_AUTO -1 49910a37fd7SStephen Warren #define MAX_SEARCH_PARTITIONS 16 50010a37fd7SStephen Warren int get_device_and_partition(const char *ifname, const char *dev_part_str, 50199d2c205SRob Herring block_dev_desc_t **dev_desc, 50210a37fd7SStephen Warren disk_partition_t *info, int allow_whole_dev) 50399d2c205SRob Herring { 50410a37fd7SStephen Warren int ret = -1; 50510a37fd7SStephen Warren const char *part_str; 50610a37fd7SStephen Warren char *dup_str = NULL; 50710a37fd7SStephen Warren const char *dev_str; 50899d2c205SRob Herring int dev; 50910a37fd7SStephen Warren char *ep; 51010a37fd7SStephen Warren int p; 51110a37fd7SStephen Warren int part; 51210a37fd7SStephen Warren disk_partition_t tmpinfo; 51399d2c205SRob Herring 5144d907025SStephen Warren /* 5153f9eb6e1SPavel Machek * Special-case a pseudo block device "hostfs", to allow access to the 5164d907025SStephen Warren * host's own filesystem. 5174d907025SStephen Warren */ 5184d907025SStephen Warren if (0 == strcmp(ifname, "hostfs")) { 5194d907025SStephen Warren *dev_desc = NULL; 5204d907025SStephen Warren info->start = 0; 5214d907025SStephen Warren info->size = 0; 5224d907025SStephen Warren info->blksz = 0; 5234d907025SStephen Warren info->bootable = 0; 5244d907025SStephen Warren strcpy((char *)info->type, BOOT_PART_TYPE); 5254d907025SStephen Warren strcpy((char *)info->name, "Sandbox host"); 5264d907025SStephen Warren #ifdef CONFIG_PARTITION_UUIDS 5274d907025SStephen Warren info->uuid[0] = 0; 5284d907025SStephen Warren #endif 5294d907025SStephen Warren 5304d907025SStephen Warren return 0; 5314d907025SStephen Warren } 5324d907025SStephen Warren 53310a37fd7SStephen Warren /* If no dev_part_str, use bootdevice environment variable */ 534a10973e7SStephen Warren if (!dev_part_str || !strlen(dev_part_str) || 535a10973e7SStephen Warren !strcmp(dev_part_str, "-")) 53610a37fd7SStephen Warren dev_part_str = getenv("bootdevice"); 53799d2c205SRob Herring 53810a37fd7SStephen Warren /* If still no dev_part_str, it's an error */ 53910a37fd7SStephen Warren if (!dev_part_str) { 54010a37fd7SStephen Warren printf("** No device specified **\n"); 54110a37fd7SStephen Warren goto cleanup; 54299d2c205SRob Herring } 54399d2c205SRob Herring 54410a37fd7SStephen Warren /* Separate device and partition ID specification */ 54510a37fd7SStephen Warren part_str = strchr(dev_part_str, ':'); 54610a37fd7SStephen Warren if (part_str) { 54710a37fd7SStephen Warren dup_str = strdup(dev_part_str); 54810a37fd7SStephen Warren dup_str[part_str - dev_part_str] = 0; 54910a37fd7SStephen Warren dev_str = dup_str; 55010a37fd7SStephen Warren part_str++; 55110a37fd7SStephen Warren } else { 55210a37fd7SStephen Warren dev_str = dev_part_str; 55399d2c205SRob Herring } 55410a37fd7SStephen Warren 55510a37fd7SStephen Warren /* Look up the device */ 55610a37fd7SStephen Warren dev = get_device(ifname, dev_str, dev_desc); 55710a37fd7SStephen Warren if (dev < 0) 55810a37fd7SStephen Warren goto cleanup; 55910a37fd7SStephen Warren 56010a37fd7SStephen Warren /* Convert partition ID string to number */ 56110a37fd7SStephen Warren if (!part_str || !*part_str) { 56210a37fd7SStephen Warren part = PART_UNSPECIFIED; 56310a37fd7SStephen Warren } else if (!strcmp(part_str, "auto")) { 56410a37fd7SStephen Warren part = PART_AUTO; 56510a37fd7SStephen Warren } else { 56610a37fd7SStephen Warren /* Something specified -> use exactly that */ 56710a37fd7SStephen Warren part = (int)simple_strtoul(part_str, &ep, 16); 56810a37fd7SStephen Warren /* 56910a37fd7SStephen Warren * Less than whole string converted, 57010a37fd7SStephen Warren * or request for whole device, but caller requires partition. 57110a37fd7SStephen Warren */ 57210a37fd7SStephen Warren if (*ep || (part == 0 && !allow_whole_dev)) { 57310a37fd7SStephen Warren printf("** Bad partition specification %s %s **\n", 57410a37fd7SStephen Warren ifname, dev_part_str); 57510a37fd7SStephen Warren goto cleanup; 57610a37fd7SStephen Warren } 57710a37fd7SStephen Warren } 57810a37fd7SStephen Warren 57910a37fd7SStephen Warren /* 58010a37fd7SStephen Warren * No partition table on device, 58110a37fd7SStephen Warren * or user requested partition 0 (entire device). 58210a37fd7SStephen Warren */ 58310a37fd7SStephen Warren if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) || 58410a37fd7SStephen Warren (part == 0)) { 58510a37fd7SStephen Warren if (!(*dev_desc)->lba) { 58610a37fd7SStephen Warren printf("** Bad device size - %s %s **\n", ifname, 58710a37fd7SStephen Warren dev_str); 58810a37fd7SStephen Warren goto cleanup; 58910a37fd7SStephen Warren } 59010a37fd7SStephen Warren 59110a37fd7SStephen Warren /* 59210a37fd7SStephen Warren * If user specified a partition ID other than 0, 59310a37fd7SStephen Warren * or the calling command only accepts partitions, 59410a37fd7SStephen Warren * it's an error. 59510a37fd7SStephen Warren */ 59610a37fd7SStephen Warren if ((part > 0) || (!allow_whole_dev)) { 59710a37fd7SStephen Warren printf("** No partition table - %s %s **\n", ifname, 59810a37fd7SStephen Warren dev_str); 59910a37fd7SStephen Warren goto cleanup; 60010a37fd7SStephen Warren } 60110a37fd7SStephen Warren 60250ffc3b6SLan Yixun (dlan) (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); 60350ffc3b6SLan Yixun (dlan) 60499d2c205SRob Herring info->start = 0; 60510a37fd7SStephen Warren info->size = (*dev_desc)->lba; 60610a37fd7SStephen Warren info->blksz = (*dev_desc)->blksz; 60710a37fd7SStephen Warren info->bootable = 0; 6086ab6a650SStephen Warren strcpy((char *)info->type, BOOT_PART_TYPE); 6096ab6a650SStephen Warren strcpy((char *)info->name, "Whole Disk"); 61010a37fd7SStephen Warren #ifdef CONFIG_PARTITION_UUIDS 61110a37fd7SStephen Warren info->uuid[0] = 0; 61210a37fd7SStephen Warren #endif 61399d2c205SRob Herring 61410a37fd7SStephen Warren ret = 0; 61510a37fd7SStephen Warren goto cleanup; 61699d2c205SRob Herring } 61799d2c205SRob Herring 61810a37fd7SStephen Warren /* 61910a37fd7SStephen Warren * Now there's known to be a partition table, 62010a37fd7SStephen Warren * not specifying a partition means to pick partition 1. 62110a37fd7SStephen Warren */ 62210a37fd7SStephen Warren if (part == PART_UNSPECIFIED) 62310a37fd7SStephen Warren part = 1; 62499d2c205SRob Herring 62510a37fd7SStephen Warren /* 62610a37fd7SStephen Warren * If user didn't specify a partition number, or did specify something 62710a37fd7SStephen Warren * other than "auto", use that partition number directly. 62810a37fd7SStephen Warren */ 62910a37fd7SStephen Warren if (part != PART_AUTO) { 63010a37fd7SStephen Warren ret = get_partition_info(*dev_desc, part, info); 63199d2c205SRob Herring if (ret) { 63210a37fd7SStephen Warren printf("** Invalid partition %d **\n", part); 63310a37fd7SStephen Warren goto cleanup; 63410a37fd7SStephen Warren } 63510a37fd7SStephen Warren } else { 63610a37fd7SStephen Warren /* 63710a37fd7SStephen Warren * Find the first bootable partition. 63810a37fd7SStephen Warren * If none are bootable, fall back to the first valid partition. 63910a37fd7SStephen Warren */ 64010a37fd7SStephen Warren part = 0; 64110a37fd7SStephen Warren for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { 64210a37fd7SStephen Warren ret = get_partition_info(*dev_desc, p, info); 64310a37fd7SStephen Warren if (ret) 64410a37fd7SStephen Warren continue; 64510a37fd7SStephen Warren 64610a37fd7SStephen Warren /* 64710a37fd7SStephen Warren * First valid partition, or new better partition? 64810a37fd7SStephen Warren * If so, save partition ID. 64910a37fd7SStephen Warren */ 65010a37fd7SStephen Warren if (!part || info->bootable) 65110a37fd7SStephen Warren part = p; 65210a37fd7SStephen Warren 65310a37fd7SStephen Warren /* Best possible partition? Stop searching. */ 65410a37fd7SStephen Warren if (info->bootable) 65510a37fd7SStephen Warren break; 65610a37fd7SStephen Warren 65710a37fd7SStephen Warren /* 65810a37fd7SStephen Warren * We now need to search further for best possible. 65910a37fd7SStephen Warren * If we what we just queried was the best so far, 66010a37fd7SStephen Warren * save the info since we over-write it next loop. 66110a37fd7SStephen Warren */ 66210a37fd7SStephen Warren if (part == p) 66310a37fd7SStephen Warren tmpinfo = *info; 66410a37fd7SStephen Warren } 66510a37fd7SStephen Warren /* If we found any acceptable partition */ 66610a37fd7SStephen Warren if (part) { 66710a37fd7SStephen Warren /* 66810a37fd7SStephen Warren * If we searched all possible partition IDs, 66910a37fd7SStephen Warren * return the first valid partition we found. 67010a37fd7SStephen Warren */ 67110a37fd7SStephen Warren if (p == MAX_SEARCH_PARTITIONS + 1) 67210a37fd7SStephen Warren *info = tmpinfo; 67310a37fd7SStephen Warren } else { 67410a37fd7SStephen Warren printf("** No valid partitions found **\n"); 67571bba424SStephen Warren ret = -1; 67610a37fd7SStephen Warren goto cleanup; 67710a37fd7SStephen Warren } 67899d2c205SRob Herring } 67999d2c205SRob Herring if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) { 68099d2c205SRob Herring printf("** Invalid partition type \"%.32s\"" 68199d2c205SRob Herring " (expect \"" BOOT_PART_TYPE "\")\n", 68299d2c205SRob Herring info->type); 68310a37fd7SStephen Warren ret = -1; 68410a37fd7SStephen Warren goto cleanup; 68599d2c205SRob Herring } 68699d2c205SRob Herring 68750ffc3b6SLan Yixun (dlan) (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); 68850ffc3b6SLan Yixun (dlan) 68910a37fd7SStephen Warren ret = part; 69010a37fd7SStephen Warren goto cleanup; 69199d2c205SRob Herring 69210a37fd7SStephen Warren cleanup: 69310a37fd7SStephen Warren free(dup_str); 69410a37fd7SStephen Warren return ret; 69599d2c205SRob Herring } 696