blob: bf224b2c5280ac274a921661168707a932595165 [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
Alexander Graf3b3d9152016-03-04 01:09:56 +010024const struct block_drvr block_drvr[] = {
Jon Loeligerf7f54a82007-07-09 17:22:37 -050025#if defined(CONFIG_CMD_IDE)
Simon Glass844ef1d2016-05-01 11:36:14 -060026 { .name = "ide", },
Grant Likelyffc2dd72007-02-20 09:04:34 +010027#endif
Dave Liu76596ba2008-03-26 22:49:44 +080028#if defined(CONFIG_CMD_SATA)
29 {.name = "sata", .get_dev = sata_get_dev, },
30#endif
Simon Glass8706b812016-05-01 11:36:02 -060031#if defined(CONFIG_SCSI)
Grant Likelyffc2dd72007-02-20 09:04:34 +010032 { .name = "scsi", .get_dev = scsi_get_dev, },
33#endif
Jon Loeligerf7f54a82007-07-09 17:22:37 -050034#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
Simon Glasscf437782016-05-01 11:36:13 -060035 { .name = "usb", },
Grant Likelyffc2dd72007-02-20 09:04:34 +010036#endif
37#if defined(CONFIG_MMC)
Stephen Warren52c44e42014-05-07 12:19:02 -060038 {
39 .name = "mmc",
40 .get_dev = mmc_get_dev,
41 .select_hwpart = mmc_select_hwpart,
42 },
Grant Likelyffc2dd72007-02-20 09:04:34 +010043#endif
44#if defined(CONFIG_SYSTEMACE)
45 { .name = "ace", .get_dev = systemace_get_dev, },
46#endif
Henrik Nordström26f9a6c2013-11-10 10:26:56 -070047#if defined(CONFIG_SANDBOX)
48 { .name = "host", .get_dev = host_get_dev, },
49#endif
Grant Likelyffc2dd72007-02-20 09:04:34 +010050 { },
51};
52
Stefan Roese61ddee42007-02-20 13:21:57 +010053DECLARE_GLOBAL_DATA_PTR;
Stefan Roese61ddee42007-02-20 13:21:57 +010054
Gabe Black0fc71b52012-10-12 14:26:08 +000055#ifdef HAVE_BLOCK_DEVICE
Simon Glass83ce5632016-02-29 15:25:47 -070056static struct part_driver *part_driver_lookup_type(int part_type)
57{
58 struct part_driver *drv =
59 ll_entry_start(struct part_driver, part_driver);
60 const int n_ents = ll_entry_count(struct part_driver, part_driver);
61 struct part_driver *entry;
62
63 for (entry = drv; entry != drv + n_ents; entry++) {
64 if (part_type == entry->part_type)
65 return entry;
66 }
67
68 /* Not found */
69 return NULL;
70}
71
Simon Glasse3394752016-02-29 15:25:34 -070072static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Grant Likelyffc2dd72007-02-20 09:04:34 +010073{
74 const struct block_drvr *drvr = block_drvr;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060075 int (*select_hwpart)(int dev_num, int hwpart);
Heiko Schocher9b987232010-09-17 13:10:36 +020076 char *name;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060077 int ret;
Grant Likelyffc2dd72007-02-20 09:04:34 +010078
Tim Kientzle47e444c2012-03-27 11:43:25 +020079 if (!ifname)
80 return NULL;
81
Heiko Schocher9b987232010-09-17 13:10:36 +020082 name = drvr->name;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020083#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020084 name += gd->reloc_off;
85#endif
Lei Wen2a010672011-02-15 16:56:40 +080086 while (drvr->name) {
Heiko Schocher9b987232010-09-17 13:10:36 +020087 name = drvr->name;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060088 select_hwpart = drvr->select_hwpart;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020089#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020090 name += gd->reloc_off;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060091 if (select_hwpart)
92 select_hwpart += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -050093#endif
Stephen Warrenaeca52f2014-05-07 12:19:01 -060094 if (strncmp(ifname, name, strlen(name)) == 0) {
Simon Glass61c74962016-05-01 11:36:12 -060095 struct blk_desc *dev_desc;
96
97 dev_desc = blk_get_devnum_by_typename(name, dev);
Stephen Warrenaeca52f2014-05-07 12:19:01 -060098 if (!dev_desc)
99 return NULL;
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600100 if (hwpart == 0 && !select_hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600101 return dev_desc;
102 if (!select_hwpart)
103 return NULL;
Simon Glass2f26fff2016-02-29 15:25:51 -0700104 ret = select_hwpart(dev_desc->devnum, hwpart);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600105 if (ret < 0)
106 return NULL;
107 return dev_desc;
108 }
Grant Likelyffc2dd72007-02-20 09:04:34 +0100109 drvr++;
110 }
111 return NULL;
112}
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600113
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700114struct blk_desc *blk_get_dev(const char *ifname, int dev)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600115{
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600116 return get_dev_hwpart(ifname, dev, 0);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600117}
Grant Likelyffc2dd72007-02-20 09:04:34 +0100118#else
Simon Glasse3394752016-02-29 15:25:34 -0700119struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600120{
121 return NULL;
122}
123
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700124struct blk_desc *blk_get_dev(const char *ifname, int dev)
Grant Likelyffc2dd72007-02-20 09:04:34 +0100125{
126 return NULL;
127}
128#endif
129
Gabe Black0fc71b52012-10-12 14:26:08 +0000130#ifdef HAVE_BLOCK_DEVICE
Grant Likelyffc2dd72007-02-20 09:04:34 +0100131
wdenkaffae2b2002-08-17 09:36:01 +0000132/* ------------------------------------------------------------------------- */
133/*
134 * reports device info to the user
135 */
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300136
wdenkc35ba4e2004-03-14 22:25:36 +0000137#ifdef CONFIG_LBA48
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300138typedef uint64_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000139#else
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300140typedef lbaint_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000141#endif
wdenkaffae2b2002-08-17 09:36:01 +0000142
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300143/*
144 * Overflowless variant of (block_count * mul_by / div_by)
145 * when div_by > mul_by
146 */
Pavel Macheka4264e12014-09-09 15:19:42 +0200147static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300148{
149 lba512_t bc_quot, bc_rem;
150
151 /* x * m / d == x / d * m + (x % d) * m / d */
152 bc_quot = block_count / div_by;
153 bc_rem = block_count - div_by * bc_quot;
154 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
155}
156
Simon Glasse3394752016-02-29 15:25:34 -0700157void dev_print (struct blk_desc *dev_desc)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300158{
159 lba512_t lba512; /* number of blocks if 512bytes block size */
160
Wolfgang Denk8561e712009-05-15 09:27:58 +0200161 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
162 puts ("not available\n");
163 return;
164 }
165
Tor Krill03ff3302008-05-29 11:10:30 +0200166 switch (dev_desc->if_type) {
Detlev Zundelbec6b322008-05-05 16:11:21 +0200167 case IF_TYPE_SCSI:
168 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
169 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000170 dev_desc->vendor,
Detlev Zundelbec6b322008-05-05 16:11:21 +0200171 dev_desc->product,
172 dev_desc->revision);
173 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200174 case IF_TYPE_ATAPI:
Detlev Zundelbec6b322008-05-05 16:11:21 +0200175 case IF_TYPE_IDE:
176 case IF_TYPE_SATA:
Dave Liu76596ba2008-03-26 22:49:44 +0800177 printf ("Model: %s Firm: %s Ser#: %s\n",
178 dev_desc->vendor,
179 dev_desc->revision,
180 dev_desc->product);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200181 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200182 case IF_TYPE_SD:
183 case IF_TYPE_MMC:
Nícolas Carneiro Lebedencoef3edd42008-09-04 15:35:46 -0300184 case IF_TYPE_USB:
185 printf ("Vendor: %s Rev: %s Prod: %s\n",
186 dev_desc->vendor,
187 dev_desc->revision,
188 dev_desc->product);
189 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200190 case IF_TYPE_DOC:
191 puts("device type DOC\n");
192 return;
Tor Krill03ff3302008-05-29 11:10:30 +0200193 case IF_TYPE_UNKNOWN:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200194 puts("device type unknown\n");
195 return;
Detlev Zundelbec6b322008-05-05 16:11:21 +0200196 default:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200197 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200198 return;
wdenkaffae2b2002-08-17 09:36:01 +0000199 }
200 puts (" Type: ");
201 if (dev_desc->removable)
202 puts ("Removable ");
203 switch (dev_desc->type & 0x1F) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200204 case DEV_TYPE_HARDDISK:
205 puts ("Hard Disk");
206 break;
207 case DEV_TYPE_CDROM:
208 puts ("CD ROM");
209 break;
210 case DEV_TYPE_OPDISK:
211 puts ("Optical Device");
212 break;
213 case DEV_TYPE_TAPE:
214 puts ("Tape");
215 break;
216 default:
217 printf ("# %02X #", dev_desc->type & 0x1F);
218 break;
wdenkaffae2b2002-08-17 09:36:01 +0000219 }
220 puts ("\n");
Jerry Huang6a4feb42012-11-06 15:33:12 +0000221 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000222 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkf602aa02004-03-13 23:29:43 +0000223 lbaint_t lba;
wdenk05939202004-04-18 17:39:38 +0000224
225 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000226
wdenkf602aa02004-03-13 23:29:43 +0000227 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000228 /* round to 1 digit */
Pavel Macheka4264e12014-09-09 15:19:42 +0200229 /* 2048 = (1024 * 1024) / 512 MB */
230 mb = lba512_muldiv(lba512, 10, 2048);
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300231
wdenkaffae2b2002-08-17 09:36:01 +0000232 mb_quot = mb / 10;
233 mb_rem = mb - (10 * mb_quot);
234
235 gb = mb / 1024;
236 gb_quot = gb / 10;
237 gb_rem = gb - (10 * gb_quot);
wdenkc35ba4e2004-03-14 22:25:36 +0000238#ifdef CONFIG_LBA48
wdenk05939202004-04-18 17:39:38 +0000239 if (dev_desc->lba48)
wdenkf602aa02004-03-13 23:29:43 +0000240 printf (" Supports 48-bit addressing\n");
241#endif
Heiko Schocher0f602e12009-12-03 11:21:21 +0100242#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500243 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
wdenkf602aa02004-03-13 23:29:43 +0000244 mb_quot, mb_rem,
245 gb_quot, gb_rem,
246 lba,
247 dev_desc->blksz);
248#else
wdenkaffae2b2002-08-17 09:36:01 +0000249 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
250 mb_quot, mb_rem,
251 gb_quot, gb_rem,
wdenkf602aa02004-03-13 23:29:43 +0000252 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000253 dev_desc->blksz);
wdenkf602aa02004-03-13 23:29:43 +0000254#endif
wdenkaffae2b2002-08-17 09:36:01 +0000255 } else {
256 puts (" Capacity: not available\n");
257 }
258}
Jon Loeliger1670a5b2007-07-10 11:19:50 -0500259#endif
wdenkaffae2b2002-08-17 09:36:01 +0000260
Gabe Black0fc71b52012-10-12 14:26:08 +0000261#ifdef HAVE_BLOCK_DEVICE
wdenkaffae2b2002-08-17 09:36:01 +0000262
Simon Glassb89a8442016-02-29 15:25:48 -0700263void part_init(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000264{
Simon Glass83ce5632016-02-29 15:25:47 -0700265 struct part_driver *drv =
266 ll_entry_start(struct part_driver, part_driver);
267 const int n_ents = ll_entry_count(struct part_driver, part_driver);
268 struct part_driver *entry;
richardretanubune6745592008-09-26 11:13:22 -0400269
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700270 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
271
Simon Glass83ce5632016-02-29 15:25:47 -0700272 dev_desc->part_type = PART_TYPE_UNKNOWN;
273 for (entry = drv; entry != drv + n_ents; entry++) {
274 int ret;
wdenk452cfd62002-11-19 11:04:11 +0000275
Simon Glass83ce5632016-02-29 15:25:47 -0700276 ret = entry->test(dev_desc);
277 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
278 if (!ret) {
279 dev_desc->part_type = entry->part_type;
280 break;
281 }
wdenk452cfd62002-11-19 11:04:11 +0000282 }
wdenkaffae2b2002-08-17 09:36:01 +0000283}
284
Simon Glass83ce5632016-02-29 15:25:47 -0700285static void print_part_header(const char *type, struct blk_desc *dev_desc)
286{
Gabe Black0fc71b52012-10-12 14:26:08 +0000287#if defined(CONFIG_MAC_PARTITION) || \
288 defined(CONFIG_DOS_PARTITION) || \
289 defined(CONFIG_ISO_PARTITION) || \
290 defined(CONFIG_AMIGA_PARTITION) || \
291 defined(CONFIG_EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000292 puts ("\nPartition Map for ");
293 switch (dev_desc->if_type) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200294 case IF_TYPE_IDE:
295 puts ("IDE");
296 break;
297 case IF_TYPE_SATA:
298 puts ("SATA");
299 break;
300 case IF_TYPE_SCSI:
301 puts ("SCSI");
302 break;
303 case IF_TYPE_ATAPI:
304 puts ("ATAPI");
305 break;
306 case IF_TYPE_USB:
307 puts ("USB");
308 break;
309 case IF_TYPE_DOC:
310 puts ("DOC");
311 break;
Lei Wen61dba932010-09-13 22:07:28 +0800312 case IF_TYPE_MMC:
313 puts ("MMC");
314 break;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700315 case IF_TYPE_HOST:
316 puts("HOST");
317 break;
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200318 default:
319 puts ("UNKNOWN");
320 break;
wdenkaffae2b2002-08-17 09:36:01 +0000321 }
322 printf (" device %d -- Partition Type: %s\n\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700323 dev_desc->devnum, type);
Gabe Black0fc71b52012-10-12 14:26:08 +0000324#endif /* any CONFIG_..._PARTITION */
Simon Glass83ce5632016-02-29 15:25:47 -0700325}
Gabe Black0fc71b52012-10-12 14:26:08 +0000326
Simon Glassb89a8442016-02-29 15:25:48 -0700327void part_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000328{
Simon Glass83ce5632016-02-29 15:25:47 -0700329 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000330
Simon Glass83ce5632016-02-29 15:25:47 -0700331 drv = part_driver_lookup_type(dev_desc->part_type);
332 if (!drv) {
333 printf("## Unknown partition table type %x\n",
334 dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +0000335 return;
wdenkaffae2b2002-08-17 09:36:01 +0000336 }
Simon Glass83ce5632016-02-29 15:25:47 -0700337
338 PRINTF("## Testing for valid %s partition ##\n", drv->name);
339 print_part_header(drv->name, dev_desc);
340 if (drv->print)
341 drv->print(dev_desc);
wdenkaffae2b2002-08-17 09:36:01 +0000342}
343
Gabe Black0fc71b52012-10-12 14:26:08 +0000344#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000345
Simon Glassb89a8442016-02-29 15:25:48 -0700346int part_get_info(struct blk_desc *dev_desc, int part,
Pavel Machek13334e72014-07-19 23:50:44 +0200347 disk_partition_t *info)
Stephen Warren07c5bff2012-09-21 12:46:54 +0000348{
Gabe Black0fc71b52012-10-12 14:26:08 +0000349#ifdef HAVE_BLOCK_DEVICE
Simon Glass83ce5632016-02-29 15:25:47 -0700350 struct part_driver *drv;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000351
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000352#ifdef CONFIG_PARTITION_UUIDS
353 /* The common case is no UUID support */
354 info->uuid[0] = 0;
355#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100356#ifdef CONFIG_PARTITION_TYPE_GUID
357 info->type_guid[0] = 0;
358#endif
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000359
Simon Glass83ce5632016-02-29 15:25:47 -0700360 drv = part_driver_lookup_type(dev_desc->part_type);
361 if (!drv) {
362 debug("## Unknown partition table type %x\n",
363 dev_desc->part_type);
364 return -EPROTONOSUPPORT;
365 }
366 if (!drv->get_info) {
Nishanth Menonef1851a2016-03-15 16:15:32 -0500367 PRINTF("## Driver %s does not have the get_info() method\n",
368 drv->name);
Simon Glass83ce5632016-02-29 15:25:47 -0700369 return -ENOSYS;
370 }
371 if (drv->get_info(dev_desc, part, info) == 0) {
372 PRINTF("## Valid %s partition found ##\n", drv->name);
373 return 0;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000374 }
Gabe Black0fc71b52012-10-12 14:26:08 +0000375#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000376
377 return -1;
378}
Rob Herringd586cec2012-09-21 04:08:17 +0000379
Simon Glasse6649a62016-02-29 15:25:43 -0700380int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
381 struct blk_desc **dev_desc)
Stephen Warrenafd909c2012-09-21 09:50:56 +0000382{
383 char *ep;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600384 char *dup_str = NULL;
385 const char *dev_str, *hwpart_str;
386 int dev, hwpart;
387
388 hwpart_str = strchr(dev_hwpart_str, '.');
389 if (hwpart_str) {
390 dup_str = strdup(dev_hwpart_str);
391 dup_str[hwpart_str - dev_hwpart_str] = 0;
392 dev_str = dup_str;
393 hwpart_str++;
394 } else {
395 dev_str = dev_hwpart_str;
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600396 hwpart = 0;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600397 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000398
399 dev = simple_strtoul(dev_str, &ep, 16);
400 if (*ep) {
401 printf("** Bad device specification %s %s **\n",
402 ifname, dev_str);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600403 dev = -1;
404 goto cleanup;
405 }
406
407 if (hwpart_str) {
408 hwpart = simple_strtoul(hwpart_str, &ep, 16);
409 if (*ep) {
410 printf("** Bad HW partition specification %s %s **\n",
411 ifname, hwpart_str);
412 dev = -1;
413 goto cleanup;
414 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000415 }
416
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600417 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000418 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600419 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
420 dev = -1;
421 goto cleanup;
Stephen Warrenafd909c2012-09-21 09:50:56 +0000422 }
423
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000424#ifdef HAVE_BLOCK_DEVICE
425 /*
426 * Updates the partition table for the specified hw partition.
427 * Does not need to be done for hwpart 0 since it is default and
428 * already loaded.
429 */
430 if(hwpart != 0)
Simon Glassb89a8442016-02-29 15:25:48 -0700431 part_init(*dev_desc);
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000432#endif
433
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600434cleanup:
435 free(dup_str);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000436 return dev;
437}
438
Stephen Warrenc87ff222012-09-21 09:50:57 +0000439#define PART_UNSPECIFIED -2
440#define PART_AUTO -1
441#define MAX_SEARCH_PARTITIONS 16
Simon Glasse76ee972016-02-29 15:25:44 -0700442int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glasse3394752016-02-29 15:25:34 -0700443 struct blk_desc **dev_desc,
Stephen Warrenc87ff222012-09-21 09:50:57 +0000444 disk_partition_t *info, int allow_whole_dev)
Rob Herringd586cec2012-09-21 04:08:17 +0000445{
Stephen Warrenc87ff222012-09-21 09:50:57 +0000446 int ret = -1;
447 const char *part_str;
448 char *dup_str = NULL;
449 const char *dev_str;
Rob Herringd586cec2012-09-21 04:08:17 +0000450 int dev;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000451 char *ep;
452 int p;
453 int part;
454 disk_partition_t tmpinfo;
455
Hans de Goede4763ee32015-09-17 18:46:55 -0400456#ifdef CONFIG_SANDBOX
Stephen Warrenbff6c312014-06-12 10:28:32 -0600457 /*
Pavel Machek13334e72014-07-19 23:50:44 +0200458 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warrenbff6c312014-06-12 10:28:32 -0600459 * host's own filesystem.
460 */
461 if (0 == strcmp(ifname, "hostfs")) {
462 *dev_desc = NULL;
463 info->start = 0;
464 info->size = 0;
465 info->blksz = 0;
466 info->bootable = 0;
467 strcpy((char *)info->type, BOOT_PART_TYPE);
468 strcpy((char *)info->name, "Sandbox host");
469#ifdef CONFIG_PARTITION_UUIDS
470 info->uuid[0] = 0;
471#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100472#ifdef CONFIG_PARTITION_TYPE_GUID
473 info->type_guid[0] = 0;
474#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600475
Hans de Goede690c7962015-09-17 18:46:58 -0400476 return 0;
477 }
478#endif
479
480#ifdef CONFIG_CMD_UBIFS
481 /*
482 * Special-case ubi, ubi goes through a mtd, rathen then through
483 * a regular block device.
484 */
485 if (0 == strcmp(ifname, "ubi")) {
486 if (!ubifs_is_mounted()) {
487 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
488 return -1;
489 }
490
491 *dev_desc = NULL;
492 memset(info, 0, sizeof(*info));
493 strcpy((char *)info->type, BOOT_PART_TYPE);
494 strcpy((char *)info->name, "UBI");
495#ifdef CONFIG_PARTITION_UUIDS
496 info->uuid[0] = 0;
497#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600498 return 0;
499 }
Hans de Goede4763ee32015-09-17 18:46:55 -0400500#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600501
Stephen Warrenc87ff222012-09-21 09:50:57 +0000502 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena95af942012-09-28 05:34:09 +0000503 if (!dev_part_str || !strlen(dev_part_str) ||
504 !strcmp(dev_part_str, "-"))
Stephen Warrenc87ff222012-09-21 09:50:57 +0000505 dev_part_str = getenv("bootdevice");
506
507 /* If still no dev_part_str, it's an error */
508 if (!dev_part_str) {
509 printf("** No device specified **\n");
510 goto cleanup;
511 }
512
513 /* Separate device and partition ID specification */
514 part_str = strchr(dev_part_str, ':');
515 if (part_str) {
516 dup_str = strdup(dev_part_str);
517 dup_str[part_str - dev_part_str] = 0;
518 dev_str = dup_str;
519 part_str++;
520 } else {
521 dev_str = dev_part_str;
522 }
Rob Herringd586cec2012-09-21 04:08:17 +0000523
Stephen Warrenc87ff222012-09-21 09:50:57 +0000524 /* Look up the device */
Simon Glasse6649a62016-02-29 15:25:43 -0700525 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000526 if (dev < 0)
527 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000528
Stephen Warrenc87ff222012-09-21 09:50:57 +0000529 /* Convert partition ID string to number */
530 if (!part_str || !*part_str) {
531 part = PART_UNSPECIFIED;
532 } else if (!strcmp(part_str, "auto")) {
533 part = PART_AUTO;
534 } else {
535 /* Something specified -> use exactly that */
536 part = (int)simple_strtoul(part_str, &ep, 16);
537 /*
538 * Less than whole string converted,
539 * or request for whole device, but caller requires partition.
540 */
541 if (*ep || (part == 0 && !allow_whole_dev)) {
542 printf("** Bad partition specification %s %s **\n",
543 ifname, dev_part_str);
544 goto cleanup;
545 }
Rob Herringd586cec2012-09-21 04:08:17 +0000546 }
547
Stephen Warrenc87ff222012-09-21 09:50:57 +0000548 /*
549 * No partition table on device,
550 * or user requested partition 0 (entire device).
551 */
552 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
553 (part == 0)) {
554 if (!(*dev_desc)->lba) {
555 printf("** Bad device size - %s %s **\n", ifname,
556 dev_str);
557 goto cleanup;
558 }
Rob Herringd586cec2012-09-21 04:08:17 +0000559
Stephen Warrenc87ff222012-09-21 09:50:57 +0000560 /*
561 * If user specified a partition ID other than 0,
562 * or the calling command only accepts partitions,
563 * it's an error.
564 */
565 if ((part > 0) || (!allow_whole_dev)) {
566 printf("** No partition table - %s %s **\n", ifname,
567 dev_str);
568 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000569 }
Stephen Warrenc87ff222012-09-21 09:50:57 +0000570
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800571 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
572
Rob Herringd586cec2012-09-21 04:08:17 +0000573 info->start = 0;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000574 info->size = (*dev_desc)->lba;
575 info->blksz = (*dev_desc)->blksz;
576 info->bootable = 0;
Stephen Warren65be8492012-10-10 07:57:51 +0000577 strcpy((char *)info->type, BOOT_PART_TYPE);
578 strcpy((char *)info->name, "Whole Disk");
Stephen Warrenc87ff222012-09-21 09:50:57 +0000579#ifdef CONFIG_PARTITION_UUIDS
580 info->uuid[0] = 0;
581#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100582#ifdef CONFIG_PARTITION_TYPE_GUID
583 info->type_guid[0] = 0;
584#endif
Rob Herringd586cec2012-09-21 04:08:17 +0000585
Stephen Warrenc87ff222012-09-21 09:50:57 +0000586 ret = 0;
587 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000588 }
589
Stephen Warrenc87ff222012-09-21 09:50:57 +0000590 /*
591 * Now there's known to be a partition table,
592 * not specifying a partition means to pick partition 1.
593 */
594 if (part == PART_UNSPECIFIED)
595 part = 1;
Rob Herringd586cec2012-09-21 04:08:17 +0000596
Stephen Warrenc87ff222012-09-21 09:50:57 +0000597 /*
598 * If user didn't specify a partition number, or did specify something
599 * other than "auto", use that partition number directly.
600 */
601 if (part != PART_AUTO) {
Simon Glassb89a8442016-02-29 15:25:48 -0700602 ret = part_get_info(*dev_desc, part, info);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000603 if (ret) {
604 printf("** Invalid partition %d **\n", part);
605 goto cleanup;
606 }
607 } else {
608 /*
609 * Find the first bootable partition.
610 * If none are bootable, fall back to the first valid partition.
611 */
612 part = 0;
613 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Simon Glassb89a8442016-02-29 15:25:48 -0700614 ret = part_get_info(*dev_desc, p, info);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000615 if (ret)
616 continue;
617
618 /*
619 * First valid partition, or new better partition?
620 * If so, save partition ID.
621 */
622 if (!part || info->bootable)
623 part = p;
624
625 /* Best possible partition? Stop searching. */
626 if (info->bootable)
627 break;
628
629 /*
630 * We now need to search further for best possible.
631 * If we what we just queried was the best so far,
632 * save the info since we over-write it next loop.
633 */
634 if (part == p)
635 tmpinfo = *info;
636 }
637 /* If we found any acceptable partition */
638 if (part) {
639 /*
640 * If we searched all possible partition IDs,
641 * return the first valid partition we found.
642 */
643 if (p == MAX_SEARCH_PARTITIONS + 1)
644 *info = tmpinfo;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000645 } else {
646 printf("** No valid partitions found **\n");
Stephen Warren3c28be92012-10-08 07:45:54 +0000647 ret = -1;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000648 goto cleanup;
649 }
Rob Herringd586cec2012-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);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000655 ret = -1;
656 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000657 }
658
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800659 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
660
Stephen Warrenc87ff222012-09-21 09:50:57 +0000661 ret = part;
662 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000663
Stephen Warrenc87ff222012-09-21 09:50:57 +0000664cleanup:
665 free(dup_str);
666 return ret;
Rob Herringd586cec2012-09-21 04:08:17 +0000667}