blob: 9a78cceadbc77971146882fc3a17fd13d0460c33 [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
Grant Likelyffc2dd72007-02-20 09:04:34 +010024struct block_drvr {
25 char *name;
Simon Glasse3394752016-02-29 15:25:34 -070026 struct blk_desc* (*get_dev)(int dev);
Stephen Warrenaeca52f2014-05-07 12:19:01 -060027 int (*select_hwpart)(int dev_num, int hwpart);
Grant Likelyffc2dd72007-02-20 09:04:34 +010028};
29
30static const struct block_drvr block_drvr[] = {
Jon Loeligerf7f54a82007-07-09 17:22:37 -050031#if defined(CONFIG_CMD_IDE)
Grant Likelyffc2dd72007-02-20 09:04:34 +010032 { .name = "ide", .get_dev = ide_get_dev, },
33#endif
Dave Liu76596ba2008-03-26 22:49:44 +080034#if defined(CONFIG_CMD_SATA)
35 {.name = "sata", .get_dev = sata_get_dev, },
36#endif
Jon Loeligerf7f54a82007-07-09 17:22:37 -050037#if defined(CONFIG_CMD_SCSI)
Grant Likelyffc2dd72007-02-20 09:04:34 +010038 { .name = "scsi", .get_dev = scsi_get_dev, },
39#endif
Jon Loeligerf7f54a82007-07-09 17:22:37 -050040#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
Grant Likelyffc2dd72007-02-20 09:04:34 +010041 { .name = "usb", .get_dev = usb_stor_get_dev, },
42#endif
43#if defined(CONFIG_MMC)
Stephen Warren52c44e42014-05-07 12:19:02 -060044 {
45 .name = "mmc",
46 .get_dev = mmc_get_dev,
47 .select_hwpart = mmc_select_hwpart,
48 },
Grant Likelyffc2dd72007-02-20 09:04:34 +010049#endif
50#if defined(CONFIG_SYSTEMACE)
51 { .name = "ace", .get_dev = systemace_get_dev, },
52#endif
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070053#if defined(CONFIG_SANDBOX)
54 { .name = "host", .get_dev = host_get_dev, },
55#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010056 { },
57};
58
Stefan Roese61ddee42007-02-20 13:21:57 +010059DECLARE_GLOBAL_DATA_PTR;
Stefan Roese61ddee42007-02-20 13:21:57 +010060
Gabe Black0fc71b52012-10-12 14:26:08 +000061#ifdef HAVE_BLOCK_DEVICE
Simon Glass83ce5632016-02-29 15:25:47 -070062static struct part_driver *part_driver_lookup_type(int part_type)
63{
64 struct part_driver *drv =
65 ll_entry_start(struct part_driver, part_driver);
66 const int n_ents = ll_entry_count(struct part_driver, part_driver);
67 struct part_driver *entry;
68
69 for (entry = drv; entry != drv + n_ents; entry++) {
70 if (part_type == entry->part_type)
71 return entry;
72 }
73
74 /* Not found */
75 return NULL;
76}
77
Simon Glasse3394752016-02-29 15:25:34 -070078static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Grant Likelyffc2dd72007-02-20 09:04:34 +010079{
80 const struct block_drvr *drvr = block_drvr;
Simon Glasse3394752016-02-29 15:25:34 -070081 struct blk_desc* (*reloc_get_dev)(int dev);
Stephen Warrenaeca52f2014-05-07 12:19:01 -060082 int (*select_hwpart)(int dev_num, int hwpart);
Heiko Schocher9b987232010-09-17 13:10:36 +020083 char *name;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060084 int ret;
Grant Likelyffc2dd72007-02-20 09:04:34 +010085
Tim Kientzle47e444c2012-03-27 11:43:25 +020086 if (!ifname)
87 return NULL;
88
Heiko Schocher9b987232010-09-17 13:10:36 +020089 name = drvr->name;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020090#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020091 name += gd->reloc_off;
92#endif
Lei Wen2a010672011-02-15 16:56:40 +080093 while (drvr->name) {
Heiko Schocher9b987232010-09-17 13:10:36 +020094 name = drvr->name;
Peter Tyser9057cbf2009-09-21 11:20:36 -050095 reloc_get_dev = drvr->get_dev;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060096 select_hwpart = drvr->select_hwpart;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020097#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020098 name += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -050099 reloc_get_dev += gd->reloc_off;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600100 if (select_hwpart)
101 select_hwpart += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500102#endif
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600103 if (strncmp(ifname, name, strlen(name)) == 0) {
Simon Glasse3394752016-02-29 15:25:34 -0700104 struct blk_desc *dev_desc = reloc_get_dev(dev);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600105 if (!dev_desc)
106 return NULL;
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600107 if (hwpart == 0 && !select_hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600108 return dev_desc;
109 if (!select_hwpart)
110 return NULL;
111 ret = select_hwpart(dev_desc->dev, hwpart);
112 if (ret < 0)
113 return NULL;
114 return dev_desc;
115 }
Grant Likelyffc2dd72007-02-20 09:04:34 +0100116 drvr++;
117 }
118 return NULL;
119}
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600120
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700121struct blk_desc *blk_get_dev(const char *ifname, int dev)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600122{
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600123 return get_dev_hwpart(ifname, dev, 0);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600124}
Grant Likelyffc2dd72007-02-20 09:04:34 +0100125#else
Simon Glasse3394752016-02-29 15:25:34 -0700126struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600127{
128 return NULL;
129}
130
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700131struct blk_desc *blk_get_dev(const char *ifname, int dev)
Grant Likelyffc2dd72007-02-20 09:04:34 +0100132{
133 return NULL;
134}
135#endif
136
Gabe Black0fc71b52012-10-12 14:26:08 +0000137#ifdef HAVE_BLOCK_DEVICE
Grant Likelyffc2dd72007-02-20 09:04:34 +0100138
wdenkaffae2b2002-08-17 09:36:01 +0000139/* ------------------------------------------------------------------------- */
140/*
141 * reports device info to the user
142 */
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300143
wdenkc35ba4e2004-03-14 22:25:36 +0000144#ifdef CONFIG_LBA48
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300145typedef uint64_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000146#else
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300147typedef lbaint_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000148#endif
wdenkaffae2b2002-08-17 09:36:01 +0000149
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300150/*
151 * Overflowless variant of (block_count * mul_by / div_by)
152 * when div_by > mul_by
153 */
Pavel Macheka4264e12014-09-09 15:19:42 +0200154static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300155{
156 lba512_t bc_quot, bc_rem;
157
158 /* x * m / d == x / d * m + (x % d) * m / d */
159 bc_quot = block_count / div_by;
160 bc_rem = block_count - div_by * bc_quot;
161 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
162}
163
Simon Glasse3394752016-02-29 15:25:34 -0700164void dev_print (struct blk_desc *dev_desc)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300165{
166 lba512_t lba512; /* number of blocks if 512bytes block size */
167
Wolfgang Denk8561e712009-05-15 09:27:58 +0200168 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
169 puts ("not available\n");
170 return;
171 }
172
Tor Krill03ff3302008-05-29 11:10:30 +0200173 switch (dev_desc->if_type) {
Detlev Zundelbec6b322008-05-05 16:11:21 +0200174 case IF_TYPE_SCSI:
175 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
176 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000177 dev_desc->vendor,
Detlev Zundelbec6b322008-05-05 16:11:21 +0200178 dev_desc->product,
179 dev_desc->revision);
180 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200181 case IF_TYPE_ATAPI:
Detlev Zundelbec6b322008-05-05 16:11:21 +0200182 case IF_TYPE_IDE:
183 case IF_TYPE_SATA:
Dave Liu76596ba2008-03-26 22:49:44 +0800184 printf ("Model: %s Firm: %s Ser#: %s\n",
185 dev_desc->vendor,
186 dev_desc->revision,
187 dev_desc->product);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200188 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200189 case IF_TYPE_SD:
190 case IF_TYPE_MMC:
Nícolas Carneiro Lebedencoef3edd42008-09-04 15:35:46 -0300191 case IF_TYPE_USB:
192 printf ("Vendor: %s Rev: %s Prod: %s\n",
193 dev_desc->vendor,
194 dev_desc->revision,
195 dev_desc->product);
196 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200197 case IF_TYPE_DOC:
198 puts("device type DOC\n");
199 return;
Tor Krill03ff3302008-05-29 11:10:30 +0200200 case IF_TYPE_UNKNOWN:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200201 puts("device type unknown\n");
202 return;
Detlev Zundelbec6b322008-05-05 16:11:21 +0200203 default:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200204 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200205 return;
wdenkaffae2b2002-08-17 09:36:01 +0000206 }
207 puts (" Type: ");
208 if (dev_desc->removable)
209 puts ("Removable ");
210 switch (dev_desc->type & 0x1F) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200211 case DEV_TYPE_HARDDISK:
212 puts ("Hard Disk");
213 break;
214 case DEV_TYPE_CDROM:
215 puts ("CD ROM");
216 break;
217 case DEV_TYPE_OPDISK:
218 puts ("Optical Device");
219 break;
220 case DEV_TYPE_TAPE:
221 puts ("Tape");
222 break;
223 default:
224 printf ("# %02X #", dev_desc->type & 0x1F);
225 break;
wdenkaffae2b2002-08-17 09:36:01 +0000226 }
227 puts ("\n");
Jerry Huang6a4feb42012-11-06 15:33:12 +0000228 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000229 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkf602aa02004-03-13 23:29:43 +0000230 lbaint_t lba;
wdenk05939202004-04-18 17:39:38 +0000231
232 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000233
wdenkf602aa02004-03-13 23:29:43 +0000234 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000235 /* round to 1 digit */
Pavel Macheka4264e12014-09-09 15:19:42 +0200236 /* 2048 = (1024 * 1024) / 512 MB */
237 mb = lba512_muldiv(lba512, 10, 2048);
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300238
wdenkaffae2b2002-08-17 09:36:01 +0000239 mb_quot = mb / 10;
240 mb_rem = mb - (10 * mb_quot);
241
242 gb = mb / 1024;
243 gb_quot = gb / 10;
244 gb_rem = gb - (10 * gb_quot);
wdenkc35ba4e2004-03-14 22:25:36 +0000245#ifdef CONFIG_LBA48
wdenk05939202004-04-18 17:39:38 +0000246 if (dev_desc->lba48)
wdenkf602aa02004-03-13 23:29:43 +0000247 printf (" Supports 48-bit addressing\n");
248#endif
Heiko Schocher0f602e12009-12-03 11:21:21 +0100249#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500250 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
wdenkf602aa02004-03-13 23:29:43 +0000251 mb_quot, mb_rem,
252 gb_quot, gb_rem,
253 lba,
254 dev_desc->blksz);
255#else
wdenkaffae2b2002-08-17 09:36:01 +0000256 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
257 mb_quot, mb_rem,
258 gb_quot, gb_rem,
wdenkf602aa02004-03-13 23:29:43 +0000259 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000260 dev_desc->blksz);
wdenkf602aa02004-03-13 23:29:43 +0000261#endif
wdenkaffae2b2002-08-17 09:36:01 +0000262 } else {
263 puts (" Capacity: not available\n");
264 }
265}
Jon Loeliger1670a5b2007-07-10 11:19:50 -0500266#endif
wdenkaffae2b2002-08-17 09:36:01 +0000267
Gabe Black0fc71b52012-10-12 14:26:08 +0000268#ifdef HAVE_BLOCK_DEVICE
wdenkaffae2b2002-08-17 09:36:01 +0000269
Simon Glassb89a8442016-02-29 15:25:48 -0700270void part_init(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000271{
Simon Glass83ce5632016-02-29 15:25:47 -0700272 struct part_driver *drv =
273 ll_entry_start(struct part_driver, part_driver);
274 const int n_ents = ll_entry_count(struct part_driver, part_driver);
275 struct part_driver *entry;
richardretanubune6745592008-09-26 11:13:22 -0400276
Simon Glass83ce5632016-02-29 15:25:47 -0700277 dev_desc->part_type = PART_TYPE_UNKNOWN;
278 for (entry = drv; entry != drv + n_ents; entry++) {
279 int ret;
wdenk452cfd62002-11-19 11:04:11 +0000280
Simon Glass83ce5632016-02-29 15:25:47 -0700281 ret = entry->test(dev_desc);
282 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
283 if (!ret) {
284 dev_desc->part_type = entry->part_type;
285 break;
286 }
wdenk452cfd62002-11-19 11:04:11 +0000287 }
wdenkaffae2b2002-08-17 09:36:01 +0000288}
289
Simon Glass83ce5632016-02-29 15:25:47 -0700290static void print_part_header(const char *type, struct blk_desc *dev_desc)
291{
Gabe Black0fc71b52012-10-12 14:26:08 +0000292#if defined(CONFIG_MAC_PARTITION) || \
293 defined(CONFIG_DOS_PARTITION) || \
294 defined(CONFIG_ISO_PARTITION) || \
295 defined(CONFIG_AMIGA_PARTITION) || \
296 defined(CONFIG_EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000297 puts ("\nPartition Map for ");
298 switch (dev_desc->if_type) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200299 case IF_TYPE_IDE:
300 puts ("IDE");
301 break;
302 case IF_TYPE_SATA:
303 puts ("SATA");
304 break;
305 case IF_TYPE_SCSI:
306 puts ("SCSI");
307 break;
308 case IF_TYPE_ATAPI:
309 puts ("ATAPI");
310 break;
311 case IF_TYPE_USB:
312 puts ("USB");
313 break;
314 case IF_TYPE_DOC:
315 puts ("DOC");
316 break;
Lei Wen61dba932010-09-13 22:07:28 +0800317 case IF_TYPE_MMC:
318 puts ("MMC");
319 break;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700320 case IF_TYPE_HOST:
321 puts("HOST");
322 break;
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200323 default:
324 puts ("UNKNOWN");
325 break;
wdenkaffae2b2002-08-17 09:36:01 +0000326 }
327 printf (" device %d -- Partition Type: %s\n\n",
328 dev_desc->dev, type);
Gabe Black0fc71b52012-10-12 14:26:08 +0000329#endif /* any CONFIG_..._PARTITION */
Simon Glass83ce5632016-02-29 15:25:47 -0700330}
Gabe Black0fc71b52012-10-12 14:26:08 +0000331
Simon Glassb89a8442016-02-29 15:25:48 -0700332void part_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000333{
Simon Glass83ce5632016-02-29 15:25:47 -0700334 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000335
Simon Glass83ce5632016-02-29 15:25:47 -0700336 drv = part_driver_lookup_type(dev_desc->part_type);
337 if (!drv) {
338 printf("## Unknown partition table type %x\n",
339 dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +0000340 return;
wdenkaffae2b2002-08-17 09:36:01 +0000341 }
Simon Glass83ce5632016-02-29 15:25:47 -0700342
343 PRINTF("## Testing for valid %s partition ##\n", drv->name);
344 print_part_header(drv->name, dev_desc);
345 if (drv->print)
346 drv->print(dev_desc);
wdenkaffae2b2002-08-17 09:36:01 +0000347}
348
Gabe Black0fc71b52012-10-12 14:26:08 +0000349#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000350
Simon Glassb89a8442016-02-29 15:25:48 -0700351int part_get_info(struct blk_desc *dev_desc, int part,
Pavel Machek13334e72014-07-19 23:50:44 +0200352 disk_partition_t *info)
Stephen Warren07c5bff2012-09-21 12:46:54 +0000353{
Gabe Black0fc71b52012-10-12 14:26:08 +0000354#ifdef HAVE_BLOCK_DEVICE
Simon Glass83ce5632016-02-29 15:25:47 -0700355 struct part_driver *drv;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000356
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000357#ifdef CONFIG_PARTITION_UUIDS
358 /* The common case is no UUID support */
359 info->uuid[0] = 0;
360#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100361#ifdef CONFIG_PARTITION_TYPE_GUID
362 info->type_guid[0] = 0;
363#endif
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000364
Simon Glass83ce5632016-02-29 15:25:47 -0700365 drv = part_driver_lookup_type(dev_desc->part_type);
366 if (!drv) {
367 debug("## Unknown partition table type %x\n",
368 dev_desc->part_type);
369 return -EPROTONOSUPPORT;
370 }
371 if (!drv->get_info) {
372 PRINTF("## Driver %s does not have the get_info() method\n");
373 return -ENOSYS;
374 }
375 if (drv->get_info(dev_desc, part, info) == 0) {
376 PRINTF("## Valid %s partition found ##\n", drv->name);
377 return 0;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000378 }
Gabe Black0fc71b52012-10-12 14:26:08 +0000379#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000380
381 return -1;
382}
Rob Herringd586cec2012-09-21 04:08:17 +0000383
Simon Glasse6649a62016-02-29 15:25:43 -0700384int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
385 struct blk_desc **dev_desc)
Stephen Warrenafd909c2012-09-21 09:50:56 +0000386{
387 char *ep;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600388 char *dup_str = NULL;
389 const char *dev_str, *hwpart_str;
390 int dev, hwpart;
391
392 hwpart_str = strchr(dev_hwpart_str, '.');
393 if (hwpart_str) {
394 dup_str = strdup(dev_hwpart_str);
395 dup_str[hwpart_str - dev_hwpart_str] = 0;
396 dev_str = dup_str;
397 hwpart_str++;
398 } else {
399 dev_str = dev_hwpart_str;
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600400 hwpart = 0;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600401 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000402
403 dev = simple_strtoul(dev_str, &ep, 16);
404 if (*ep) {
405 printf("** Bad device specification %s %s **\n",
406 ifname, dev_str);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600407 dev = -1;
408 goto cleanup;
409 }
410
411 if (hwpart_str) {
412 hwpart = simple_strtoul(hwpart_str, &ep, 16);
413 if (*ep) {
414 printf("** Bad HW partition specification %s %s **\n",
415 ifname, hwpart_str);
416 dev = -1;
417 goto cleanup;
418 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000419 }
420
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600421 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000422 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600423 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
424 dev = -1;
425 goto cleanup;
Stephen Warrenafd909c2012-09-21 09:50:56 +0000426 }
427
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000428#ifdef HAVE_BLOCK_DEVICE
429 /*
430 * Updates the partition table for the specified hw partition.
431 * Does not need to be done for hwpart 0 since it is default and
432 * already loaded.
433 */
434 if(hwpart != 0)
Simon Glassb89a8442016-02-29 15:25:48 -0700435 part_init(*dev_desc);
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000436#endif
437
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600438cleanup:
439 free(dup_str);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000440 return dev;
441}
442
Stephen Warrenc87ff222012-09-21 09:50:57 +0000443#define PART_UNSPECIFIED -2
444#define PART_AUTO -1
445#define MAX_SEARCH_PARTITIONS 16
Simon Glasse76ee972016-02-29 15:25:44 -0700446int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glasse3394752016-02-29 15:25:34 -0700447 struct blk_desc **dev_desc,
Stephen Warrenc87ff222012-09-21 09:50:57 +0000448 disk_partition_t *info, int allow_whole_dev)
Rob Herringd586cec2012-09-21 04:08:17 +0000449{
Stephen Warrenc87ff222012-09-21 09:50:57 +0000450 int ret = -1;
451 const char *part_str;
452 char *dup_str = NULL;
453 const char *dev_str;
Rob Herringd586cec2012-09-21 04:08:17 +0000454 int dev;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000455 char *ep;
456 int p;
457 int part;
458 disk_partition_t tmpinfo;
459
Hans de Goede690c7962015-09-17 18:46:58 -0400460#if defined CONFIG_SANDBOX && defined CONFIG_CMD_UBIFS
461#error Only one of CONFIG_SANDBOX and CONFIG_CMD_UBIFS may be selected
462#endif
463
Hans de Goede4763ee32015-09-17 18:46:55 -0400464#ifdef CONFIG_SANDBOX
Stephen Warrenbff6c312014-06-12 10:28:32 -0600465 /*
Pavel Machek13334e72014-07-19 23:50:44 +0200466 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warrenbff6c312014-06-12 10:28:32 -0600467 * host's own filesystem.
468 */
469 if (0 == strcmp(ifname, "hostfs")) {
470 *dev_desc = NULL;
471 info->start = 0;
472 info->size = 0;
473 info->blksz = 0;
474 info->bootable = 0;
475 strcpy((char *)info->type, BOOT_PART_TYPE);
476 strcpy((char *)info->name, "Sandbox host");
477#ifdef CONFIG_PARTITION_UUIDS
478 info->uuid[0] = 0;
479#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100480#ifdef CONFIG_PARTITION_TYPE_GUID
481 info->type_guid[0] = 0;
482#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600483
Hans de Goede690c7962015-09-17 18:46:58 -0400484 return 0;
485 }
486#endif
487
488#ifdef CONFIG_CMD_UBIFS
489 /*
490 * Special-case ubi, ubi goes through a mtd, rathen then through
491 * a regular block device.
492 */
493 if (0 == strcmp(ifname, "ubi")) {
494 if (!ubifs_is_mounted()) {
495 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
496 return -1;
497 }
498
499 *dev_desc = NULL;
500 memset(info, 0, sizeof(*info));
501 strcpy((char *)info->type, BOOT_PART_TYPE);
502 strcpy((char *)info->name, "UBI");
503#ifdef CONFIG_PARTITION_UUIDS
504 info->uuid[0] = 0;
505#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600506 return 0;
507 }
Hans de Goede4763ee32015-09-17 18:46:55 -0400508#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600509
Stephen Warrenc87ff222012-09-21 09:50:57 +0000510 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena95af942012-09-28 05:34:09 +0000511 if (!dev_part_str || !strlen(dev_part_str) ||
512 !strcmp(dev_part_str, "-"))
Stephen Warrenc87ff222012-09-21 09:50:57 +0000513 dev_part_str = getenv("bootdevice");
514
515 /* If still no dev_part_str, it's an error */
516 if (!dev_part_str) {
517 printf("** No device specified **\n");
518 goto cleanup;
519 }
520
521 /* Separate device and partition ID specification */
522 part_str = strchr(dev_part_str, ':');
523 if (part_str) {
524 dup_str = strdup(dev_part_str);
525 dup_str[part_str - dev_part_str] = 0;
526 dev_str = dup_str;
527 part_str++;
528 } else {
529 dev_str = dev_part_str;
530 }
Rob Herringd586cec2012-09-21 04:08:17 +0000531
Stephen Warrenc87ff222012-09-21 09:50:57 +0000532 /* Look up the device */
Simon Glasse6649a62016-02-29 15:25:43 -0700533 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000534 if (dev < 0)
535 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000536
Stephen Warrenc87ff222012-09-21 09:50:57 +0000537 /* Convert partition ID string to number */
538 if (!part_str || !*part_str) {
539 part = PART_UNSPECIFIED;
540 } else if (!strcmp(part_str, "auto")) {
541 part = PART_AUTO;
542 } else {
543 /* Something specified -> use exactly that */
544 part = (int)simple_strtoul(part_str, &ep, 16);
545 /*
546 * Less than whole string converted,
547 * or request for whole device, but caller requires partition.
548 */
549 if (*ep || (part == 0 && !allow_whole_dev)) {
550 printf("** Bad partition specification %s %s **\n",
551 ifname, dev_part_str);
552 goto cleanup;
553 }
Rob Herringd586cec2012-09-21 04:08:17 +0000554 }
555
Stephen Warrenc87ff222012-09-21 09:50:57 +0000556 /*
557 * No partition table on device,
558 * or user requested partition 0 (entire device).
559 */
560 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
561 (part == 0)) {
562 if (!(*dev_desc)->lba) {
563 printf("** Bad device size - %s %s **\n", ifname,
564 dev_str);
565 goto cleanup;
566 }
Rob Herringd586cec2012-09-21 04:08:17 +0000567
Stephen Warrenc87ff222012-09-21 09:50:57 +0000568 /*
569 * If user specified a partition ID other than 0,
570 * or the calling command only accepts partitions,
571 * it's an error.
572 */
573 if ((part > 0) || (!allow_whole_dev)) {
574 printf("** No partition table - %s %s **\n", ifname,
575 dev_str);
576 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000577 }
Stephen Warrenc87ff222012-09-21 09:50:57 +0000578
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800579 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
580
Rob Herringd586cec2012-09-21 04:08:17 +0000581 info->start = 0;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000582 info->size = (*dev_desc)->lba;
583 info->blksz = (*dev_desc)->blksz;
584 info->bootable = 0;
Stephen Warren65be8492012-10-10 07:57:51 +0000585 strcpy((char *)info->type, BOOT_PART_TYPE);
586 strcpy((char *)info->name, "Whole Disk");
Stephen Warrenc87ff222012-09-21 09:50:57 +0000587#ifdef CONFIG_PARTITION_UUIDS
588 info->uuid[0] = 0;
589#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100590#ifdef CONFIG_PARTITION_TYPE_GUID
591 info->type_guid[0] = 0;
592#endif
Rob Herringd586cec2012-09-21 04:08:17 +0000593
Stephen Warrenc87ff222012-09-21 09:50:57 +0000594 ret = 0;
595 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000596 }
597
Stephen Warrenc87ff222012-09-21 09:50:57 +0000598 /*
599 * Now there's known to be a partition table,
600 * not specifying a partition means to pick partition 1.
601 */
602 if (part == PART_UNSPECIFIED)
603 part = 1;
Rob Herringd586cec2012-09-21 04:08:17 +0000604
Stephen Warrenc87ff222012-09-21 09:50:57 +0000605 /*
606 * If user didn't specify a partition number, or did specify something
607 * other than "auto", use that partition number directly.
608 */
609 if (part != PART_AUTO) {
Simon Glassb89a8442016-02-29 15:25:48 -0700610 ret = part_get_info(*dev_desc, part, info);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000611 if (ret) {
612 printf("** Invalid partition %d **\n", part);
613 goto cleanup;
614 }
615 } else {
616 /*
617 * Find the first bootable partition.
618 * If none are bootable, fall back to the first valid partition.
619 */
620 part = 0;
621 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Simon Glassb89a8442016-02-29 15:25:48 -0700622 ret = part_get_info(*dev_desc, p, info);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000623 if (ret)
624 continue;
625
626 /*
627 * First valid partition, or new better partition?
628 * If so, save partition ID.
629 */
630 if (!part || info->bootable)
631 part = p;
632
633 /* Best possible partition? Stop searching. */
634 if (info->bootable)
635 break;
636
637 /*
638 * We now need to search further for best possible.
639 * If we what we just queried was the best so far,
640 * save the info since we over-write it next loop.
641 */
642 if (part == p)
643 tmpinfo = *info;
644 }
645 /* If we found any acceptable partition */
646 if (part) {
647 /*
648 * If we searched all possible partition IDs,
649 * return the first valid partition we found.
650 */
651 if (p == MAX_SEARCH_PARTITIONS + 1)
652 *info = tmpinfo;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000653 } else {
654 printf("** No valid partitions found **\n");
Stephen Warren3c28be92012-10-08 07:45:54 +0000655 ret = -1;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000656 goto cleanup;
657 }
Rob Herringd586cec2012-09-21 04:08:17 +0000658 }
659 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
660 printf("** Invalid partition type \"%.32s\""
661 " (expect \"" BOOT_PART_TYPE "\")\n",
662 info->type);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000663 ret = -1;
664 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000665 }
666
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800667 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
668
Stephen Warrenc87ff222012-09-21 09:50:57 +0000669 ret = part;
670 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000671
Stephen Warrenc87ff222012-09-21 09:50:57 +0000672cleanup:
673 free(dup_str);
674 return ret;
Rob Herringd586cec2012-09-21 04:08:17 +0000675}