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 Denkd79de1d2013-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 Glass83ce5632016-02-29 15:25:47 -070010#include <errno.h>
wdenkaffae2b2002-08-17 09:36:01 +000011#include <ide.h>
Stephen Warrenc87ff222012-09-21 09:50:57 +000012#include <malloc.h>
Grant Likelyffc2dd72007-02-20 09:04:34 +010013#include <part.h>
Hans de Goede690c7962015-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 Protsenkodee80382017-09-22 01:51:58 +030024/* Check all partition types */
25#define PART_TYPE_ALL -1
26
Kever Yang1c636242018-02-10 17:55:37 +080027static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
Simon Glass83ce5632016-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 Yang1c636242018-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 Glass83ce5632016-02-29 15:25:47 -070049 }
50
51 /* Not found */
52 return NULL;
53}
54
Kever Yang91829662018-02-10 17:55:38 +080055#ifdef CONFIG_HAVE_BLOCK_DEVICE
Simon Glasse3394752016-02-29 15:25:34 -070056static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Grant Likelyffc2dd72007-02-20 09:04:34 +010057{
Simon Glasse3dfa912016-05-01 13:52:32 -060058 struct blk_desc *dev_desc;
59 int ret;
Grant Likelyffc2dd72007-02-20 09:04:34 +010060
Simon Glasse3dfa912016-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 Kientzle47e444c2012-03-27 11:43:25 +020065 return NULL;
Grant Likelyffc2dd72007-02-20 09:04:34 +010066 }
Simon Glasse3dfa912016-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 Likelyffc2dd72007-02-20 09:04:34 +010075}
Stephen Warrenaeca52f2014-05-07 12:19:01 -060076
Simon Glassbe1e9bb2016-02-29 15:25:42 -070077struct blk_desc *blk_get_dev(const char *ifname, int dev)
Stephen Warrenaeca52f2014-05-07 12:19:01 -060078{
Stephen Warrenad17c0f2014-05-23 12:48:11 -060079 return get_dev_hwpart(ifname, dev, 0);
Stephen Warrenaeca52f2014-05-07 12:19:01 -060080}
Grant Likelyffc2dd72007-02-20 09:04:34 +010081#else
Simon Glasse3394752016-02-29 15:25:34 -070082struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -060083{
84 return NULL;
85}
86
Simon Glassbe1e9bb2016-02-29 15:25:42 -070087struct blk_desc *blk_get_dev(const char *ifname, int dev)
Grant Likelyffc2dd72007-02-20 09:04:34 +010088{
89 return NULL;
90}
91#endif
92
Adam Fordb10ba902018-02-06 12:43:56 -060093#ifdef CONFIG_HAVE_BLOCK_DEVICE
Grant Likelyffc2dd72007-02-20 09:04:34 +010094
wdenkaffae2b2002-08-17 09:36:01 +000095/* ------------------------------------------------------------------------- */
96/*
97 * reports device info to the user
98 */
Sergei Trofimovich89d935b2010-08-08 15:05:39 +030099
wdenkc35ba4e2004-03-14 22:25:36 +0000100#ifdef CONFIG_LBA48
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300101typedef uint64_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000102#else
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300103typedef lbaint_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000104#endif
wdenkaffae2b2002-08-17 09:36:01 +0000105
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300106/*
107 * Overflowless variant of (block_count * mul_by / div_by)
108 * when div_by > mul_by
109 */
Pavel Macheka4264e12014-09-09 15:19:42 +0200110static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
Sergei Trofimovich89d935b2010-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 Glasse3394752016-02-29 15:25:34 -0700120void dev_print (struct blk_desc *dev_desc)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300121{
122 lba512_t lba512; /* number of blocks if 512bytes block size */
123
Wolfgang Denk8561e712009-05-15 09:27:58 +0200124 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
125 puts ("not available\n");
126 return;
127 }
128
Tor Krill03ff3302008-05-29 11:10:30 +0200129 switch (dev_desc->if_type) {
Detlev Zundelbec6b322008-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,
Detlev Zundelbec6b322008-05-05 16:11:21 +0200134 dev_desc->product,
135 dev_desc->revision);
136 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200137 case IF_TYPE_ATAPI:
Detlev Zundelbec6b322008-05-05 16:11:21 +0200138 case IF_TYPE_IDE:
139 case IF_TYPE_SATA:
Dave Liu76596ba2008-03-26 22:49:44 +0800140 printf ("Model: %s Firm: %s Ser#: %s\n",
141 dev_desc->vendor,
142 dev_desc->revision,
143 dev_desc->product);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200144 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200145 case IF_TYPE_SD:
146 case IF_TYPE_MMC:
Nícolas Carneiro Lebedencoef3edd42008-09-04 15:35:46 -0300147 case IF_TYPE_USB:
Zhikang Zhang182fccd2017-08-03 02:30:56 -0700148 case IF_TYPE_NVME:
Nícolas Carneiro Lebedencoef3edd42008-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 Bohmerf9f718b2008-09-19 13:30:06 +0200154 case IF_TYPE_DOC:
155 puts("device type DOC\n");
156 return;
Tor Krill03ff3302008-05-29 11:10:30 +0200157 case IF_TYPE_UNKNOWN:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200158 puts("device type unknown\n");
159 return;
Detlev Zundelbec6b322008-05-05 16:11:21 +0200160 default:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200161 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundelbec6b322008-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 Zundel0e11cc62008-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 Huang6a4feb42012-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;
wdenkf602aa02004-03-13 23:29:43 +0000187 lbaint_t lba;
wdenk05939202004-04-18 17:39:38 +0000188
189 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000190
wdenkf602aa02004-03-13 23:29:43 +0000191 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000192 /* round to 1 digit */
Pavel Macheka4264e12014-09-09 15:19:42 +0200193 /* 2048 = (1024 * 1024) / 512 MB */
194 mb = lba512_muldiv(lba512, 10, 2048);
Sergei Trofimovich89d935b2010-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);
wdenkc35ba4e2004-03-14 22:25:36 +0000202#ifdef CONFIG_LBA48
wdenk05939202004-04-18 17:39:38 +0000203 if (dev_desc->lba48)
wdenkf602aa02004-03-13 23:29:43 +0000204 printf (" Supports 48-bit addressing\n");
205#endif
Heiko Schocher0f602e12009-12-03 11:21:21 +0100206#if defined(CONFIG_SYS_64BIT_LBA)
Jean-Jacques Hiblotccbb9152016-12-23 10:45:43 +0100207 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
wdenkf602aa02004-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 Hiblotccbb9152016-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,
wdenkf602aa02004-03-13 23:29:43 +0000216 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000217 dev_desc->blksz);
wdenkf602aa02004-03-13 23:29:43 +0000218#endif
wdenkaffae2b2002-08-17 09:36:01 +0000219 } else {
220 puts (" Capacity: not available\n");
221 }
222}
Jon Loeliger1670a5b2007-07-10 11:19:50 -0500223#endif
wdenkaffae2b2002-08-17 09:36:01 +0000224
Adam Fordb10ba902018-02-06 12:43:56 -0600225#ifdef CONFIG_HAVE_BLOCK_DEVICE
wdenkaffae2b2002-08-17 09:36:01 +0000226
Simon Glassb89a8442016-02-29 15:25:48 -0700227void part_init(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000228{
Simon Glass83ce5632016-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;
richardretanubune6745592008-09-26 11:13:22 -0400233
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700234 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
235
Simon Glass83ce5632016-02-29 15:25:47 -0700236 dev_desc->part_type = PART_TYPE_UNKNOWN;
237 for (entry = drv; entry != drv + n_ents; entry++) {
238 int ret;
wdenk452cfd62002-11-19 11:04:11 +0000239
Simon Glass83ce5632016-02-29 15:25:47 -0700240 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 }
wdenk452cfd62002-11-19 11:04:11 +0000246 }
wdenkaffae2b2002-08-17 09:36:01 +0000247}
248
Simon Glass83ce5632016-02-29 15:25:47 -0700249static void print_part_header(const char *type, struct blk_desc *dev_desc)
250{
Patrick Delaunayc4bbbec2017-01-27 11:00:36 +0100251#if CONFIG_IS_ENABLED(MAC_PARTITION) || \
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100252 CONFIG_IS_ENABLED(DOS_PARTITION) || \
Patrick Delaunay21d3bce2017-01-27 11:00:38 +0100253 CONFIG_IS_ENABLED(ISO_PARTITION) || \
Patrick Delaunay1aa32a82017-01-27 11:00:39 +0100254 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
Patrick Delaunay8a4f2bd2017-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 Zundel0e11cc62008-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 Wen61dba932010-09-13 22:07:28 +0800276 case IF_TYPE_MMC:
277 puts ("MMC");
278 break;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700279 case IF_TYPE_HOST:
Zhikang Zhang182fccd2017-08-03 02:30:56 -0700280 puts ("HOST");
281 break;
282 case IF_TYPE_NVME:
283 puts ("NVMe");
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700284 break;
Detlev Zundel0e11cc62008-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 Glass2f26fff2016-02-29 15:25:51 -0700290 dev_desc->devnum, type);
Gabe Black0fc71b52012-10-12 14:26:08 +0000291#endif /* any CONFIG_..._PARTITION */
Simon Glass83ce5632016-02-29 15:25:47 -0700292}
Gabe Black0fc71b52012-10-12 14:26:08 +0000293
Simon Glassb89a8442016-02-29 15:25:48 -0700294void part_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000295{
Simon Glass83ce5632016-02-29 15:25:47 -0700296 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000297
Kever Yang1c636242018-02-10 17:55:37 +0800298 drv = part_driver_lookup_type(dev_desc);
Simon Glass83ce5632016-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 Glass83ce5632016-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 Fordb10ba902018-02-06 12:43:56 -0600311#endif /* CONFIG_HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000312
Simon Glassb89a8442016-02-29 15:25:48 -0700313int part_get_info(struct blk_desc *dev_desc, int part,
Pavel Machek13334e72014-07-19 23:50:44 +0200314 disk_partition_t *info)
Stephen Warren07c5bff2012-09-21 12:46:54 +0000315{
Adam Fordb10ba902018-02-06 12:43:56 -0600316#ifdef CONFIG_HAVE_BLOCK_DEVICE
Simon Glass83ce5632016-02-29 15:25:47 -0700317 struct part_driver *drv;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000318
Patrick Delaunay73287092017-01-27 11:00:42 +0100319#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000320 /* The common case is no UUID support */
321 info->uuid[0] = 0;
322#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100323#ifdef CONFIG_PARTITION_TYPE_GUID
324 info->type_guid[0] = 0;
325#endif
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000326
Kever Yang1c636242018-02-10 17:55:37 +0800327 drv = part_driver_lookup_type(dev_desc);
Simon Glass83ce5632016-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 Menonef1851a2016-03-15 16:15:32 -0500334 PRINTF("## Driver %s does not have the get_info() method\n",
335 drv->name);
Simon Glass83ce5632016-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 Warren07c5bff2012-09-21 12:46:54 +0000341 }
Adam Fordb10ba902018-02-06 12:43:56 -0600342#endif /* CONFIG_HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000343
344 return -1;
345}
Rob Herringd586cec2012-09-21 04:08:17 +0000346
Rob Clark2b7bfd92017-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 Glasse6649a62016-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 Warrenafd909c2012-09-21 09:50:56 +0000367{
368 char *ep;
Stephen Warrenaeca52f2014-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 Warrenad17c0f2014-05-23 12:48:11 -0600381 hwpart = 0;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600382 }
Stephen Warrenafd909c2012-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 Glass356e0e62016-05-01 13:52:36 -0600388 dev = -EINVAL;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600389 goto cleanup;
390 }
391
392 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 Glass356e0e62016-05-01 13:52:36 -0600397 dev = -EINVAL;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600398 goto cleanup;
399 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000400 }
401
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600402 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000403 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600404 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
Simon Glass356e0e62016-05-01 13:52:36 -0600405 dev = -ENOENT;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600406 goto cleanup;
Stephen Warrenafd909c2012-09-21 09:50:56 +0000407 }
408
Adam Fordb10ba902018-02-06 12:43:56 -0600409#ifdef CONFIG_HAVE_BLOCK_DEVICE
Erik Tideman8d11b8e2016-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 Glassb89a8442016-02-29 15:25:48 -0700416 part_init(*dev_desc);
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000417#endif
418
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600419cleanup:
420 free(dup_str);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000421 return dev;
422}
423
Stephen Warrenc87ff222012-09-21 09:50:57 +0000424#define PART_UNSPECIFIED -2
425#define PART_AUTO -1
Simon Glasse76ee972016-02-29 15:25:44 -0700426int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glasse3394752016-02-29 15:25:34 -0700427 struct blk_desc **dev_desc,
Stephen Warrenc87ff222012-09-21 09:50:57 +0000428 disk_partition_t *info, int allow_whole_dev)
Rob Herringd586cec2012-09-21 04:08:17 +0000429{
Stephen Warrenc87ff222012-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 Herringd586cec2012-09-21 04:08:17 +0000434 int dev;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000435 char *ep;
436 int p;
437 int part;
438 disk_partition_t tmpinfo;
439
Hans de Goede4763ee32015-09-17 18:46:55 -0400440#ifdef CONFIG_SANDBOX
Stephen Warrenbff6c312014-06-12 10:28:32 -0600441 /*
Pavel Machek13334e72014-07-19 23:50:44 +0200442 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warrenbff6c312014-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 Delaunay73287092017-01-27 11:00:42 +0100453#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Stephen Warrenbff6c312014-06-12 10:28:32 -0600454 info->uuid[0] = 0;
455#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100456#ifdef CONFIG_PARTITION_TYPE_GUID
457 info->type_guid[0] = 0;
458#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600459
Hans de Goede690c7962015-09-17 18:46:58 -0400460 return 0;
461 }
462#endif
463
464#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 Delaunay73287092017-01-27 11:00:42 +0100479#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
Hans de Goede690c7962015-09-17 18:46:58 -0400480 info->uuid[0] = 0;
481#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600482 return 0;
483 }
Hans de Goede4763ee32015-09-17 18:46:55 -0400484#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600485
Stephen Warrenc87ff222012-09-21 09:50:57 +0000486 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena95af942012-09-28 05:34:09 +0000487 if (!dev_part_str || !strlen(dev_part_str) ||
488 !strcmp(dev_part_str, "-"))
Simon Glass64b723f2017-08-03 12:22:12 -0600489 dev_part_str = env_get("bootdevice");
Stephen Warrenc87ff222012-09-21 09:50:57 +0000490
491 /* If still no dev_part_str, it's an error */
492 if (!dev_part_str) {
493 printf("** No device specified **\n");
494 goto cleanup;
495 }
496
497 /* 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 Herringd586cec2012-09-21 04:08:17 +0000507
Stephen Warrenc87ff222012-09-21 09:50:57 +0000508 /* Look up the device */
Simon Glasse6649a62016-02-29 15:25:43 -0700509 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000510 if (dev < 0)
511 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000512
Stephen Warrenc87ff222012-09-21 09:50:57 +0000513 /* 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;
529 }
Rob Herringd586cec2012-09-21 04:08:17 +0000530 }
531
Stephen Warrenc87ff222012-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 Herringd586cec2012-09-21 04:08:17 +0000543
Stephen Warrenc87ff222012-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;
Rob Herringd586cec2012-09-21 04:08:17 +0000553 }
Stephen Warrenc87ff222012-09-21 09:50:57 +0000554
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800555 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
556
Rob Clark2b7bfd92017-09-09 13:15:55 -0400557 part_get_info_whole_disk(*dev_desc, info);
Rob Herringd586cec2012-09-21 04:08:17 +0000558
Stephen Warrenc87ff222012-09-21 09:50:57 +0000559 ret = 0;
560 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000561 }
562
Stephen Warrenc87ff222012-09-21 09:50:57 +0000563 /*
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;
Rob Herringd586cec2012-09-21 04:08:17 +0000569
Stephen Warrenc87ff222012-09-21 09:50:57 +0000570 /*
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 Glassb89a8442016-02-29 15:25:48 -0700575 ret = part_get_info(*dev_desc, part, info);
Stephen Warrenc87ff222012-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 Glassb89a8442016-02-29 15:25:48 -0700587 ret = part_get_info(*dev_desc, p, info);
Stephen Warrenc87ff222012-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 Warrenc87ff222012-09-21 09:50:57 +0000618 } else {
619 printf("** No valid partitions found **\n");
Stephen Warren3c28be92012-10-08 07:45:54 +0000620 ret = -1;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000621 goto cleanup;
622 }
Rob Herringd586cec2012-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 Warrenc87ff222012-09-21 09:50:57 +0000628 ret = -1;
629 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000630 }
631
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800632 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
633
Stephen Warrenc87ff222012-09-21 09:50:57 +0000634 ret = part;
635 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000636
Stephen Warrenc87ff222012-09-21 09:50:57 +0000637cleanup:
638 free(dup_str);
639 return ret;
Rob Herringd586cec2012-09-21 04:08:17 +0000640}
Petr Kulhavy712257e2016-09-09 10:27:15 +0200641
Sam Protsenkodee80382017-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 Kulhavy712257e2016-09-09 10:27:15 +0200644{
Petr Kulhavy712257e2016-09-09 10:27:15 +0200645 struct part_driver *part_drv;
Kever Yang91829662018-02-10 17:55:38 +0800646 int ret;
647 int i;
Petr Kulhavy712257e2016-09-09 10:27:15 +0200648
Kever Yang91829662018-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 Kulhavy712257e2016-09-09 10:27:15 +0200661 }
662 }
Kever Yang91829662018-02-10 17:55:38 +0800663
Petr Kulhavy712257e2016-09-09 10:27:15 +0200664 return -1;
665}
Petr Kulhavy2e790d52016-09-09 10:27:17 +0200666
Sam Protsenkodee80382017-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 Kulhavy2e790d52016-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}