blob: e31aca4877fb6cd655d3c17035e8d5649e71ad17 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <command.h>
Simon Glass96e5b032016-02-29 15:25:47 -070010#include <errno.h>
wdenkaffae2b2002-08-17 09:36:01 +000011#include <ide.h>
Stephen Warren10a37fd2012-09-21 09:50:57 +000012#include <malloc.h>
Grant Likely735dd972007-02-20 09:04:34 +010013#include <part.h>
Hans de Goede251cee02015-09-17 18:46:58 -040014#include <ubifs_uboot.h>
wdenkaffae2b2002-08-17 09:36:01 +000015
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 Protsenko30789092017-09-22 01:51:58 +030024/* Check all partition types */
25#define PART_TYPE_ALL -1
26
Kever Yangd4729262018-02-10 17:55:37 +080027static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
Simon Glass96e5b032016-02-29 15:25:47 -070028{
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 Yangd4729262018-02-10 17:55:37 +080034 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 Glass96e5b032016-02-29 15:25:47 -070049 }
50
51 /* Not found */
52 return NULL;
53}
54
Kever Yang56670d62018-02-10 17:55:38 +080055#ifdef CONFIG_HAVE_BLOCK_DEVICE
Simon Glass4101f682016-02-29 15:25:34 -070056static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Grant Likely735dd972007-02-20 09:04:34 +010057{
Simon Glass6dd9faf2016-05-01 13:52:32 -060058 struct blk_desc *dev_desc;
59 int ret;
Grant Likely735dd972007-02-20 09:04:34 +010060
Simon Glass6dd9faf2016-05-01 13:52:32 -060061 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 Kientzle7e71dc62012-03-27 11:43:25 +020065 return NULL;
Grant Likely735dd972007-02-20 09:04:34 +010066 }
Simon Glass6dd9faf2016-05-01 13:52:32 -060067 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 Likely735dd972007-02-20 09:04:34 +010075}
Stephen Warren336b6f92014-05-07 12:19:01 -060076
Simon Glassdb1d9e72016-02-29 15:25:42 -070077struct blk_desc *blk_get_dev(const char *ifname, int dev)
Stephen Warren336b6f92014-05-07 12:19:01 -060078{
Stephen Warrenecdd57e2014-05-23 12:48:11 -060079 return get_dev_hwpart(ifname, dev, 0);
Stephen Warren336b6f92014-05-07 12:19:01 -060080}
Grant Likely735dd972007-02-20 09:04:34 +010081#else
Simon Glass4101f682016-02-29 15:25:34 -070082struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Stephen Warren336b6f92014-05-07 12:19:01 -060083{
84 return NULL;
85}
86
Simon Glassdb1d9e72016-02-29 15:25:42 -070087struct blk_desc *blk_get_dev(const char *ifname, int dev)
Grant Likely735dd972007-02-20 09:04:34 +010088{
89 return NULL;
90}
91#endif
92
Adam Ford1811a922018-02-06 12:43:56 -060093#ifdef CONFIG_HAVE_BLOCK_DEVICE
Grant Likely735dd972007-02-20 09:04:34 +010094
wdenkaffae2b2002-08-17 09:36:01 +000095/* ------------------------------------------------------------------------- */
96/*
97 * reports device info to the user
98 */
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +030099
100#ifdef CONFIG_LBA48
101typedef uint64_t lba512_t;
102#else
103typedef lbaint_t lba512_t;
104#endif
105
106/*
107 * Overflowless variant of (block_count * mul_by / div_by)
108 * when div_by > mul_by
109 */
Pavel Machek214b3f32014-09-09 15:19:42 +0200110static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300111{
112 lba512_t bc_quot, bc_rem;
113
114 /* x * m / d == x / d * m + (x % d) * m / d */
115 bc_quot = block_count / div_by;
116 bc_rem = block_count - div_by * bc_quot;
117 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
118}
119
Simon Glass4101f682016-02-29 15:25:34 -0700120void dev_print (struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000121{
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300122 lba512_t lba512; /* number of blocks if 512bytes block size */
wdenkaffae2b2002-08-17 09:36:01 +0000123
Wolfgang Denkaf75a452009-05-15 09:27:58 +0200124 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
125 puts ("not available\n");
126 return;
127 }
128
Tor Krill8ec6e332008-05-29 11:10:30 +0200129 switch (dev_desc->if_type) {
Detlev Zundel574b3192008-05-05 16:11:21 +0200130 case IF_TYPE_SCSI:
131 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
132 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000133 dev_desc->vendor,
134 dev_desc->product,
135 dev_desc->revision);
Detlev Zundel574b3192008-05-05 16:11:21 +0200136 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200137 case IF_TYPE_ATAPI:
Detlev Zundel574b3192008-05-05 16:11:21 +0200138 case IF_TYPE_IDE:
139 case IF_TYPE_SATA:
140 printf ("Model: %s Firm: %s Ser#: %s\n",
141 dev_desc->vendor,
142 dev_desc->revision,
143 dev_desc->product);
144 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200145 case IF_TYPE_SD:
146 case IF_TYPE_MMC:
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300147 case IF_TYPE_USB:
Zhikang Zhangffab6942017-08-03 02:30:56 -0700148 case IF_TYPE_NVME:
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300149 printf ("Vendor: %s Rev: %s Prod: %s\n",
150 dev_desc->vendor,
151 dev_desc->revision,
152 dev_desc->product);
153 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200154 case IF_TYPE_DOC:
155 puts("device type DOC\n");
156 return;
Tor Krill8ec6e332008-05-29 11:10:30 +0200157 case IF_TYPE_UNKNOWN:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200158 puts("device type unknown\n");
159 return;
Detlev Zundel574b3192008-05-05 16:11:21 +0200160 default:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200161 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundel574b3192008-05-05 16:11:21 +0200162 return;
wdenkaffae2b2002-08-17 09:36:01 +0000163 }
164 puts (" Type: ");
165 if (dev_desc->removable)
166 puts ("Removable ");
167 switch (dev_desc->type & 0x1F) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200168 case DEV_TYPE_HARDDISK:
169 puts ("Hard Disk");
170 break;
171 case DEV_TYPE_CDROM:
172 puts ("CD ROM");
173 break;
174 case DEV_TYPE_OPDISK:
175 puts ("Optical Device");
176 break;
177 case DEV_TYPE_TAPE:
178 puts ("Tape");
179 break;
180 default:
181 printf ("# %02X #", dev_desc->type & 0x1F);
182 break;
wdenkaffae2b2002-08-17 09:36:01 +0000183 }
184 puts ("\n");
Jerry Huang33699df2012-11-06 15:33:12 +0000185 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000186 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkc40b2952004-03-13 23:29:43 +0000187 lbaint_t lba;
wdenk6e592382004-04-18 17:39:38 +0000188
189 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000190
wdenkc40b2952004-03-13 23:29:43 +0000191 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000192 /* round to 1 digit */
Pavel Machek214b3f32014-09-09 15:19:42 +0200193 /* 2048 = (1024 * 1024) / 512 MB */
194 mb = lba512_muldiv(lba512, 10, 2048);
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300195
wdenkaffae2b2002-08-17 09:36:01 +0000196 mb_quot = mb / 10;
197 mb_rem = mb - (10 * mb_quot);
198
199 gb = mb / 1024;
200 gb_quot = gb / 10;
201 gb_rem = gb - (10 * gb_quot);
wdenk42dfe7a2004-03-14 22:25:36 +0000202#ifdef CONFIG_LBA48
wdenk6e592382004-04-18 17:39:38 +0000203 if (dev_desc->lba48)
wdenkc40b2952004-03-13 23:29:43 +0000204 printf (" Supports 48-bit addressing\n");
205#endif
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100206#if defined(CONFIG_SYS_64BIT_LBA)
Jean-Jacques Hiblot139f7b12016-12-23 10:45:43 +0100207 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
wdenkc40b2952004-03-13 23:29:43 +0000208 mb_quot, mb_rem,
209 gb_quot, gb_rem,
210 lba,
211 dev_desc->blksz);
212#else
Jean-Jacques Hiblot139f7b12016-12-23 10:45:43 +0100213 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
wdenkaffae2b2002-08-17 09:36:01 +0000214 mb_quot, mb_rem,
215 gb_quot, gb_rem,
wdenkc40b2952004-03-13 23:29:43 +0000216 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000217 dev_desc->blksz);
wdenkc40b2952004-03-13 23:29:43 +0000218#endif
wdenkaffae2b2002-08-17 09:36:01 +0000219 } else {
220 puts (" Capacity: not available\n");
221 }
222}
Jon Loeligerb3aff0c2007-07-10 11:19:50 -0500223#endif
wdenkaffae2b2002-08-17 09:36:01 +0000224
Adam Ford1811a922018-02-06 12:43:56 -0600225#ifdef CONFIG_HAVE_BLOCK_DEVICE
wdenkaffae2b2002-08-17 09:36:01 +0000226
Simon Glass3e8bd462016-02-29 15:25:48 -0700227void part_init(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000228{
Simon Glass96e5b032016-02-29 15:25:47 -0700229 struct part_driver *drv =
230 ll_entry_start(struct part_driver, part_driver);
231 const int n_ents = ll_entry_count(struct part_driver, part_driver);
232 struct part_driver *entry;
wdenkaffae2b2002-08-17 09:36:01 +0000233
Eric Nelsone40cf342016-03-28 10:05:44 -0700234 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
235
Rob Herring99d2c202012-09-21 04:08:17 +0000236 dev_desc->part_type = PART_TYPE_UNKNOWN;
Simon Glass96e5b032016-02-29 15:25:47 -0700237 for (entry = drv; entry != drv + n_ents; entry++) {
238 int ret;
239
240 ret = entry->test(dev_desc);
241 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
242 if (!ret) {
243 dev_desc->part_type = entry->part_type;
244 break;
245 }
246 }
wdenkaffae2b2002-08-17 09:36:01 +0000247}
248
Simon Glass96e5b032016-02-29 15:25:47 -0700249static void print_part_header(const char *type, struct blk_desc *dev_desc)
250{
Patrick Delaunayf18fa312017-01-27 11:00:36 +0100251#if CONFIG_IS_ENABLED(MAC_PARTITION) || \
Patrick Delaunayb0cf7332017-01-27 11:00:37 +0100252 CONFIG_IS_ENABLED(DOS_PARTITION) || \
Patrick Delaunay1acc0082017-01-27 11:00:38 +0100253 CONFIG_IS_ENABLED(ISO_PARTITION) || \
Patrick Delaunay863c5b62017-01-27 11:00:39 +0100254 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100255 CONFIG_IS_ENABLED(EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000256 puts ("\nPartition Map for ");
257 switch (dev_desc->if_type) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200258 case IF_TYPE_IDE:
259 puts ("IDE");
260 break;
261 case IF_TYPE_SATA:
262 puts ("SATA");
263 break;
264 case IF_TYPE_SCSI:
265 puts ("SCSI");
266 break;
267 case IF_TYPE_ATAPI:
268 puts ("ATAPI");
269 break;
270 case IF_TYPE_USB:
271 puts ("USB");
272 break;
273 case IF_TYPE_DOC:
274 puts ("DOC");
275 break;
Lei Wen8f3b9642010-09-13 22:07:28 +0800276 case IF_TYPE_MMC:
277 puts ("MMC");
278 break;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700279 case IF_TYPE_HOST:
Zhikang Zhangffab6942017-08-03 02:30:56 -0700280 puts ("HOST");
281 break;
282 case IF_TYPE_NVME:
283 puts ("NVMe");
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700284 break;
Detlev Zundel726c0f12008-05-05 16:11:22 +0200285 default:
286 puts ("UNKNOWN");
287 break;
wdenkaffae2b2002-08-17 09:36:01 +0000288 }
289 printf (" device %d -- Partition Type: %s\n\n",
Simon Glassbcce53d2016-02-29 15:25:51 -0700290 dev_desc->devnum, type);
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000291#endif /* any CONFIG_..._PARTITION */
Simon Glass96e5b032016-02-29 15:25:47 -0700292}
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000293
Simon Glass3e8bd462016-02-29 15:25:48 -0700294void part_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000295{
Simon Glass96e5b032016-02-29 15:25:47 -0700296 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000297
Kever Yangd4729262018-02-10 17:55:37 +0800298 drv = part_driver_lookup_type(dev_desc);
Simon Glass96e5b032016-02-29 15:25:47 -0700299 if (!drv) {
300 printf("## Unknown partition table type %x\n",
301 dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +0000302 return;
wdenkaffae2b2002-08-17 09:36:01 +0000303 }
Simon Glass96e5b032016-02-29 15:25:47 -0700304
305 PRINTF("## Testing for valid %s partition ##\n", drv->name);
306 print_part_header(drv->name, dev_desc);
307 if (drv->print)
308 drv->print(dev_desc);
wdenkaffae2b2002-08-17 09:36:01 +0000309}
310
Adam Ford1811a922018-02-06 12:43:56 -0600311#endif /* CONFIG_HAVE_BLOCK_DEVICE */
Stephen Warren2f501642012-09-21 12:46:54 +0000312
Simon Glass3e8bd462016-02-29 15:25:48 -0700313int part_get_info(struct blk_desc *dev_desc, int part,
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200314 disk_partition_t *info)
Stephen Warren2f501642012-09-21 12:46:54 +0000315{
Adam Ford1811a922018-02-06 12:43:56 -0600316#ifdef CONFIG_HAVE_BLOCK_DEVICE
Simon Glass96e5b032016-02-29 15:25:47 -0700317 struct part_driver *drv;
Stephen Warren2f501642012-09-21 12:46:54 +0000318
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100319#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren894bfbb2012-09-21 09:50:59 +0000320 /* The common case is no UUID support */
321 info->uuid[0] = 0;
322#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100323#ifdef CONFIG_PARTITION_TYPE_GUID
324 info->type_guid[0] = 0;
325#endif
Stephen Warren894bfbb2012-09-21 09:50:59 +0000326
Kever Yangd4729262018-02-10 17:55:37 +0800327 drv = part_driver_lookup_type(dev_desc);
Simon Glass96e5b032016-02-29 15:25:47 -0700328 if (!drv) {
329 debug("## Unknown partition table type %x\n",
330 dev_desc->part_type);
331 return -EPROTONOSUPPORT;
332 }
333 if (!drv->get_info) {
Nishanth Menon2ae67ae2016-03-15 16:15:32 -0500334 PRINTF("## Driver %s does not have the get_info() method\n",
335 drv->name);
Simon Glass96e5b032016-02-29 15:25:47 -0700336 return -ENOSYS;
337 }
338 if (drv->get_info(dev_desc, part, info) == 0) {
339 PRINTF("## Valid %s partition found ##\n", drv->name);
340 return 0;
Stephen Warren2f501642012-09-21 12:46:54 +0000341 }
Adam Ford1811a922018-02-06 12:43:56 -0600342#endif /* CONFIG_HAVE_BLOCK_DEVICE */
Stephen Warren2f501642012-09-21 12:46:54 +0000343
344 return -1;
345}
Rob Herring99d2c202012-09-21 04:08:17 +0000346
Rob Clark4bbcc962017-09-09 13:15:55 -0400347int part_get_info_whole_disk(struct blk_desc *dev_desc, disk_partition_t *info)
348{
349 info->start = 0;
350 info->size = dev_desc->lba;
351 info->blksz = dev_desc->blksz;
352 info->bootable = 0;
353 strcpy((char *)info->type, BOOT_PART_TYPE);
354 strcpy((char *)info->name, "Whole Disk");
355#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
356 info->uuid[0] = 0;
357#endif
358#ifdef CONFIG_PARTITION_TYPE_GUID
359 info->type_guid[0] = 0;
360#endif
361
362 return 0;
363}
364
Simon Glassebac37c2016-02-29 15:25:43 -0700365int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
366 struct blk_desc **dev_desc)
Stephen Warren2023e602012-09-21 09:50:56 +0000367{
368 char *ep;
Stephen Warren336b6f92014-05-07 12:19:01 -0600369 char *dup_str = NULL;
370 const char *dev_str, *hwpart_str;
371 int dev, hwpart;
372
373 hwpart_str = strchr(dev_hwpart_str, '.');
374 if (hwpart_str) {
375 dup_str = strdup(dev_hwpart_str);
376 dup_str[hwpart_str - dev_hwpart_str] = 0;
377 dev_str = dup_str;
378 hwpart_str++;
379 } else {
380 dev_str = dev_hwpart_str;
Stephen Warrenecdd57e2014-05-23 12:48:11 -0600381 hwpart = 0;
Stephen Warren336b6f92014-05-07 12:19:01 -0600382 }
Stephen Warren2023e602012-09-21 09:50:56 +0000383
384 dev = simple_strtoul(dev_str, &ep, 16);
385 if (*ep) {
386 printf("** Bad device specification %s %s **\n",
387 ifname, dev_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600388 dev = -EINVAL;
Stephen Warren336b6f92014-05-07 12:19:01 -0600389 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000390 }
391
Stephen Warren336b6f92014-05-07 12:19:01 -0600392 if (hwpart_str) {
393 hwpart = simple_strtoul(hwpart_str, &ep, 16);
394 if (*ep) {
395 printf("** Bad HW partition specification %s %s **\n",
396 ifname, hwpart_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600397 dev = -EINVAL;
Stephen Warren336b6f92014-05-07 12:19:01 -0600398 goto cleanup;
399 }
400 }
401
402 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warren2023e602012-09-21 09:50:56 +0000403 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Stephen Warren336b6f92014-05-07 12:19:01 -0600404 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
Simon Glass1598dfc2016-05-01 13:52:36 -0600405 dev = -ENOENT;
Stephen Warren336b6f92014-05-07 12:19:01 -0600406 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000407 }
408
Adam Ford1811a922018-02-06 12:43:56 -0600409#ifdef CONFIG_HAVE_BLOCK_DEVICE
Erik Tideman99e7fc82016-01-11 13:39:07 +0000410 /*
411 * Updates the partition table for the specified hw partition.
412 * Does not need to be done for hwpart 0 since it is default and
413 * already loaded.
414 */
415 if(hwpart != 0)
Simon Glass3e8bd462016-02-29 15:25:48 -0700416 part_init(*dev_desc);
Erik Tideman99e7fc82016-01-11 13:39:07 +0000417#endif
418
Stephen Warren336b6f92014-05-07 12:19:01 -0600419cleanup:
420 free(dup_str);
Stephen Warren2023e602012-09-21 09:50:56 +0000421 return dev;
422}
423
Stephen Warren10a37fd2012-09-21 09:50:57 +0000424#define PART_UNSPECIFIED -2
425#define PART_AUTO -1
Simon Glasse35929e2016-02-29 15:25:44 -0700426int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glass4101f682016-02-29 15:25:34 -0700427 struct blk_desc **dev_desc,
Stephen Warren10a37fd2012-09-21 09:50:57 +0000428 disk_partition_t *info, int allow_whole_dev)
Rob Herring99d2c202012-09-21 04:08:17 +0000429{
Stephen Warren10a37fd2012-09-21 09:50:57 +0000430 int ret = -1;
431 const char *part_str;
432 char *dup_str = NULL;
433 const char *dev_str;
Rob Herring99d2c202012-09-21 04:08:17 +0000434 int dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000435 char *ep;
436 int p;
437 int part;
438 disk_partition_t tmpinfo;
Rob Herring99d2c202012-09-21 04:08:17 +0000439
Hans de Goedeafc17442015-09-17 18:46:55 -0400440#ifdef CONFIG_SANDBOX
Stephen Warren4d907022014-06-12 10:28:32 -0600441 /*
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200442 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warren4d907022014-06-12 10:28:32 -0600443 * host's own filesystem.
444 */
445 if (0 == strcmp(ifname, "hostfs")) {
446 *dev_desc = NULL;
447 info->start = 0;
448 info->size = 0;
449 info->blksz = 0;
450 info->bootable = 0;
451 strcpy((char *)info->type, BOOT_PART_TYPE);
452 strcpy((char *)info->name, "Sandbox host");
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100453#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warren4d907022014-06-12 10:28:32 -0600454 info->uuid[0] = 0;
455#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100456#ifdef CONFIG_PARTITION_TYPE_GUID
457 info->type_guid[0] = 0;
458#endif
Stephen Warren4d907022014-06-12 10:28:32 -0600459
460 return 0;
461 }
Hans de Goedeafc17442015-09-17 18:46:55 -0400462#endif
Stephen Warren4d907022014-06-12 10:28:32 -0600463
Hans de Goede251cee02015-09-17 18:46:58 -0400464#ifdef CONFIG_CMD_UBIFS
465 /*
466 * Special-case ubi, ubi goes through a mtd, rathen then through
467 * a regular block device.
468 */
469 if (0 == strcmp(ifname, "ubi")) {
470 if (!ubifs_is_mounted()) {
471 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
472 return -1;
473 }
474
475 *dev_desc = NULL;
476 memset(info, 0, sizeof(*info));
477 strcpy((char *)info->type, BOOT_PART_TYPE);
478 strcpy((char *)info->name, "UBI");
Patrick Delaunayb331cd62017-01-27 11:00:42 +0100479#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Hans de Goede251cee02015-09-17 18:46:58 -0400480 info->uuid[0] = 0;
481#endif
482 return 0;
483 }
484#endif
485
Stephen Warren10a37fd2012-09-21 09:50:57 +0000486 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena10973e2012-09-28 05:34:09 +0000487 if (!dev_part_str || !strlen(dev_part_str) ||
488 !strcmp(dev_part_str, "-"))
Simon Glass00caae62017-08-03 12:22:12 -0600489 dev_part_str = env_get("bootdevice");
Rob Herring99d2c202012-09-21 04:08:17 +0000490
Stephen Warren10a37fd2012-09-21 09:50:57 +0000491 /* If still no dev_part_str, it's an error */
492 if (!dev_part_str) {
493 printf("** No device specified **\n");
494 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000495 }
496
Stephen Warren10a37fd2012-09-21 09:50:57 +0000497 /* Separate device and partition ID specification */
498 part_str = strchr(dev_part_str, ':');
499 if (part_str) {
500 dup_str = strdup(dev_part_str);
501 dup_str[part_str - dev_part_str] = 0;
502 dev_str = dup_str;
503 part_str++;
504 } else {
505 dev_str = dev_part_str;
506 }
Rob Herring99d2c202012-09-21 04:08:17 +0000507
Stephen Warren10a37fd2012-09-21 09:50:57 +0000508 /* Look up the device */
Simon Glassebac37c2016-02-29 15:25:43 -0700509 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000510 if (dev < 0)
511 goto cleanup;
512
513 /* Convert partition ID string to number */
514 if (!part_str || !*part_str) {
515 part = PART_UNSPECIFIED;
516 } else if (!strcmp(part_str, "auto")) {
517 part = PART_AUTO;
518 } else {
519 /* Something specified -> use exactly that */
520 part = (int)simple_strtoul(part_str, &ep, 16);
521 /*
522 * Less than whole string converted,
523 * or request for whole device, but caller requires partition.
524 */
525 if (*ep || (part == 0 && !allow_whole_dev)) {
526 printf("** Bad partition specification %s %s **\n",
527 ifname, dev_part_str);
528 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000529 }
Rob Herring99d2c202012-09-21 04:08:17 +0000530 }
531
Stephen Warren10a37fd2012-09-21 09:50:57 +0000532 /*
533 * No partition table on device,
534 * or user requested partition 0 (entire device).
535 */
536 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
537 (part == 0)) {
538 if (!(*dev_desc)->lba) {
539 printf("** Bad device size - %s %s **\n", ifname,
540 dev_str);
541 goto cleanup;
542 }
Rob Herring99d2c202012-09-21 04:08:17 +0000543
Stephen Warren10a37fd2012-09-21 09:50:57 +0000544 /*
545 * If user specified a partition ID other than 0,
546 * or the calling command only accepts partitions,
547 * it's an error.
548 */
549 if ((part > 0) || (!allow_whole_dev)) {
550 printf("** No partition table - %s %s **\n", ifname,
551 dev_str);
552 goto cleanup;
553 }
554
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800555 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
556
Rob Clark4bbcc962017-09-09 13:15:55 -0400557 part_get_info_whole_disk(*dev_desc, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000558
559 ret = 0;
560 goto cleanup;
561 }
562
563 /*
564 * Now there's known to be a partition table,
565 * not specifying a partition means to pick partition 1.
566 */
567 if (part == PART_UNSPECIFIED)
568 part = 1;
569
570 /*
571 * If user didn't specify a partition number, or did specify something
572 * other than "auto", use that partition number directly.
573 */
574 if (part != PART_AUTO) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700575 ret = part_get_info(*dev_desc, part, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000576 if (ret) {
577 printf("** Invalid partition %d **\n", part);
578 goto cleanup;
579 }
580 } else {
581 /*
582 * Find the first bootable partition.
583 * If none are bootable, fall back to the first valid partition.
584 */
585 part = 0;
586 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Simon Glass3e8bd462016-02-29 15:25:48 -0700587 ret = part_get_info(*dev_desc, p, info);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000588 if (ret)
589 continue;
590
591 /*
592 * First valid partition, or new better partition?
593 * If so, save partition ID.
594 */
595 if (!part || info->bootable)
596 part = p;
597
598 /* Best possible partition? Stop searching. */
599 if (info->bootable)
600 break;
601
602 /*
603 * We now need to search further for best possible.
604 * If we what we just queried was the best so far,
605 * save the info since we over-write it next loop.
606 */
607 if (part == p)
608 tmpinfo = *info;
609 }
610 /* If we found any acceptable partition */
611 if (part) {
612 /*
613 * If we searched all possible partition IDs,
614 * return the first valid partition we found.
615 */
616 if (p == MAX_SEARCH_PARTITIONS + 1)
617 *info = tmpinfo;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000618 } else {
619 printf("** No valid partitions found **\n");
Stephen Warren71bba422012-10-08 07:45:54 +0000620 ret = -1;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000621 goto cleanup;
622 }
Rob Herring99d2c202012-09-21 04:08:17 +0000623 }
624 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
625 printf("** Invalid partition type \"%.32s\""
626 " (expect \"" BOOT_PART_TYPE "\")\n",
627 info->type);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000628 ret = -1;
629 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000630 }
631
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800632 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
633
Stephen Warren10a37fd2012-09-21 09:50:57 +0000634 ret = part;
635 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000636
Stephen Warren10a37fd2012-09-21 09:50:57 +0000637cleanup:
638 free(dup_str);
639 return ret;
Rob Herring99d2c202012-09-21 04:08:17 +0000640}
Petr Kulhavy87b85302016-09-09 10:27:15 +0200641
Sam Protsenko30789092017-09-22 01:51:58 +0300642int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
643 disk_partition_t *info, int part_type)
Petr Kulhavy87b85302016-09-09 10:27:15 +0200644{
Petr Kulhavy87b85302016-09-09 10:27:15 +0200645 struct part_driver *part_drv;
Kever Yang56670d62018-02-10 17:55:38 +0800646 int ret;
647 int i;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200648
Kever Yang56670d62018-02-10 17:55:38 +0800649 part_drv = part_driver_lookup_type(dev_desc);
650 if (!part_drv)
651 return -1;
652 for (i = 1; i < part_drv->max_entries; i++) {
653 ret = part_drv->get_info(dev_desc, i, info);
654 if (ret != 0) {
655 /* no more entries in table */
656 break;
657 }
658 if (strcmp(name, (const char *)info->name) == 0) {
659 /* matched */
660 return i;
Petr Kulhavy87b85302016-09-09 10:27:15 +0200661 }
662 }
Kever Yang56670d62018-02-10 17:55:38 +0800663
Petr Kulhavy87b85302016-09-09 10:27:15 +0200664 return -1;
665}
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200666
Sam Protsenko30789092017-09-22 01:51:58 +0300667int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
668 disk_partition_t *info)
669{
670 return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
671}
672
Petr Kulhavyda2ee242016-09-09 10:27:17 +0200673void part_set_generic_name(const struct blk_desc *dev_desc,
674 int part_num, char *name)
675{
676 char *devtype;
677
678 switch (dev_desc->if_type) {
679 case IF_TYPE_IDE:
680 case IF_TYPE_SATA:
681 case IF_TYPE_ATAPI:
682 devtype = "hd";
683 break;
684 case IF_TYPE_SCSI:
685 devtype = "sd";
686 break;
687 case IF_TYPE_USB:
688 devtype = "usbd";
689 break;
690 case IF_TYPE_DOC:
691 devtype = "docd";
692 break;
693 case IF_TYPE_MMC:
694 case IF_TYPE_SD:
695 devtype = "mmcsd";
696 break;
697 default:
698 devtype = "xx";
699 break;
700 }
701
702 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);
703}