blob: 1935b28841011268cf6451594120a6e126f09d16 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 */
7
8#include <common.h>
9#include <command.h>
10#include <ide.h>
Stephen Warren10a37fd2012-09-21 09:50:57 +000011#include <malloc.h>
Grant Likely735dd972007-02-20 09:04:34 +010012#include <part.h>
Hans de Goede251cee02015-09-17 18:46:58 -040013#include <ubifs_uboot.h>
wdenkaffae2b2002-08-17 09:36:01 +000014
15#undef PART_DEBUG
16
17#ifdef PART_DEBUG
18#define PRINTF(fmt,args...) printf (fmt ,##args)
19#else
20#define PRINTF(fmt,args...)
21#endif
22
Grant Likely735dd972007-02-20 09:04:34 +010023struct block_drvr {
24 char *name;
25 block_dev_desc_t* (*get_dev)(int dev);
Stephen Warren336b6f92014-05-07 12:19:01 -060026 int (*select_hwpart)(int dev_num, int hwpart);
Grant Likely735dd972007-02-20 09:04:34 +010027};
28
29static const struct block_drvr block_drvr[] = {
Jon Loeligercde5c642007-07-09 17:22:37 -050030#if defined(CONFIG_CMD_IDE)
Grant Likely735dd972007-02-20 09:04:34 +010031 { .name = "ide", .get_dev = ide_get_dev, },
32#endif
Dave Liuc7057b52008-03-26 22:49:44 +080033#if defined(CONFIG_CMD_SATA)
34 {.name = "sata", .get_dev = sata_get_dev, },
35#endif
Jon Loeligercde5c642007-07-09 17:22:37 -050036#if defined(CONFIG_CMD_SCSI)
Grant Likely735dd972007-02-20 09:04:34 +010037 { .name = "scsi", .get_dev = scsi_get_dev, },
38#endif
Jon Loeligercde5c642007-07-09 17:22:37 -050039#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
Grant Likely735dd972007-02-20 09:04:34 +010040 { .name = "usb", .get_dev = usb_stor_get_dev, },
41#endif
42#if defined(CONFIG_MMC)
Stephen Warrend2356282014-05-07 12:19:02 -060043 {
44 .name = "mmc",
45 .get_dev = mmc_get_dev,
46 .select_hwpart = mmc_select_hwpart,
47 },
Grant Likely735dd972007-02-20 09:04:34 +010048#endif
49#if defined(CONFIG_SYSTEMACE)
50 { .name = "ace", .get_dev = systemace_get_dev, },
51#endif
Henrik Nordströmf4d8de42013-11-10 10:26:56 -070052#if defined(CONFIG_SANDBOX)
53 { .name = "host", .get_dev = host_get_dev, },
54#endif
Grant Likely735dd972007-02-20 09:04:34 +010055 { },
56};
57
Stefan Roese751bb572007-02-20 13:21:57 +010058DECLARE_GLOBAL_DATA_PTR;
Stefan Roese751bb572007-02-20 13:21:57 +010059
Gabe Black0c9c8fb2012-10-12 14:26:08 +000060#ifdef HAVE_BLOCK_DEVICE
Jeroen Hofstee76ee65b2014-10-08 22:57:33 +020061static block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
Grant Likely735dd972007-02-20 09:04:34 +010062{
63 const struct block_drvr *drvr = block_drvr;
Stefan Roese6c7cac82007-02-22 07:43:34 +010064 block_dev_desc_t* (*reloc_get_dev)(int dev);
Stephen Warren336b6f92014-05-07 12:19:01 -060065 int (*select_hwpart)(int dev_num, int hwpart);
Heiko Schocher23090da2010-09-17 13:10:36 +020066 char *name;
Stephen Warren336b6f92014-05-07 12:19:01 -060067 int ret;
Grant Likely735dd972007-02-20 09:04:34 +010068
Tim Kientzle7e71dc62012-03-27 11:43:25 +020069 if (!ifname)
70 return NULL;
71
Heiko Schocher23090da2010-09-17 13:10:36 +020072 name = drvr->name;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020073#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher23090da2010-09-17 13:10:36 +020074 name += gd->reloc_off;
75#endif
Lei Wenb16aadf2011-02-15 16:56:40 +080076 while (drvr->name) {
Heiko Schocher23090da2010-09-17 13:10:36 +020077 name = drvr->name;
Peter Tyser521af042009-09-21 11:20:36 -050078 reloc_get_dev = drvr->get_dev;
Stephen Warren336b6f92014-05-07 12:19:01 -060079 select_hwpart = drvr->select_hwpart;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020080#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher23090da2010-09-17 13:10:36 +020081 name += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -050082 reloc_get_dev += gd->reloc_off;
Stephen Warren336b6f92014-05-07 12:19:01 -060083 if (select_hwpart)
84 select_hwpart += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -050085#endif
Stephen Warren336b6f92014-05-07 12:19:01 -060086 if (strncmp(ifname, name, strlen(name)) == 0) {
87 block_dev_desc_t *dev_desc = reloc_get_dev(dev);
88 if (!dev_desc)
89 return NULL;
Stephen Warrenecdd57e2014-05-23 12:48:11 -060090 if (hwpart == 0 && !select_hwpart)
Stephen Warren336b6f92014-05-07 12:19:01 -060091 return dev_desc;
92 if (!select_hwpart)
93 return NULL;
94 ret = select_hwpart(dev_desc->dev, hwpart);
95 if (ret < 0)
96 return NULL;
97 return dev_desc;
98 }
Grant Likely735dd972007-02-20 09:04:34 +010099 drvr++;
100 }
101 return NULL;
102}
Stephen Warren336b6f92014-05-07 12:19:01 -0600103
104block_dev_desc_t *get_dev(const char *ifname, int dev)
105{
Stephen Warrenecdd57e2014-05-23 12:48:11 -0600106 return get_dev_hwpart(ifname, dev, 0);
Stephen Warren336b6f92014-05-07 12:19:01 -0600107}
Grant Likely735dd972007-02-20 09:04:34 +0100108#else
Stephen Warren336b6f92014-05-07 12:19:01 -0600109block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
110{
111 return NULL;
112}
113
Rob Herring99d2c202012-09-21 04:08:17 +0000114block_dev_desc_t *get_dev(const char *ifname, int dev)
Grant Likely735dd972007-02-20 09:04:34 +0100115{
116 return NULL;
117}
118#endif
119
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000120#ifdef HAVE_BLOCK_DEVICE
Grant Likely735dd972007-02-20 09:04:34 +0100121
wdenkaffae2b2002-08-17 09:36:01 +0000122/* ------------------------------------------------------------------------- */
123/*
124 * reports device info to the user
125 */
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300126
127#ifdef CONFIG_LBA48
128typedef uint64_t lba512_t;
129#else
130typedef lbaint_t lba512_t;
131#endif
132
133/*
134 * Overflowless variant of (block_count * mul_by / div_by)
135 * when div_by > mul_by
136 */
Pavel Machek214b3f32014-09-09 15:19:42 +0200137static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300138{
139 lba512_t bc_quot, bc_rem;
140
141 /* x * m / d == x / d * m + (x % d) * m / d */
142 bc_quot = block_count / div_by;
143 bc_rem = block_count - div_by * bc_quot;
144 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
145}
146
wdenkaffae2b2002-08-17 09:36:01 +0000147void dev_print (block_dev_desc_t *dev_desc)
148{
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300149 lba512_t lba512; /* number of blocks if 512bytes block size */
wdenkaffae2b2002-08-17 09:36:01 +0000150
Wolfgang Denkaf75a452009-05-15 09:27:58 +0200151 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
152 puts ("not available\n");
153 return;
154 }
155
Tor Krill8ec6e332008-05-29 11:10:30 +0200156 switch (dev_desc->if_type) {
Detlev Zundel574b3192008-05-05 16:11:21 +0200157 case IF_TYPE_SCSI:
158 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
159 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000160 dev_desc->vendor,
161 dev_desc->product,
162 dev_desc->revision);
Detlev Zundel574b3192008-05-05 16:11:21 +0200163 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200164 case IF_TYPE_ATAPI:
Detlev Zundel574b3192008-05-05 16:11:21 +0200165 case IF_TYPE_IDE:
166 case IF_TYPE_SATA:
167 printf ("Model: %s Firm: %s Ser#: %s\n",
168 dev_desc->vendor,
169 dev_desc->revision,
170 dev_desc->product);
171 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200172 case IF_TYPE_SD:
173 case IF_TYPE_MMC:
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300174 case IF_TYPE_USB:
175 printf ("Vendor: %s Rev: %s Prod: %s\n",
176 dev_desc->vendor,
177 dev_desc->revision,
178 dev_desc->product);
179 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200180 case IF_TYPE_DOC:
181 puts("device type DOC\n");
182 return;
Tor Krill8ec6e332008-05-29 11:10:30 +0200183 case IF_TYPE_UNKNOWN:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200184 puts("device type unknown\n");
185 return;
Detlev Zundel574b3192008-05-05 16:11:21 +0200186 default:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200187 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundel574b3192008-05-05 16:11:21 +0200188 return;
wdenkaffae2b2002-08-17 09:36:01 +0000189 }
190 puts (" Type: ");
191 if (dev_desc->removable)
192 puts ("Removable ");
193 switch (dev_desc->type & 0x1F) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200194 case DEV_TYPE_HARDDISK:
195 puts ("Hard Disk");
196 break;
197 case DEV_TYPE_CDROM:
198 puts ("CD ROM");
199 break;
200 case DEV_TYPE_OPDISK:
201 puts ("Optical Device");
202 break;
203 case DEV_TYPE_TAPE:
204 puts ("Tape");
205 break;
206 default:
207 printf ("# %02X #", dev_desc->type & 0x1F);
208 break;
wdenkaffae2b2002-08-17 09:36:01 +0000209 }
210 puts ("\n");
Jerry Huang33699df2012-11-06 15:33:12 +0000211 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
wdenkaffae2b2002-08-17 09:36:01 +0000212 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkc40b2952004-03-13 23:29:43 +0000213 lbaint_t lba;
wdenk6e592382004-04-18 17:39:38 +0000214
215 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000216
wdenkc40b2952004-03-13 23:29:43 +0000217 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000218 /* round to 1 digit */
Pavel Machek214b3f32014-09-09 15:19:42 +0200219 /* 2048 = (1024 * 1024) / 512 MB */
220 mb = lba512_muldiv(lba512, 10, 2048);
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300221
wdenkaffae2b2002-08-17 09:36:01 +0000222 mb_quot = mb / 10;
223 mb_rem = mb - (10 * mb_quot);
224
225 gb = mb / 1024;
226 gb_quot = gb / 10;
227 gb_rem = gb - (10 * gb_quot);
wdenk42dfe7a2004-03-14 22:25:36 +0000228#ifdef CONFIG_LBA48
wdenk6e592382004-04-18 17:39:38 +0000229 if (dev_desc->lba48)
wdenkc40b2952004-03-13 23:29:43 +0000230 printf (" Supports 48-bit addressing\n");
231#endif
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100232#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500233 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
wdenkc40b2952004-03-13 23:29:43 +0000234 mb_quot, mb_rem,
235 gb_quot, gb_rem,
236 lba,
237 dev_desc->blksz);
238#else
wdenkaffae2b2002-08-17 09:36:01 +0000239 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
240 mb_quot, mb_rem,
241 gb_quot, gb_rem,
wdenkc40b2952004-03-13 23:29:43 +0000242 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000243 dev_desc->blksz);
wdenkc40b2952004-03-13 23:29:43 +0000244#endif
wdenkaffae2b2002-08-17 09:36:01 +0000245 } else {
246 puts (" Capacity: not available\n");
247 }
248}
Jon Loeligerb3aff0c2007-07-10 11:19:50 -0500249#endif
wdenkaffae2b2002-08-17 09:36:01 +0000250
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000251#ifdef HAVE_BLOCK_DEVICE
wdenkaffae2b2002-08-17 09:36:01 +0000252
Pavel Machek214b3f32014-09-09 15:19:42 +0200253void init_part(block_dev_desc_t *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000254{
255#ifdef CONFIG_ISO_PARTITION
256 if (test_part_iso(dev_desc) == 0) {
257 dev_desc->part_type = PART_TYPE_ISO;
258 return;
259 }
260#endif
261
262#ifdef CONFIG_MAC_PARTITION
263 if (test_part_mac(dev_desc) == 0) {
264 dev_desc->part_type = PART_TYPE_MAC;
265 return;
266 }
267#endif
268
richardretanubun07f3d782008-09-26 11:13:22 -0400269/* must be placed before DOS partition detection */
270#ifdef CONFIG_EFI_PARTITION
271 if (test_part_efi(dev_desc) == 0) {
272 dev_desc->part_type = PART_TYPE_EFI;
273 return;
274 }
275#endif
276
wdenkaffae2b2002-08-17 09:36:01 +0000277#ifdef CONFIG_DOS_PARTITION
278 if (test_part_dos(dev_desc) == 0) {
279 dev_desc->part_type = PART_TYPE_DOS;
280 return;
281 }
282#endif
wdenkc7de8292002-11-19 11:04:11 +0000283
284#ifdef CONFIG_AMIGA_PARTITION
285 if (test_part_amiga(dev_desc) == 0) {
286 dev_desc->part_type = PART_TYPE_AMIGA;
287 return;
288 }
289#endif
Rob Herring99d2c202012-09-21 04:08:17 +0000290 dev_desc->part_type = PART_TYPE_UNKNOWN;
wdenkaffae2b2002-08-17 09:36:01 +0000291}
292
293
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000294#if defined(CONFIG_MAC_PARTITION) || \
295 defined(CONFIG_DOS_PARTITION) || \
296 defined(CONFIG_ISO_PARTITION) || \
297 defined(CONFIG_AMIGA_PARTITION) || \
298 defined(CONFIG_EFI_PARTITION)
299
Pavel Machek214b3f32014-09-09 15:19:42 +0200300static void print_part_header(const char *type, block_dev_desc_t *dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000301{
302 puts ("\nPartition Map for ");
303 switch (dev_desc->if_type) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200304 case IF_TYPE_IDE:
305 puts ("IDE");
306 break;
307 case IF_TYPE_SATA:
308 puts ("SATA");
309 break;
310 case IF_TYPE_SCSI:
311 puts ("SCSI");
312 break;
313 case IF_TYPE_ATAPI:
314 puts ("ATAPI");
315 break;
316 case IF_TYPE_USB:
317 puts ("USB");
318 break;
319 case IF_TYPE_DOC:
320 puts ("DOC");
321 break;
Lei Wen8f3b9642010-09-13 22:07:28 +0800322 case IF_TYPE_MMC:
323 puts ("MMC");
324 break;
Henrik Nordströmf4d8de42013-11-10 10:26:56 -0700325 case IF_TYPE_HOST:
326 puts("HOST");
327 break;
Detlev Zundel726c0f12008-05-05 16:11:22 +0200328 default:
329 puts ("UNKNOWN");
330 break;
wdenkaffae2b2002-08-17 09:36:01 +0000331 }
332 printf (" device %d -- Partition Type: %s\n\n",
333 dev_desc->dev, type);
334}
335
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000336#endif /* any CONFIG_..._PARTITION */
337
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200338void print_part(block_dev_desc_t * dev_desc)
wdenkaffae2b2002-08-17 09:36:01 +0000339{
340
341 switch (dev_desc->part_type) {
342#ifdef CONFIG_MAC_PARTITION
343 case PART_TYPE_MAC:
344 PRINTF ("## Testing for valid MAC partition ##\n");
345 print_part_header ("MAC", dev_desc);
346 print_part_mac (dev_desc);
347 return;
348#endif
349#ifdef CONFIG_DOS_PARTITION
350 case PART_TYPE_DOS:
351 PRINTF ("## Testing for valid DOS partition ##\n");
352 print_part_header ("DOS", dev_desc);
353 print_part_dos (dev_desc);
354 return;
355#endif
356
357#ifdef CONFIG_ISO_PARTITION
358 case PART_TYPE_ISO:
359 PRINTF ("## Testing for valid ISO Boot partition ##\n");
360 print_part_header ("ISO", dev_desc);
361 print_part_iso (dev_desc);
362 return;
363#endif
wdenkc7de8292002-11-19 11:04:11 +0000364
365#ifdef CONFIG_AMIGA_PARTITION
366 case PART_TYPE_AMIGA:
367 PRINTF ("## Testing for a valid Amiga partition ##\n");
368 print_part_header ("AMIGA", dev_desc);
369 print_part_amiga (dev_desc);
370 return;
371#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400372
373#ifdef CONFIG_EFI_PARTITION
374 case PART_TYPE_EFI:
375 PRINTF ("## Testing for valid EFI partition ##\n");
376 print_part_header ("EFI", dev_desc);
377 print_part_efi (dev_desc);
378 return;
379#endif
wdenkaffae2b2002-08-17 09:36:01 +0000380 }
381 puts ("## Unknown partition table\n");
382}
383
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000384#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren2f501642012-09-21 12:46:54 +0000385
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200386int get_partition_info(block_dev_desc_t *dev_desc, int part,
387 disk_partition_t *info)
Stephen Warren2f501642012-09-21 12:46:54 +0000388{
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000389#ifdef HAVE_BLOCK_DEVICE
Stephen Warren2f501642012-09-21 12:46:54 +0000390
Stephen Warren894bfbb2012-09-21 09:50:59 +0000391#ifdef CONFIG_PARTITION_UUIDS
392 /* The common case is no UUID support */
393 info->uuid[0] = 0;
394#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100395#ifdef CONFIG_PARTITION_TYPE_GUID
396 info->type_guid[0] = 0;
397#endif
Stephen Warren894bfbb2012-09-21 09:50:59 +0000398
Stephen Warren2f501642012-09-21 12:46:54 +0000399 switch (dev_desc->part_type) {
400#ifdef CONFIG_MAC_PARTITION
401 case PART_TYPE_MAC:
402 if (get_partition_info_mac(dev_desc, part, info) == 0) {
403 PRINTF("## Valid MAC partition found ##\n");
404 return 0;
405 }
406 break;
407#endif
408
409#ifdef CONFIG_DOS_PARTITION
410 case PART_TYPE_DOS:
411 if (get_partition_info_dos(dev_desc, part, info) == 0) {
412 PRINTF("## Valid DOS partition found ##\n");
413 return 0;
414 }
415 break;
416#endif
417
418#ifdef CONFIG_ISO_PARTITION
419 case PART_TYPE_ISO:
420 if (get_partition_info_iso(dev_desc, part, info) == 0) {
421 PRINTF("## Valid ISO boot partition found ##\n");
422 return 0;
423 }
424 break;
425#endif
426
427#ifdef CONFIG_AMIGA_PARTITION
428 case PART_TYPE_AMIGA:
429 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
430 PRINTF("## Valid Amiga partition found ##\n");
431 return 0;
432 }
433 break;
434#endif
435
436#ifdef CONFIG_EFI_PARTITION
437 case PART_TYPE_EFI:
438 if (get_partition_info_efi(dev_desc, part, info) == 0) {
439 PRINTF("## Valid EFI partition found ##\n");
440 return 0;
441 }
442 break;
443#endif
444 default:
445 break;
446 }
Gabe Black0c9c8fb2012-10-12 14:26:08 +0000447#endif /* HAVE_BLOCK_DEVICE */
Stephen Warren2f501642012-09-21 12:46:54 +0000448
449 return -1;
450}
Rob Herring99d2c202012-09-21 04:08:17 +0000451
Stephen Warren336b6f92014-05-07 12:19:01 -0600452int get_device(const char *ifname, const char *dev_hwpart_str,
Stephen Warren2023e602012-09-21 09:50:56 +0000453 block_dev_desc_t **dev_desc)
454{
455 char *ep;
Stephen Warren336b6f92014-05-07 12:19:01 -0600456 char *dup_str = NULL;
457 const char *dev_str, *hwpart_str;
458 int dev, hwpart;
459
460 hwpart_str = strchr(dev_hwpart_str, '.');
461 if (hwpart_str) {
462 dup_str = strdup(dev_hwpart_str);
463 dup_str[hwpart_str - dev_hwpart_str] = 0;
464 dev_str = dup_str;
465 hwpart_str++;
466 } else {
467 dev_str = dev_hwpart_str;
Stephen Warrenecdd57e2014-05-23 12:48:11 -0600468 hwpart = 0;
Stephen Warren336b6f92014-05-07 12:19:01 -0600469 }
Stephen Warren2023e602012-09-21 09:50:56 +0000470
471 dev = simple_strtoul(dev_str, &ep, 16);
472 if (*ep) {
473 printf("** Bad device specification %s %s **\n",
474 ifname, dev_str);
Stephen Warren336b6f92014-05-07 12:19:01 -0600475 dev = -1;
476 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000477 }
478
Stephen Warren336b6f92014-05-07 12:19:01 -0600479 if (hwpart_str) {
480 hwpart = simple_strtoul(hwpart_str, &ep, 16);
481 if (*ep) {
482 printf("** Bad HW partition specification %s %s **\n",
483 ifname, hwpart_str);
484 dev = -1;
485 goto cleanup;
486 }
487 }
488
489 *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
Stephen Warren2023e602012-09-21 09:50:56 +0000490 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
Stephen Warren336b6f92014-05-07 12:19:01 -0600491 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
492 dev = -1;
493 goto cleanup;
Stephen Warren2023e602012-09-21 09:50:56 +0000494 }
495
Erik Tideman99e7fc82016-01-11 13:39:07 +0000496#ifdef HAVE_BLOCK_DEVICE
497 /*
498 * Updates the partition table for the specified hw partition.
499 * Does not need to be done for hwpart 0 since it is default and
500 * already loaded.
501 */
502 if(hwpart != 0)
503 init_part(*dev_desc);
504#endif
505
Stephen Warren336b6f92014-05-07 12:19:01 -0600506cleanup:
507 free(dup_str);
Stephen Warren2023e602012-09-21 09:50:56 +0000508 return dev;
509}
510
Stephen Warren10a37fd2012-09-21 09:50:57 +0000511#define PART_UNSPECIFIED -2
512#define PART_AUTO -1
513#define MAX_SEARCH_PARTITIONS 16
514int get_device_and_partition(const char *ifname, const char *dev_part_str,
Rob Herring99d2c202012-09-21 04:08:17 +0000515 block_dev_desc_t **dev_desc,
Stephen Warren10a37fd2012-09-21 09:50:57 +0000516 disk_partition_t *info, int allow_whole_dev)
Rob Herring99d2c202012-09-21 04:08:17 +0000517{
Stephen Warren10a37fd2012-09-21 09:50:57 +0000518 int ret = -1;
519 const char *part_str;
520 char *dup_str = NULL;
521 const char *dev_str;
Rob Herring99d2c202012-09-21 04:08:17 +0000522 int dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000523 char *ep;
524 int p;
525 int part;
526 disk_partition_t tmpinfo;
Rob Herring99d2c202012-09-21 04:08:17 +0000527
Hans de Goede251cee02015-09-17 18:46:58 -0400528#if defined CONFIG_SANDBOX && defined CONFIG_CMD_UBIFS
529#error Only one of CONFIG_SANDBOX and CONFIG_CMD_UBIFS may be selected
530#endif
531
Hans de Goedeafc17442015-09-17 18:46:55 -0400532#ifdef CONFIG_SANDBOX
Stephen Warren4d907022014-06-12 10:28:32 -0600533 /*
Pavel Machek3f9eb6e2014-07-19 23:50:44 +0200534 * Special-case a pseudo block device "hostfs", to allow access to the
Stephen Warren4d907022014-06-12 10:28:32 -0600535 * host's own filesystem.
536 */
537 if (0 == strcmp(ifname, "hostfs")) {
538 *dev_desc = NULL;
539 info->start = 0;
540 info->size = 0;
541 info->blksz = 0;
542 info->bootable = 0;
543 strcpy((char *)info->type, BOOT_PART_TYPE);
544 strcpy((char *)info->name, "Sandbox host");
545#ifdef CONFIG_PARTITION_UUIDS
546 info->uuid[0] = 0;
547#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100548#ifdef CONFIG_PARTITION_TYPE_GUID
549 info->type_guid[0] = 0;
550#endif
Stephen Warren4d907022014-06-12 10:28:32 -0600551
552 return 0;
553 }
Hans de Goedeafc17442015-09-17 18:46:55 -0400554#endif
Stephen Warren4d907022014-06-12 10:28:32 -0600555
Hans de Goede251cee02015-09-17 18:46:58 -0400556#ifdef CONFIG_CMD_UBIFS
557 /*
558 * Special-case ubi, ubi goes through a mtd, rathen then through
559 * a regular block device.
560 */
561 if (0 == strcmp(ifname, "ubi")) {
562 if (!ubifs_is_mounted()) {
563 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
564 return -1;
565 }
566
567 *dev_desc = NULL;
568 memset(info, 0, sizeof(*info));
569 strcpy((char *)info->type, BOOT_PART_TYPE);
570 strcpy((char *)info->name, "UBI");
571#ifdef CONFIG_PARTITION_UUIDS
572 info->uuid[0] = 0;
573#endif
574 return 0;
575 }
576#endif
577
Stephen Warren10a37fd2012-09-21 09:50:57 +0000578 /* If no dev_part_str, use bootdevice environment variable */
Stephen Warrena10973e2012-09-28 05:34:09 +0000579 if (!dev_part_str || !strlen(dev_part_str) ||
580 !strcmp(dev_part_str, "-"))
Stephen Warren10a37fd2012-09-21 09:50:57 +0000581 dev_part_str = getenv("bootdevice");
Rob Herring99d2c202012-09-21 04:08:17 +0000582
Stephen Warren10a37fd2012-09-21 09:50:57 +0000583 /* If still no dev_part_str, it's an error */
584 if (!dev_part_str) {
585 printf("** No device specified **\n");
586 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000587 }
588
Stephen Warren10a37fd2012-09-21 09:50:57 +0000589 /* Separate device and partition ID specification */
590 part_str = strchr(dev_part_str, ':');
591 if (part_str) {
592 dup_str = strdup(dev_part_str);
593 dup_str[part_str - dev_part_str] = 0;
594 dev_str = dup_str;
595 part_str++;
596 } else {
597 dev_str = dev_part_str;
598 }
Rob Herring99d2c202012-09-21 04:08:17 +0000599
Stephen Warren10a37fd2012-09-21 09:50:57 +0000600 /* Look up the device */
601 dev = get_device(ifname, dev_str, dev_desc);
602 if (dev < 0)
603 goto cleanup;
604
605 /* Convert partition ID string to number */
606 if (!part_str || !*part_str) {
607 part = PART_UNSPECIFIED;
608 } else if (!strcmp(part_str, "auto")) {
609 part = PART_AUTO;
610 } else {
611 /* Something specified -> use exactly that */
612 part = (int)simple_strtoul(part_str, &ep, 16);
613 /*
614 * Less than whole string converted,
615 * or request for whole device, but caller requires partition.
616 */
617 if (*ep || (part == 0 && !allow_whole_dev)) {
618 printf("** Bad partition specification %s %s **\n",
619 ifname, dev_part_str);
620 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000621 }
Rob Herring99d2c202012-09-21 04:08:17 +0000622 }
623
Stephen Warren10a37fd2012-09-21 09:50:57 +0000624 /*
625 * No partition table on device,
626 * or user requested partition 0 (entire device).
627 */
628 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
629 (part == 0)) {
630 if (!(*dev_desc)->lba) {
631 printf("** Bad device size - %s %s **\n", ifname,
632 dev_str);
633 goto cleanup;
634 }
Rob Herring99d2c202012-09-21 04:08:17 +0000635
Stephen Warren10a37fd2012-09-21 09:50:57 +0000636 /*
637 * If user specified a partition ID other than 0,
638 * or the calling command only accepts partitions,
639 * it's an error.
640 */
641 if ((part > 0) || (!allow_whole_dev)) {
642 printf("** No partition table - %s %s **\n", ifname,
643 dev_str);
644 goto cleanup;
645 }
646
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800647 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
648
Stephen Warren10a37fd2012-09-21 09:50:57 +0000649 info->start = 0;
650 info->size = (*dev_desc)->lba;
651 info->blksz = (*dev_desc)->blksz;
652 info->bootable = 0;
Stephen Warren6ab6a652012-10-10 07:57:51 +0000653 strcpy((char *)info->type, BOOT_PART_TYPE);
654 strcpy((char *)info->name, "Whole Disk");
Stephen Warren10a37fd2012-09-21 09:50:57 +0000655#ifdef CONFIG_PARTITION_UUIDS
656 info->uuid[0] = 0;
657#endif
Patrick Delaunay7561b252015-10-27 11:00:27 +0100658#ifdef CONFIG_PARTITION_TYPE_GUID
659 info->type_guid[0] = 0;
660#endif
Stephen Warren10a37fd2012-09-21 09:50:57 +0000661
662 ret = 0;
663 goto cleanup;
664 }
665
666 /*
667 * Now there's known to be a partition table,
668 * not specifying a partition means to pick partition 1.
669 */
670 if (part == PART_UNSPECIFIED)
671 part = 1;
672
673 /*
674 * If user didn't specify a partition number, or did specify something
675 * other than "auto", use that partition number directly.
676 */
677 if (part != PART_AUTO) {
678 ret = get_partition_info(*dev_desc, part, info);
679 if (ret) {
680 printf("** Invalid partition %d **\n", part);
681 goto cleanup;
682 }
683 } else {
684 /*
685 * Find the first bootable partition.
686 * If none are bootable, fall back to the first valid partition.
687 */
688 part = 0;
689 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
690 ret = get_partition_info(*dev_desc, p, info);
691 if (ret)
692 continue;
693
694 /*
695 * First valid partition, or new better partition?
696 * If so, save partition ID.
697 */
698 if (!part || info->bootable)
699 part = p;
700
701 /* Best possible partition? Stop searching. */
702 if (info->bootable)
703 break;
704
705 /*
706 * We now need to search further for best possible.
707 * If we what we just queried was the best so far,
708 * save the info since we over-write it next loop.
709 */
710 if (part == p)
711 tmpinfo = *info;
712 }
713 /* If we found any acceptable partition */
714 if (part) {
715 /*
716 * If we searched all possible partition IDs,
717 * return the first valid partition we found.
718 */
719 if (p == MAX_SEARCH_PARTITIONS + 1)
720 *info = tmpinfo;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000721 } else {
722 printf("** No valid partitions found **\n");
Stephen Warren71bba422012-10-08 07:45:54 +0000723 ret = -1;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000724 goto cleanup;
725 }
Rob Herring99d2c202012-09-21 04:08:17 +0000726 }
727 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
728 printf("** Invalid partition type \"%.32s\""
729 " (expect \"" BOOT_PART_TYPE "\")\n",
730 info->type);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000731 ret = -1;
732 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000733 }
734
Lan Yixun (dlan)50ffc3b2013-07-20 08:17:59 +0800735 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
736
Stephen Warren10a37fd2012-09-21 09:50:57 +0000737 ret = part;
738 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000739
Stephen Warren10a37fd2012-09-21 09:50:57 +0000740cleanup:
741 free(dup_str);
742 return ret;
Rob Herring99d2c202012-09-21 04:08:17 +0000743}