blob: 8035dcb8a429e30b0731192af3288c0d81697036 [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
Simon Glass5aab05d2023-08-24 13:55:24 -060045static struct part_driver *part_driver_lookup_type(struct blk_desc *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
Simon Glass5aab05d2023-08-24 13:55:24 -060052 if (desc->part_type == PART_TYPE_UNKNOWN) {
Kever Yangd4729262018-02-10 17:55:37 +080053 for (entry = drv; entry != drv + n_ents; entry++) {
54 int ret;
55
Simon Glass5aab05d2023-08-24 13:55:24 -060056 ret = entry->test(desc);
Kever Yangd4729262018-02-10 17:55:37 +080057 if (!ret) {
Simon Glass5aab05d2023-08-24 13:55:24 -060058 desc->part_type = entry->part_type;
Kever Yangd4729262018-02-10 17:55:37 +080059 return entry;
60 }
61 }
62 } else {
Simon Glass5aab05d2023-08-24 13:55:24 -060063 return part_driver_get_type(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 Glass5aab05d2023-08-24 13:55:24 -060088 struct blk_desc *desc;
Simon Glass6dd9faf2016-05-01 13:52:32 -060089 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 Glass5aab05d2023-08-24 13:55:24 -060093 desc = blk_get_devnum_by_uclass_idname(ifname, dev);
94 if (!desc) {
Simon Glass6dd9faf2016-05-01 13:52:32 -060095 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 Glass5aab05d2023-08-24 13:55:24 -060099 ret = blk_dselect_hwpart(desc, hwpart);
Simon Glass6dd9faf2016-05-01 13:52:32 -0600100 if (ret) {
101 debug("%s: Failed to select h/w partition: err-%d\n", __func__,
102 ret);
103 return NULL;
104 }
105
Simon Glass5aab05d2023-08-24 13:55:24 -0600106 return 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 Glass5aab05d2023-08-24 13:55:24 -0600143void dev_print(struct blk_desc *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
Simon Glass5aab05d2023-08-24 13:55:24 -0600147 if (desc->type == DEV_TYPE_UNKNOWN) {
Wolfgang Denkaf75a452009-05-15 09:27:58 +0200148 puts ("not available\n");
149 return;
150 }
151
Simon Glass5aab05d2023-08-24 13:55:24 -0600152 switch (desc->uclass_id) {
Simon Glasse33a5c62022-08-11 19:34:59 -0600153 case UCLASS_SCSI:
Simon Glass5aab05d2023-08-24 13:55:24 -0600154 printf("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", desc->target,
155 desc->lun, desc->vendor, desc->product, desc->revision);
Detlev Zundel574b3192008-05-05 16:11:21 +0200156 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600157 case UCLASS_IDE:
158 case UCLASS_AHCI:
Simon Glass5aab05d2023-08-24 13:55:24 -0600159 printf("Model: %s Firm: %s Ser#: %s\n", desc->vendor,
160 desc->revision, desc->product);
Detlev Zundel574b3192008-05-05 16:11:21 +0200161 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600162 case UCLASS_MMC:
163 case UCLASS_USB:
164 case UCLASS_NVME:
165 case UCLASS_PVBLOCK:
Simon Glass95201812022-10-29 19:47:17 -0600166 case UCLASS_HOST:
Tobias Waldekranzbb56da12023-02-16 16:33:52 +0100167 case UCLASS_BLKMAP:
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300168 printf ("Vendor: %s Rev: %s Prod: %s\n",
Simon Glass5aab05d2023-08-24 13:55:24 -0600169 desc->vendor,
170 desc->revision,
171 desc->product);
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300172 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600173 case UCLASS_VIRTIO:
Simon Glass5aab05d2023-08-24 13:55:24 -0600174 printf("%s VirtIO Block Device\n", desc->vendor);
Tuomas Tynkkynen4ad54ec2018-10-15 02:21:10 -0700175 break;
Simon Glassef4b66b2022-08-11 19:35:00 -0600176 case UCLASS_EFI_MEDIA:
Simon Glass5aab05d2023-08-24 13:55:24 -0600177 printf("EFI media Block Device %d\n", desc->devnum);
Simon Glassef4b66b2022-08-11 19:35:00 -0600178 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600179 case UCLASS_INVALID:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200180 puts("device type unknown\n");
181 return;
Detlev Zundel574b3192008-05-05 16:11:21 +0200182 default:
Simon Glass5aab05d2023-08-24 13:55:24 -0600183 printf("Unhandled device type: %i\n", desc->uclass_id);
Detlev Zundel574b3192008-05-05 16:11:21 +0200184 return;
wdenkaffae2b2002-08-17 09:36:01 +0000185 }
186 puts (" Type: ");
Simon Glass5aab05d2023-08-24 13:55:24 -0600187 if (desc->removable)
wdenkaffae2b2002-08-17 09:36:01 +0000188 puts ("Removable ");
Simon Glass5aab05d2023-08-24 13:55:24 -0600189 switch (desc->type & 0x1F) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200190 case DEV_TYPE_HARDDISK:
191 puts ("Hard Disk");
192 break;
193 case DEV_TYPE_CDROM:
194 puts ("CD ROM");
195 break;
196 case DEV_TYPE_OPDISK:
197 puts ("Optical Device");
198 break;
199 case DEV_TYPE_TAPE:
200 puts ("Tape");
201 break;
202 default:
Simon Glass5aab05d2023-08-24 13:55:24 -0600203 printf("# %02X #", desc->type & 0x1F);
Detlev Zundel726c0f12008-05-05 16:11:22 +0200204 break;
wdenkaffae2b2002-08-17 09:36:01 +0000205 }
206 puts ("\n");
Simon Glass5aab05d2023-08-24 13:55:24 -0600207 if (desc->lba > 0L && desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000208 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkc40b2952004-03-13 23:29:43 +0000209 lbaint_t lba;
wdenk6e592382004-04-18 17:39:38 +0000210
Simon Glass5aab05d2023-08-24 13:55:24 -0600211 lba = desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000212
Simon Glass5aab05d2023-08-24 13:55:24 -0600213 lba512 = lba * (desc->blksz / 512);
wdenkaffae2b2002-08-17 09:36:01 +0000214 /* round to 1 digit */
Pavel Machek214b3f32014-09-09 15:19:42 +0200215 /* 2048 = (1024 * 1024) / 512 MB */
Heinrich Schuchardt17416ea2019-06-02 00:04:50 +0200216 mb = lba512_muldiv(lba512, 10, 11);
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300217
wdenkaffae2b2002-08-17 09:36:01 +0000218 mb_quot = mb / 10;
219 mb_rem = mb - (10 * mb_quot);
220
221 gb = mb / 1024;
222 gb_quot = gb / 10;
223 gb_rem = gb - (10 * gb_quot);
wdenk42dfe7a2004-03-14 22:25:36 +0000224#ifdef CONFIG_LBA48
Simon Glass5aab05d2023-08-24 13:55:24 -0600225 if (desc->lba48)
wdenkc40b2952004-03-13 23:29:43 +0000226 printf (" Supports 48-bit addressing\n");
227#endif
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100228#if defined(CONFIG_SYS_64BIT_LBA)
Jean-Jacques Hiblot139f7b12016-12-23 10:45:43 +0100229 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
wdenkc40b2952004-03-13 23:29:43 +0000230 mb_quot, mb_rem,
231 gb_quot, gb_rem,
232 lba,
Simon Glass5aab05d2023-08-24 13:55:24 -0600233 desc->blksz);
wdenkc40b2952004-03-13 23:29:43 +0000234#else
Jean-Jacques Hiblot139f7b12016-12-23 10:45:43 +0100235 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
wdenkaffae2b2002-08-17 09:36:01 +0000236 mb_quot, mb_rem,
237 gb_quot, gb_rem,
wdenkc40b2952004-03-13 23:29:43 +0000238 (ulong)lba,
Simon Glass5aab05d2023-08-24 13:55:24 -0600239 desc->blksz);
wdenkc40b2952004-03-13 23:29:43 +0000240#endif
wdenkaffae2b2002-08-17 09:36:01 +0000241 } else {
242 puts (" Capacity: not available\n");
243 }
244}
wdenkaffae2b2002-08-17 09:36:01 +0000245
Simon Glass5aab05d2023-08-24 13:55:24 -0600246void part_init(struct blk_desc *desc)
wdenkaffae2b2002-08-17 09:36:01 +0000247{
Simon Glass96e5b032016-02-29 15:25:47 -0700248 struct part_driver *drv =
249 ll_entry_start(struct part_driver, part_driver);
250 const int n_ents = ll_entry_count(struct part_driver, part_driver);
251 struct part_driver *entry;
wdenkaffae2b2002-08-17 09:36:01 +0000252
Simon Glass5aab05d2023-08-24 13:55:24 -0600253 blkcache_invalidate(desc->uclass_id, desc->devnum);
Eric Nelsone40cf342016-03-28 10:05:44 -0700254
Simon Glass5aab05d2023-08-24 13:55:24 -0600255 desc->part_type = PART_TYPE_UNKNOWN;
Simon Glass96e5b032016-02-29 15:25:47 -0700256 for (entry = drv; entry != drv + n_ents; entry++) {
257 int ret;
258
Simon Glass5aab05d2023-08-24 13:55:24 -0600259 ret = entry->test(desc);
Simon Glass96e5b032016-02-29 15:25:47 -0700260 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
261 if (!ret) {
Simon Glass5aab05d2023-08-24 13:55:24 -0600262 desc->part_type = entry->part_type;
Simon Glass96e5b032016-02-29 15:25:47 -0700263 break;
264 }
265 }
wdenkaffae2b2002-08-17 09:36:01 +0000266}
267
Simon Glass5aab05d2023-08-24 13:55:24 -0600268static void print_part_header(const char *type, struct blk_desc *desc)
Simon Glass96e5b032016-02-29 15:25:47 -0700269{
Patrick Delaunayf18fa312017-01-27 11:00:36 +0100270#if CONFIG_IS_ENABLED(MAC_PARTITION) || \
Patrick Delaunayb0cf7332017-01-27 11:00:37 +0100271 CONFIG_IS_ENABLED(DOS_PARTITION) || \
Patrick Delaunay1acc0082017-01-27 11:00:38 +0100272 CONFIG_IS_ENABLED(ISO_PARTITION) || \
Patrick Delaunay863c5b62017-01-27 11:00:39 +0100273 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100274 CONFIG_IS_ENABLED(EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000275 puts ("\nPartition Map for ");
Simon Glass5aab05d2023-08-24 13:55:24 -0600276 switch (desc->uclass_id) {
Simon Glasse33a5c62022-08-11 19:34:59 -0600277 case UCLASS_IDE:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200278 puts ("IDE");
279 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600280 case UCLASS_AHCI:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200281 puts ("SATA");
282 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600283 case UCLASS_SCSI:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200284 puts ("SCSI");
285 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600286 case UCLASS_USB:
Detlev Zundel726c0f12008-05-05 16:11:22 +0200287 puts ("USB");
288 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600289 case UCLASS_MMC:
Lei Wen8f3b9642010-09-13 22:07:28 +0800290 puts ("MMC");
291 break;
Simon Glass95201812022-10-29 19:47:17 -0600292 case UCLASS_HOST:
Zhikang Zhangffab6942017-08-03 02:30:56 -0700293 puts ("HOST");
294 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600295 case UCLASS_NVME:
Zhikang Zhangffab6942017-08-03 02:30:56 -0700296 puts ("NVMe");
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700297 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600298 case UCLASS_PVBLOCK:
Anastasiia Lukianenko722bc5b2020-08-06 12:42:55 +0300299 puts("PV BLOCK");
300 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600301 case UCLASS_VIRTIO:
Tuomas Tynkkynen4ad54ec2018-10-15 02:21:10 -0700302 puts("VirtIO");
303 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600304 case UCLASS_EFI_MEDIA:
Simon Glass42b7f422021-12-04 08:56:31 -0700305 puts("EFI");
306 break;
Detlev Zundel726c0f12008-05-05 16:11:22 +0200307 default:
Simon Glass42b7f422021-12-04 08:56:31 -0700308 puts("UNKNOWN");
Detlev Zundel726c0f12008-05-05 16:11:22 +0200309 break;
wdenkaffae2b2002-08-17 09:36:01 +0000310 }
311 printf (" device %d -- Partition Type: %s\n\n",
Simon Glass5aab05d2023-08-24 13:55:24 -0600312 desc->devnum, type);
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000313#endif /* any CONFIG_..._PARTITION */
Simon Glass96e5b032016-02-29 15:25:47 -0700314}
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000315
Simon Glass5aab05d2023-08-24 13:55:24 -0600316void part_print(struct blk_desc *desc)
wdenkaffae2b2002-08-17 09:36:01 +0000317{
Simon Glass96e5b032016-02-29 15:25:47 -0700318 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000319
Simon Glass5aab05d2023-08-24 13:55:24 -0600320 drv = part_driver_lookup_type(desc);
Simon Glass96e5b032016-02-29 15:25:47 -0700321 if (!drv) {
322 printf("## Unknown partition table type %x\n",
Simon Glass5aab05d2023-08-24 13:55:24 -0600323 desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +0000324 return;
wdenkaffae2b2002-08-17 09:36:01 +0000325 }
Simon Glass96e5b032016-02-29 15:25:47 -0700326
327 PRINTF("## Testing for valid %s partition ##\n", drv->name);
Simon Glass5aab05d2023-08-24 13:55:24 -0600328 print_part_header(drv->name, desc);
Simon Glass96e5b032016-02-29 15:25:47 -0700329 if (drv->print)
Simon Glass5aab05d2023-08-24 13:55:24 -0600330 drv->print(desc);
wdenkaffae2b2002-08-17 09:36:01 +0000331}
332
Simon Glass5aab05d2023-08-24 13:55:24 -0600333int part_get_info_by_type(struct blk_desc *desc, int part, int part_type,
Joshua Watt387f8be2023-07-03 08:39:54 -0500334 struct disk_partition *info)
Stephen Warren2f501642012-09-21 12:46:54 +0000335{
Simon Glass96e5b032016-02-29 15:25:47 -0700336 struct part_driver *drv;
Stephen Warren2f501642012-09-21 12:46:54 +0000337
Simon Glassa51eb8d2022-08-11 19:34:45 -0600338 if (blk_enabled()) {
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100339#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Simon Glassa51eb8d2022-08-11 19:34:45 -0600340 /* The common case is no UUID support */
341 info->uuid[0] = 0;
Stephen Warren894bfbb2012-09-21 09:50:59 +0000342#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100343#ifdef CONFIG_PARTITION_TYPE_GUID
Simon Glassa51eb8d2022-08-11 19:34:45 -0600344 info->type_guid[0] = 0;
Patrick Delaunay7561b252015-10-27 11:00:27 +0100345#endif
Stephen Warren894bfbb2012-09-21 09:50:59 +0000346
Joshua Watt387f8be2023-07-03 08:39:54 -0500347 if (part_type == PART_TYPE_UNKNOWN) {
Simon Glass5aab05d2023-08-24 13:55:24 -0600348 drv = part_driver_lookup_type(desc);
Joshua Watt387f8be2023-07-03 08:39:54 -0500349 } else {
350 drv = part_driver_get_type(part_type);
351 }
352
Simon Glassa51eb8d2022-08-11 19:34:45 -0600353 if (!drv) {
354 debug("## Unknown partition table type %x\n",
Simon Glass5aab05d2023-08-24 13:55:24 -0600355 desc->part_type);
Simon Glassa51eb8d2022-08-11 19:34:45 -0600356 return -EPROTONOSUPPORT;
357 }
358 if (!drv->get_info) {
359 PRINTF("## Driver %s does not have the get_info() method\n",
360 drv->name);
361 return -ENOSYS;
362 }
Simon Glass5aab05d2023-08-24 13:55:24 -0600363 if (drv->get_info(desc, part, info) == 0) {
Simon Glassa51eb8d2022-08-11 19:34:45 -0600364 PRINTF("## Valid %s partition found ##\n", drv->name);
365 return 0;
366 }
Simon Glass96e5b032016-02-29 15:25:47 -0700367 }
Stephen Warren2f501642012-09-21 12:46:54 +0000368
Sean Anderson59715752021-02-05 09:38:55 -0500369 return -ENOENT;
Stephen Warren2f501642012-09-21 12:46:54 +0000370}
Rob Herring99d2c202012-09-21 04:08:17 +0000371
Simon Glass5aab05d2023-08-24 13:55:24 -0600372int part_get_info(struct blk_desc *desc, int part,
Joshua Watt387f8be2023-07-03 08:39:54 -0500373 struct disk_partition *info)
374{
Simon Glass5aab05d2023-08-24 13:55:24 -0600375 return part_get_info_by_type(desc, part, PART_TYPE_UNKNOWN, info);
Joshua Watt387f8be2023-07-03 08:39:54 -0500376}
377
Simon Glass5aab05d2023-08-24 13:55:24 -0600378int part_get_info_whole_disk(struct blk_desc *desc,
Simon Glass05289792020-05-10 11:39:57 -0600379 struct disk_partition *info)
Rob Clark4bbcc962017-09-09 13:15:55 -0400380{
381 info->start = 0;
Simon Glass5aab05d2023-08-24 13:55:24 -0600382 info->size = desc->lba;
383 info->blksz = desc->blksz;
Rob Clark4bbcc962017-09-09 13:15:55 -0400384 info->bootable = 0;
385 strcpy((char *)info->type, BOOT_PART_TYPE);
386 strcpy((char *)info->name, "Whole Disk");
387#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
388 info->uuid[0] = 0;
389#endif
390#ifdef CONFIG_PARTITION_TYPE_GUID
391 info->type_guid[0] = 0;
392#endif
393
394 return 0;
395}
396
Simon Glassebac37c2016-02-29 15:25:43 -0700397int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
Simon Glass5aab05d2023-08-24 13:55:24 -0600398 struct blk_desc **desc)
Stephen Warren2023e602012-09-21 09:50:56 +0000399{
400 char *ep;
Stephen Warren336b6f92014-05-07 12:19:01 -0600401 char *dup_str = NULL;
402 const char *dev_str, *hwpart_str;
403 int dev, hwpart;
404
405 hwpart_str = strchr(dev_hwpart_str, '.');
406 if (hwpart_str) {
407 dup_str = strdup(dev_hwpart_str);
408 dup_str[hwpart_str - dev_hwpart_str] = 0;
409 dev_str = dup_str;
410 hwpart_str++;
411 } else {
412 dev_str = dev_hwpart_str;
Stephen Warrenecdd57e2014-05-23 12:48:11 -0600413 hwpart = 0;
Stephen Warren336b6f92014-05-07 12:19:01 -0600414 }
Stephen Warren2023e602012-09-21 09:50:56 +0000415
Simon Glass7e5f4602021-07-24 09:03:29 -0600416 dev = hextoul(dev_str, &ep);
Stephen Warren2023e602012-09-21 09:50:56 +0000417 if (*ep) {
418 printf("** Bad device specification %s %s **\n",
419 ifname, dev_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600420 dev = -EINVAL;
Stephen Warren336b6f92014-05-07 12:19:01 -0600421 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000422 }
423
Stephen Warren336b6f92014-05-07 12:19:01 -0600424 if (hwpart_str) {
Simon Glass7e5f4602021-07-24 09:03:29 -0600425 hwpart = hextoul(hwpart_str, &ep);
Stephen Warren336b6f92014-05-07 12:19:01 -0600426 if (*ep) {
427 printf("** Bad HW partition specification %s %s **\n",
428 ifname, hwpart_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600429 dev = -EINVAL;
Stephen Warren336b6f92014-05-07 12:19:01 -0600430 goto cleanup;
431 }
432 }
433
Simon Glass5aab05d2023-08-24 13:55:24 -0600434 *desc = get_dev_hwpart(ifname, dev, hwpart);
435 if (!(*desc) || ((*desc)->type == DEV_TYPE_UNKNOWN)) {
Sam Protsenko8f690842018-07-30 19:19:27 +0300436 debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
Sean Anderson59715752021-02-05 09:38:55 -0500437 dev = -ENODEV;
Stephen Warren336b6f92014-05-07 12:19:01 -0600438 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000439 }
440
Simon Glassa51eb8d2022-08-11 19:34:45 -0600441 if (blk_enabled()) {
442 /*
443 * Updates the partition table for the specified hw partition.
444 * Always should be done, otherwise hw partition 0 will return
445 * stale data after displaying a non-zero hw partition.
446 */
Simon Glass5aab05d2023-08-24 13:55:24 -0600447 if ((*desc)->uclass_id == UCLASS_MMC)
448 part_init(*desc);
Simon Glassa51eb8d2022-08-11 19:34:45 -0600449 }
Erik Tideman99e7fc82016-01-11 13:39:07 +0000450
Stephen Warren336b6f92014-05-07 12:19:01 -0600451cleanup:
452 free(dup_str);
Stephen Warren2023e602012-09-21 09:50:56 +0000453 return dev;
454}
455
Stephen Warren10a37fd2012-09-21 09:50:57 +0000456#define PART_UNSPECIFIED -2
457#define PART_AUTO -1
Simon Glasse35929e2016-02-29 15:25:44 -0700458int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glass5aab05d2023-08-24 13:55:24 -0600459 struct blk_desc **desc,
Simon Glass05289792020-05-10 11:39:57 -0600460 struct disk_partition *info, int allow_whole_dev)
Rob Herring99d2c202012-09-21 04:08:17 +0000461{
Sean Anderson59715752021-02-05 09:38:55 -0500462 int ret;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000463 const char *part_str;
464 char *dup_str = NULL;
465 const char *dev_str;
Rob Herring99d2c202012-09-21 04:08:17 +0000466 int dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000467 char *ep;
468 int p;
469 int part;
Simon Glass05289792020-05-10 11:39:57 -0600470 struct disk_partition tmpinfo;
Rob Herring99d2c202012-09-21 04:08:17 +0000471
Simon Glass5aab05d2023-08-24 13:55:24 -0600472 *desc = NULL;
Heinrich Schuchardtbe2e42f2022-11-20 11:23:24 +0100473 memset(info, 0, sizeof(*info));
474
Sean Andersonf676b452022-03-22 16:59:20 -0400475#if IS_ENABLED(CONFIG_SANDBOX) || IS_ENABLED(CONFIG_SEMIHOSTING)
Stephen Warren4d907022014-06-12 10:28:32 -0600476 /*
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200477 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warren4d907022014-06-12 10:28:32 -0600478 * host's own filesystem.
479 */
Heinrich Schuchardtbe2e42f2022-11-20 11:23:24 +0100480 if (!strcmp(ifname, "hostfs")) {
Stephen Warren4d907022014-06-12 10:28:32 -0600481 strcpy((char *)info->type, BOOT_PART_TYPE);
Sean Andersonf676b452022-03-22 16:59:20 -0400482 strcpy((char *)info->name, "Host filesystem");
Stephen Warren4d907022014-06-12 10:28:32 -0600483
484 return 0;
485 }
Hans de Goedeafc17442015-09-17 18:46:55 -0400486#endif
Stephen Warren4d907022014-06-12 10:28:32 -0600487
Stefan Herbrechtsmeier2f03a632022-08-08 16:45:17 +0200488#if IS_ENABLED(CONFIG_CMD_UBIFS) && !IS_ENABLED(CONFIG_SPL_BUILD)
Hans de Goede251cee02015-09-17 18:46:58 -0400489 /*
Heinrich Schuchardt3174caf2019-04-10 18:59:26 +0200490 * Special-case ubi, ubi goes through a mtd, rather than through
Hans de Goede251cee02015-09-17 18:46:58 -0400491 * a regular block device.
492 */
Heinrich Schuchardtbe2e42f2022-11-20 11:23:24 +0100493 if (!strcmp(ifname, "ubi")) {
Hans de Goede251cee02015-09-17 18:46:58 -0400494 if (!ubifs_is_mounted()) {
495 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
Sean Anderson59715752021-02-05 09:38:55 -0500496 return -EINVAL;
Hans de Goede251cee02015-09-17 18:46:58 -0400497 }
498
Hans de Goede251cee02015-09-17 18:46:58 -0400499 strcpy((char *)info->type, BOOT_PART_TYPE);
500 strcpy((char *)info->name, "UBI");
Hans de Goede251cee02015-09-17 18:46:58 -0400501 return 0;
502 }
503#endif
504
Stephen Warren10a37fd2012-09-21 09:50:57 +0000505 /* If no dev_part_str, use bootdevice environment variable */
Heinrich Schuchardt350635f2023-07-21 17:37:37 +0200506 if (CONFIG_IS_ENABLED(ENV_SUPPORT)) {
507 if (!dev_part_str || !strlen(dev_part_str) ||
508 !strcmp(dev_part_str, "-"))
509 dev_part_str = env_get("bootdevice");
510 }
Rob Herring99d2c202012-09-21 04:08:17 +0000511
Stephen Warren10a37fd2012-09-21 09:50:57 +0000512 /* If still no dev_part_str, it's an error */
513 if (!dev_part_str) {
514 printf("** No device specified **\n");
Sean Anderson59715752021-02-05 09:38:55 -0500515 ret = -ENODEV;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000516 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000517 }
518
Stephen Warren10a37fd2012-09-21 09:50:57 +0000519 /* Separate device and partition ID specification */
520 part_str = strchr(dev_part_str, ':');
521 if (part_str) {
522 dup_str = strdup(dev_part_str);
523 dup_str[part_str - dev_part_str] = 0;
524 dev_str = dup_str;
525 part_str++;
526 } else {
527 dev_str = dev_part_str;
528 }
Rob Herring99d2c202012-09-21 04:08:17 +0000529
Stephen Warren10a37fd2012-09-21 09:50:57 +0000530 /* Look up the device */
Simon Glass5aab05d2023-08-24 13:55:24 -0600531 dev = blk_get_device_by_str(ifname, dev_str, desc);
Sean Anderson59715752021-02-05 09:38:55 -0500532 if (dev < 0) {
Oleksii Bidnichenko192e7012022-04-08 10:07:13 +0200533 printf("** Bad device specification %s %s **\n",
534 ifname, dev_str);
Sean Anderson59715752021-02-05 09:38:55 -0500535 ret = dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000536 goto cleanup;
Sean Anderson59715752021-02-05 09:38:55 -0500537 }
Stephen Warren10a37fd2012-09-21 09:50:57 +0000538
539 /* Convert partition ID string to number */
540 if (!part_str || !*part_str) {
541 part = PART_UNSPECIFIED;
542 } else if (!strcmp(part_str, "auto")) {
543 part = PART_AUTO;
544 } else {
545 /* Something specified -> use exactly that */
Simon Glass7e5f4602021-07-24 09:03:29 -0600546 part = (int)hextoul(part_str, &ep);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000547 /*
548 * Less than whole string converted,
549 * or request for whole device, but caller requires partition.
550 */
551 if (*ep || (part == 0 && !allow_whole_dev)) {
552 printf("** Bad partition specification %s %s **\n",
553 ifname, dev_part_str);
Sean Anderson59715752021-02-05 09:38:55 -0500554 ret = -ENOENT;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000555 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000556 }
Rob Herring99d2c202012-09-21 04:08:17 +0000557 }
558
Stephen Warren10a37fd2012-09-21 09:50:57 +0000559 /*
560 * No partition table on device,
561 * or user requested partition 0 (entire device).
562 */
Simon Glass5aab05d2023-08-24 13:55:24 -0600563 if (((*desc)->part_type == PART_TYPE_UNKNOWN) || !part) {
564 if (!(*desc)->lba) {
Stephen Warren10a37fd2012-09-21 09:50:57 +0000565 printf("** Bad device size - %s %s **\n", ifname,
566 dev_str);
Sean Anderson59715752021-02-05 09:38:55 -0500567 ret = -EINVAL;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000568 goto cleanup;
569 }
Rob Herring99d2c202012-09-21 04:08:17 +0000570
Stephen Warren10a37fd2012-09-21 09:50:57 +0000571 /*
572 * If user specified a partition ID other than 0,
573 * or the calling command only accepts partitions,
574 * it's an error.
575 */
576 if ((part > 0) || (!allow_whole_dev)) {
577 printf("** No partition table - %s %s **\n", ifname,
578 dev_str);
Sean Anderson59715752021-02-05 09:38:55 -0500579 ret = -EPROTONOSUPPORT;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000580 goto cleanup;
581 }
582
Simon Glass5aab05d2023-08-24 13:55:24 -0600583 (*desc)->log2blksz = LOG2((*desc)->blksz);
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800584
Simon Glass5aab05d2023-08-24 13:55:24 -0600585 part_get_info_whole_disk(*desc, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000586
587 ret = 0;
588 goto cleanup;
589 }
590
591 /*
592 * Now there's known to be a partition table,
593 * not specifying a partition means to pick partition 1.
594 */
595 if (part == PART_UNSPECIFIED)
596 part = 1;
597
598 /*
599 * If user didn't specify a partition number, or did specify something
600 * other than "auto", use that partition number directly.
601 */
602 if (part != PART_AUTO) {
Simon Glass5aab05d2023-08-24 13:55:24 -0600603 ret = part_get_info(*desc, part, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000604 if (ret) {
605 printf("** Invalid partition %d **\n", part);
606 goto cleanup;
607 }
608 } else {
609 /*
610 * Find the first bootable partition.
611 * If none are bootable, fall back to the first valid partition.
612 */
613 part = 0;
614 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Simon Glass5aab05d2023-08-24 13:55:24 -0600615 ret = part_get_info(*desc, p, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000616 if (ret)
617 continue;
618
619 /*
620 * First valid partition, or new better partition?
621 * If so, save partition ID.
622 */
623 if (!part || info->bootable)
624 part = p;
625
626 /* Best possible partition? Stop searching. */
627 if (info->bootable)
628 break;
629
630 /*
631 * We now need to search further for best possible.
632 * If we what we just queried was the best so far,
633 * save the info since we over-write it next loop.
634 */
635 if (part == p)
636 tmpinfo = *info;
637 }
638 /* If we found any acceptable partition */
639 if (part) {
640 /*
641 * If we searched all possible partition IDs,
642 * return the first valid partition we found.
643 */
644 if (p == MAX_SEARCH_PARTITIONS + 1)
645 *info = tmpinfo;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000646 } else {
647 printf("** No valid partitions found **\n");
648 goto cleanup;
649 }
Rob Herring99d2c202012-09-21 04:08:17 +0000650 }
651 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
652 printf("** Invalid partition type \"%.32s\""
653 " (expect \"" BOOT_PART_TYPE "\")\n",
654 info->type);
Sean Anderson59715752021-02-05 09:38:55 -0500655 ret = -EINVAL;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000656 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000657 }
658
Simon Glass5aab05d2023-08-24 13:55:24 -0600659 (*desc)->log2blksz = LOG2((*desc)->blksz);
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800660
Stephen Warren10a37fd2012-09-21 09:50:57 +0000661 ret = part;
662 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000663
Stephen Warren10a37fd2012-09-21 09:50:57 +0000664cleanup:
665 free(dup_str);
666 return ret;
Rob Herring99d2c202012-09-21 04:08:17 +0000667}
Petr Kulhavy87b85302016-09-09 10:27:15 +0200668
Simon Glass5aab05d2023-08-24 13:55:24 -0600669int part_get_info_by_name(struct blk_desc *desc, const char *name,
Heinrich Schuchardt8faeb1d2023-07-16 13:34:44 +0200670 struct disk_partition *info)
Petr Kulhavy87b85302016-09-09 10:27:15 +0200671{
Petr Kulhavy87b85302016-09-09 10:27:15 +0200672 struct part_driver *part_drv;
Kever Yang56670d62018-02-10 17:55:38 +0800673 int ret;
674 int i;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200675
Simon Glass5aab05d2023-08-24 13:55:24 -0600676 part_drv = part_driver_lookup_type(desc);
Kever Yang56670d62018-02-10 17:55:38 +0800677 if (!part_drv)
678 return -1;
schspa50f7b2e2021-10-20 20:15:55 +0800679
680 if (!part_drv->get_info) {
681 log_debug("## Driver %s does not have the get_info() method\n",
682 part_drv->name);
683 return -ENOSYS;
684 }
685
Kever Yang56670d62018-02-10 17:55:38 +0800686 for (i = 1; i < part_drv->max_entries; i++) {
Simon Glass5aab05d2023-08-24 13:55:24 -0600687 ret = part_drv->get_info(desc, i, info);
Kever Yang56670d62018-02-10 17:55:38 +0800688 if (ret != 0) {
689 /* no more entries in table */
690 break;
691 }
692 if (strcmp(name, (const char *)info->name) == 0) {
693 /* matched */
694 return i;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200695 }
696 }
Kever Yang56670d62018-02-10 17:55:38 +0800697
Sean Anderson59715752021-02-05 09:38:55 -0500698 return -ENOENT;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200699}
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200700
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300701/**
702 * Get partition info from device number and partition name.
703 *
704 * Parse a device number and partition name string in the form of
Sean Anderson81c1aa82021-02-05 09:38:57 -0500705 * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
706 * hwpartnum are both optional, defaulting to 0. If the partition is found,
Simon Glass5aab05d2023-08-24 13:55:24 -0600707 * sets desc and part_info accordingly with the information of the
Sean Anderson81c1aa82021-02-05 09:38:57 -0500708 * partition with the given partition_name.
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300709 *
710 * @param[in] dev_iface Device interface
Sean Anderson81c1aa82021-02-05 09:38:57 -0500711 * @param[in] dev_part_str Input string argument, like "0.1#misc"
Simon Glass5aab05d2023-08-24 13:55:24 -0600712 * @param[out] desc Place to store the device description pointer
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300713 * @param[out] part_info Place to store the partition information
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100714 * Return: 0 on success, or a negative on error
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300715 */
716static int part_get_info_by_dev_and_name(const char *dev_iface,
717 const char *dev_part_str,
Simon Glass5aab05d2023-08-24 13:55:24 -0600718 struct blk_desc **desc,
Simon Glass05289792020-05-10 11:39:57 -0600719 struct disk_partition *part_info)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300720{
Sean Anderson81c1aa82021-02-05 09:38:57 -0500721 char *dup_str = NULL;
722 const char *dev_str, *part_str;
723 int ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300724
Sean Anderson81c1aa82021-02-05 09:38:57 -0500725 /* Separate device and partition name specification */
Sean Anderson26de4292021-05-15 14:13:54 -0400726 if (dev_part_str)
727 part_str = strchr(dev_part_str, '#');
728 else
729 part_str = NULL;
730
Sean Anderson81c1aa82021-02-05 09:38:57 -0500731 if (part_str) {
732 dup_str = strdup(dev_part_str);
733 dup_str[part_str - dev_part_str] = 0;
734 dev_str = dup_str;
735 part_str++;
736 } else {
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300737 return -EINVAL;
738 }
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300739
Simon Glass5aab05d2023-08-24 13:55:24 -0600740 ret = blk_get_device_by_str(dev_iface, dev_str, desc);
Sean Andersonfe5a5092021-04-12 18:53:06 -0400741 if (ret < 0)
Sean Anderson81c1aa82021-02-05 09:38:57 -0500742 goto cleanup;
743
Simon Glass5aab05d2023-08-24 13:55:24 -0600744 ret = part_get_info_by_name(*desc, part_str, part_info);
Sean Anderson59715752021-02-05 09:38:55 -0500745 if (ret < 0)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300746 printf("Could not find \"%s\" partition\n", part_str);
Sean Anderson81c1aa82021-02-05 09:38:57 -0500747
748cleanup:
749 free(dup_str);
Sean Anderson59715752021-02-05 09:38:55 -0500750 return ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300751}
752
753int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
754 const char *dev_part_str,
Simon Glass5aab05d2023-08-24 13:55:24 -0600755 struct blk_desc **desc,
Sean Anderson9f7bb282021-02-05 09:38:56 -0500756 struct disk_partition *part_info,
757 int allow_whole_dev)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300758{
Sean Anderson59715752021-02-05 09:38:55 -0500759 int ret;
760
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300761 /* Split the part_name if passed as "$dev_num#part_name". */
Simon Glass5aab05d2023-08-24 13:55:24 -0600762 ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str, desc,
763 part_info);
Sean Anderson59715752021-02-05 09:38:55 -0500764 if (ret >= 0)
765 return ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300766 /*
767 * Couldn't lookup by name, try looking up the partition description
768 * directly.
769 */
Simon Glass5aab05d2023-08-24 13:55:24 -0600770 ret = blk_get_device_part_str(dev_iface, dev_part_str, desc, part_info,
771 allow_whole_dev);
Sean Anderson59715752021-02-05 09:38:55 -0500772 if (ret < 0)
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300773 printf("Couldn't find partition %s %s\n",
774 dev_iface, dev_part_str);
Sean Anderson59715752021-02-05 09:38:55 -0500775 return ret;
Ruslan Trofymenko6eccd1f2019-07-05 15:37:31 +0300776}
777
Simon Glass5aab05d2023-08-24 13:55:24 -0600778void part_set_generic_name(const struct blk_desc *desc, int part_num,
779 char *name)
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200780{
781 char *devtype;
782
Simon Glass5aab05d2023-08-24 13:55:24 -0600783 switch (desc->uclass_id) {
Simon Glasse33a5c62022-08-11 19:34:59 -0600784 case UCLASS_IDE:
785 case UCLASS_AHCI:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200786 devtype = "hd";
787 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600788 case UCLASS_SCSI:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200789 devtype = "sd";
790 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600791 case UCLASS_USB:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200792 devtype = "usbd";
793 break;
Simon Glasse33a5c62022-08-11 19:34:59 -0600794 case UCLASS_MMC:
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200795 devtype = "mmcsd";
796 break;
797 default:
798 devtype = "xx";
799 break;
800 }
801
Simon Glass5aab05d2023-08-24 13:55:24 -0600802 sprintf(name, "%s%c%d", devtype, 'a' + desc->devnum, part_num);
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200803}
Simon Glassdcffa442023-01-17 10:47:41 -0700804
805int part_get_bootable(struct blk_desc *desc)
806{
807 struct disk_partition info;
808 int p;
809
810 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
811 int ret;
812
813 ret = part_get_info(desc, p, &info);
814 if (!ret && info.bootable)
815 return p;
816 }
817
818 return 0;
819}