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