wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <ide.h> |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 12 | #include <part.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 13 | |
| 14 | #undef PART_DEBUG |
| 15 | |
| 16 | #ifdef PART_DEBUG |
| 17 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 18 | #else |
| 19 | #define PRINTF(fmt,args...) |
| 20 | #endif |
| 21 | |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 22 | struct block_drvr { |
| 23 | char *name; |
| 24 | block_dev_desc_t* (*get_dev)(int dev); |
| 25 | }; |
| 26 | |
| 27 | static const struct block_drvr block_drvr[] = { |
Jon Loeliger | f7f54a8 | 2007-07-09 17:22:37 -0500 | [diff] [blame] | 28 | #if defined(CONFIG_CMD_IDE) |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 29 | { .name = "ide", .get_dev = ide_get_dev, }, |
| 30 | #endif |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 31 | #if defined(CONFIG_CMD_SATA) |
| 32 | {.name = "sata", .get_dev = sata_get_dev, }, |
| 33 | #endif |
Jon Loeliger | f7f54a8 | 2007-07-09 17:22:37 -0500 | [diff] [blame] | 34 | #if defined(CONFIG_CMD_SCSI) |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 35 | { .name = "scsi", .get_dev = scsi_get_dev, }, |
| 36 | #endif |
Jon Loeliger | f7f54a8 | 2007-07-09 17:22:37 -0500 | [diff] [blame] | 37 | #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 38 | { .name = "usb", .get_dev = usb_stor_get_dev, }, |
| 39 | #endif |
| 40 | #if defined(CONFIG_MMC) |
| 41 | { .name = "mmc", .get_dev = mmc_get_dev, }, |
| 42 | #endif |
| 43 | #if defined(CONFIG_SYSTEMACE) |
| 44 | { .name = "ace", .get_dev = systemace_get_dev, }, |
| 45 | #endif |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 46 | #if defined(CONFIG_SANDBOX) |
| 47 | { .name = "host", .get_dev = host_get_dev, }, |
| 48 | #endif |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 49 | { }, |
| 50 | }; |
| 51 | |
Stefan Roese | 61ddee4 | 2007-02-20 13:21:57 +0100 | [diff] [blame] | 52 | DECLARE_GLOBAL_DATA_PTR; |
Stefan Roese | 61ddee4 | 2007-02-20 13:21:57 +0100 | [diff] [blame] | 53 | |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 54 | #ifdef HAVE_BLOCK_DEVICE |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 55 | block_dev_desc_t *get_dev(const char *ifname, int dev) |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 56 | { |
| 57 | const struct block_drvr *drvr = block_drvr; |
Stefan Roese | 5b084e1 | 2007-02-22 07:43:34 +0100 | [diff] [blame] | 58 | block_dev_desc_t* (*reloc_get_dev)(int dev); |
Heiko Schocher | 9b98723 | 2010-09-17 13:10:36 +0200 | [diff] [blame] | 59 | char *name; |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 60 | |
Tim Kientzle | 47e444c | 2012-03-27 11:43:25 +0200 | [diff] [blame] | 61 | if (!ifname) |
| 62 | return NULL; |
| 63 | |
Heiko Schocher | 9b98723 | 2010-09-17 13:10:36 +0200 | [diff] [blame] | 64 | name = drvr->name; |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 65 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Heiko Schocher | 9b98723 | 2010-09-17 13:10:36 +0200 | [diff] [blame] | 66 | name += gd->reloc_off; |
| 67 | #endif |
Lei Wen | 2a01067 | 2011-02-15 16:56:40 +0800 | [diff] [blame] | 68 | while (drvr->name) { |
Heiko Schocher | 9b98723 | 2010-09-17 13:10:36 +0200 | [diff] [blame] | 69 | name = drvr->name; |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 70 | reloc_get_dev = drvr->get_dev; |
Wolfgang Denk | d0813e5 | 2010-10-28 20:00:11 +0200 | [diff] [blame] | 71 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
Heiko Schocher | 9b98723 | 2010-09-17 13:10:36 +0200 | [diff] [blame] | 72 | name += gd->reloc_off; |
Peter Tyser | 9057cbf | 2009-09-21 11:20:36 -0500 | [diff] [blame] | 73 | reloc_get_dev += gd->reloc_off; |
| 74 | #endif |
Heiko Schocher | 9b98723 | 2010-09-17 13:10:36 +0200 | [diff] [blame] | 75 | if (strncmp(ifname, name, strlen(name)) == 0) |
Stefan Roese | 61ddee4 | 2007-02-20 13:21:57 +0100 | [diff] [blame] | 76 | return reloc_get_dev(dev); |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 77 | drvr++; |
| 78 | } |
| 79 | return NULL; |
| 80 | } |
| 81 | #else |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 82 | block_dev_desc_t *get_dev(const char *ifname, int dev) |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 83 | { |
| 84 | return NULL; |
| 85 | } |
| 86 | #endif |
| 87 | |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 88 | #ifdef HAVE_BLOCK_DEVICE |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 89 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 90 | /* ------------------------------------------------------------------------- */ |
| 91 | /* |
| 92 | * reports device info to the user |
| 93 | */ |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 94 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 95 | #ifdef CONFIG_LBA48 |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 96 | typedef uint64_t lba512_t; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 97 | #else |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 98 | typedef lbaint_t lba512_t; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 99 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 100 | |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 101 | /* |
| 102 | * Overflowless variant of (block_count * mul_by / div_by) |
| 103 | * when div_by > mul_by |
| 104 | */ |
| 105 | static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by) |
| 106 | { |
| 107 | lba512_t bc_quot, bc_rem; |
| 108 | |
| 109 | /* x * m / d == x / d * m + (x % d) * m / d */ |
| 110 | bc_quot = block_count / div_by; |
| 111 | bc_rem = block_count - div_by * bc_quot; |
| 112 | return bc_quot * mul_by + (bc_rem * mul_by) / div_by; |
| 113 | } |
| 114 | |
| 115 | void dev_print (block_dev_desc_t *dev_desc) |
| 116 | { |
| 117 | lba512_t lba512; /* number of blocks if 512bytes block size */ |
| 118 | |
Wolfgang Denk | 8561e71 | 2009-05-15 09:27:58 +0200 | [diff] [blame] | 119 | if (dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 120 | puts ("not available\n"); |
| 121 | return; |
| 122 | } |
| 123 | |
Tor Krill | 03ff330 | 2008-05-29 11:10:30 +0200 | [diff] [blame] | 124 | switch (dev_desc->if_type) { |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 125 | case IF_TYPE_SCSI: |
| 126 | printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", |
| 127 | dev_desc->target,dev_desc->lun, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 128 | dev_desc->vendor, |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 129 | dev_desc->product, |
| 130 | dev_desc->revision); |
| 131 | break; |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 132 | case IF_TYPE_ATAPI: |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 133 | case IF_TYPE_IDE: |
| 134 | case IF_TYPE_SATA: |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 135 | printf ("Model: %s Firm: %s Ser#: %s\n", |
| 136 | dev_desc->vendor, |
| 137 | dev_desc->revision, |
| 138 | dev_desc->product); |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 139 | break; |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 140 | case IF_TYPE_SD: |
| 141 | case IF_TYPE_MMC: |
NÃcolas Carneiro Lebedenco | ef3edd4 | 2008-09-04 15:35:46 -0300 | [diff] [blame] | 142 | case IF_TYPE_USB: |
| 143 | printf ("Vendor: %s Rev: %s Prod: %s\n", |
| 144 | dev_desc->vendor, |
| 145 | dev_desc->revision, |
| 146 | dev_desc->product); |
| 147 | break; |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 148 | case IF_TYPE_DOC: |
| 149 | puts("device type DOC\n"); |
| 150 | return; |
Tor Krill | 03ff330 | 2008-05-29 11:10:30 +0200 | [diff] [blame] | 151 | case IF_TYPE_UNKNOWN: |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 152 | puts("device type unknown\n"); |
| 153 | return; |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 154 | default: |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 155 | printf("Unhandled device type: %i\n", dev_desc->if_type); |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 156 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 157 | } |
| 158 | puts (" Type: "); |
| 159 | if (dev_desc->removable) |
| 160 | puts ("Removable "); |
| 161 | switch (dev_desc->type & 0x1F) { |
Detlev Zundel | 0e11cc6 | 2008-05-05 16:11:22 +0200 | [diff] [blame] | 162 | case DEV_TYPE_HARDDISK: |
| 163 | puts ("Hard Disk"); |
| 164 | break; |
| 165 | case DEV_TYPE_CDROM: |
| 166 | puts ("CD ROM"); |
| 167 | break; |
| 168 | case DEV_TYPE_OPDISK: |
| 169 | puts ("Optical Device"); |
| 170 | break; |
| 171 | case DEV_TYPE_TAPE: |
| 172 | puts ("Tape"); |
| 173 | break; |
| 174 | default: |
| 175 | printf ("# %02X #", dev_desc->type & 0x1F); |
| 176 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 177 | } |
| 178 | puts ("\n"); |
Jerry Huang | 6a4feb4 | 2012-11-06 15:33:12 +0000 | [diff] [blame] | 179 | if (dev_desc->lba > 0L && dev_desc->blksz > 0L) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 180 | ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 181 | lbaint_t lba; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 182 | |
| 183 | lba = dev_desc->lba; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 184 | |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 185 | lba512 = (lba * (dev_desc->blksz/512)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 186 | /* round to 1 digit */ |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 187 | mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */ |
| 188 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 189 | mb_quot = mb / 10; |
| 190 | mb_rem = mb - (10 * mb_quot); |
| 191 | |
| 192 | gb = mb / 1024; |
| 193 | gb_quot = gb / 10; |
| 194 | gb_rem = gb - (10 * gb_quot); |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_LBA48 |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 196 | if (dev_desc->lba48) |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 197 | printf (" Supports 48-bit addressing\n"); |
| 198 | #endif |
Heiko Schocher | 0f602e1 | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 199 | #if defined(CONFIG_SYS_64BIT_LBA) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 200 | printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n", |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 201 | mb_quot, mb_rem, |
| 202 | gb_quot, gb_rem, |
| 203 | lba, |
| 204 | dev_desc->blksz); |
| 205 | #else |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 206 | printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n", |
| 207 | mb_quot, mb_rem, |
| 208 | gb_quot, gb_rem, |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 209 | (ulong)lba, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 210 | dev_desc->blksz); |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 211 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 212 | } else { |
| 213 | puts (" Capacity: not available\n"); |
| 214 | } |
| 215 | } |
Jon Loeliger | 1670a5b | 2007-07-10 11:19:50 -0500 | [diff] [blame] | 216 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 217 | |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 218 | #ifdef HAVE_BLOCK_DEVICE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 219 | |
| 220 | void init_part (block_dev_desc_t * dev_desc) |
| 221 | { |
| 222 | #ifdef CONFIG_ISO_PARTITION |
| 223 | if (test_part_iso(dev_desc) == 0) { |
| 224 | dev_desc->part_type = PART_TYPE_ISO; |
| 225 | return; |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | #ifdef CONFIG_MAC_PARTITION |
| 230 | if (test_part_mac(dev_desc) == 0) { |
| 231 | dev_desc->part_type = PART_TYPE_MAC; |
| 232 | return; |
| 233 | } |
| 234 | #endif |
| 235 | |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 236 | /* must be placed before DOS partition detection */ |
| 237 | #ifdef CONFIG_EFI_PARTITION |
| 238 | if (test_part_efi(dev_desc) == 0) { |
| 239 | dev_desc->part_type = PART_TYPE_EFI; |
| 240 | return; |
| 241 | } |
| 242 | #endif |
| 243 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 244 | #ifdef CONFIG_DOS_PARTITION |
| 245 | if (test_part_dos(dev_desc) == 0) { |
| 246 | dev_desc->part_type = PART_TYPE_DOS; |
| 247 | return; |
| 248 | } |
| 249 | #endif |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef CONFIG_AMIGA_PARTITION |
| 252 | if (test_part_amiga(dev_desc) == 0) { |
| 253 | dev_desc->part_type = PART_TYPE_AMIGA; |
| 254 | return; |
| 255 | } |
| 256 | #endif |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 257 | dev_desc->part_type = PART_TYPE_UNKNOWN; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 261 | #if defined(CONFIG_MAC_PARTITION) || \ |
| 262 | defined(CONFIG_DOS_PARTITION) || \ |
| 263 | defined(CONFIG_ISO_PARTITION) || \ |
| 264 | defined(CONFIG_AMIGA_PARTITION) || \ |
| 265 | defined(CONFIG_EFI_PARTITION) |
| 266 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 267 | static void print_part_header (const char *type, block_dev_desc_t * dev_desc) |
| 268 | { |
| 269 | puts ("\nPartition Map for "); |
| 270 | switch (dev_desc->if_type) { |
Detlev Zundel | 0e11cc6 | 2008-05-05 16:11:22 +0200 | [diff] [blame] | 271 | case IF_TYPE_IDE: |
| 272 | puts ("IDE"); |
| 273 | break; |
| 274 | case IF_TYPE_SATA: |
| 275 | puts ("SATA"); |
| 276 | break; |
| 277 | case IF_TYPE_SCSI: |
| 278 | puts ("SCSI"); |
| 279 | break; |
| 280 | case IF_TYPE_ATAPI: |
| 281 | puts ("ATAPI"); |
| 282 | break; |
| 283 | case IF_TYPE_USB: |
| 284 | puts ("USB"); |
| 285 | break; |
| 286 | case IF_TYPE_DOC: |
| 287 | puts ("DOC"); |
| 288 | break; |
Lei Wen | 61dba93 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 289 | case IF_TYPE_MMC: |
| 290 | puts ("MMC"); |
| 291 | break; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 292 | case IF_TYPE_HOST: |
| 293 | puts("HOST"); |
| 294 | break; |
Detlev Zundel | 0e11cc6 | 2008-05-05 16:11:22 +0200 | [diff] [blame] | 295 | default: |
| 296 | puts ("UNKNOWN"); |
| 297 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 298 | } |
| 299 | printf (" device %d -- Partition Type: %s\n\n", |
| 300 | dev_desc->dev, type); |
| 301 | } |
| 302 | |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 303 | #endif /* any CONFIG_..._PARTITION */ |
| 304 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 305 | void print_part (block_dev_desc_t * dev_desc) |
| 306 | { |
| 307 | |
| 308 | switch (dev_desc->part_type) { |
| 309 | #ifdef CONFIG_MAC_PARTITION |
| 310 | case PART_TYPE_MAC: |
| 311 | PRINTF ("## Testing for valid MAC partition ##\n"); |
| 312 | print_part_header ("MAC", dev_desc); |
| 313 | print_part_mac (dev_desc); |
| 314 | return; |
| 315 | #endif |
| 316 | #ifdef CONFIG_DOS_PARTITION |
| 317 | case PART_TYPE_DOS: |
| 318 | PRINTF ("## Testing for valid DOS partition ##\n"); |
| 319 | print_part_header ("DOS", dev_desc); |
| 320 | print_part_dos (dev_desc); |
| 321 | return; |
| 322 | #endif |
| 323 | |
| 324 | #ifdef CONFIG_ISO_PARTITION |
| 325 | case PART_TYPE_ISO: |
| 326 | PRINTF ("## Testing for valid ISO Boot partition ##\n"); |
| 327 | print_part_header ("ISO", dev_desc); |
| 328 | print_part_iso (dev_desc); |
| 329 | return; |
| 330 | #endif |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 331 | |
| 332 | #ifdef CONFIG_AMIGA_PARTITION |
| 333 | case PART_TYPE_AMIGA: |
| 334 | PRINTF ("## Testing for a valid Amiga partition ##\n"); |
| 335 | print_part_header ("AMIGA", dev_desc); |
| 336 | print_part_amiga (dev_desc); |
| 337 | return; |
| 338 | #endif |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 339 | |
| 340 | #ifdef CONFIG_EFI_PARTITION |
| 341 | case PART_TYPE_EFI: |
| 342 | PRINTF ("## Testing for valid EFI partition ##\n"); |
| 343 | print_part_header ("EFI", dev_desc); |
| 344 | print_part_efi (dev_desc); |
| 345 | return; |
| 346 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 347 | } |
| 348 | puts ("## Unknown partition table\n"); |
| 349 | } |
| 350 | |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 351 | #endif /* HAVE_BLOCK_DEVICE */ |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 352 | |
| 353 | int get_partition_info(block_dev_desc_t *dev_desc, int part |
| 354 | , disk_partition_t *info) |
| 355 | { |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 356 | #ifdef HAVE_BLOCK_DEVICE |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 357 | |
Stephen Warren | c6c7b7a | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 358 | #ifdef CONFIG_PARTITION_UUIDS |
| 359 | /* The common case is no UUID support */ |
| 360 | info->uuid[0] = 0; |
| 361 | #endif |
| 362 | |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 363 | switch (dev_desc->part_type) { |
| 364 | #ifdef CONFIG_MAC_PARTITION |
| 365 | case PART_TYPE_MAC: |
| 366 | if (get_partition_info_mac(dev_desc, part, info) == 0) { |
| 367 | PRINTF("## Valid MAC partition found ##\n"); |
| 368 | return 0; |
| 369 | } |
| 370 | break; |
| 371 | #endif |
| 372 | |
| 373 | #ifdef CONFIG_DOS_PARTITION |
| 374 | case PART_TYPE_DOS: |
| 375 | if (get_partition_info_dos(dev_desc, part, info) == 0) { |
| 376 | PRINTF("## Valid DOS partition found ##\n"); |
| 377 | return 0; |
| 378 | } |
| 379 | break; |
| 380 | #endif |
| 381 | |
| 382 | #ifdef CONFIG_ISO_PARTITION |
| 383 | case PART_TYPE_ISO: |
| 384 | if (get_partition_info_iso(dev_desc, part, info) == 0) { |
| 385 | PRINTF("## Valid ISO boot partition found ##\n"); |
| 386 | return 0; |
| 387 | } |
| 388 | break; |
| 389 | #endif |
| 390 | |
| 391 | #ifdef CONFIG_AMIGA_PARTITION |
| 392 | case PART_TYPE_AMIGA: |
| 393 | if (get_partition_info_amiga(dev_desc, part, info) == 0) { |
| 394 | PRINTF("## Valid Amiga partition found ##\n"); |
| 395 | return 0; |
| 396 | } |
| 397 | break; |
| 398 | #endif |
| 399 | |
| 400 | #ifdef CONFIG_EFI_PARTITION |
| 401 | case PART_TYPE_EFI: |
| 402 | if (get_partition_info_efi(dev_desc, part, info) == 0) { |
| 403 | PRINTF("## Valid EFI partition found ##\n"); |
| 404 | return 0; |
| 405 | } |
| 406 | break; |
| 407 | #endif |
| 408 | default: |
| 409 | break; |
| 410 | } |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 411 | #endif /* HAVE_BLOCK_DEVICE */ |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 412 | |
| 413 | return -1; |
| 414 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 415 | |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 416 | int get_device(const char *ifname, const char *dev_str, |
| 417 | block_dev_desc_t **dev_desc) |
| 418 | { |
| 419 | char *ep; |
| 420 | int dev; |
| 421 | |
| 422 | dev = simple_strtoul(dev_str, &ep, 16); |
| 423 | if (*ep) { |
| 424 | printf("** Bad device specification %s %s **\n", |
| 425 | ifname, dev_str); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | *dev_desc = get_dev(ifname, dev); |
| 430 | if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) { |
| 431 | printf("** Bad device %s %s **\n", ifname, dev_str); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | return dev; |
| 436 | } |
| 437 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 438 | #define PART_UNSPECIFIED -2 |
| 439 | #define PART_AUTO -1 |
| 440 | #define MAX_SEARCH_PARTITIONS 16 |
| 441 | int get_device_and_partition(const char *ifname, const char *dev_part_str, |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 442 | block_dev_desc_t **dev_desc, |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 443 | disk_partition_t *info, int allow_whole_dev) |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 444 | { |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 445 | int ret = -1; |
| 446 | const char *part_str; |
| 447 | char *dup_str = NULL; |
| 448 | const char *dev_str; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 449 | int dev; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 450 | char *ep; |
| 451 | int p; |
| 452 | int part; |
| 453 | disk_partition_t tmpinfo; |
| 454 | |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 455 | /* |
| 456 | * For now, we have a special case for sandbox, since there is no |
| 457 | * real block device support. |
| 458 | */ |
| 459 | if (0 == strcmp(ifname, "host")) { |
| 460 | *dev_desc = NULL; |
| 461 | info->start = info->size = info->blksz = 0; |
| 462 | info->bootable = 0; |
| 463 | strcpy((char *)info->type, BOOT_PART_TYPE); |
| 464 | strcpy((char *)info->name, "Sandbox host"); |
| 465 | #ifdef CONFIG_PARTITION_UUIDS |
| 466 | info->uuid[0] = 0; |
| 467 | #endif |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 472 | /* If no dev_part_str, use bootdevice environment variable */ |
Stephen Warren | a95af94 | 2012-09-28 05:34:09 +0000 | [diff] [blame] | 473 | if (!dev_part_str || !strlen(dev_part_str) || |
| 474 | !strcmp(dev_part_str, "-")) |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 475 | dev_part_str = getenv("bootdevice"); |
| 476 | |
| 477 | /* If still no dev_part_str, it's an error */ |
| 478 | if (!dev_part_str) { |
| 479 | printf("** No device specified **\n"); |
| 480 | goto cleanup; |
| 481 | } |
| 482 | |
| 483 | /* Separate device and partition ID specification */ |
| 484 | part_str = strchr(dev_part_str, ':'); |
| 485 | if (part_str) { |
| 486 | dup_str = strdup(dev_part_str); |
| 487 | dup_str[part_str - dev_part_str] = 0; |
| 488 | dev_str = dup_str; |
| 489 | part_str++; |
| 490 | } else { |
| 491 | dev_str = dev_part_str; |
| 492 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 493 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 494 | /* Look up the device */ |
| 495 | dev = get_device(ifname, dev_str, dev_desc); |
| 496 | if (dev < 0) |
| 497 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 498 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 499 | /* Convert partition ID string to number */ |
| 500 | if (!part_str || !*part_str) { |
| 501 | part = PART_UNSPECIFIED; |
| 502 | } else if (!strcmp(part_str, "auto")) { |
| 503 | part = PART_AUTO; |
| 504 | } else { |
| 505 | /* Something specified -> use exactly that */ |
| 506 | part = (int)simple_strtoul(part_str, &ep, 16); |
| 507 | /* |
| 508 | * Less than whole string converted, |
| 509 | * or request for whole device, but caller requires partition. |
| 510 | */ |
| 511 | if (*ep || (part == 0 && !allow_whole_dev)) { |
| 512 | printf("** Bad partition specification %s %s **\n", |
| 513 | ifname, dev_part_str); |
| 514 | goto cleanup; |
| 515 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 518 | /* |
| 519 | * No partition table on device, |
| 520 | * or user requested partition 0 (entire device). |
| 521 | */ |
| 522 | if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) || |
| 523 | (part == 0)) { |
| 524 | if (!(*dev_desc)->lba) { |
| 525 | printf("** Bad device size - %s %s **\n", ifname, |
| 526 | dev_str); |
| 527 | goto cleanup; |
| 528 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 529 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 530 | /* |
| 531 | * If user specified a partition ID other than 0, |
| 532 | * or the calling command only accepts partitions, |
| 533 | * it's an error. |
| 534 | */ |
| 535 | if ((part > 0) || (!allow_whole_dev)) { |
| 536 | printf("** No partition table - %s %s **\n", ifname, |
| 537 | dev_str); |
| 538 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 539 | } |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 540 | |
Lan Yixun (dlan) | 138914e | 2013-07-20 08:17:59 +0800 | [diff] [blame] | 541 | (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); |
| 542 | |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 543 | info->start = 0; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 544 | info->size = (*dev_desc)->lba; |
| 545 | info->blksz = (*dev_desc)->blksz; |
| 546 | info->bootable = 0; |
Stephen Warren | 65be849 | 2012-10-10 07:57:51 +0000 | [diff] [blame] | 547 | strcpy((char *)info->type, BOOT_PART_TYPE); |
| 548 | strcpy((char *)info->name, "Whole Disk"); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 549 | #ifdef CONFIG_PARTITION_UUIDS |
| 550 | info->uuid[0] = 0; |
| 551 | #endif |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 552 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 553 | ret = 0; |
| 554 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 557 | /* |
| 558 | * Now there's known to be a partition table, |
| 559 | * not specifying a partition means to pick partition 1. |
| 560 | */ |
| 561 | if (part == PART_UNSPECIFIED) |
| 562 | part = 1; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 563 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 564 | /* |
| 565 | * If user didn't specify a partition number, or did specify something |
| 566 | * other than "auto", use that partition number directly. |
| 567 | */ |
| 568 | if (part != PART_AUTO) { |
| 569 | ret = get_partition_info(*dev_desc, part, info); |
| 570 | if (ret) { |
| 571 | printf("** Invalid partition %d **\n", part); |
| 572 | goto cleanup; |
| 573 | } |
| 574 | } else { |
| 575 | /* |
| 576 | * Find the first bootable partition. |
| 577 | * If none are bootable, fall back to the first valid partition. |
| 578 | */ |
| 579 | part = 0; |
| 580 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 581 | ret = get_partition_info(*dev_desc, p, info); |
| 582 | if (ret) |
| 583 | continue; |
| 584 | |
| 585 | /* |
| 586 | * First valid partition, or new better partition? |
| 587 | * If so, save partition ID. |
| 588 | */ |
| 589 | if (!part || info->bootable) |
| 590 | part = p; |
| 591 | |
| 592 | /* Best possible partition? Stop searching. */ |
| 593 | if (info->bootable) |
| 594 | break; |
| 595 | |
| 596 | /* |
| 597 | * We now need to search further for best possible. |
| 598 | * If we what we just queried was the best so far, |
| 599 | * save the info since we over-write it next loop. |
| 600 | */ |
| 601 | if (part == p) |
| 602 | tmpinfo = *info; |
| 603 | } |
| 604 | /* If we found any acceptable partition */ |
| 605 | if (part) { |
| 606 | /* |
| 607 | * If we searched all possible partition IDs, |
| 608 | * return the first valid partition we found. |
| 609 | */ |
| 610 | if (p == MAX_SEARCH_PARTITIONS + 1) |
| 611 | *info = tmpinfo; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 612 | } else { |
| 613 | printf("** No valid partitions found **\n"); |
Stephen Warren | 3c28be9 | 2012-10-08 07:45:54 +0000 | [diff] [blame] | 614 | ret = -1; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 615 | goto cleanup; |
| 616 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 617 | } |
| 618 | if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) { |
| 619 | printf("** Invalid partition type \"%.32s\"" |
| 620 | " (expect \"" BOOT_PART_TYPE "\")\n", |
| 621 | info->type); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 622 | ret = -1; |
| 623 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Lan Yixun (dlan) | 138914e | 2013-07-20 08:17:59 +0800 | [diff] [blame] | 626 | (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); |
| 627 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 628 | ret = part; |
| 629 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 630 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 631 | cleanup: |
| 632 | free(dup_str); |
| 633 | return ret; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 634 | } |