Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 9 | #include <env.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 | |
Sam Protsenko | dee8038 | 2017-09-22 01:51:58 +0300 | [diff] [blame] | 24 | /* Check all partition types */ |
| 25 | #define PART_TYPE_ALL -1 |
| 26 | |
Kever Yang | 1c63624 | 2018-02-10 17:55:37 +0800 | [diff] [blame] | 27 | static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc) |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 28 | { |
| 29 | struct part_driver *drv = |
| 30 | ll_entry_start(struct part_driver, part_driver); |
| 31 | const int n_ents = ll_entry_count(struct part_driver, part_driver); |
| 32 | struct part_driver *entry; |
| 33 | |
Kever Yang | 1c63624 | 2018-02-10 17:55:37 +0800 | [diff] [blame] | 34 | if (dev_desc->part_type == PART_TYPE_UNKNOWN) { |
| 35 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 36 | int ret; |
| 37 | |
| 38 | ret = entry->test(dev_desc); |
| 39 | if (!ret) { |
| 40 | dev_desc->part_type = entry->part_type; |
| 41 | return entry; |
| 42 | } |
| 43 | } |
| 44 | } else { |
| 45 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 46 | if (dev_desc->part_type == entry->part_type) |
| 47 | return entry; |
| 48 | } |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* Not found */ |
| 52 | return NULL; |
| 53 | } |
| 54 | |
Kever Yang | 9182966 | 2018-02-10 17:55:38 +0800 | [diff] [blame] | 55 | #ifdef CONFIG_HAVE_BLOCK_DEVICE |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 56 | 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] | 57 | { |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 58 | struct blk_desc *dev_desc; |
| 59 | int ret; |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 60 | |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 61 | dev_desc = blk_get_devnum_by_typename(ifname, dev); |
| 62 | if (!dev_desc) { |
| 63 | debug("%s: No device for iface '%s', dev %d\n", __func__, |
| 64 | ifname, dev); |
Tim Kientzle | 47e444c | 2012-03-27 11:43:25 +0200 | [diff] [blame] | 65 | return NULL; |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 66 | } |
Simon Glass | e3dfa91 | 2016-05-01 13:52:32 -0600 | [diff] [blame] | 67 | ret = blk_dselect_hwpart(dev_desc, hwpart); |
| 68 | if (ret) { |
| 69 | debug("%s: Failed to select h/w partition: err-%d\n", __func__, |
| 70 | ret); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | return dev_desc; |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 75 | } |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 76 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 77 | struct blk_desc *blk_get_dev(const char *ifname, int dev) |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 78 | { |
Stephen Warren | ad17c0f | 2014-05-23 12:48:11 -0600 | [diff] [blame] | 79 | return get_dev_hwpart(ifname, dev, 0); |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 80 | } |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 81 | #else |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | return NULL; |
| 85 | } |
| 86 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 87 | struct blk_desc *blk_get_dev(const char *ifname, int dev) |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 88 | { |
| 89 | return NULL; |
| 90 | } |
| 91 | #endif |
| 92 | |
Adam Ford | b10ba90 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 93 | #ifdef CONFIG_HAVE_BLOCK_DEVICE |
Grant Likely | ffc2dd7 | 2007-02-20 09:04:34 +0100 | [diff] [blame] | 94 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 95 | /* ------------------------------------------------------------------------- */ |
| 96 | /* |
| 97 | * reports device info to the user |
| 98 | */ |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 99 | |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 100 | #ifdef CONFIG_LBA48 |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 101 | typedef uint64_t lba512_t; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 102 | #else |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 103 | typedef lbaint_t lba512_t; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 104 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 105 | |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 106 | /* |
Heinrich Schuchardt | 296d4dc | 2020-01-16 20:36:58 +0100 | [diff] [blame] | 107 | * Overflowless variant of (block_count * mul_by / 2**right_shift) |
| 108 | * when 2**right_shift > mul_by |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 109 | */ |
Heinrich Schuchardt | 296d4dc | 2020-01-16 20:36:58 +0100 | [diff] [blame] | 110 | static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, |
| 111 | int right_shift) |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 112 | { |
| 113 | lba512_t bc_quot, bc_rem; |
| 114 | |
| 115 | /* x * m / d == x / d * m + (x % d) * m / d */ |
Heinrich Schuchardt | 296d4dc | 2020-01-16 20:36:58 +0100 | [diff] [blame] | 116 | bc_quot = block_count >> right_shift; |
| 117 | bc_rem = block_count - (bc_quot << right_shift); |
| 118 | return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift); |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 119 | } |
| 120 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 121 | void dev_print (struct blk_desc *dev_desc) |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 122 | { |
| 123 | lba512_t lba512; /* number of blocks if 512bytes block size */ |
| 124 | |
Wolfgang Denk | 8561e71 | 2009-05-15 09:27:58 +0200 | [diff] [blame] | 125 | if (dev_desc->type == DEV_TYPE_UNKNOWN) { |
| 126 | puts ("not available\n"); |
| 127 | return; |
| 128 | } |
| 129 | |
Tor Krill | 03ff330 | 2008-05-29 11:10:30 +0200 | [diff] [blame] | 130 | switch (dev_desc->if_type) { |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 131 | case IF_TYPE_SCSI: |
| 132 | printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", |
| 133 | dev_desc->target,dev_desc->lun, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 134 | dev_desc->vendor, |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 135 | dev_desc->product, |
| 136 | dev_desc->revision); |
| 137 | break; |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 138 | case IF_TYPE_ATAPI: |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 139 | case IF_TYPE_IDE: |
| 140 | case IF_TYPE_SATA: |
Dave Liu | 76596ba | 2008-03-26 22:49:44 +0800 | [diff] [blame] | 141 | printf ("Model: %s Firm: %s Ser#: %s\n", |
| 142 | dev_desc->vendor, |
| 143 | dev_desc->revision, |
| 144 | dev_desc->product); |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 145 | break; |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 146 | case IF_TYPE_SD: |
| 147 | case IF_TYPE_MMC: |
NÃcolas Carneiro Lebedenco | ef3edd4 | 2008-09-04 15:35:46 -0300 | [diff] [blame] | 148 | case IF_TYPE_USB: |
Zhikang Zhang | 182fccd | 2017-08-03 02:30:56 -0700 | [diff] [blame] | 149 | case IF_TYPE_NVME: |
NÃcolas Carneiro Lebedenco | ef3edd4 | 2008-09-04 15:35:46 -0300 | [diff] [blame] | 150 | printf ("Vendor: %s Rev: %s Prod: %s\n", |
| 151 | dev_desc->vendor, |
| 152 | dev_desc->revision, |
| 153 | dev_desc->product); |
| 154 | break; |
Tuomas Tynkkynen | d458006 | 2018-10-15 02:21:10 -0700 | [diff] [blame] | 155 | case IF_TYPE_VIRTIO: |
| 156 | printf("%s VirtIO Block Device\n", dev_desc->vendor); |
| 157 | break; |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 158 | case IF_TYPE_DOC: |
| 159 | puts("device type DOC\n"); |
| 160 | return; |
Tor Krill | 03ff330 | 2008-05-29 11:10:30 +0200 | [diff] [blame] | 161 | case IF_TYPE_UNKNOWN: |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 162 | puts("device type unknown\n"); |
| 163 | return; |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 164 | default: |
Remy Bohmer | f9f718b | 2008-09-19 13:30:06 +0200 | [diff] [blame] | 165 | printf("Unhandled device type: %i\n", dev_desc->if_type); |
Detlev Zundel | bec6b32 | 2008-05-05 16:11:21 +0200 | [diff] [blame] | 166 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 167 | } |
| 168 | puts (" Type: "); |
| 169 | if (dev_desc->removable) |
| 170 | puts ("Removable "); |
| 171 | switch (dev_desc->type & 0x1F) { |
Detlev Zundel | 0e11cc6 | 2008-05-05 16:11:22 +0200 | [diff] [blame] | 172 | case DEV_TYPE_HARDDISK: |
| 173 | puts ("Hard Disk"); |
| 174 | break; |
| 175 | case DEV_TYPE_CDROM: |
| 176 | puts ("CD ROM"); |
| 177 | break; |
| 178 | case DEV_TYPE_OPDISK: |
| 179 | puts ("Optical Device"); |
| 180 | break; |
| 181 | case DEV_TYPE_TAPE: |
| 182 | puts ("Tape"); |
| 183 | break; |
| 184 | default: |
| 185 | printf ("# %02X #", dev_desc->type & 0x1F); |
| 186 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 187 | } |
| 188 | puts ("\n"); |
Jerry Huang | 6a4feb4 | 2012-11-06 15:33:12 +0000 | [diff] [blame] | 189 | if (dev_desc->lba > 0L && dev_desc->blksz > 0L) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 190 | ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 191 | lbaint_t lba; |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 192 | |
| 193 | lba = dev_desc->lba; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 194 | |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 195 | lba512 = (lba * (dev_desc->blksz/512)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 196 | /* round to 1 digit */ |
Pavel Machek | a4264e1 | 2014-09-09 15:19:42 +0200 | [diff] [blame] | 197 | /* 2048 = (1024 * 1024) / 512 MB */ |
Heinrich Schuchardt | 2cb615e | 2019-06-02 00:04:50 +0200 | [diff] [blame] | 198 | mb = lba512_muldiv(lba512, 10, 11); |
Sergei Trofimovich | 89d935b | 2010-08-08 15:05:39 +0300 | [diff] [blame] | 199 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 200 | mb_quot = mb / 10; |
| 201 | mb_rem = mb - (10 * mb_quot); |
| 202 | |
| 203 | gb = mb / 1024; |
| 204 | gb_quot = gb / 10; |
| 205 | gb_rem = gb - (10 * gb_quot); |
wdenk | c35ba4e | 2004-03-14 22:25:36 +0000 | [diff] [blame] | 206 | #ifdef CONFIG_LBA48 |
wdenk | 0593920 | 2004-04-18 17:39:38 +0000 | [diff] [blame] | 207 | if (dev_desc->lba48) |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 208 | printf (" Supports 48-bit addressing\n"); |
| 209 | #endif |
Heiko Schocher | 0f602e1 | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 210 | #if defined(CONFIG_SYS_64BIT_LBA) |
Jean-Jacques Hiblot | ccbb915 | 2016-12-23 10:45:43 +0100 | [diff] [blame] | 211 | printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n", |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 212 | mb_quot, mb_rem, |
| 213 | gb_quot, gb_rem, |
| 214 | lba, |
| 215 | dev_desc->blksz); |
| 216 | #else |
Jean-Jacques Hiblot | ccbb915 | 2016-12-23 10:45:43 +0100 | [diff] [blame] | 217 | printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n", |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 218 | mb_quot, mb_rem, |
| 219 | gb_quot, gb_rem, |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 220 | (ulong)lba, |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 221 | dev_desc->blksz); |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 222 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 223 | } else { |
| 224 | puts (" Capacity: not available\n"); |
| 225 | } |
| 226 | } |
Jon Loeliger | 1670a5b | 2007-07-10 11:19:50 -0500 | [diff] [blame] | 227 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 228 | |
Adam Ford | b10ba90 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 229 | #ifdef CONFIG_HAVE_BLOCK_DEVICE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 230 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 231 | void part_init(struct blk_desc *dev_desc) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 232 | { |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 233 | struct part_driver *drv = |
| 234 | ll_entry_start(struct part_driver, part_driver); |
| 235 | const int n_ents = ll_entry_count(struct part_driver, part_driver); |
| 236 | struct part_driver *entry; |
richardretanubun | e674559 | 2008-09-26 11:13:22 -0400 | [diff] [blame] | 237 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 238 | blkcache_invalidate(dev_desc->if_type, dev_desc->devnum); |
| 239 | |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 240 | dev_desc->part_type = PART_TYPE_UNKNOWN; |
| 241 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 242 | int ret; |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 243 | |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 244 | ret = entry->test(dev_desc); |
| 245 | debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret); |
| 246 | if (!ret) { |
| 247 | dev_desc->part_type = entry->part_type; |
| 248 | break; |
| 249 | } |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 250 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 253 | static void print_part_header(const char *type, struct blk_desc *dev_desc) |
| 254 | { |
Patrick Delaunay | c4bbbec | 2017-01-27 11:00:36 +0100 | [diff] [blame] | 255 | #if CONFIG_IS_ENABLED(MAC_PARTITION) || \ |
Patrick Delaunay | f7e0772 | 2017-01-27 11:00:37 +0100 | [diff] [blame] | 256 | CONFIG_IS_ENABLED(DOS_PARTITION) || \ |
Patrick Delaunay | 21d3bce | 2017-01-27 11:00:38 +0100 | [diff] [blame] | 257 | CONFIG_IS_ENABLED(ISO_PARTITION) || \ |
Patrick Delaunay | 1aa32a8 | 2017-01-27 11:00:39 +0100 | [diff] [blame] | 258 | CONFIG_IS_ENABLED(AMIGA_PARTITION) || \ |
Patrick Delaunay | 8a4f2bd | 2017-01-27 11:00:41 +0100 | [diff] [blame] | 259 | CONFIG_IS_ENABLED(EFI_PARTITION) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 260 | puts ("\nPartition Map for "); |
| 261 | switch (dev_desc->if_type) { |
Detlev Zundel | 0e11cc6 | 2008-05-05 16:11:22 +0200 | [diff] [blame] | 262 | case IF_TYPE_IDE: |
| 263 | puts ("IDE"); |
| 264 | break; |
| 265 | case IF_TYPE_SATA: |
| 266 | puts ("SATA"); |
| 267 | break; |
| 268 | case IF_TYPE_SCSI: |
| 269 | puts ("SCSI"); |
| 270 | break; |
| 271 | case IF_TYPE_ATAPI: |
| 272 | puts ("ATAPI"); |
| 273 | break; |
| 274 | case IF_TYPE_USB: |
| 275 | puts ("USB"); |
| 276 | break; |
| 277 | case IF_TYPE_DOC: |
| 278 | puts ("DOC"); |
| 279 | break; |
Lei Wen | 61dba93 | 2010-09-13 22:07:28 +0800 | [diff] [blame] | 280 | case IF_TYPE_MMC: |
| 281 | puts ("MMC"); |
| 282 | break; |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 283 | case IF_TYPE_HOST: |
Zhikang Zhang | 182fccd | 2017-08-03 02:30:56 -0700 | [diff] [blame] | 284 | puts ("HOST"); |
| 285 | break; |
| 286 | case IF_TYPE_NVME: |
| 287 | puts ("NVMe"); |
Henrik Nordström | 26f9a6c | 2013-11-10 10:26:56 -0700 | [diff] [blame] | 288 | break; |
Tuomas Tynkkynen | d458006 | 2018-10-15 02:21:10 -0700 | [diff] [blame] | 289 | case IF_TYPE_VIRTIO: |
| 290 | puts("VirtIO"); |
| 291 | break; |
Detlev Zundel | 0e11cc6 | 2008-05-05 16:11:22 +0200 | [diff] [blame] | 292 | default: |
| 293 | puts ("UNKNOWN"); |
| 294 | break; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 295 | } |
| 296 | printf (" device %d -- Partition Type: %s\n\n", |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 297 | dev_desc->devnum, type); |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 298 | #endif /* any CONFIG_..._PARTITION */ |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 299 | } |
Gabe Black | 0fc71b5 | 2012-10-12 14:26:08 +0000 | [diff] [blame] | 300 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 301 | void part_print(struct blk_desc *dev_desc) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 302 | { |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 303 | struct part_driver *drv; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 304 | |
Kever Yang | 1c63624 | 2018-02-10 17:55:37 +0800 | [diff] [blame] | 305 | drv = part_driver_lookup_type(dev_desc); |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 306 | if (!drv) { |
| 307 | printf("## Unknown partition table type %x\n", |
| 308 | dev_desc->part_type); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 309 | return; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 310 | } |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 311 | |
| 312 | PRINTF("## Testing for valid %s partition ##\n", drv->name); |
| 313 | print_part_header(drv->name, dev_desc); |
| 314 | if (drv->print) |
| 315 | drv->print(dev_desc); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Adam Ford | b10ba90 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 318 | #endif /* CONFIG_HAVE_BLOCK_DEVICE */ |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 319 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 320 | int part_get_info(struct blk_desc *dev_desc, int part, |
Pavel Machek | 13334e7 | 2014-07-19 23:50:44 +0200 | [diff] [blame] | 321 | disk_partition_t *info) |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 322 | { |
Adam Ford | b10ba90 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 323 | #ifdef CONFIG_HAVE_BLOCK_DEVICE |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 324 | struct part_driver *drv; |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 325 | |
Patrick Delaunay | 7328709 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 326 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Stephen Warren | c6c7b7a | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 327 | /* The common case is no UUID support */ |
| 328 | info->uuid[0] = 0; |
| 329 | #endif |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 330 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 331 | info->type_guid[0] = 0; |
| 332 | #endif |
Stephen Warren | c6c7b7a | 2012-09-21 09:50:59 +0000 | [diff] [blame] | 333 | |
Kever Yang | 1c63624 | 2018-02-10 17:55:37 +0800 | [diff] [blame] | 334 | drv = part_driver_lookup_type(dev_desc); |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 335 | if (!drv) { |
| 336 | debug("## Unknown partition table type %x\n", |
| 337 | dev_desc->part_type); |
| 338 | return -EPROTONOSUPPORT; |
| 339 | } |
| 340 | if (!drv->get_info) { |
Nishanth Menon | ef1851a | 2016-03-15 16:15:32 -0500 | [diff] [blame] | 341 | PRINTF("## Driver %s does not have the get_info() method\n", |
| 342 | drv->name); |
Simon Glass | 83ce563 | 2016-02-29 15:25:47 -0700 | [diff] [blame] | 343 | return -ENOSYS; |
| 344 | } |
| 345 | if (drv->get_info(dev_desc, part, info) == 0) { |
| 346 | PRINTF("## Valid %s partition found ##\n", drv->name); |
| 347 | return 0; |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 348 | } |
Adam Ford | b10ba90 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 349 | #endif /* CONFIG_HAVE_BLOCK_DEVICE */ |
Stephen Warren | 07c5bff | 2012-09-21 12:46:54 +0000 | [diff] [blame] | 350 | |
| 351 | return -1; |
| 352 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 353 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 354 | int part_get_info_whole_disk(struct blk_desc *dev_desc, disk_partition_t *info) |
| 355 | { |
| 356 | info->start = 0; |
| 357 | info->size = dev_desc->lba; |
| 358 | info->blksz = dev_desc->blksz; |
| 359 | info->bootable = 0; |
| 360 | strcpy((char *)info->type, BOOT_PART_TYPE); |
| 361 | strcpy((char *)info->name, "Whole Disk"); |
| 362 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
| 363 | info->uuid[0] = 0; |
| 364 | #endif |
| 365 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 366 | info->type_guid[0] = 0; |
| 367 | #endif |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 372 | int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str, |
| 373 | struct blk_desc **dev_desc) |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 374 | { |
| 375 | char *ep; |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 376 | char *dup_str = NULL; |
| 377 | const char *dev_str, *hwpart_str; |
| 378 | int dev, hwpart; |
| 379 | |
| 380 | hwpart_str = strchr(dev_hwpart_str, '.'); |
| 381 | if (hwpart_str) { |
| 382 | dup_str = strdup(dev_hwpart_str); |
| 383 | dup_str[hwpart_str - dev_hwpart_str] = 0; |
| 384 | dev_str = dup_str; |
| 385 | hwpart_str++; |
| 386 | } else { |
| 387 | dev_str = dev_hwpart_str; |
Stephen Warren | ad17c0f | 2014-05-23 12:48:11 -0600 | [diff] [blame] | 388 | hwpart = 0; |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 389 | } |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 390 | |
| 391 | dev = simple_strtoul(dev_str, &ep, 16); |
| 392 | if (*ep) { |
| 393 | printf("** Bad device specification %s %s **\n", |
| 394 | ifname, dev_str); |
Simon Glass | 356e0e6 | 2016-05-01 13:52:36 -0600 | [diff] [blame] | 395 | dev = -EINVAL; |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 396 | goto cleanup; |
| 397 | } |
| 398 | |
| 399 | if (hwpart_str) { |
| 400 | hwpart = simple_strtoul(hwpart_str, &ep, 16); |
| 401 | if (*ep) { |
| 402 | printf("** Bad HW partition specification %s %s **\n", |
| 403 | ifname, hwpart_str); |
Simon Glass | 356e0e6 | 2016-05-01 13:52:36 -0600 | [diff] [blame] | 404 | dev = -EINVAL; |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 405 | goto cleanup; |
| 406 | } |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 409 | *dev_desc = get_dev_hwpart(ifname, dev, hwpart); |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 410 | if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) { |
Sam Protsenko | 6839baf | 2018-07-30 19:19:27 +0300 | [diff] [blame] | 411 | debug("** Bad device %s %s **\n", ifname, dev_hwpart_str); |
Simon Glass | 356e0e6 | 2016-05-01 13:52:36 -0600 | [diff] [blame] | 412 | dev = -ENOENT; |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 413 | goto cleanup; |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Adam Ford | b10ba90 | 2018-02-06 12:43:56 -0600 | [diff] [blame] | 416 | #ifdef CONFIG_HAVE_BLOCK_DEVICE |
Erik Tideman | 8d11b8e | 2016-01-11 13:39:07 +0000 | [diff] [blame] | 417 | /* |
| 418 | * Updates the partition table for the specified hw partition. |
Robert Hancock | 219907e | 2019-06-18 09:53:04 -0600 | [diff] [blame] | 419 | * Always should be done, otherwise hw partition 0 will return stale |
| 420 | * data after displaying a non-zero hw partition. |
Erik Tideman | 8d11b8e | 2016-01-11 13:39:07 +0000 | [diff] [blame] | 421 | */ |
Robert Hancock | 219907e | 2019-06-18 09:53:04 -0600 | [diff] [blame] | 422 | part_init(*dev_desc); |
Erik Tideman | 8d11b8e | 2016-01-11 13:39:07 +0000 | [diff] [blame] | 423 | #endif |
| 424 | |
Stephen Warren | aeca52f | 2014-05-07 12:19:01 -0600 | [diff] [blame] | 425 | cleanup: |
| 426 | free(dup_str); |
Stephen Warren | afd909c | 2012-09-21 09:50:56 +0000 | [diff] [blame] | 427 | return dev; |
| 428 | } |
| 429 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 430 | #define PART_UNSPECIFIED -2 |
| 431 | #define PART_AUTO -1 |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 432 | 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] | 433 | struct blk_desc **dev_desc, |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 434 | disk_partition_t *info, int allow_whole_dev) |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 435 | { |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 436 | int ret = -1; |
| 437 | const char *part_str; |
| 438 | char *dup_str = NULL; |
| 439 | const char *dev_str; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 440 | int dev; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 441 | char *ep; |
| 442 | int p; |
| 443 | int part; |
| 444 | disk_partition_t tmpinfo; |
| 445 | |
Hans de Goede | 4763ee3 | 2015-09-17 18:46:55 -0400 | [diff] [blame] | 446 | #ifdef CONFIG_SANDBOX |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 447 | /* |
Pavel Machek | 13334e7 | 2014-07-19 23:50:44 +0200 | [diff] [blame] | 448 | * Special-case a pseudo block device "hostfs", to allow access to the |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 449 | * host's own filesystem. |
| 450 | */ |
| 451 | if (0 == strcmp(ifname, "hostfs")) { |
| 452 | *dev_desc = NULL; |
| 453 | info->start = 0; |
| 454 | info->size = 0; |
| 455 | info->blksz = 0; |
| 456 | info->bootable = 0; |
| 457 | strcpy((char *)info->type, BOOT_PART_TYPE); |
| 458 | strcpy((char *)info->name, "Sandbox host"); |
Patrick Delaunay | 7328709 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 459 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 460 | info->uuid[0] = 0; |
| 461 | #endif |
Patrick Delaunay | 8248c8d | 2015-10-27 11:00:27 +0100 | [diff] [blame] | 462 | #ifdef CONFIG_PARTITION_TYPE_GUID |
| 463 | info->type_guid[0] = 0; |
| 464 | #endif |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 465 | |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | #endif |
| 469 | |
| 470 | #ifdef CONFIG_CMD_UBIFS |
| 471 | /* |
Heinrich Schuchardt | a4388bb | 2019-04-10 18:59:26 +0200 | [diff] [blame] | 472 | * Special-case ubi, ubi goes through a mtd, rather than through |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 473 | * a regular block device. |
| 474 | */ |
| 475 | if (0 == strcmp(ifname, "ubi")) { |
| 476 | if (!ubifs_is_mounted()) { |
| 477 | printf("UBIFS not mounted, use ubifsmount to mount volume first!\n"); |
| 478 | return -1; |
| 479 | } |
| 480 | |
| 481 | *dev_desc = NULL; |
| 482 | memset(info, 0, sizeof(*info)); |
| 483 | strcpy((char *)info->type, BOOT_PART_TYPE); |
| 484 | strcpy((char *)info->name, "UBI"); |
Patrick Delaunay | 7328709 | 2017-01-27 11:00:42 +0100 | [diff] [blame] | 485 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 486 | info->uuid[0] = 0; |
| 487 | #endif |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 488 | return 0; |
| 489 | } |
Hans de Goede | 4763ee3 | 2015-09-17 18:46:55 -0400 | [diff] [blame] | 490 | #endif |
Stephen Warren | bff6c31 | 2014-06-12 10:28:32 -0600 | [diff] [blame] | 491 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 492 | /* If no dev_part_str, use bootdevice environment variable */ |
Stephen Warren | a95af94 | 2012-09-28 05:34:09 +0000 | [diff] [blame] | 493 | if (!dev_part_str || !strlen(dev_part_str) || |
| 494 | !strcmp(dev_part_str, "-")) |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 495 | dev_part_str = env_get("bootdevice"); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 496 | |
| 497 | /* If still no dev_part_str, it's an error */ |
| 498 | if (!dev_part_str) { |
| 499 | printf("** No device specified **\n"); |
| 500 | goto cleanup; |
| 501 | } |
| 502 | |
| 503 | /* Separate device and partition ID specification */ |
| 504 | part_str = strchr(dev_part_str, ':'); |
| 505 | if (part_str) { |
| 506 | dup_str = strdup(dev_part_str); |
| 507 | dup_str[part_str - dev_part_str] = 0; |
| 508 | dev_str = dup_str; |
| 509 | part_str++; |
| 510 | } else { |
| 511 | dev_str = dev_part_str; |
| 512 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 513 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 514 | /* Look up the device */ |
Simon Glass | e6649a6 | 2016-02-29 15:25:43 -0700 | [diff] [blame] | 515 | dev = blk_get_device_by_str(ifname, dev_str, dev_desc); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 516 | if (dev < 0) |
| 517 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 518 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 519 | /* Convert partition ID string to number */ |
| 520 | if (!part_str || !*part_str) { |
| 521 | part = PART_UNSPECIFIED; |
| 522 | } else if (!strcmp(part_str, "auto")) { |
| 523 | part = PART_AUTO; |
| 524 | } else { |
| 525 | /* Something specified -> use exactly that */ |
| 526 | part = (int)simple_strtoul(part_str, &ep, 16); |
| 527 | /* |
| 528 | * Less than whole string converted, |
| 529 | * or request for whole device, but caller requires partition. |
| 530 | */ |
| 531 | if (*ep || (part == 0 && !allow_whole_dev)) { |
| 532 | printf("** Bad partition specification %s %s **\n", |
| 533 | ifname, dev_part_str); |
| 534 | goto cleanup; |
| 535 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 538 | /* |
| 539 | * No partition table on device, |
| 540 | * or user requested partition 0 (entire device). |
| 541 | */ |
| 542 | if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) || |
| 543 | (part == 0)) { |
| 544 | if (!(*dev_desc)->lba) { |
| 545 | printf("** Bad device size - %s %s **\n", ifname, |
| 546 | dev_str); |
| 547 | goto cleanup; |
| 548 | } |
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 | /* |
| 551 | * If user specified a partition ID other than 0, |
| 552 | * or the calling command only accepts partitions, |
| 553 | * it's an error. |
| 554 | */ |
| 555 | if ((part > 0) || (!allow_whole_dev)) { |
| 556 | printf("** No partition table - %s %s **\n", ifname, |
| 557 | dev_str); |
| 558 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 559 | } |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 560 | |
Lan Yixun (dlan) | 138914e | 2013-07-20 08:17:59 +0800 | [diff] [blame] | 561 | (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); |
| 562 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 563 | part_get_info_whole_disk(*dev_desc, info); |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 564 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 565 | ret = 0; |
| 566 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 569 | /* |
| 570 | * Now there's known to be a partition table, |
| 571 | * not specifying a partition means to pick partition 1. |
| 572 | */ |
| 573 | if (part == PART_UNSPECIFIED) |
| 574 | part = 1; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 575 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 576 | /* |
| 577 | * If user didn't specify a partition number, or did specify something |
| 578 | * other than "auto", use that partition number directly. |
| 579 | */ |
| 580 | if (part != PART_AUTO) { |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 581 | ret = part_get_info(*dev_desc, part, info); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 582 | if (ret) { |
| 583 | printf("** Invalid partition %d **\n", part); |
| 584 | goto cleanup; |
| 585 | } |
| 586 | } else { |
| 587 | /* |
| 588 | * Find the first bootable partition. |
| 589 | * If none are bootable, fall back to the first valid partition. |
| 590 | */ |
| 591 | part = 0; |
| 592 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 593 | ret = part_get_info(*dev_desc, p, info); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 594 | if (ret) |
| 595 | continue; |
| 596 | |
| 597 | /* |
| 598 | * First valid partition, or new better partition? |
| 599 | * If so, save partition ID. |
| 600 | */ |
| 601 | if (!part || info->bootable) |
| 602 | part = p; |
| 603 | |
| 604 | /* Best possible partition? Stop searching. */ |
| 605 | if (info->bootable) |
| 606 | break; |
| 607 | |
| 608 | /* |
| 609 | * We now need to search further for best possible. |
| 610 | * If we what we just queried was the best so far, |
| 611 | * save the info since we over-write it next loop. |
| 612 | */ |
| 613 | if (part == p) |
| 614 | tmpinfo = *info; |
| 615 | } |
| 616 | /* If we found any acceptable partition */ |
| 617 | if (part) { |
| 618 | /* |
| 619 | * If we searched all possible partition IDs, |
| 620 | * return the first valid partition we found. |
| 621 | */ |
| 622 | if (p == MAX_SEARCH_PARTITIONS + 1) |
| 623 | *info = tmpinfo; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 624 | } else { |
| 625 | printf("** No valid partitions found **\n"); |
Stephen Warren | 3c28be9 | 2012-10-08 07:45:54 +0000 | [diff] [blame] | 626 | ret = -1; |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 627 | goto cleanup; |
| 628 | } |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 629 | } |
| 630 | if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) { |
| 631 | printf("** Invalid partition type \"%.32s\"" |
| 632 | " (expect \"" BOOT_PART_TYPE "\")\n", |
| 633 | info->type); |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 634 | ret = -1; |
| 635 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Lan Yixun (dlan) | 138914e | 2013-07-20 08:17:59 +0800 | [diff] [blame] | 638 | (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); |
| 639 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 640 | ret = part; |
| 641 | goto cleanup; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 642 | |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 643 | cleanup: |
| 644 | free(dup_str); |
| 645 | return ret; |
Rob Herring | d586cec | 2012-09-21 04:08:17 +0000 | [diff] [blame] | 646 | } |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 647 | |
Sam Protsenko | dee8038 | 2017-09-22 01:51:58 +0300 | [diff] [blame] | 648 | int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name, |
| 649 | disk_partition_t *info, int part_type) |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 650 | { |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 651 | struct part_driver *part_drv; |
Kever Yang | 9182966 | 2018-02-10 17:55:38 +0800 | [diff] [blame] | 652 | int ret; |
| 653 | int i; |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 654 | |
Kever Yang | 9182966 | 2018-02-10 17:55:38 +0800 | [diff] [blame] | 655 | part_drv = part_driver_lookup_type(dev_desc); |
| 656 | if (!part_drv) |
| 657 | return -1; |
| 658 | for (i = 1; i < part_drv->max_entries; i++) { |
| 659 | ret = part_drv->get_info(dev_desc, i, info); |
| 660 | if (ret != 0) { |
| 661 | /* no more entries in table */ |
| 662 | break; |
| 663 | } |
| 664 | if (strcmp(name, (const char *)info->name) == 0) { |
| 665 | /* matched */ |
| 666 | return i; |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 667 | } |
| 668 | } |
Kever Yang | 9182966 | 2018-02-10 17:55:38 +0800 | [diff] [blame] | 669 | |
Petr Kulhavy | 712257e | 2016-09-09 10:27:15 +0200 | [diff] [blame] | 670 | return -1; |
| 671 | } |
Petr Kulhavy | 2e790d5 | 2016-09-09 10:27:17 +0200 | [diff] [blame] | 672 | |
Sam Protsenko | dee8038 | 2017-09-22 01:51:58 +0300 | [diff] [blame] | 673 | int part_get_info_by_name(struct blk_desc *dev_desc, const char *name, |
| 674 | disk_partition_t *info) |
| 675 | { |
| 676 | return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL); |
| 677 | } |
| 678 | |
Ruslan Trofymenko | 5e171ef | 2019-07-05 15:37:31 +0300 | [diff] [blame] | 679 | /** |
| 680 | * Get partition info from device number and partition name. |
| 681 | * |
| 682 | * Parse a device number and partition name string in the form of |
| 683 | * "device_num#partition_name", for example "0#misc". If the partition |
| 684 | * is found, sets dev_desc and part_info accordingly with the information |
| 685 | * of the partition with the given partition_name. |
| 686 | * |
| 687 | * @param[in] dev_iface Device interface |
| 688 | * @param[in] dev_part_str Input string argument, like "0#misc" |
| 689 | * @param[out] dev_desc Place to store the device description pointer |
| 690 | * @param[out] part_info Place to store the partition information |
| 691 | * @return 0 on success, or a negative on error |
| 692 | */ |
| 693 | static int part_get_info_by_dev_and_name(const char *dev_iface, |
| 694 | const char *dev_part_str, |
| 695 | struct blk_desc **dev_desc, |
| 696 | disk_partition_t *part_info) |
| 697 | { |
| 698 | char *ep; |
| 699 | const char *part_str; |
| 700 | int dev_num; |
| 701 | |
| 702 | part_str = strchr(dev_part_str, '#'); |
| 703 | if (!part_str || part_str == dev_part_str) |
| 704 | return -EINVAL; |
| 705 | |
| 706 | dev_num = simple_strtoul(dev_part_str, &ep, 16); |
| 707 | if (ep != part_str) { |
| 708 | /* Not all the first part before the # was parsed. */ |
| 709 | return -EINVAL; |
| 710 | } |
| 711 | part_str++; |
| 712 | |
| 713 | *dev_desc = blk_get_dev(dev_iface, dev_num); |
| 714 | if (!*dev_desc) { |
| 715 | printf("Could not find %s %d\n", dev_iface, dev_num); |
| 716 | return -EINVAL; |
| 717 | } |
| 718 | if (part_get_info_by_name(*dev_desc, part_str, part_info) < 0) { |
| 719 | printf("Could not find \"%s\" partition\n", part_str); |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | int part_get_info_by_dev_and_name_or_num(const char *dev_iface, |
| 726 | const char *dev_part_str, |
| 727 | struct blk_desc **dev_desc, |
| 728 | disk_partition_t *part_info) |
| 729 | { |
| 730 | /* Split the part_name if passed as "$dev_num#part_name". */ |
| 731 | if (!part_get_info_by_dev_and_name(dev_iface, dev_part_str, |
| 732 | dev_desc, part_info)) |
| 733 | return 0; |
| 734 | /* |
| 735 | * Couldn't lookup by name, try looking up the partition description |
| 736 | * directly. |
| 737 | */ |
| 738 | if (blk_get_device_part_str(dev_iface, dev_part_str, |
| 739 | dev_desc, part_info, 1) < 0) { |
| 740 | printf("Couldn't find partition %s %s\n", |
| 741 | dev_iface, dev_part_str); |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | return 0; |
| 745 | } |
| 746 | |
Petr Kulhavy | 2e790d5 | 2016-09-09 10:27:17 +0200 | [diff] [blame] | 747 | void part_set_generic_name(const struct blk_desc *dev_desc, |
| 748 | int part_num, char *name) |
| 749 | { |
| 750 | char *devtype; |
| 751 | |
| 752 | switch (dev_desc->if_type) { |
| 753 | case IF_TYPE_IDE: |
| 754 | case IF_TYPE_SATA: |
| 755 | case IF_TYPE_ATAPI: |
| 756 | devtype = "hd"; |
| 757 | break; |
| 758 | case IF_TYPE_SCSI: |
| 759 | devtype = "sd"; |
| 760 | break; |
| 761 | case IF_TYPE_USB: |
| 762 | devtype = "usbd"; |
| 763 | break; |
| 764 | case IF_TYPE_DOC: |
| 765 | devtype = "docd"; |
| 766 | break; |
| 767 | case IF_TYPE_MMC: |
| 768 | case IF_TYPE_SD: |
| 769 | devtype = "mmcsd"; |
| 770 | break; |
| 771 | default: |
| 772 | devtype = "xx"; |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num); |
| 777 | } |