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