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