blob: 67d98fe8443464800bf3cc8d5ceb4639983c6925 [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)
Grant Likelyffc2dd72007-02-20 09:04:34 +010026 { .name = "ide", .get_dev = ide_get_dev, },
27#endif
Dave Liu76596ba2008-03-26 22:49:44 +080028#if defined(CONFIG_CMD_SATA)
29 {.name = "sata", .get_dev = sata_get_dev, },
30#endif
Jon Loeligerf7f54a82007-07-09 17:22:37 -050031#if defined(CONFIG_CMD_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)
Grant Likelyffc2dd72007-02-20 09:04:34 +010035 { .name = "usb", .get_dev = usb_stor_get_dev, },
36#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;
Simon Glasse3394752016-02-29 15:25:34 -070075 struct blk_desc* (*reloc_get_dev)(int dev);
Stephen Warrenaeca52f2014-05-07 12:19:01 -060076 int (*select_hwpart)(int dev_num, int hwpart);
Heiko Schocher9b987232010-09-17 13:10:36 +020077 char *name;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060078 int ret;
Grant Likelyffc2dd72007-02-20 09:04:34 +010079
Tim Kientzle47e444c2012-03-27 11:43:25 +020080 if (!ifname)
81 return NULL;
82
Heiko Schocher9b987232010-09-17 13:10:36 +020083 name = drvr->name;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020084#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020085 name += gd->reloc_off;
86#endif
Lei Wen2a010672011-02-15 16:56:40 +080087 while (drvr->name) {
Heiko Schocher9b987232010-09-17 13:10:36 +020088 name = drvr->name;
Peter Tyser9057cbf2009-09-21 11:20:36 -050089 reloc_get_dev = drvr->get_dev;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060090 select_hwpart = drvr->select_hwpart;
Wolfgang Denkd0813e52010-10-28 20:00:11 +020091#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher9b987232010-09-17 13:10:36 +020092 name += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -050093 reloc_get_dev += gd->reloc_off;
Stephen Warrenaeca52f2014-05-07 12:19:01 -060094 if (select_hwpart)
95 select_hwpart += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -050096#endif
Stephen Warrenaeca52f2014-05-07 12:19:01 -060097 if (strncmp(ifname, name, strlen(name)) == 0) {
Simon Glasse3394752016-02-29 15:25:34 -070098 struct blk_desc *dev_desc = reloc_get_dev(dev);
Stephen Warrenaeca52f2014-05-07 12:19:01 -060099 if (!dev_desc)
100 return NULL;
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600101 if (hwpart == 0 && !select_hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600102 return dev_desc;
103 if (!select_hwpart)
104 return NULL;
Simon Glass2f26fff2016-02-29 15:25:51 -0700105 ret = select_hwpart(dev_desc->devnum, hwpart);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600106 if (ret < 0)
107 return NULL;
108 return dev_desc;
109 }
Grant Likelyffc2dd72007-02-20 09:04:34 +0100110 drvr++;
111 }
112 return NULL;
113}
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600114
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700115struct blk_desc *blk_get_dev(const char *ifname, int dev)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600116{
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600117 return get_dev_hwpart(ifname, dev, 0);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600118}
Grant Likelyffc2dd72007-02-20 09:04:34 +0100119#else
Simon Glasse3394752016-02-29 15:25:34 -0700120struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600121{
122 return NULL;
123}
124
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700125struct blk_desc *blk_get_dev(const char *ifname, int dev)
Grant Likelyffc2dd72007-02-20 09:04:34 +0100126{
127 return NULL;
128}
129#endif
130
Gabe Black0fc71b52012-10-12 14:26:08 +0000131#ifdef HAVE_BLOCK_DEVICE
Grant Likelyffc2dd72007-02-20 09:04:34 +0100132
wdenkaffae2b2002-08-17 09:36:01 +0000133/* ------------------------------------------------------------------------- */
134/*
135 * reports device info to the user
136 */
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300137
wdenkc35ba4e2004-03-14 22:25:36 +0000138#ifdef CONFIG_LBA48
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300139typedef uint64_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000140#else
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300141typedef lbaint_t lba512_t;
wdenkf602aa02004-03-13 23:29:43 +0000142#endif
wdenkaffae2b2002-08-17 09:36:01 +0000143
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300144/*
145 * Overflowless variant of (block_count * mul_by / div_by)
146 * when div_by > mul_by
147 */
Pavel Macheka4264e12014-09-09 15:19:42 +0200148static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300149{
150 lba512_t bc_quot, bc_rem;
151
152 /* x * m / d == x / d * m + (x % d) * m / d */
153 bc_quot = block_count / div_by;
154 bc_rem = block_count - div_by * bc_quot;
155 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
156}
157
Simon Glasse3394752016-02-29 15:25:34 -0700158void dev_print (struct blk_desc *dev_desc)
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300159{
160 lba512_t lba512; /* number of blocks if 512bytes block size */
161
Wolfgang Denk8561e712009-05-15 09:27:58 +0200162 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
163 puts ("not available\n");
164 return;
165 }
166
Tor Krill03ff3302008-05-29 11:10:30 +0200167 switch (dev_desc->if_type) {
Detlev Zundelbec6b322008-05-05 16:11:21 +0200168 case IF_TYPE_SCSI:
169 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
170 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000171 dev_desc->vendor,
Detlev Zundelbec6b322008-05-05 16:11:21 +0200172 dev_desc->product,
173 dev_desc->revision);
174 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200175 case IF_TYPE_ATAPI:
Detlev Zundelbec6b322008-05-05 16:11:21 +0200176 case IF_TYPE_IDE:
177 case IF_TYPE_SATA:
Dave Liu76596ba2008-03-26 22:49:44 +0800178 printf ("Model: %s Firm: %s Ser#: %s\n",
179 dev_desc->vendor,
180 dev_desc->revision,
181 dev_desc->product);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200182 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200183 case IF_TYPE_SD:
184 case IF_TYPE_MMC:
Nícolas Carneiro Lebedencoef3edd42008-09-04 15:35:46 -0300185 case IF_TYPE_USB:
186 printf ("Vendor: %s Rev: %s Prod: %s\n",
187 dev_desc->vendor,
188 dev_desc->revision,
189 dev_desc->product);
190 break;
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200191 case IF_TYPE_DOC:
192 puts("device type DOC\n");
193 return;
Tor Krill03ff3302008-05-29 11:10:30 +0200194 case IF_TYPE_UNKNOWN:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200195 puts("device type unknown\n");
196 return;
Detlev Zundelbec6b322008-05-05 16:11:21 +0200197 default:
Remy Bohmerf9f718b2008-09-19 13:30:06 +0200198 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundelbec6b322008-05-05 16:11:21 +0200199 return;
wdenkaffae2b2002-08-17 09:36:01 +0000200 }
201 puts (" Type: ");
202 if (dev_desc->removable)
203 puts ("Removable ");
204 switch (dev_desc->type & 0x1F) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200205 case DEV_TYPE_HARDDISK:
206 puts ("Hard Disk");
207 break;
208 case DEV_TYPE_CDROM:
209 puts ("CD ROM");
210 break;
211 case DEV_TYPE_OPDISK:
212 puts ("Optical Device");
213 break;
214 case DEV_TYPE_TAPE:
215 puts ("Tape");
216 break;
217 default:
218 printf ("# %02X #", dev_desc->type & 0x1F);
219 break;
wdenkaffae2b2002-08-17 09:36:01 +0000220 }
221 puts ("\n");
Jerry Huang6a4feb42012-11-06 15:33:12 +0000222 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000223 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkf602aa02004-03-13 23:29:43 +0000224 lbaint_t lba;
wdenk05939202004-04-18 17:39:38 +0000225
226 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000227
wdenkf602aa02004-03-13 23:29:43 +0000228 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000229 /* round to 1 digit */
Pavel Macheka4264e12014-09-09 15:19:42 +0200230 /* 2048 = (1024 * 1024) / 512 MB */
231 mb = lba512_muldiv(lba512, 10, 2048);
Sergei Trofimovich89d935b2010-08-08 15:05:39 +0300232
wdenkaffae2b2002-08-17 09:36:01 +0000233 mb_quot = mb / 10;
234 mb_rem = mb - (10 * mb_quot);
235
236 gb = mb / 1024;
237 gb_quot = gb / 10;
238 gb_rem = gb - (10 * gb_quot);
wdenkc35ba4e2004-03-14 22:25:36 +0000239#ifdef CONFIG_LBA48
wdenk05939202004-04-18 17:39:38 +0000240 if (dev_desc->lba48)
wdenkf602aa02004-03-13 23:29:43 +0000241 printf (" Supports 48-bit addressing\n");
242#endif
Heiko Schocher0f602e12009-12-03 11:21:21 +0100243#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500244 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
wdenkf602aa02004-03-13 23:29:43 +0000245 mb_quot, mb_rem,
246 gb_quot, gb_rem,
247 lba,
248 dev_desc->blksz);
249#else
wdenkaffae2b2002-08-17 09:36:01 +0000250 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
251 mb_quot, mb_rem,
252 gb_quot, gb_rem,
wdenkf602aa02004-03-13 23:29:43 +0000253 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000254 dev_desc->blksz);
wdenkf602aa02004-03-13 23:29:43 +0000255#endif
wdenkaffae2b2002-08-17 09:36:01 +0000256 } else {
257 puts (" Capacity: not available\n");
258 }
259}
Jon Loeliger1670a5b2007-07-10 11:19:50 -0500260#endif
wdenkaffae2b2002-08-17 09:36:01 +0000261
Gabe Black0fc71b52012-10-12 14:26:08 +0000262#ifdef HAVE_BLOCK_DEVICE
wdenkaffae2b2002-08-17 09:36:01 +0000263
Simon Glassb89a8442016-02-29 15:25:48 -0700264void part_init(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000265{
Simon Glass83ce5632016-02-29 15:25:47 -0700266 struct part_driver *drv =
267 ll_entry_start(struct part_driver, part_driver);
268 const int n_ents = ll_entry_count(struct part_driver, part_driver);
269 struct part_driver *entry;
richardretanubune6745592008-09-26 11:13:22 -0400270
Simon Glass83ce5632016-02-29 15:25:47 -0700271 dev_desc->part_type = PART_TYPE_UNKNOWN;
272 for (entry = drv; entry != drv + n_ents; entry++) {
273 int ret;
wdenk452cfd62002-11-19 11:04:11 +0000274
Simon Glass83ce5632016-02-29 15:25:47 -0700275 ret = entry->test(dev_desc);
276 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
277 if (!ret) {
278 dev_desc->part_type = entry->part_type;
279 break;
280 }
wdenk452cfd62002-11-19 11:04:11 +0000281 }
wdenkaffae2b2002-08-17 09:36:01 +0000282}
283
Simon Glass83ce5632016-02-29 15:25:47 -0700284static void print_part_header(const char *type, struct blk_desc *dev_desc)
285{
Gabe Black0fc71b52012-10-12 14:26:08 +0000286#if defined(CONFIG_MAC_PARTITION) || \
287 defined(CONFIG_DOS_PARTITION) || \
288 defined(CONFIG_ISO_PARTITION) || \
289 defined(CONFIG_AMIGA_PARTITION) || \
290 defined(CONFIG_EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000291 puts ("\nPartition Map for ");
292 switch (dev_desc->if_type) {
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200293 case IF_TYPE_IDE:
294 puts ("IDE");
295 break;
296 case IF_TYPE_SATA:
297 puts ("SATA");
298 break;
299 case IF_TYPE_SCSI:
300 puts ("SCSI");
301 break;
302 case IF_TYPE_ATAPI:
303 puts ("ATAPI");
304 break;
305 case IF_TYPE_USB:
306 puts ("USB");
307 break;
308 case IF_TYPE_DOC:
309 puts ("DOC");
310 break;
Lei Wen61dba932010-09-13 22:07:28 +0800311 case IF_TYPE_MMC:
312 puts ("MMC");
313 break;
Henrik Nordström26f9a6c2013-11-10 10:26:56 -0700314 case IF_TYPE_HOST:
315 puts("HOST");
316 break;
Detlev Zundel0e11cc62008-05-05 16:11:22 +0200317 default:
318 puts ("UNKNOWN");
319 break;
wdenkaffae2b2002-08-17 09:36:01 +0000320 }
321 printf (" device %d -- Partition Type: %s\n\n",
Simon Glass2f26fff2016-02-29 15:25:51 -0700322 dev_desc->devnum, type);
Gabe Black0fc71b52012-10-12 14:26:08 +0000323#endif /* any CONFIG_..._PARTITION */
Simon Glass83ce5632016-02-29 15:25:47 -0700324}
Gabe Black0fc71b52012-10-12 14:26:08 +0000325
Simon Glassb89a8442016-02-29 15:25:48 -0700326void part_print(struct blk_desc *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000327{
Simon Glass83ce5632016-02-29 15:25:47 -0700328 struct part_driver *drv;
wdenkaffae2b2002-08-17 09:36:01 +0000329
Simon Glass83ce5632016-02-29 15:25:47 -0700330 drv = part_driver_lookup_type(dev_desc->part_type);
331 if (!drv) {
332 printf("## Unknown partition table type %x\n",
333 dev_desc->part_type);
wdenkaffae2b2002-08-17 09:36:01 +0000334 return;
wdenkaffae2b2002-08-17 09:36:01 +0000335 }
Simon Glass83ce5632016-02-29 15:25:47 -0700336
337 PRINTF("## Testing for valid %s partition ##\n", drv->name);
338 print_part_header(drv->name, dev_desc);
339 if (drv->print)
340 drv->print(dev_desc);
wdenkaffae2b2002-08-17 09:36:01 +0000341}
342
Gabe Black0fc71b52012-10-12 14:26:08 +0000343#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000344
Simon Glassb89a8442016-02-29 15:25:48 -0700345int part_get_info(struct blk_desc *dev_desc, int part,
Pavel Machek13334e72014-07-19 23:50:44 +0200346 disk_partition_t *info)
Stephen Warren07c5bff2012-09-21 12:46:54 +0000347{
Gabe Black0fc71b52012-10-12 14:26:08 +0000348#ifdef HAVE_BLOCK_DEVICE
Simon Glass83ce5632016-02-29 15:25:47 -0700349 struct part_driver *drv;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000350
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000351#ifdef CONFIG_PARTITION_UUIDS
352 /* The common case is no UUID support */
353 info->uuid[0] = 0;
354#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100355#ifdef CONFIG_PARTITION_TYPE_GUID
356 info->type_guid[0] = 0;
357#endif
Stephen Warrenc6c7b7a2012-09-21 09:50:59 +0000358
Simon Glass83ce5632016-02-29 15:25:47 -0700359 drv = part_driver_lookup_type(dev_desc->part_type);
360 if (!drv) {
361 debug("## Unknown partition table type %x\n",
362 dev_desc->part_type);
363 return -EPROTONOSUPPORT;
364 }
365 if (!drv->get_info) {
366 PRINTF("## Driver %s does not have the get_info() method\n");
367 return -ENOSYS;
368 }
369 if (drv->get_info(dev_desc, part, info) == 0) {
370 PRINTF("## Valid %s partition found ##\n", drv->name);
371 return 0;
Stephen Warren07c5bff2012-09-21 12:46:54 +0000372 }
Gabe Black0fc71b52012-10-12 14:26:08 +0000373#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren07c5bff2012-09-21 12:46:54 +0000374
375 return -1;
376}
Rob Herringd586cec2012-09-21 04:08:17 +0000377
Simon Glasse6649a62016-02-29 15:25:43 -0700378int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
379 struct blk_desc **dev_desc)
Stephen Warrenafd909c2012-09-21 09:50:56 +0000380{
381 char *ep;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600382 char *dup_str = NULL;
383 const char *dev_str, *hwpart_str;
384 int dev, hwpart;
385
386 hwpart_str = strchr(dev_hwpart_str, '.');
387 if (hwpart_str) {
388 dup_str = strdup(dev_hwpart_str);
389 dup_str[hwpart_str - dev_hwpart_str] = 0;
390 dev_str = dup_str;
391 hwpart_str++;
392 } else {
393 dev_str = dev_hwpart_str;
Stephen Warrenad17c0f2014-05-23 12:48:11 -0600394 hwpart = 0;
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600395 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000396
397 dev = simple_strtoul(dev_str, &ep, 16);
398 if (*ep) {
399 printf("** Bad device specification %s %s **\n",
400 ifname, dev_str);
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600401 dev = -1;
402 goto cleanup;
403 }
404
405 if (hwpart_str) {
406 hwpart = simple_strtoul(hwpart_str, &ep, 16);
407 if (*ep) {
408 printf("** Bad HW partition specification %s %s **\n",
409 ifname, hwpart_str);
410 dev = -1;
411 goto cleanup;
412 }
Stephen Warrenafd909c2012-09-21 09:50:56 +0000413 }
414
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600415 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000416 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600417 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
418 dev = -1;
419 goto cleanup;
Stephen Warrenafd909c2012-09-21 09:50:56 +0000420 }
421
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000422#ifdef HAVE_BLOCK_DEVICE
423 /*
424 * Updates the partition table for the specified hw partition.
425 * Does not need to be done for hwpart 0 since it is default and
426 * already loaded.
427 */
428 if(hwpart != 0)
Simon Glassb89a8442016-02-29 15:25:48 -0700429 part_init(*dev_desc);
Erik Tideman8d11b8e2016-01-11 13:39:07 +0000430#endif
431
Stephen Warrenaeca52f2014-05-07 12:19:01 -0600432cleanup:
433 free(dup_str);
Stephen Warrenafd909c2012-09-21 09:50:56 +0000434 return dev;
435}
436
Stephen Warrenc87ff222012-09-21 09:50:57 +0000437#define PART_UNSPECIFIED -2
438#define PART_AUTO -1
439#define MAX_SEARCH_PARTITIONS 16
Simon Glasse76ee972016-02-29 15:25:44 -0700440int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
Simon Glasse3394752016-02-29 15:25:34 -0700441 struct blk_desc **dev_desc,
Stephen Warrenc87ff222012-09-21 09:50:57 +0000442 disk_partition_t *info, int allow_whole_dev)
Rob Herringd586cec2012-09-21 04:08:17 +0000443{
Stephen Warrenc87ff222012-09-21 09:50:57 +0000444 int ret = -1;
445 const char *part_str;
446 char *dup_str = NULL;
447 const char *dev_str;
Rob Herringd586cec2012-09-21 04:08:17 +0000448 int dev;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000449 char *ep;
450 int p;
451 int part;
452 disk_partition_t tmpinfo;
453
Hans de Goede4763ee32015-09-17 18:46:55 -0400454#ifdef CONFIG_SANDBOX
Stephen Warrenbff6c312014-06-12 10:28:32 -0600455 /*
Pavel Machek13334e72014-07-19 23:50:44 +0200456 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warrenbff6c312014-06-12 10:28:32 -0600457 * host's own filesystem.
458 */
459 if (0 == strcmp(ifname, "hostfs")) {
460 *dev_desc = NULL;
461 info->start = 0;
462 info->size = 0;
463 info->blksz = 0;
464 info->bootable = 0;
465 strcpy((char *)info->type, BOOT_PART_TYPE);
466 strcpy((char *)info->name, "Sandbox host");
467#ifdef CONFIG_PARTITION_UUIDS
468 info->uuid[0] = 0;
469#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100470#ifdef CONFIG_PARTITION_TYPE_GUID
471 info->type_guid[0] = 0;
472#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600473
Hans de Goede690c7962015-09-17 18:46:58 -0400474 return 0;
475 }
476#endif
477
478#ifdef CONFIG_CMD_UBIFS
479 /*
480 * Special-case ubi, ubi goes through a mtd, rathen then through
481 * a regular block device.
482 */
483 if (0 == strcmp(ifname, "ubi")) {
484 if (!ubifs_is_mounted()) {
485 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
486 return -1;
487 }
488
489 *dev_desc = NULL;
490 memset(info, 0, sizeof(*info));
491 strcpy((char *)info->type, BOOT_PART_TYPE);
492 strcpy((char *)info->name, "UBI");
493#ifdef CONFIG_PARTITION_UUIDS
494 info->uuid[0] = 0;
495#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600496 return 0;
497 }
Hans de Goede4763ee32015-09-17 18:46:55 -0400498#endif
Stephen Warrenbff6c312014-06-12 10:28:32 -0600499
Stephen Warrenc87ff222012-09-21 09:50:57 +0000500 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena95af942012-09-28 05:34:09 +0000501 if (!dev_part_str || !strlen(dev_part_str) ||
502 !strcmp(dev_part_str, "-"))
Stephen Warrenc87ff222012-09-21 09:50:57 +0000503 dev_part_str = getenv("bootdevice");
504
505 /* If still no dev_part_str, it's an error */
506 if (!dev_part_str) {
507 printf("** No device specified **\n");
508 goto cleanup;
509 }
510
511 /* Separate device and partition ID specification */
512 part_str = strchr(dev_part_str, ':');
513 if (part_str) {
514 dup_str = strdup(dev_part_str);
515 dup_str[part_str - dev_part_str] = 0;
516 dev_str = dup_str;
517 part_str++;
518 } else {
519 dev_str = dev_part_str;
520 }
Rob Herringd586cec2012-09-21 04:08:17 +0000521
Stephen Warrenc87ff222012-09-21 09:50:57 +0000522 /* Look up the device */
Simon Glasse6649a62016-02-29 15:25:43 -0700523 dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000524 if (dev < 0)
525 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000526
Stephen Warrenc87ff222012-09-21 09:50:57 +0000527 /* Convert partition ID string to number */
528 if (!part_str || !*part_str) {
529 part = PART_UNSPECIFIED;
530 } else if (!strcmp(part_str, "auto")) {
531 part = PART_AUTO;
532 } else {
533 /* Something specified -> use exactly that */
534 part = (int)simple_strtoul(part_str, &ep, 16);
535 /*
536 * Less than whole string converted,
537 * or request for whole device, but caller requires partition.
538 */
539 if (*ep || (part == 0 && !allow_whole_dev)) {
540 printf("** Bad partition specification %s %s **\n",
541 ifname, dev_part_str);
542 goto cleanup;
543 }
Rob Herringd586cec2012-09-21 04:08:17 +0000544 }
545
Stephen Warrenc87ff222012-09-21 09:50:57 +0000546 /*
547 * No partition table on device,
548 * or user requested partition 0 (entire device).
549 */
550 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
551 (part == 0)) {
552 if (!(*dev_desc)->lba) {
553 printf("** Bad device size - %s %s **\n", ifname,
554 dev_str);
555 goto cleanup;
556 }
Rob Herringd586cec2012-09-21 04:08:17 +0000557
Stephen Warrenc87ff222012-09-21 09:50:57 +0000558 /*
559 * If user specified a partition ID other than 0,
560 * or the calling command only accepts partitions,
561 * it's an error.
562 */
563 if ((part > 0) || (!allow_whole_dev)) {
564 printf("** No partition table - %s %s **\n", ifname,
565 dev_str);
566 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000567 }
Stephen Warrenc87ff222012-09-21 09:50:57 +0000568
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800569 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
570
Rob Herringd586cec2012-09-21 04:08:17 +0000571 info->start = 0;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000572 info->size = (*dev_desc)->lba;
573 info->blksz = (*dev_desc)->blksz;
574 info->bootable = 0;
Stephen Warren65be8492012-10-10 07:57:51 +0000575 strcpy((char *)info->type, BOOT_PART_TYPE);
576 strcpy((char *)info->name, "Whole Disk");
Stephen Warrenc87ff222012-09-21 09:50:57 +0000577#ifdef CONFIG_PARTITION_UUIDS
578 info->uuid[0] = 0;
579#endif
Patrick Delaunay8248c8d2015-10-27 11:00:27 +0100580#ifdef CONFIG_PARTITION_TYPE_GUID
581 info->type_guid[0] = 0;
582#endif
Rob Herringd586cec2012-09-21 04:08:17 +0000583
Stephen Warrenc87ff222012-09-21 09:50:57 +0000584 ret = 0;
585 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000586 }
587
Stephen Warrenc87ff222012-09-21 09:50:57 +0000588 /*
589 * Now there's known to be a partition table,
590 * not specifying a partition means to pick partition 1.
591 */
592 if (part == PART_UNSPECIFIED)
593 part = 1;
Rob Herringd586cec2012-09-21 04:08:17 +0000594
Stephen Warrenc87ff222012-09-21 09:50:57 +0000595 /*
596 * If user didn't specify a partition number, or did specify something
597 * other than "auto", use that partition number directly.
598 */
599 if (part != PART_AUTO) {
Simon Glassb89a8442016-02-29 15:25:48 -0700600 ret = part_get_info(*dev_desc, part, info);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000601 if (ret) {
602 printf("** Invalid partition %d **\n", part);
603 goto cleanup;
604 }
605 } else {
606 /*
607 * Find the first bootable partition.
608 * If none are bootable, fall back to the first valid partition.
609 */
610 part = 0;
611 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
Simon Glassb89a8442016-02-29 15:25:48 -0700612 ret = part_get_info(*dev_desc, p, info);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000613 if (ret)
614 continue;
615
616 /*
617 * First valid partition, or new better partition?
618 * If so, save partition ID.
619 */
620 if (!part || info->bootable)
621 part = p;
622
623 /* Best possible partition? Stop searching. */
624 if (info->bootable)
625 break;
626
627 /*
628 * We now need to search further for best possible.
629 * If we what we just queried was the best so far,
630 * save the info since we over-write it next loop.
631 */
632 if (part == p)
633 tmpinfo = *info;
634 }
635 /* If we found any acceptable partition */
636 if (part) {
637 /*
638 * If we searched all possible partition IDs,
639 * return the first valid partition we found.
640 */
641 if (p == MAX_SEARCH_PARTITIONS + 1)
642 *info = tmpinfo;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000643 } else {
644 printf("** No valid partitions found **\n");
Stephen Warren3c28be92012-10-08 07:45:54 +0000645 ret = -1;
Stephen Warrenc87ff222012-09-21 09:50:57 +0000646 goto cleanup;
647 }
Rob Herringd586cec2012-09-21 04:08:17 +0000648 }
649 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
650 printf("** Invalid partition type \"%.32s\""
651 " (expect \"" BOOT_PART_TYPE "\")\n",
652 info->type);
Stephen Warrenc87ff222012-09-21 09:50:57 +0000653 ret = -1;
654 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000655 }
656
Lan Yixun (dlan)138914e2013-07-20 08:17:59 +0800657 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
658
Stephen Warrenc87ff222012-09-21 09:50:57 +0000659 ret = part;
660 goto cleanup;
Rob Herringd586cec2012-09-21 04:08:17 +0000661
Stephen Warrenc87ff222012-09-21 09:50:57 +0000662cleanup:
663 free(dup_str);
664 return ret;
Rob Herringd586cec2012-09-21 04:08:17 +0000665}