blob: 0a03b8213d8fee053fcd650f5754be633d201b96 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
3 * (C) Copyright 2001
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkaffae2b2002-08-17 09:36:01 +00005 */
6
7#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <blk.h>
wdenkaffae2b2002-08-17 09:36:01 +00009#include <command.h>
Simon Glass7b51b572019-08-01 09:46:52 -060010#include <env.h>
Simon Glass96e5b032016-02-29 15:25:47 -070011#include <errno.h>
wdenkaffae2b2002-08-17 09:36:01 +000012#include <ide.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Stephen Warren10a37fd2012-09-21 09:50:57 +000014#include <malloc.h>
Grant Likely735dd972007-02-20 09:04:34 +010015#include <part.h>
Hans de Goede251cee02015-09-17 18:46:58 -040016#include <ubifs_uboot.h>
wdenkaffae2b2002-08-17 09:36:01 +000017
18#undef PART_DEBUG
19
20#ifdef PART_DEBUG
21#define PRINTF(fmt,args...) printf (fmt ,##args)
22#else
23#define PRINTF(fmt,args...)
24#endif
25
Sam Protsenko30789092017-09-22 01:51:58 +030026/* Check all partition types */
27#define PART_TYPE_ALL -1
28
Joshua Watt387f8be2023-07-03 08:39:54 -050029static struct part_driver *part_driver_get_type(int part_type)
30{
31 struct part_driver *drv =
32 ll_entry_start(struct part_driver, part_driver);
33 const int n_ents = ll_entry_count(struct part_driver, part_driver);
34 struct part_driver *entry;
35
36 for (entry = drv; entry != drv + n_ents; entry++) {
37 if (part_type == entry->part_type)
38 return entry;
39 }
40
41 /* Not found */
42 return NULL;
43}
44
Kever Yangd4729262018-02-10 17:55:37 +080045static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
Simon Glass96e5b032016-02-29 15:25:47 -070046{
47 struct part_driver *drv =
48 ll_entry_start(struct part_driver, part_driver);
49 const int n_ents = ll_entry_count(struct part_driver, part_driver);
50 struct part_driver *entry;
51
Kever Yangd4729262018-02-10 17:55:37 +080052 if (dev_desc->part_type == PART_TYPE_UNKNOWN) {
53 for (entry = drv; entry != drv + n_ents; entry++) {
54 int ret;
55
56 ret = entry->test(dev_desc);
57 if (!ret) {
58 dev_desc->part_type = entry->part_type;
59 return entry;
60 }
61 }
62 } else {
Joshua Watt387f8be2023-07-03 08:39:54 -050063 return part_driver_get_type(dev_desc->part_type);
Simon Glass96e5b032016-02-29 15:25:47 -070064 }
65
66 /* Not found */
67 return NULL;
68}
69
Simon Glass125194e2023-07-15 21:38:47 -060070int part_get_type_by_name(const char *name)
71{
72 struct part_driver *drv =
73 ll_entry_start(struct part_driver, part_driver);
74 const int n_ents = ll_entry_count(struct part_driver, part_driver);
75 struct part_driver *entry;
76
77 for (entry = drv; entry != drv + n_ents; entry++) {
78 if (!strcasecmp(name, entry->name))
79 return entry->part_type;
80 }
81
82 /* Not found */
83 return PART_TYPE_UNKNOWN;
84}
85
Simon Glass4101f682016-02-29 15:25:34 -070086static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Grant Likely735dd972007-02-20 09:04:34 +010087{
Simon Glass6dd9faf2016-05-01 13:52:32 -060088 struct blk_desc *dev_desc;
89 int ret;
Grant Likely735dd972007-02-20 09:04:34 +010090
Simon Glassa51eb8d2022-08-11 19:34:45 -060091 if (!blk_enabled())
92 return NULL;
Simon Glass8149b152022-09-17 09:00:09 -060093 dev_desc = blk_get_devnum_by_uclass_idname(ifname, dev);
Simon Glass6dd9faf2016-05-01 13:52:32 -060094 if (!dev_desc) {
95 debug("%s: No device for iface '%s', dev %d\n", __func__,
96 ifname, dev);
Tim Kientzle7e71dc62012-03-27 11:43:25 +020097 return NULL;
Grant Likely735dd972007-02-20 09:04:34 +010098 }
Simon Glass6dd9faf2016-05-01 13:52:32 -060099 ret = blk_dselect_hwpart(dev_desc, hwpart);
100 if (ret) {
101 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
102 ret);
103 return NULL;
104 }
105
106 return dev_desc;
Grant Likely735dd972007-02-20 09:04:34 +0100107}
Stephen Warren336b6f92014-05-07 12:19:01 -0600108
Simon Glassdb1d9e72016-02-29 15:25:42 -0700109struct blk_desc *blk_get_dev(const char *ifname, int dev)
Stephen Warren336b6f92014-05-07 12:19:01 -0600110{
Simon Glassa51eb8d2022-08-11 19:34:45 -0600111 if (!blk_enabled())
112 return NULL;
113
Stephen Warrenecdd57e2014-05-23 12:48:11 -0600114 return get_dev_hwpart(ifname, dev, 0);
Stephen Warren336b6f92014-05-07 12:19:01 -0600115}
Grant Likely735dd972007-02-20 09:04:34 +0100116
wdenkaffae2b2002-08-17 09:36:01 +0000117/* ------------------------------------------------------------------------- */
118/*
119 * reports device info to the user
120 */
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300121
122#ifdef CONFIG_LBA48
123typedef uint64_t lba512_t;
124#else
125typedef lbaint_t lba512_t;
126#endif
127
128/*
Heinrich Schuchardta9d1c0e2020-01-16 20:36:58 +0100129 * Overflowless variant of (block_count * mul_by / 2**right_shift)
130 * when 2**right_shift > mul_by
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300131 */
Heinrich Schuchardta9d1c0e2020-01-16 20:36:58 +0100132static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
133 int right_shift)
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300134{
135 lba512_t bc_quot, bc_rem;
136
137 /* x * m / d == x / d * m + (x % d) * m / d */
Heinrich Schuchardta9d1c0e2020-01-16 20:36:58 +0100138 bc_quot = block_count >> right_shift;
139 bc_rem = block_count - (bc_quot << right_shift);
140 return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300141}
142
Simon Glassef4b66b2022-08-11 19:35:00 -0600143void dev_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000144{
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300145 lba512_t lba512; /* number of blocks if 512bytes block size */
wdenkaffae2b2002-08-17 09:36:01 +0000146
Wolfgang Denkaf75a452009-05-15 09:27:58 +0200147 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
148 puts ("not available\n");
149 return;
150 }
151
Simon Glass8149b152022-09-17 09:00:09 -0600152 switch (dev_desc->uclass_id) {
Simon Glasse33a5c62022-08-11 19:34:59 -0600153 case UCLASS_SCSI:
Detlev Zundel574b3192008-05-05 16:11:21 +0200154 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
155 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000156 dev_desc->vendor,
157 dev_desc->product,
158 dev_desc->revision);
Detlev Zundel574b3192008-05-05 16:11:21 +0200159 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600160 case UCLASS_IDE:
161 case UCLASS_AHCI:
Detlev Zundel574b3192008-05-05 16:11:21 +0200162 printf ("Model: %s Firm: %s Ser#: %s\n",
163 dev_desc->vendor,
164 dev_desc->revision,
165 dev_desc->product);
166 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600167 case UCLASS_MMC:
168 case UCLASS_USB:
169 case UCLASS_NVME:
170 case UCLASS_PVBLOCK:
Simon Glass95201812022-10-29 19:47:17 -0600171 case UCLASS_HOST:
Tobias Waldekranzbb56da12023-02-16 16:33:52 +0100172 case UCLASS_BLKMAP:
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300173 printf ("Vendor: %s Rev: %s Prod: %s\n",
174 dev_desc->vendor,
175 dev_desc->revision,
176 dev_desc->product);
177 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600178 case UCLASS_VIRTIO:
Tuomas Tynkkynen4ad54ec2018-10-15 02:21:10 -0700179 printf("%s VirtIO Block Device\n", dev_desc->vendor);
180 break;
Simon Glassef4b66b2022-08-11 19:35:00 -0600181 case UCLASS_EFI_MEDIA:
182 printf("EFI media Block Device %d\n", dev_desc->devnum);
183 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600184 case UCLASS_INVALID:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200185 puts("device type unknown\n");
186 return;
Detlev Zundel574b3192008-05-05 16:11:21 +0200187 default:
Simon Glass8149b152022-09-17 09:00:09 -0600188 printf("Unhandled device type: %i\n", dev_desc->uclass_id);
Detlev Zundel574b3192008-05-05 16:11:21 +0200189 return;
wdenkaffae2b2002-08-17 09:36:01 +0000190 }
191 puts (" Type: ");
192 if (dev_desc->removable)
193 puts ("Removable ");
194 switch (dev_desc->type & 0x1F) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200195 case DEV_TYPE_HARDDISK:
196 puts ("Hard Disk");
197 break;
198 case DEV_TYPE_CDROM:
199 puts ("CD ROM");
200 break;
201 case DEV_TYPE_OPDISK:
202 puts ("Optical Device");
203 break;
204 case DEV_TYPE_TAPE:
205 puts ("Tape");
206 break;
207 default:
208 printf ("# %02X #", dev_desc->type & 0x1F);
209 break;
wdenkaffae2b2002-08-17 09:36:01 +0000210 }
211 puts ("\n");
Jerry Huang33699df2012-11-06 15:33:12 +0000212 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000213 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkc40b2952004-03-13 23:29:43 +0000214 lbaint_t lba;
wdenk6e592382004-04-18 17:39:38 +0000215
216 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000217
wdenkc40b2952004-03-13 23:29:43 +0000218 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000219 /* round to 1 digit */
Pavel Machek214b3f32014-09-09 15:19:42 +0200220 /* 2048 = (1024 * 1024) / 512 MB */
Heinrich Schuchardt17416ea2019-06-02 00:04:50 +0200221 mb = lba512_muldiv(lba512, 10, 11);
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300222
wdenkaffae2b2002-08-17 09:36:01 +0000223 mb_quot = mb / 10;
224 mb_rem = mb - (10 * mb_quot);
225
226 gb = mb / 1024;
227 gb_quot = gb / 10;
228 gb_rem = gb - (10 * gb_quot);
wdenk42dfe7a2004-03-14 22:25:36 +0000229#ifdef CONFIG_LBA48
wdenk6e592382004-04-18 17:39:38 +0000230 if (dev_desc->lba48)
wdenkc40b2952004-03-13 23:29:43 +0000231 printf (" Supports 48-bit addressing\n");
232#endif
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100233#if defined(CONFIG_SYS_64BIT_LBA)
Jean-Jacques Hiblot139f7b12016-12-23 10:45:43 +0100234 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
wdenkc40b2952004-03-13 23:29:43 +0000235 mb_quot, mb_rem,
236 gb_quot, gb_rem,
237 lba,
238 dev_desc->blksz);
239#else
Jean-Jacques Hiblot139f7b12016-12-23 10:45:43 +0100240 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
wdenkaffae2b2002-08-17 09:36:01 +0000241 mb_quot, mb_rem,
242 gb_quot, gb_rem,
wdenkc40b2952004-03-13 23:29:43 +0000243 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000244 dev_desc->blksz);
wdenkc40b2952004-03-13 23:29:43 +0000245#endif
wdenkaffae2b2002-08-17 09:36:01 +0000246 } else {
247 puts (" Capacity: not available\n");
248 }
249}
wdenkaffae2b2002-08-17 09:36:01 +0000250
Simon Glass3e8bd462016-02-29 15:25:48 -0700251void part_init(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000252{
Simon Glass96e5b032016-02-29 15:25:47 -0700253 struct part_driver *drv =
254 ll_entry_start(struct part_driver, part_driver);
255 const int n_ents = ll_entry_count(struct part_driver, part_driver);
256 struct part_driver *entry;
wdenkaffae2b2002-08-17 09:36:01 +0000257
Simon Glass8149b152022-09-17 09:00:09 -0600258 blkcache_invalidate(dev_desc->uclass_id, dev_desc->devnum);
Eric Nelsone40cf342016-03-28 10:05:44 -0700259
Rob Herring99d2c202012-09-21 04:08:17 +0000260 dev_desc->part_type = PART_TYPE_UNKNOWN;
Simon Glass96e5b032016-02-29 15:25:47 -0700261 for (entry = drv; entry != drv + n_ents; entry++) {
262 int ret;
263
264 ret = entry->test(dev_desc);
265 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
266 if (!ret) {
267 dev_desc->part_type = entry->part_type;
268 break;
269 }
270 }
wdenkaffae2b2002-08-17 09:36:01 +0000271}
272
Simon Glass96e5b032016-02-29 15:25:47 -0700273static void print_part_header(const char *type, struct blk_desc *dev_desc)
274{
Patrick Delaunayf18fa312017-01-27 11:00:36 +0100275#if CONFIG_IS_ENABLED(MAC_PARTITION) || \
Patrick Delaunayb0cf7332017-01-27 11:00:37 +0100276 CONFIG_IS_ENABLED(DOS_PARTITION) || \
Patrick Delaunay1acc0082017-01-27 11:00:38 +0100277 CONFIG_IS_ENABLED(ISO_PARTITION) || \
Patrick Delaunay863c5b62017-01-27 11:00:39 +0100278 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100279 CONFIG_IS_ENABLED(EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000280 puts ("\nPartition Map for ");
Simon Glass8149b152022-09-17 09:00:09 -0600281 switch (dev_desc->uclass_id) {
Simon Glasse33a5c62022-08-11 19:34:59 -0600282 case UCLASS_IDE:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200283 puts ("IDE");
284 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600285 case UCLASS_AHCI:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200286 puts ("SATA");
287 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600288 case UCLASS_SCSI:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200289 puts ("SCSI");
290 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600291 case UCLASS_USB:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200292 puts ("USB");
293 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600294 case UCLASS_MMC:
Lei Wen8f3b9642010-09-13 22:07:28 +0800295 puts ("MMC");
296 break;
Simon Glass95201812022-10-29 19:47:17 -0600297 case UCLASS_HOST:
Zhikang Zhangffab6942017-08-03 02:30:56 -0700298 puts ("HOST");
299 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600300 case UCLASS_NVME:
Zhikang Zhangffab6942017-08-03 02:30:56 -0700301 puts ("NVMe");
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700302 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600303 case UCLASS_PVBLOCK:
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300304 puts("PV BLOCK");
305 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600306 case UCLASS_VIRTIO:
Tuomas Tynkkynen4ad54ec2018-10-15 02:21:10 -0700307 puts("VirtIO");
308 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600309 case UCLASS_EFI_MEDIA:
Simon Glass42b7f422021-12-04 08:56:31 -0700310 puts("EFI");
311 break;
Detlev Zundel726c0f12008-05-05 16:11:22 +0200312 default:
Simon Glass42b7f422021-12-04 08:56:31 -0700313 puts("UNKNOWN");
Detlev Zundel726c0f12008-05-05 16:11:22 +0200314 break;
wdenkaffae2b2002-08-17 09:36:01 +0000315 }
316 printf (" device %d -- Partition Type: %s\n\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700317 dev_desc->devnum, type);
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000318#endif /* any CONFIG_..._PARTITION */
Simon Glass96e5b032016-02-29 15:25:47 -0700319}
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000320
Simon Glass3e8bd462016-02-29 15:25:48 -0700321void part_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000322{
Simon Glass96e5b032016-02-29 15:25:47 -0700323 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000324
Kever Yangd4729262018-02-10 17:55:37 +0800325 drv = part_driver_lookup_type(dev_desc);
Simon Glass96e5b032016-02-29 15:25:47 -0700326 if (!drv) {
327 printf("## Unknown partition table type %x\n",
328 dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +0000329 return;
wdenkaffae2b2002-08-17 09:36:01 +0000330 }
Simon Glass96e5b032016-02-29 15:25:47 -0700331
332 PRINTF("## Testing for valid %s partition ##\n", drv->name);
333 print_part_header(drv->name, dev_desc);
334 if (drv->print)
335 drv->print(dev_desc);
wdenkaffae2b2002-08-17 09:36:01 +0000336}
337
Joshua Watt387f8be2023-07-03 08:39:54 -0500338int part_get_info_by_type(struct blk_desc *dev_desc, int part, int part_type,
339 struct disk_partition *info)
Stephen Warren2f501642012-09-21 12:46:54 +0000340{
Simon Glass96e5b032016-02-29 15:25:47 -0700341 struct part_driver *drv;
Stephen Warren2f501642012-09-21 12:46:54 +0000342
Simon Glassa51eb8d2022-08-11 19:34:45 -0600343 if (blk_enabled()) {
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100344#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Simon Glassa51eb8d2022-08-11 19:34:45 -0600345 /* The common case is no UUID support */
346 info->uuid[0] = 0;
Stephen Warren894bfbb2012-09-21 09:50:59 +0000347#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100348#ifdef CONFIG_PARTITION_TYPE_GUID
Simon Glassa51eb8d2022-08-11 19:34:45 -0600349 info->type_guid[0] = 0;
Patrick Delaunay7561b252015-10-27 11:00:27 +0100350#endif
Stephen Warren894bfbb2012-09-21 09:50:59 +0000351
Joshua Watt387f8be2023-07-03 08:39:54 -0500352 if (part_type == PART_TYPE_UNKNOWN) {
353 drv = part_driver_lookup_type(dev_desc);
354 } else {
355 drv = part_driver_get_type(part_type);
356 }
357
Simon Glassa51eb8d2022-08-11 19:34:45 -0600358 if (!drv) {
359 debug("## Unknown partition table type %x\n",
360 dev_desc->part_type);
361 return -EPROTONOSUPPORT;
362 }
363 if (!drv->get_info) {
364 PRINTF("## Driver %s does not have the get_info() method\n",
365 drv->name);
366 return -ENOSYS;
367 }
368 if (drv->get_info(dev_desc, part, info) == 0) {
369 PRINTF("## Valid %s partition found ##\n", drv->name);
370 return 0;
371 }
Simon Glass96e5b032016-02-29 15:25:47 -0700372 }
Stephen Warren2f501642012-09-21 12:46:54 +0000373
Sean Anderson59715752021-02-05 09:38:55 -0500374 return -ENOENT;
Stephen Warren2f501642012-09-21 12:46:54 +0000375}
Rob Herring99d2c202012-09-21 04:08:17 +0000376
Joshua Watt387f8be2023-07-03 08:39:54 -0500377int part_get_info(struct blk_desc *dev_desc, int part,
378 struct disk_partition *info)
379{
380 return part_get_info_by_type(dev_desc, part, PART_TYPE_UNKNOWN, info);
381}
382
Simon Glass05289792020-05-10 11:39:57 -0600383int part_get_info_whole_disk(struct blk_desc *dev_desc,
384 struct disk_partition *info)
Rob Clark4bbcc962017-09-09 13:15:55 -0400385{
386 info->start = 0;
387 info->size = dev_desc->lba;
388 info->blksz = dev_desc->blksz;
389 info->bootable = 0;
390 strcpy((char *)info->type, BOOT_PART_TYPE);
391 strcpy((char *)info->name, "Whole Disk");
392#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
393 info->uuid[0] = 0;
394#endif
395#ifdef CONFIG_PARTITION_TYPE_GUID
396 info->type_guid[0] = 0;
397#endif
398
399 return 0;
400}
401
Simon Glassebac37c2016-02-29 15:25:43 -0700402int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
403 struct blk_desc **dev_desc)
Stephen Warren2023e602012-09-21 09:50:56 +0000404{
405 char *ep;
Stephen Warren336b6f92014-05-07 12:19:01 -0600406 char *dup_str = NULL;
407 const char *dev_str, *hwpart_str;
408 int dev, hwpart;
409
410 hwpart_str = strchr(dev_hwpart_str, '.');
411 if (hwpart_str) {
412 dup_str = strdup(dev_hwpart_str);
413 dup_str[hwpart_str - dev_hwpart_str] = 0;
414 dev_str = dup_str;
415 hwpart_str++;
416 } else {
417 dev_str = dev_hwpart_str;
Stephen Warrenecdd57e2014-05-23 12:48:11 -0600418 hwpart = 0;
Stephen Warren336b6f92014-05-07 12:19:01 -0600419 }
Stephen Warren2023e602012-09-21 09:50:56 +0000420
Simon Glass7e5f4602021-07-24 09:03:29 -0600421 dev = hextoul(dev_str, &ep);
Stephen Warren2023e602012-09-21 09:50:56 +0000422 if (*ep) {
423 printf("** Bad device specification %s %s **\n",
424 ifname, dev_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600425 dev = -EINVAL;
Stephen Warren336b6f92014-05-07 12:19:01 -0600426 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000427 }
428
Stephen Warren336b6f92014-05-07 12:19:01 -0600429 if (hwpart_str) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600430 hwpart = hextoul(hwpart_str, &ep);
Stephen Warren336b6f92014-05-07 12:19:01 -0600431 if (*ep) {
432 printf("** Bad HW partition specification %s %s **\n",
433 ifname, hwpart_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600434 dev = -EINVAL;
Stephen Warren336b6f92014-05-07 12:19:01 -0600435 goto cleanup;
436 }
437 }
438
439 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warren2023e602012-09-21 09:50:56 +0000440 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Sam Protsenko8f690842018-07-30 19:19:27 +0300441 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
Sean Anderson59715752021-02-05 09:38:55 -0500442 dev = -ENODEV;
Stephen Warren336b6f92014-05-07 12:19:01 -0600443 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000444 }
445
Simon Glassa51eb8d2022-08-11 19:34:45 -0600446 if (blk_enabled()) {
447 /*
448 * Updates the partition table for the specified hw partition.
449 * Always should be done, otherwise hw partition 0 will return
450 * stale data after displaying a non-zero hw partition.
451 */
Simon Glass8149b152022-09-17 09:00:09 -0600452 if ((*dev_desc)->uclass_id == UCLASS_MMC)
Simon Glassa51eb8d2022-08-11 19:34:45 -0600453 part_init(*dev_desc);
454 }
Erik Tideman99e7fc82016-01-11 13:39:07 +0000455
Stephen Warren336b6f92014-05-07 12:19:01 -0600456cleanup:
457 free(dup_str);
Stephen Warren2023e602012-09-21 09:50:56 +0000458 return dev;
459}
460
Stephen Warren10a37fd2012-09-21 09:50:57 +0000461#define PART_UNSPECIFIED -2
462#define PART_AUTO -1
Simon Glasse35929e2016-02-29 15:25:44 -0700463int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glass4101f682016-02-29 15:25:34 -0700464 struct blk_desc **dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600465 struct disk_partition *info, int allow_whole_dev)
Rob Herring99d2c202012-09-21 04:08:17 +0000466{
Sean Anderson59715752021-02-05 09:38:55 -0500467 int ret;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000468 const char *part_str;
469 char *dup_str = NULL;
470 const char *dev_str;
Rob Herring99d2c202012-09-21 04:08:17 +0000471 int dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000472 char *ep;
473 int p;
474 int part;
Simon Glass05289792020-05-10 11:39:57 -0600475 struct disk_partition tmpinfo;
Rob Herring99d2c202012-09-21 04:08:17 +0000476
Heinrich Schuchardtbe2e42f2022-11-20 11:23:24 +0100477 *dev_desc = NULL;
478 memset(info, 0, sizeof(*info));
479
Sean Andersonf676b452022-03-22 16:59:20 -0400480#if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
Stephen Warren4d907022014-06-12 10:28:32 -0600481 /*
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200482 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warren4d907022014-06-12 10:28:32 -0600483 * host's own filesystem.
484 */
Heinrich Schuchardtbe2e42f2022-11-20 11:23:24 +0100485 if (!strcmp(ifname, "hostfs")) {
Stephen Warren4d907022014-06-12 10:28:32 -0600486 strcpy((char *)info->type, BOOT_PART_TYPE);
Sean Andersonf676b452022-03-22 16:59:20 -0400487 strcpy((char *)info->name, "Host filesystem");
Stephen Warren4d907022014-06-12 10:28:32 -0600488
489 return 0;
490 }
Hans de Goedeafc17442015-09-17 18:46:55 -0400491#endif
Stephen Warren4d907022014-06-12 10:28:32 -0600492
Stefan Herbrechtsmeier2f03a632022-08-08 16:45:17 +0200493#if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_SPL_BUILD)
Hans de Goede251cee02015-09-17 18:46:58 -0400494 /*
Heinrich Schuchardt3174caf2019-04-10 18:59:26 +0200495 * Special-case ubi, ubi goes through a mtd, rather than through
Hans de Goede251cee02015-09-17 18:46:58 -0400496 * a regular block device.
497 */
Heinrich Schuchardtbe2e42f2022-11-20 11:23:24 +0100498 if (!strcmp(ifname, "ubi")) {
Hans de Goede251cee02015-09-17 18:46:58 -0400499 if (!ubifs_is_mounted()) {
500 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Sean Anderson59715752021-02-05 09:38:55 -0500501 return -EINVAL;
Hans de Goede251cee02015-09-17 18:46:58 -0400502 }
503
Hans de Goede251cee02015-09-17 18:46:58 -0400504 strcpy((char *)info->type, BOOT_PART_TYPE);
505 strcpy((char *)info->name, "UBI");
Hans de Goede251cee02015-09-17 18:46:58 -0400506 return 0;
507 }
508#endif
509
Stephen Warren10a37fd2012-09-21 09:50:57 +0000510 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena10973e2012-09-28 05:34:09 +0000511 if (!dev_part_str || !strlen(dev_part_str) ||
512 !strcmp(dev_part_str, "-"))
Simon Glass00caae62017-08-03 12:22:12 -0600513 dev_part_str = env_get("bootdevice");
Rob Herring99d2c202012-09-21 04:08:17 +0000514
Stephen Warren10a37fd2012-09-21 09:50:57 +0000515 /* If still no dev_part_str, it's an error */
516 if (!dev_part_str) {
517 printf("** No device specified **\n");
Sean Anderson59715752021-02-05 09:38:55 -0500518 ret = -ENODEV;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000519 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000520 }
521
Stephen Warren10a37fd2012-09-21 09:50:57 +0000522 /* Separate device and partition ID specification */
523 part_str = strchr(dev_part_str, ':');
524 if (part_str) {
525 dup_str = strdup(dev_part_str);
526 dup_str[part_str - dev_part_str] = 0;
527 dev_str = dup_str;
528 part_str++;
529 } else {
530 dev_str = dev_part_str;
531 }
Rob Herring99d2c202012-09-21 04:08:17 +0000532
Stephen Warren10a37fd2012-09-21 09:50:57 +0000533 /* Look up the device */
Simon Glassebac37c2016-02-29 15:25:43 -0700534 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
Sean Anderson59715752021-02-05 09:38:55 -0500535 if (dev < 0) {
Oleksii Bidnichenko192e7012022-04-08 10:07:13 +0200536 printf("** Bad device specification %s %s **\n",
537 ifname, dev_str);
Sean Anderson59715752021-02-05 09:38:55 -0500538 ret = dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000539 goto cleanup;
Sean Anderson59715752021-02-05 09:38:55 -0500540 }
Stephen Warren10a37fd2012-09-21 09:50:57 +0000541
542 /* Convert partition ID string to number */
543 if (!part_str || !*part_str) {
544 part = PART_UNSPECIFIED;
545 } else if (!strcmp(part_str, "auto")) {
546 part = PART_AUTO;
547 } else {
548 /* Something specified -> use exactly that */
Simon Glass7e5f4602021-07-24 09:03:29 -0600549 part = (int)hextoul(part_str, &ep);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000550 /*
551 * Less than whole string converted,
552 * or request for whole device, but caller requires partition.
553 */
554 if (*ep || (part == 0 && !allow_whole_dev)) {
555 printf("** Bad partition specification %s %s **\n",
556 ifname, dev_part_str);
Sean Anderson59715752021-02-05 09:38:55 -0500557 ret = -ENOENT;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000558 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000559 }
Rob Herring99d2c202012-09-21 04:08:17 +0000560 }
561
Stephen Warren10a37fd2012-09-21 09:50:57 +0000562 /*
563 * No partition table on device,
564 * or user requested partition 0 (entire device).
565 */
566 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
567 (part == 0)) {
568 if (!(*dev_desc)->lba) {
569 printf("** Bad device size - %s %s **\n", ifname,
570 dev_str);
Sean Anderson59715752021-02-05 09:38:55 -0500571 ret = -EINVAL;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000572 goto cleanup;
573 }
Rob Herring99d2c202012-09-21 04:08:17 +0000574
Stephen Warren10a37fd2012-09-21 09:50:57 +0000575 /*
576 * If user specified a partition ID other than 0,
577 * or the calling command only accepts partitions,
578 * it's an error.
579 */
580 if ((part > 0) || (!allow_whole_dev)) {
581 printf("** No partition table - %s %s **\n", ifname,
582 dev_str);
Sean Anderson59715752021-02-05 09:38:55 -0500583 ret = -EPROTONOSUPPORT;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000584 goto cleanup;
585 }
586
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800587 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
588
Rob Clark4bbcc962017-09-09 13:15:55 -0400589 part_get_info_whole_disk(*dev_desc, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000590
591 ret = 0;
592 goto cleanup;
593 }
594
595 /*
596 * Now there's known to be a partition table,
597 * not specifying a partition means to pick partition 1.
598 */
599 if (part == PART_UNSPECIFIED)
600 part = 1;
601
602 /*
603 * If user didn't specify a partition number, or did specify something
604 * other than "auto", use that partition number directly.
605 */
606 if (part != PART_AUTO) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700607 ret = part_get_info(*dev_desc, part, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000608 if (ret) {
609 printf("** Invalid partition %d **\n", part);
610 goto cleanup;
611 }
612 } else {
613 /*
614 * Find the first bootable partition.
615 * If none are bootable, fall back to the first valid partition.
616 */
617 part = 0;
618 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700619 ret = part_get_info(*dev_desc, p, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000620 if (ret)
621 continue;
622
623 /*
624 * First valid partition, or new better partition?
625 * If so, save partition ID.
626 */
627 if (!part || info->bootable)
628 part = p;
629
630 /* Best possible partition? Stop searching. */
631 if (info->bootable)
632 break;
633
634 /*
635 * We now need to search further for best possible.
636 * If we what we just queried was the best so far,
637 * save the info since we over-write it next loop.
638 */
639 if (part == p)
640 tmpinfo = *info;
641 }
642 /* If we found any acceptable partition */
643 if (part) {
644 /*
645 * If we searched all possible partition IDs,
646 * return the first valid partition we found.
647 */
648 if (p == MAX_SEARCH_PARTITIONS + 1)
649 *info = tmpinfo;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000650 } else {
651 printf("** No valid partitions found **\n");
652 goto cleanup;
653 }
Rob Herring99d2c202012-09-21 04:08:17 +0000654 }
655 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
656 printf("** Invalid partition type \"%.32s\""
657 " (expect \"" BOOT_PART_TYPE "\")\n",
658 info->type);
Sean Anderson59715752021-02-05 09:38:55 -0500659 ret = -EINVAL;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000660 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000661 }
662
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800663 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
664
Stephen Warren10a37fd2012-09-21 09:50:57 +0000665 ret = part;
666 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000667
Stephen Warren10a37fd2012-09-21 09:50:57 +0000668cleanup:
669 free(dup_str);
670 return ret;
Rob Herring99d2c202012-09-21 04:08:17 +0000671}
Petr Kulhavy87b85302016-09-09 10:27:15 +0200672
Sam Protsenko30789092017-09-22 01:51:58 +0300673int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
Simon Glass05289792020-05-10 11:39:57 -0600674 struct disk_partition *info, int part_type)
Petr Kulhavy87b85302016-09-09 10:27:15 +0200675{
Petr Kulhavy87b85302016-09-09 10:27:15 +0200676 struct part_driver *part_drv;
Kever Yang56670d62018-02-10 17:55:38 +0800677 int ret;
678 int i;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200679
Kever Yang56670d62018-02-10 17:55:38 +0800680 part_drv = part_driver_lookup_type(dev_desc);
681 if (!part_drv)
682 return -1;
schspa50f7b2e2021-10-20 20:15:55 +0800683
684 if (!part_drv->get_info) {
685 log_debug("## Driver %s does not have the get_info() method\n",
686 part_drv->name);
687 return -ENOSYS;
688 }
689
Kever Yang56670d62018-02-10 17:55:38 +0800690 for (i = 1; i < part_drv->max_entries; i++) {
691 ret = part_drv->get_info(dev_desc, i, info);
692 if (ret != 0) {
693 /* no more entries in table */
694 break;
695 }
696 if (strcmp(name, (const char *)info->name) == 0) {
697 /* matched */
698 return i;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200699 }
700 }
Kever Yang56670d62018-02-10 17:55:38 +0800701
Sean Anderson59715752021-02-05 09:38:55 -0500702 return -ENOENT;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200703}
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200704
Sam Protsenko30789092017-09-22 01:51:58 +0300705int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
Simon Glass05289792020-05-10 11:39:57 -0600706 struct disk_partition *info)
Sam Protsenko30789092017-09-22 01:51:58 +0300707{
708 return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
709}
710
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300711/**
712 * Get partition info from device number and partition name.
713 *
714 * Parse a device number and partition name string in the form of
Sean Anderson81c1aa82021-02-05 09:38:57 -0500715 * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
716 * hwpartnum are both optional, defaulting to 0. If the partition is found,
717 * sets dev_desc and part_info accordingly with the information of the
718 * partition with the given partition_name.
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300719 *
720 * @param[in] dev_iface Device interface
Sean Anderson81c1aa82021-02-05 09:38:57 -0500721 * @param[in] dev_part_str Input string argument, like "0.1#misc"
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300722 * @param[out] dev_desc Place to store the device description pointer
723 * @param[out] part_info Place to store the partition information
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100724 * Return: 0 on success, or a negative on error
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300725 */
726static int part_get_info_by_dev_and_name(const char *dev_iface,
727 const char *dev_part_str,
728 struct blk_desc **dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600729 struct disk_partition *part_info)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300730{
Sean Anderson81c1aa82021-02-05 09:38:57 -0500731 char *dup_str = NULL;
732 const char *dev_str, *part_str;
733 int ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300734
Sean Anderson81c1aa82021-02-05 09:38:57 -0500735 /* Separate device and partition name specification */
Sean Anderson26de4292021-05-15 14:13:54 -0400736 if (dev_part_str)
737 part_str = strchr(dev_part_str, '#');
738 else
739 part_str = NULL;
740
Sean Anderson81c1aa82021-02-05 09:38:57 -0500741 if (part_str) {
742 dup_str = strdup(dev_part_str);
743 dup_str[part_str - dev_part_str] = 0;
744 dev_str = dup_str;
745 part_str++;
746 } else {
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300747 return -EINVAL;
748 }
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300749
Sean Anderson81c1aa82021-02-05 09:38:57 -0500750 ret = blk_get_device_by_str(dev_iface, dev_str, dev_desc);
Sean Andersonfe5a5092021-04-12 18:53:06 -0400751 if (ret < 0)
Sean Anderson81c1aa82021-02-05 09:38:57 -0500752 goto cleanup;
753
Sean Anderson59715752021-02-05 09:38:55 -0500754 ret = part_get_info_by_name(*dev_desc, part_str, part_info);
755 if (ret < 0)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300756 printf("Could not find \"%s\" partition\n", part_str);
Sean Anderson81c1aa82021-02-05 09:38:57 -0500757
758cleanup:
759 free(dup_str);
Sean Anderson59715752021-02-05 09:38:55 -0500760 return ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300761}
762
763int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
764 const char *dev_part_str,
765 struct blk_desc **dev_desc,
Sean Anderson9f7bb282021-02-05 09:38:56 -0500766 struct disk_partition *part_info,
767 int allow_whole_dev)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300768{
Sean Anderson59715752021-02-05 09:38:55 -0500769 int ret;
770
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300771 /* Split the part_name if passed as "$dev_num#part_name". */
Sean Anderson59715752021-02-05 09:38:55 -0500772 ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str,
773 dev_desc, part_info);
774 if (ret >= 0)
775 return ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300776 /*
777 * Couldn't lookup by name, try looking up the partition description
778 * directly.
779 */
Sean Anderson59715752021-02-05 09:38:55 -0500780 ret = blk_get_device_part_str(dev_iface, dev_part_str,
Sean Anderson9f7bb282021-02-05 09:38:56 -0500781 dev_desc, part_info, allow_whole_dev);
Sean Anderson59715752021-02-05 09:38:55 -0500782 if (ret < 0)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300783 printf("Couldn't find partition %s %s\n",
784 dev_iface, dev_part_str);
Sean Anderson59715752021-02-05 09:38:55 -0500785 return ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300786}
787
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200788void part_set_generic_name(const struct blk_desc *dev_desc,
789 int part_num, char *name)
790{
791 char *devtype;
792
Simon Glass8149b152022-09-17 09:00:09 -0600793 switch (dev_desc->uclass_id) {
Simon Glasse33a5c62022-08-11 19:34:59 -0600794 case UCLASS_IDE:
795 case UCLASS_AHCI:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200796 devtype = "hd";
797 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600798 case UCLASS_SCSI:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200799 devtype = "sd";
800 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600801 case UCLASS_USB:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200802 devtype = "usbd";
803 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600804 case UCLASS_MMC:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200805 devtype = "mmcsd";
806 break;
807 default:
808 devtype = "xx";
809 break;
810 }
811
812 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);
813}
Simon Glassdcffa442023-01-17 10:47:41 -0700814
815int part_get_bootable(struct blk_desc *desc)
816{
817 struct disk_partition info;
818 int p;
819
820 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
821 int ret;
822
823 ret = part_get_info(desc, p, &info);
824 if (!ret && info.bootable)
825 return p;
826 }
827
828 return 0;
829}