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); 25735dd97bSGrant Likely }; 26735dd97bSGrant Likely 27735dd97bSGrant Likely static const struct block_drvr block_drvr[] = { 28cde5c64dSJon Loeliger #if defined(CONFIG_CMD_IDE) 29735dd97bSGrant Likely { .name = "ide", .get_dev = ide_get_dev, }, 30735dd97bSGrant Likely #endif 31c7057b52SDave Liu #if defined(CONFIG_CMD_SATA) 32c7057b52SDave Liu {.name = "sata", .get_dev = sata_get_dev, }, 33c7057b52SDave Liu #endif 34cde5c64dSJon Loeliger #if defined(CONFIG_CMD_SCSI) 35735dd97bSGrant Likely { .name = "scsi", .get_dev = scsi_get_dev, }, 36735dd97bSGrant Likely #endif 37cde5c64dSJon Loeliger #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) 38735dd97bSGrant Likely { .name = "usb", .get_dev = usb_stor_get_dev, }, 39735dd97bSGrant Likely #endif 40735dd97bSGrant Likely #if defined(CONFIG_MMC) 41735dd97bSGrant Likely { .name = "mmc", .get_dev = mmc_get_dev, }, 42735dd97bSGrant Likely #endif 43735dd97bSGrant Likely #if defined(CONFIG_SYSTEMACE) 44735dd97bSGrant Likely { .name = "ace", .get_dev = systemace_get_dev, }, 45735dd97bSGrant Likely #endif 46*f4d8de48SHenrik Nordström #if defined(CONFIG_SANDBOX) 47*f4d8de48SHenrik Nordström { .name = "host", .get_dev = host_get_dev, }, 48*f4d8de48SHenrik Nordström #endif 49735dd97bSGrant Likely { }, 50735dd97bSGrant Likely }; 51735dd97bSGrant Likely 52751bb571SStefan Roese DECLARE_GLOBAL_DATA_PTR; 53751bb571SStefan Roese 540c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 5599d2c205SRob Herring block_dev_desc_t *get_dev(const char *ifname, int dev) 56735dd97bSGrant Likely { 57735dd97bSGrant Likely const struct block_drvr *drvr = block_drvr; 58751bb571SStefan Roese block_dev_desc_t* (*reloc_get_dev)(int dev); 5923090dacSHeiko Schocher char *name; 60751bb571SStefan Roese 617e71dc68STim Kientzle if (!ifname) 627e71dc68STim Kientzle return NULL; 637e71dc68STim Kientzle 6423090dacSHeiko Schocher name = drvr->name; 652e5167ccSWolfgang Denk #ifdef CONFIG_NEEDS_MANUAL_RELOC 6623090dacSHeiko Schocher name += gd->reloc_off; 6723090dacSHeiko Schocher #endif 68b16aadf4SLei Wen while (drvr->name) { 6923090dacSHeiko Schocher name = drvr->name; 70521af04dSPeter Tyser reloc_get_dev = drvr->get_dev; 712e5167ccSWolfgang Denk #ifdef CONFIG_NEEDS_MANUAL_RELOC 7223090dacSHeiko Schocher name += gd->reloc_off; 73521af04dSPeter Tyser reloc_get_dev += gd->reloc_off; 74521af04dSPeter Tyser #endif 7523090dacSHeiko Schocher if (strncmp(ifname, name, strlen(name)) == 0) 76751bb571SStefan Roese return reloc_get_dev(dev); 77735dd97bSGrant Likely drvr++; 78735dd97bSGrant Likely } 79735dd97bSGrant Likely return NULL; 80735dd97bSGrant Likely } 81735dd97bSGrant Likely #else 8299d2c205SRob Herring block_dev_desc_t *get_dev(const char *ifname, int dev) 83735dd97bSGrant Likely { 84735dd97bSGrant Likely return NULL; 85735dd97bSGrant Likely } 86735dd97bSGrant Likely #endif 87735dd97bSGrant Likely 880c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 89735dd97bSGrant Likely 90affae2bfSwdenk /* ------------------------------------------------------------------------- */ 91affae2bfSwdenk /* 92affae2bfSwdenk * reports device info to the user 93affae2bfSwdenk */ 9469a2a4d9SSergei Trofimovich 9569a2a4d9SSergei Trofimovich #ifdef CONFIG_LBA48 9669a2a4d9SSergei Trofimovich typedef uint64_t lba512_t; 9769a2a4d9SSergei Trofimovich #else 9869a2a4d9SSergei Trofimovich typedef lbaint_t lba512_t; 9969a2a4d9SSergei Trofimovich #endif 10069a2a4d9SSergei Trofimovich 10169a2a4d9SSergei Trofimovich /* 10269a2a4d9SSergei Trofimovich * Overflowless variant of (block_count * mul_by / div_by) 10369a2a4d9SSergei Trofimovich * when div_by > mul_by 10469a2a4d9SSergei Trofimovich */ 10569a2a4d9SSergei Trofimovich static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by) 10669a2a4d9SSergei Trofimovich { 10769a2a4d9SSergei Trofimovich lba512_t bc_quot, bc_rem; 10869a2a4d9SSergei Trofimovich 10969a2a4d9SSergei Trofimovich /* x * m / d == x / d * m + (x % d) * m / d */ 11069a2a4d9SSergei Trofimovich bc_quot = block_count / div_by; 11169a2a4d9SSergei Trofimovich bc_rem = block_count - div_by * bc_quot; 11269a2a4d9SSergei Trofimovich return bc_quot * mul_by + (bc_rem * mul_by) / div_by; 11369a2a4d9SSergei Trofimovich } 11469a2a4d9SSergei Trofimovich 115affae2bfSwdenk void dev_print (block_dev_desc_t *dev_desc) 116affae2bfSwdenk { 11769a2a4d9SSergei Trofimovich lba512_t lba512; /* number of blocks if 512bytes block size */ 118affae2bfSwdenk 119af75a45dSWolfgang Denk if (dev_desc->type == DEV_TYPE_UNKNOWN) { 120af75a45dSWolfgang Denk puts ("not available\n"); 121af75a45dSWolfgang Denk return; 122af75a45dSWolfgang Denk } 123af75a45dSWolfgang Denk 1248ec6e332STor Krill switch (dev_desc->if_type) { 125574b3195SDetlev Zundel case IF_TYPE_SCSI: 126574b3195SDetlev Zundel printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", 127574b3195SDetlev Zundel dev_desc->target,dev_desc->lun, 128affae2bfSwdenk dev_desc->vendor, 129affae2bfSwdenk dev_desc->product, 130affae2bfSwdenk dev_desc->revision); 131574b3195SDetlev Zundel break; 1326e24a1ebSRemy Bohmer case IF_TYPE_ATAPI: 133574b3195SDetlev Zundel case IF_TYPE_IDE: 134574b3195SDetlev Zundel case IF_TYPE_SATA: 135574b3195SDetlev Zundel printf ("Model: %s Firm: %s Ser#: %s\n", 136574b3195SDetlev Zundel dev_desc->vendor, 137574b3195SDetlev Zundel dev_desc->revision, 138574b3195SDetlev Zundel dev_desc->product); 139574b3195SDetlev Zundel break; 1406e24a1ebSRemy Bohmer case IF_TYPE_SD: 1416e24a1ebSRemy Bohmer case IF_TYPE_MMC: 14247bebe34SNícolas Carneiro Lebedenco case IF_TYPE_USB: 14347bebe34SNícolas Carneiro Lebedenco printf ("Vendor: %s Rev: %s Prod: %s\n", 14447bebe34SNícolas Carneiro Lebedenco dev_desc->vendor, 14547bebe34SNícolas Carneiro Lebedenco dev_desc->revision, 14647bebe34SNícolas Carneiro Lebedenco dev_desc->product); 14747bebe34SNícolas Carneiro Lebedenco break; 1486e24a1ebSRemy Bohmer case IF_TYPE_DOC: 1496e24a1ebSRemy Bohmer puts("device type DOC\n"); 1506e24a1ebSRemy Bohmer return; 1518ec6e332STor Krill case IF_TYPE_UNKNOWN: 1526e24a1ebSRemy Bohmer puts("device type unknown\n"); 1536e24a1ebSRemy Bohmer return; 154574b3195SDetlev Zundel default: 1556e24a1ebSRemy Bohmer printf("Unhandled device type: %i\n", dev_desc->if_type); 156574b3195SDetlev Zundel return; 157affae2bfSwdenk } 158affae2bfSwdenk puts (" Type: "); 159affae2bfSwdenk if (dev_desc->removable) 160affae2bfSwdenk puts ("Removable "); 161affae2bfSwdenk switch (dev_desc->type & 0x1F) { 162726c0f1eSDetlev Zundel case DEV_TYPE_HARDDISK: 163726c0f1eSDetlev Zundel puts ("Hard Disk"); 164affae2bfSwdenk break; 165726c0f1eSDetlev Zundel case DEV_TYPE_CDROM: 166726c0f1eSDetlev Zundel puts ("CD ROM"); 167affae2bfSwdenk break; 168726c0f1eSDetlev Zundel case DEV_TYPE_OPDISK: 169726c0f1eSDetlev Zundel puts ("Optical Device"); 170affae2bfSwdenk break; 171726c0f1eSDetlev Zundel case DEV_TYPE_TAPE: 172726c0f1eSDetlev Zundel puts ("Tape"); 173affae2bfSwdenk break; 174726c0f1eSDetlev Zundel default: 175726c0f1eSDetlev Zundel printf ("# %02X #", dev_desc->type & 0x1F); 176affae2bfSwdenk break; 177affae2bfSwdenk } 178affae2bfSwdenk puts ("\n"); 17933699df1SJerry Huang if (dev_desc->lba > 0L && dev_desc->blksz > 0L) { 180affae2bfSwdenk ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem; 181c40b2956Swdenk lbaint_t lba; 1826e592385Swdenk 183c40b2956Swdenk lba = dev_desc->lba; 184affae2bfSwdenk 185c40b2956Swdenk lba512 = (lba * (dev_desc->blksz/512)); 186affae2bfSwdenk /* round to 1 digit */ 18769a2a4d9SSergei Trofimovich mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */ 18869a2a4d9SSergei Trofimovich 189affae2bfSwdenk mb_quot = mb / 10; 190affae2bfSwdenk mb_rem = mb - (10 * mb_quot); 191affae2bfSwdenk 192affae2bfSwdenk gb = mb / 1024; 193affae2bfSwdenk gb_quot = gb / 10; 194affae2bfSwdenk gb_rem = gb - (10 * gb_quot); 19542dfe7a1Swdenk #ifdef CONFIG_LBA48 1966e592385Swdenk if (dev_desc->lba48) 197c40b2956Swdenk printf (" Supports 48-bit addressing\n"); 198c40b2956Swdenk #endif 1994b142febSHeiko Schocher #if defined(CONFIG_SYS_64BIT_LBA) 2006c6166f5SMike Frysinger printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n", 201c40b2956Swdenk mb_quot, mb_rem, 202c40b2956Swdenk gb_quot, gb_rem, 203c40b2956Swdenk lba, 204c40b2956Swdenk dev_desc->blksz); 205c40b2956Swdenk #else 206affae2bfSwdenk printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n", 207affae2bfSwdenk mb_quot, mb_rem, 208affae2bfSwdenk gb_quot, gb_rem, 209c40b2956Swdenk (ulong)lba, 210affae2bfSwdenk dev_desc->blksz); 211c40b2956Swdenk #endif 212affae2bfSwdenk } else { 213affae2bfSwdenk puts (" Capacity: not available\n"); 214affae2bfSwdenk } 215affae2bfSwdenk } 216b3aff0cbSJon Loeliger #endif 217affae2bfSwdenk 2180c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 219affae2bfSwdenk 220affae2bfSwdenk void init_part (block_dev_desc_t * dev_desc) 221affae2bfSwdenk { 222affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION 223affae2bfSwdenk if (test_part_iso(dev_desc) == 0) { 224affae2bfSwdenk dev_desc->part_type = PART_TYPE_ISO; 225affae2bfSwdenk return; 226affae2bfSwdenk } 227affae2bfSwdenk #endif 228affae2bfSwdenk 229affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION 230affae2bfSwdenk if (test_part_mac(dev_desc) == 0) { 231affae2bfSwdenk dev_desc->part_type = PART_TYPE_MAC; 232affae2bfSwdenk return; 233affae2bfSwdenk } 234affae2bfSwdenk #endif 235affae2bfSwdenk 23607f3d789Srichardretanubun /* must be placed before DOS partition detection */ 23707f3d789Srichardretanubun #ifdef CONFIG_EFI_PARTITION 23807f3d789Srichardretanubun if (test_part_efi(dev_desc) == 0) { 23907f3d789Srichardretanubun dev_desc->part_type = PART_TYPE_EFI; 24007f3d789Srichardretanubun return; 24107f3d789Srichardretanubun } 24207f3d789Srichardretanubun #endif 24307f3d789Srichardretanubun 244affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION 245affae2bfSwdenk if (test_part_dos(dev_desc) == 0) { 246affae2bfSwdenk dev_desc->part_type = PART_TYPE_DOS; 247affae2bfSwdenk return; 248affae2bfSwdenk } 249affae2bfSwdenk #endif 250c7de829cSwdenk 251c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION 252c7de829cSwdenk if (test_part_amiga(dev_desc) == 0) { 253c7de829cSwdenk dev_desc->part_type = PART_TYPE_AMIGA; 254c7de829cSwdenk return; 255c7de829cSwdenk } 256c7de829cSwdenk #endif 25799d2c205SRob Herring dev_desc->part_type = PART_TYPE_UNKNOWN; 258affae2bfSwdenk } 259affae2bfSwdenk 260affae2bfSwdenk 2610c9c8fb5SGabe Black #if defined(CONFIG_MAC_PARTITION) || \ 2620c9c8fb5SGabe Black defined(CONFIG_DOS_PARTITION) || \ 2630c9c8fb5SGabe Black defined(CONFIG_ISO_PARTITION) || \ 2640c9c8fb5SGabe Black defined(CONFIG_AMIGA_PARTITION) || \ 2650c9c8fb5SGabe Black defined(CONFIG_EFI_PARTITION) 2660c9c8fb5SGabe Black 267affae2bfSwdenk static void print_part_header (const char *type, block_dev_desc_t * dev_desc) 268affae2bfSwdenk { 269affae2bfSwdenk puts ("\nPartition Map for "); 270affae2bfSwdenk switch (dev_desc->if_type) { 271726c0f1eSDetlev Zundel case IF_TYPE_IDE: 272726c0f1eSDetlev Zundel puts ("IDE"); 273affae2bfSwdenk break; 274726c0f1eSDetlev Zundel case IF_TYPE_SATA: 275726c0f1eSDetlev Zundel puts ("SATA"); 276c7057b52SDave Liu break; 277726c0f1eSDetlev Zundel case IF_TYPE_SCSI: 278726c0f1eSDetlev Zundel puts ("SCSI"); 279affae2bfSwdenk break; 280726c0f1eSDetlev Zundel case IF_TYPE_ATAPI: 281726c0f1eSDetlev Zundel puts ("ATAPI"); 282affae2bfSwdenk break; 283726c0f1eSDetlev Zundel case IF_TYPE_USB: 284726c0f1eSDetlev Zundel puts ("USB"); 285affae2bfSwdenk break; 286726c0f1eSDetlev Zundel case IF_TYPE_DOC: 287726c0f1eSDetlev Zundel puts ("DOC"); 288affae2bfSwdenk break; 2898f3b9642SLei Wen case IF_TYPE_MMC: 2908f3b9642SLei Wen puts ("MMC"); 2918f3b9642SLei Wen break; 292*f4d8de48SHenrik Nordström case IF_TYPE_HOST: 293*f4d8de48SHenrik Nordström puts("HOST"); 294*f4d8de48SHenrik Nordström break; 295726c0f1eSDetlev Zundel default: 296726c0f1eSDetlev Zundel puts ("UNKNOWN"); 297affae2bfSwdenk break; 298affae2bfSwdenk } 299affae2bfSwdenk printf (" device %d -- Partition Type: %s\n\n", 300affae2bfSwdenk dev_desc->dev, type); 301affae2bfSwdenk } 302affae2bfSwdenk 3030c9c8fb5SGabe Black #endif /* any CONFIG_..._PARTITION */ 3040c9c8fb5SGabe Black 305affae2bfSwdenk void print_part (block_dev_desc_t * dev_desc) 306affae2bfSwdenk { 307affae2bfSwdenk 308affae2bfSwdenk switch (dev_desc->part_type) { 309affae2bfSwdenk #ifdef CONFIG_MAC_PARTITION 310affae2bfSwdenk case PART_TYPE_MAC: 311affae2bfSwdenk PRINTF ("## Testing for valid MAC partition ##\n"); 312affae2bfSwdenk print_part_header ("MAC", dev_desc); 313affae2bfSwdenk print_part_mac (dev_desc); 314affae2bfSwdenk return; 315affae2bfSwdenk #endif 316affae2bfSwdenk #ifdef CONFIG_DOS_PARTITION 317affae2bfSwdenk case PART_TYPE_DOS: 318affae2bfSwdenk PRINTF ("## Testing for valid DOS partition ##\n"); 319affae2bfSwdenk print_part_header ("DOS", dev_desc); 320affae2bfSwdenk print_part_dos (dev_desc); 321affae2bfSwdenk return; 322affae2bfSwdenk #endif 323affae2bfSwdenk 324affae2bfSwdenk #ifdef CONFIG_ISO_PARTITION 325affae2bfSwdenk case PART_TYPE_ISO: 326affae2bfSwdenk PRINTF ("## Testing for valid ISO Boot partition ##\n"); 327affae2bfSwdenk print_part_header ("ISO", dev_desc); 328affae2bfSwdenk print_part_iso (dev_desc); 329affae2bfSwdenk return; 330affae2bfSwdenk #endif 331c7de829cSwdenk 332c7de829cSwdenk #ifdef CONFIG_AMIGA_PARTITION 333c7de829cSwdenk case PART_TYPE_AMIGA: 334c7de829cSwdenk PRINTF ("## Testing for a valid Amiga partition ##\n"); 335c7de829cSwdenk print_part_header ("AMIGA", dev_desc); 336c7de829cSwdenk print_part_amiga (dev_desc); 337c7de829cSwdenk return; 338c7de829cSwdenk #endif 33907f3d789Srichardretanubun 34007f3d789Srichardretanubun #ifdef CONFIG_EFI_PARTITION 34107f3d789Srichardretanubun case PART_TYPE_EFI: 34207f3d789Srichardretanubun PRINTF ("## Testing for valid EFI partition ##\n"); 34307f3d789Srichardretanubun print_part_header ("EFI", dev_desc); 34407f3d789Srichardretanubun print_part_efi (dev_desc); 34507f3d789Srichardretanubun return; 34607f3d789Srichardretanubun #endif 347affae2bfSwdenk } 348affae2bfSwdenk puts ("## Unknown partition table\n"); 349affae2bfSwdenk } 350affae2bfSwdenk 3510c9c8fb5SGabe Black #endif /* HAVE_BLOCK_DEVICE */ 3522f501646SStephen Warren 3532f501646SStephen Warren int get_partition_info(block_dev_desc_t *dev_desc, int part 3542f501646SStephen Warren , disk_partition_t *info) 3552f501646SStephen Warren { 3560c9c8fb5SGabe Black #ifdef HAVE_BLOCK_DEVICE 3572f501646SStephen Warren 358894bfbbfSStephen Warren #ifdef CONFIG_PARTITION_UUIDS 359894bfbbfSStephen Warren /* The common case is no UUID support */ 360894bfbbfSStephen Warren info->uuid[0] = 0; 361894bfbbfSStephen Warren #endif 362894bfbbfSStephen Warren 3632f501646SStephen Warren switch (dev_desc->part_type) { 3642f501646SStephen Warren #ifdef CONFIG_MAC_PARTITION 3652f501646SStephen Warren case PART_TYPE_MAC: 3662f501646SStephen Warren if (get_partition_info_mac(dev_desc, part, info) == 0) { 3672f501646SStephen Warren PRINTF("## Valid MAC partition found ##\n"); 3682f501646SStephen Warren return 0; 3692f501646SStephen Warren } 3702f501646SStephen Warren break; 3712f501646SStephen Warren #endif 3722f501646SStephen Warren 3732f501646SStephen Warren #ifdef CONFIG_DOS_PARTITION 3742f501646SStephen Warren case PART_TYPE_DOS: 3752f501646SStephen Warren if (get_partition_info_dos(dev_desc, part, info) == 0) { 3762f501646SStephen Warren PRINTF("## Valid DOS partition found ##\n"); 3772f501646SStephen Warren return 0; 3782f501646SStephen Warren } 3792f501646SStephen Warren break; 3802f501646SStephen Warren #endif 3812f501646SStephen Warren 3822f501646SStephen Warren #ifdef CONFIG_ISO_PARTITION 3832f501646SStephen Warren case PART_TYPE_ISO: 3842f501646SStephen Warren if (get_partition_info_iso(dev_desc, part, info) == 0) { 3852f501646SStephen Warren PRINTF("## Valid ISO boot partition found ##\n"); 3862f501646SStephen Warren return 0; 3872f501646SStephen Warren } 3882f501646SStephen Warren break; 3892f501646SStephen Warren #endif 3902f501646SStephen Warren 3912f501646SStephen Warren #ifdef CONFIG_AMIGA_PARTITION 3922f501646SStephen Warren case PART_TYPE_AMIGA: 3932f501646SStephen Warren if (get_partition_info_amiga(dev_desc, part, info) == 0) { 3942f501646SStephen Warren PRINTF("## Valid Amiga partition found ##\n"); 3952f501646SStephen Warren return 0; 3962f501646SStephen Warren } 3972f501646SStephen Warren break; 3982f501646SStephen Warren #endif 3992f501646SStephen Warren 4002f501646SStephen Warren #ifdef CONFIG_EFI_PARTITION 4012f501646SStephen Warren case PART_TYPE_EFI: 4022f501646SStephen Warren if (get_partition_info_efi(dev_desc, part, info) == 0) { 4032f501646SStephen Warren PRINTF("## Valid EFI partition found ##\n"); 4042f501646SStephen Warren return 0; 4052f501646SStephen Warren } 4062f501646SStephen Warren break; 4072f501646SStephen Warren #endif 4082f501646SStephen Warren default: 4092f501646SStephen Warren break; 4102f501646SStephen Warren } 4110c9c8fb5SGabe Black #endif /* HAVE_BLOCK_DEVICE */ 4122f501646SStephen Warren 4132f501646SStephen Warren return -1; 4142f501646SStephen Warren } 41599d2c205SRob Herring 4162023e608SStephen Warren int get_device(const char *ifname, const char *dev_str, 4172023e608SStephen Warren block_dev_desc_t **dev_desc) 4182023e608SStephen Warren { 4192023e608SStephen Warren char *ep; 4202023e608SStephen Warren int dev; 4212023e608SStephen Warren 4222023e608SStephen Warren dev = simple_strtoul(dev_str, &ep, 16); 4232023e608SStephen Warren if (*ep) { 4242023e608SStephen Warren printf("** Bad device specification %s %s **\n", 4252023e608SStephen Warren ifname, dev_str); 4262023e608SStephen Warren return -1; 4272023e608SStephen Warren } 4282023e608SStephen Warren 4292023e608SStephen Warren *dev_desc = get_dev(ifname, dev); 4302023e608SStephen Warren if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) { 4312023e608SStephen Warren printf("** Bad device %s %s **\n", ifname, dev_str); 4322023e608SStephen Warren return -1; 4332023e608SStephen Warren } 4342023e608SStephen Warren 4352023e608SStephen Warren return dev; 4362023e608SStephen Warren } 4372023e608SStephen Warren 43810a37fd7SStephen Warren #define PART_UNSPECIFIED -2 43910a37fd7SStephen Warren #define PART_AUTO -1 44010a37fd7SStephen Warren #define MAX_SEARCH_PARTITIONS 16 44110a37fd7SStephen Warren int get_device_and_partition(const char *ifname, const char *dev_part_str, 44299d2c205SRob Herring block_dev_desc_t **dev_desc, 44310a37fd7SStephen Warren disk_partition_t *info, int allow_whole_dev) 44499d2c205SRob Herring { 44510a37fd7SStephen Warren int ret = -1; 44610a37fd7SStephen Warren const char *part_str; 44710a37fd7SStephen Warren char *dup_str = NULL; 44810a37fd7SStephen Warren const char *dev_str; 44999d2c205SRob Herring int dev; 45010a37fd7SStephen Warren char *ep; 45110a37fd7SStephen Warren int p; 45210a37fd7SStephen Warren int part; 45310a37fd7SStephen Warren disk_partition_t tmpinfo; 45499d2c205SRob Herring 45592ccc96bSSimon Glass /* 45692ccc96bSSimon Glass * For now, we have a special case for sandbox, since there is no 45792ccc96bSSimon Glass * real block device support. 45892ccc96bSSimon Glass */ 45992ccc96bSSimon Glass if (0 == strcmp(ifname, "host")) { 46092ccc96bSSimon Glass *dev_desc = NULL; 46192ccc96bSSimon Glass info->start = info->size = info->blksz = 0; 46292ccc96bSSimon Glass info->bootable = 0; 46392ccc96bSSimon Glass strcpy((char *)info->type, BOOT_PART_TYPE); 46492ccc96bSSimon Glass strcpy((char *)info->name, "Sandbox host"); 46592ccc96bSSimon Glass #ifdef CONFIG_PARTITION_UUIDS 46692ccc96bSSimon Glass info->uuid[0] = 0; 46792ccc96bSSimon Glass #endif 46892ccc96bSSimon Glass 46992ccc96bSSimon Glass return 0; 47092ccc96bSSimon Glass } 47192ccc96bSSimon Glass 47210a37fd7SStephen Warren /* If no dev_part_str, use bootdevice environment variable */ 473a10973e7SStephen Warren if (!dev_part_str || !strlen(dev_part_str) || 474a10973e7SStephen Warren !strcmp(dev_part_str, "-")) 47510a37fd7SStephen Warren dev_part_str = getenv("bootdevice"); 47699d2c205SRob Herring 47710a37fd7SStephen Warren /* If still no dev_part_str, it's an error */ 47810a37fd7SStephen Warren if (!dev_part_str) { 47910a37fd7SStephen Warren printf("** No device specified **\n"); 48010a37fd7SStephen Warren goto cleanup; 48199d2c205SRob Herring } 48299d2c205SRob Herring 48310a37fd7SStephen Warren /* Separate device and partition ID specification */ 48410a37fd7SStephen Warren part_str = strchr(dev_part_str, ':'); 48510a37fd7SStephen Warren if (part_str) { 48610a37fd7SStephen Warren dup_str = strdup(dev_part_str); 48710a37fd7SStephen Warren dup_str[part_str - dev_part_str] = 0; 48810a37fd7SStephen Warren dev_str = dup_str; 48910a37fd7SStephen Warren part_str++; 49010a37fd7SStephen Warren } else { 49110a37fd7SStephen Warren dev_str = dev_part_str; 49299d2c205SRob Herring } 49310a37fd7SStephen Warren 49410a37fd7SStephen Warren /* Look up the device */ 49510a37fd7SStephen Warren dev = get_device(ifname, dev_str, dev_desc); 49610a37fd7SStephen Warren if (dev < 0) 49710a37fd7SStephen Warren goto cleanup; 49810a37fd7SStephen Warren 49910a37fd7SStephen Warren /* Convert partition ID string to number */ 50010a37fd7SStephen Warren if (!part_str || !*part_str) { 50110a37fd7SStephen Warren part = PART_UNSPECIFIED; 50210a37fd7SStephen Warren } else if (!strcmp(part_str, "auto")) { 50310a37fd7SStephen Warren part = PART_AUTO; 50410a37fd7SStephen Warren } else { 50510a37fd7SStephen Warren /* Something specified -> use exactly that */ 50610a37fd7SStephen Warren part = (int)simple_strtoul(part_str, &ep, 16); 50710a37fd7SStephen Warren /* 50810a37fd7SStephen Warren * Less than whole string converted, 50910a37fd7SStephen Warren * or request for whole device, but caller requires partition. 51010a37fd7SStephen Warren */ 51110a37fd7SStephen Warren if (*ep || (part == 0 && !allow_whole_dev)) { 51210a37fd7SStephen Warren printf("** Bad partition specification %s %s **\n", 51310a37fd7SStephen Warren ifname, dev_part_str); 51410a37fd7SStephen Warren goto cleanup; 51510a37fd7SStephen Warren } 51610a37fd7SStephen Warren } 51710a37fd7SStephen Warren 51810a37fd7SStephen Warren /* 51910a37fd7SStephen Warren * No partition table on device, 52010a37fd7SStephen Warren * or user requested partition 0 (entire device). 52110a37fd7SStephen Warren */ 52210a37fd7SStephen Warren if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) || 52310a37fd7SStephen Warren (part == 0)) { 52410a37fd7SStephen Warren if (!(*dev_desc)->lba) { 52510a37fd7SStephen Warren printf("** Bad device size - %s %s **\n", ifname, 52610a37fd7SStephen Warren dev_str); 52710a37fd7SStephen Warren goto cleanup; 52810a37fd7SStephen Warren } 52910a37fd7SStephen Warren 53010a37fd7SStephen Warren /* 53110a37fd7SStephen Warren * If user specified a partition ID other than 0, 53210a37fd7SStephen Warren * or the calling command only accepts partitions, 53310a37fd7SStephen Warren * it's an error. 53410a37fd7SStephen Warren */ 53510a37fd7SStephen Warren if ((part > 0) || (!allow_whole_dev)) { 53610a37fd7SStephen Warren printf("** No partition table - %s %s **\n", ifname, 53710a37fd7SStephen Warren dev_str); 53810a37fd7SStephen Warren goto cleanup; 53910a37fd7SStephen Warren } 54010a37fd7SStephen Warren 54150ffc3b6SLan Yixun (dlan) (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); 54250ffc3b6SLan Yixun (dlan) 54399d2c205SRob Herring info->start = 0; 54410a37fd7SStephen Warren info->size = (*dev_desc)->lba; 54510a37fd7SStephen Warren info->blksz = (*dev_desc)->blksz; 54610a37fd7SStephen Warren info->bootable = 0; 5476ab6a650SStephen Warren strcpy((char *)info->type, BOOT_PART_TYPE); 5486ab6a650SStephen Warren strcpy((char *)info->name, "Whole Disk"); 54910a37fd7SStephen Warren #ifdef CONFIG_PARTITION_UUIDS 55010a37fd7SStephen Warren info->uuid[0] = 0; 55110a37fd7SStephen Warren #endif 55299d2c205SRob Herring 55310a37fd7SStephen Warren ret = 0; 55410a37fd7SStephen Warren goto cleanup; 55599d2c205SRob Herring } 55699d2c205SRob Herring 55710a37fd7SStephen Warren /* 55810a37fd7SStephen Warren * Now there's known to be a partition table, 55910a37fd7SStephen Warren * not specifying a partition means to pick partition 1. 56010a37fd7SStephen Warren */ 56110a37fd7SStephen Warren if (part == PART_UNSPECIFIED) 56210a37fd7SStephen Warren part = 1; 56399d2c205SRob Herring 56410a37fd7SStephen Warren /* 56510a37fd7SStephen Warren * If user didn't specify a partition number, or did specify something 56610a37fd7SStephen Warren * other than "auto", use that partition number directly. 56710a37fd7SStephen Warren */ 56810a37fd7SStephen Warren if (part != PART_AUTO) { 56910a37fd7SStephen Warren ret = get_partition_info(*dev_desc, part, info); 57099d2c205SRob Herring if (ret) { 57110a37fd7SStephen Warren printf("** Invalid partition %d **\n", part); 57210a37fd7SStephen Warren goto cleanup; 57310a37fd7SStephen Warren } 57410a37fd7SStephen Warren } else { 57510a37fd7SStephen Warren /* 57610a37fd7SStephen Warren * Find the first bootable partition. 57710a37fd7SStephen Warren * If none are bootable, fall back to the first valid partition. 57810a37fd7SStephen Warren */ 57910a37fd7SStephen Warren part = 0; 58010a37fd7SStephen Warren for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { 58110a37fd7SStephen Warren ret = get_partition_info(*dev_desc, p, info); 58210a37fd7SStephen Warren if (ret) 58310a37fd7SStephen Warren continue; 58410a37fd7SStephen Warren 58510a37fd7SStephen Warren /* 58610a37fd7SStephen Warren * First valid partition, or new better partition? 58710a37fd7SStephen Warren * If so, save partition ID. 58810a37fd7SStephen Warren */ 58910a37fd7SStephen Warren if (!part || info->bootable) 59010a37fd7SStephen Warren part = p; 59110a37fd7SStephen Warren 59210a37fd7SStephen Warren /* Best possible partition? Stop searching. */ 59310a37fd7SStephen Warren if (info->bootable) 59410a37fd7SStephen Warren break; 59510a37fd7SStephen Warren 59610a37fd7SStephen Warren /* 59710a37fd7SStephen Warren * We now need to search further for best possible. 59810a37fd7SStephen Warren * If we what we just queried was the best so far, 59910a37fd7SStephen Warren * save the info since we over-write it next loop. 60010a37fd7SStephen Warren */ 60110a37fd7SStephen Warren if (part == p) 60210a37fd7SStephen Warren tmpinfo = *info; 60310a37fd7SStephen Warren } 60410a37fd7SStephen Warren /* If we found any acceptable partition */ 60510a37fd7SStephen Warren if (part) { 60610a37fd7SStephen Warren /* 60710a37fd7SStephen Warren * If we searched all possible partition IDs, 60810a37fd7SStephen Warren * return the first valid partition we found. 60910a37fd7SStephen Warren */ 61010a37fd7SStephen Warren if (p == MAX_SEARCH_PARTITIONS + 1) 61110a37fd7SStephen Warren *info = tmpinfo; 61210a37fd7SStephen Warren } else { 61310a37fd7SStephen Warren printf("** No valid partitions found **\n"); 61471bba424SStephen Warren ret = -1; 61510a37fd7SStephen Warren goto cleanup; 61610a37fd7SStephen Warren } 61799d2c205SRob Herring } 61899d2c205SRob Herring if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) { 61999d2c205SRob Herring printf("** Invalid partition type \"%.32s\"" 62099d2c205SRob Herring " (expect \"" BOOT_PART_TYPE "\")\n", 62199d2c205SRob Herring info->type); 62210a37fd7SStephen Warren ret = -1; 62310a37fd7SStephen Warren goto cleanup; 62499d2c205SRob Herring } 62599d2c205SRob Herring 62650ffc3b6SLan Yixun (dlan) (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); 62750ffc3b6SLan Yixun (dlan) 62810a37fd7SStephen Warren ret = part; 62910a37fd7SStephen Warren goto cleanup; 63099d2c205SRob Herring 63110a37fd7SStephen Warren cleanup: 63210a37fd7SStephen Warren free(dup_str); 63310a37fd7SStephen Warren return ret; 63499d2c205SRob Herring } 635