blob: 64d76e8380690fcc5f9ccb9409ed40a74d72a9d0 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <command.h>
26#include <ide.h>
Stephen Warren10a37fd2012-09-21 09:50:57 +000027#include <malloc.h>
Grant Likely735dd972007-02-20 09:04:34 +010028#include <part.h>
wdenkaffae2b2002-08-17 09:36:01 +000029
30#undef PART_DEBUG
31
32#ifdef PART_DEBUG
33#define PRINTF(fmt,args...) printf (fmt ,##args)
34#else
35#define PRINTF(fmt,args...)
36#endif
37
Jon Loeligercde5c642007-07-09 17:22:37 -050038#if (defined(CONFIG_CMD_IDE) || \
Dave Liuc7057b52008-03-26 22:49:44 +080039 defined(CONFIG_CMD_SATA) || \
Jon Loeligercde5c642007-07-09 17:22:37 -050040 defined(CONFIG_CMD_SCSI) || \
41 defined(CONFIG_CMD_USB) || \
wdenk1968e612005-02-24 23:23:29 +000042 defined(CONFIG_MMC) || \
43 defined(CONFIG_SYSTEMACE) )
wdenkaffae2b2002-08-17 09:36:01 +000044
Grant Likely735dd972007-02-20 09:04:34 +010045struct block_drvr {
46 char *name;
47 block_dev_desc_t* (*get_dev)(int dev);
48};
49
50static const struct block_drvr block_drvr[] = {
Jon Loeligercde5c642007-07-09 17:22:37 -050051#if defined(CONFIG_CMD_IDE)
Grant Likely735dd972007-02-20 09:04:34 +010052 { .name = "ide", .get_dev = ide_get_dev, },
53#endif
Dave Liuc7057b52008-03-26 22:49:44 +080054#if defined(CONFIG_CMD_SATA)
55 {.name = "sata", .get_dev = sata_get_dev, },
56#endif
Jon Loeligercde5c642007-07-09 17:22:37 -050057#if defined(CONFIG_CMD_SCSI)
Grant Likely735dd972007-02-20 09:04:34 +010058 { .name = "scsi", .get_dev = scsi_get_dev, },
59#endif
Jon Loeligercde5c642007-07-09 17:22:37 -050060#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
Grant Likely735dd972007-02-20 09:04:34 +010061 { .name = "usb", .get_dev = usb_stor_get_dev, },
62#endif
63#if defined(CONFIG_MMC)
64 { .name = "mmc", .get_dev = mmc_get_dev, },
65#endif
66#if defined(CONFIG_SYSTEMACE)
67 { .name = "ace", .get_dev = systemace_get_dev, },
68#endif
69 { },
70};
71
Stefan Roese751bb572007-02-20 13:21:57 +010072DECLARE_GLOBAL_DATA_PTR;
Stefan Roese751bb572007-02-20 13:21:57 +010073
Rob Herring99d2c202012-09-21 04:08:17 +000074block_dev_desc_t *get_dev(const char *ifname, int dev)
Grant Likely735dd972007-02-20 09:04:34 +010075{
76 const struct block_drvr *drvr = block_drvr;
Stefan Roese6c7cac82007-02-22 07:43:34 +010077 block_dev_desc_t* (*reloc_get_dev)(int dev);
Heiko Schocher23090da2010-09-17 13:10:36 +020078 char *name;
Grant Likely735dd972007-02-20 09:04:34 +010079
Tim Kientzle7e71dc62012-03-27 11:43:25 +020080 if (!ifname)
81 return NULL;
82
Heiko Schocher23090da2010-09-17 13:10:36 +020083 name = drvr->name;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020084#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher23090da2010-09-17 13:10:36 +020085 name += gd->reloc_off;
86#endif
Lei Wenb16aadf2011-02-15 16:56:40 +080087 while (drvr->name) {
Heiko Schocher23090da2010-09-17 13:10:36 +020088 name = drvr->name;
Peter Tyser521af042009-09-21 11:20:36 -050089 reloc_get_dev = drvr->get_dev;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020090#ifdef CONFIG_NEEDS_MANUAL_RELOC
Heiko Schocher23090da2010-09-17 13:10:36 +020091 name += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -050092 reloc_get_dev += gd->reloc_off;
93#endif
Heiko Schocher23090da2010-09-17 13:10:36 +020094 if (strncmp(ifname, name, strlen(name)) == 0)
Stefan Roese751bb572007-02-20 13:21:57 +010095 return reloc_get_dev(dev);
Grant Likely735dd972007-02-20 09:04:34 +010096 drvr++;
97 }
98 return NULL;
99}
100#else
Rob Herring99d2c202012-09-21 04:08:17 +0000101block_dev_desc_t *get_dev(const char *ifname, int dev)
Grant Likely735dd972007-02-20 09:04:34 +0100102{
103 return NULL;
104}
105#endif
106
Jon Loeligercde5c642007-07-09 17:22:37 -0500107#if (defined(CONFIG_CMD_IDE) || \
Dave Liuc7057b52008-03-26 22:49:44 +0800108 defined(CONFIG_CMD_SATA) || \
Jon Loeligercde5c642007-07-09 17:22:37 -0500109 defined(CONFIG_CMD_SCSI) || \
110 defined(CONFIG_CMD_USB) || \
Grant Likely735dd972007-02-20 09:04:34 +0100111 defined(CONFIG_MMC) || \
112 defined(CONFIG_SYSTEMACE) )
113
wdenkaffae2b2002-08-17 09:36:01 +0000114/* ------------------------------------------------------------------------- */
115/*
116 * reports device info to the user
117 */
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300118
119#ifdef CONFIG_LBA48
120typedef uint64_t lba512_t;
121#else
122typedef lbaint_t lba512_t;
123#endif
124
125/*
126 * Overflowless variant of (block_count * mul_by / div_by)
127 * when div_by > mul_by
128 */
129static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
130{
131 lba512_t bc_quot, bc_rem;
132
133 /* x * m / d == x / d * m + (x % d) * m / d */
134 bc_quot = block_count / div_by;
135 bc_rem = block_count - div_by * bc_quot;
136 return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
137}
138
wdenkaffae2b2002-08-17 09:36:01 +0000139void dev_print (block_dev_desc_t *dev_desc)
140{
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300141 lba512_t lba512; /* number of blocks if 512bytes block size */
wdenkaffae2b2002-08-17 09:36:01 +0000142
Wolfgang Denkaf75a452009-05-15 09:27:58 +0200143 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
144 puts ("not available\n");
145 return;
146 }
147
Tor Krill8ec6e332008-05-29 11:10:30 +0200148 switch (dev_desc->if_type) {
Detlev Zundel574b3192008-05-05 16:11:21 +0200149 case IF_TYPE_SCSI:
150 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
151 dev_desc->target,dev_desc->lun,
wdenkaffae2b2002-08-17 09:36:01 +0000152 dev_desc->vendor,
153 dev_desc->product,
154 dev_desc->revision);
Detlev Zundel574b3192008-05-05 16:11:21 +0200155 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200156 case IF_TYPE_ATAPI:
Detlev Zundel574b3192008-05-05 16:11:21 +0200157 case IF_TYPE_IDE:
158 case IF_TYPE_SATA:
159 printf ("Model: %s Firm: %s Ser#: %s\n",
160 dev_desc->vendor,
161 dev_desc->revision,
162 dev_desc->product);
163 break;
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200164 case IF_TYPE_SD:
165 case IF_TYPE_MMC:
Nícolas Carneiro Lebedenco47bebe32008-09-04 15:35:46 -0300166 case IF_TYPE_USB:
167 printf ("Vendor: %s Rev: %s Prod: %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_DOC:
173 puts("device type DOC\n");
174 return;
Tor Krill8ec6e332008-05-29 11:10:30 +0200175 case IF_TYPE_UNKNOWN:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200176 puts("device type unknown\n");
177 return;
Detlev Zundel574b3192008-05-05 16:11:21 +0200178 default:
Remy Bohmer6e24a1e2008-09-19 13:30:06 +0200179 printf("Unhandled device type: %i\n", dev_desc->if_type);
Detlev Zundel574b3192008-05-05 16:11:21 +0200180 return;
wdenkaffae2b2002-08-17 09:36:01 +0000181 }
182 puts (" Type: ");
183 if (dev_desc->removable)
184 puts ("Removable ");
185 switch (dev_desc->type & 0x1F) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200186 case DEV_TYPE_HARDDISK:
187 puts ("Hard Disk");
188 break;
189 case DEV_TYPE_CDROM:
190 puts ("CD ROM");
191 break;
192 case DEV_TYPE_OPDISK:
193 puts ("Optical Device");
194 break;
195 case DEV_TYPE_TAPE:
196 puts ("Tape");
197 break;
198 default:
199 printf ("# %02X #", dev_desc->type & 0x1F);
200 break;
wdenkaffae2b2002-08-17 09:36:01 +0000201 }
202 puts ("\n");
203 if ((dev_desc->lba * dev_desc->blksz)>0L) {
204 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
wdenkc40b2952004-03-13 23:29:43 +0000205 lbaint_t lba;
wdenk6e592382004-04-18 17:39:38 +0000206
207 lba = dev_desc->lba;
wdenkaffae2b2002-08-17 09:36:01 +0000208
wdenkc40b2952004-03-13 23:29:43 +0000209 lba512 = (lba * (dev_desc->blksz/512));
wdenkaffae2b2002-08-17 09:36:01 +0000210 /* round to 1 digit */
Sergei Trofimovich69a2a4d2010-08-08 15:05:39 +0300211 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
212
wdenkaffae2b2002-08-17 09:36:01 +0000213 mb_quot = mb / 10;
214 mb_rem = mb - (10 * mb_quot);
215
216 gb = mb / 1024;
217 gb_quot = gb / 10;
218 gb_rem = gb - (10 * gb_quot);
wdenk42dfe7a2004-03-14 22:25:36 +0000219#ifdef CONFIG_LBA48
wdenk6e592382004-04-18 17:39:38 +0000220 if (dev_desc->lba48)
wdenkc40b2952004-03-13 23:29:43 +0000221 printf (" Supports 48-bit addressing\n");
222#endif
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100223#if defined(CONFIG_SYS_64BIT_LBA)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500224 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
wdenkc40b2952004-03-13 23:29:43 +0000225 mb_quot, mb_rem,
226 gb_quot, gb_rem,
227 lba,
228 dev_desc->blksz);
229#else
wdenkaffae2b2002-08-17 09:36:01 +0000230 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
231 mb_quot, mb_rem,
232 gb_quot, gb_rem,
wdenkc40b2952004-03-13 23:29:43 +0000233 (ulong)lba,
wdenkaffae2b2002-08-17 09:36:01 +0000234 dev_desc->blksz);
wdenkc40b2952004-03-13 23:29:43 +0000235#endif
wdenkaffae2b2002-08-17 09:36:01 +0000236 } else {
237 puts (" Capacity: not available\n");
238 }
239}
Jon Loeligerb3aff0c2007-07-10 11:19:50 -0500240#endif
wdenkaffae2b2002-08-17 09:36:01 +0000241
Jon Loeligercde5c642007-07-09 17:22:37 -0500242#if (defined(CONFIG_CMD_IDE) || \
Dave Liuc7057b52008-03-26 22:49:44 +0800243 defined(CONFIG_CMD_SATA) || \
Jon Loeligercde5c642007-07-09 17:22:37 -0500244 defined(CONFIG_CMD_SCSI) || \
245 defined(CONFIG_CMD_USB) || \
Haavard Skinnemoen7fac3f62006-12-17 18:53:56 +0100246 defined(CONFIG_MMC) || \
unsik Kim75eb82e2009-02-25 11:31:24 +0900247 defined(CONFIG_SYSTEMACE) )
wdenkaffae2b2002-08-17 09:36:01 +0000248
wdenkaffae2b2002-08-17 09:36:01 +0000249#if defined(CONFIG_MAC_PARTITION) || \
250 defined(CONFIG_DOS_PARTITION) || \
wdenkc7de8292002-11-19 11:04:11 +0000251 defined(CONFIG_ISO_PARTITION) || \
richardretanubun07f3d782008-09-26 11:13:22 -0400252 defined(CONFIG_AMIGA_PARTITION) || \
253 defined(CONFIG_EFI_PARTITION)
wdenkaffae2b2002-08-17 09:36:01 +0000254
255void init_part (block_dev_desc_t * dev_desc)
256{
257#ifdef CONFIG_ISO_PARTITION
258 if (test_part_iso(dev_desc) == 0) {
259 dev_desc->part_type = PART_TYPE_ISO;
260 return;
261 }
262#endif
263
264#ifdef CONFIG_MAC_PARTITION
265 if (test_part_mac(dev_desc) == 0) {
266 dev_desc->part_type = PART_TYPE_MAC;
267 return;
268 }
269#endif
270
richardretanubun07f3d782008-09-26 11:13:22 -0400271/* must be placed before DOS partition detection */
272#ifdef CONFIG_EFI_PARTITION
273 if (test_part_efi(dev_desc) == 0) {
274 dev_desc->part_type = PART_TYPE_EFI;
275 return;
276 }
277#endif
278
wdenkaffae2b2002-08-17 09:36:01 +0000279#ifdef CONFIG_DOS_PARTITION
280 if (test_part_dos(dev_desc) == 0) {
281 dev_desc->part_type = PART_TYPE_DOS;
282 return;
283 }
284#endif
wdenkc7de8292002-11-19 11:04:11 +0000285
286#ifdef CONFIG_AMIGA_PARTITION
287 if (test_part_amiga(dev_desc) == 0) {
288 dev_desc->part_type = PART_TYPE_AMIGA;
289 return;
290 }
291#endif
Rob Herring99d2c202012-09-21 04:08:17 +0000292 dev_desc->part_type = PART_TYPE_UNKNOWN;
wdenkaffae2b2002-08-17 09:36:01 +0000293}
294
295
wdenkaffae2b2002-08-17 09:36:01 +0000296static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
297{
298 puts ("\nPartition Map for ");
299 switch (dev_desc->if_type) {
Detlev Zundel726c0f12008-05-05 16:11:22 +0200300 case IF_TYPE_IDE:
301 puts ("IDE");
302 break;
303 case IF_TYPE_SATA:
304 puts ("SATA");
305 break;
306 case IF_TYPE_SCSI:
307 puts ("SCSI");
308 break;
309 case IF_TYPE_ATAPI:
310 puts ("ATAPI");
311 break;
312 case IF_TYPE_USB:
313 puts ("USB");
314 break;
315 case IF_TYPE_DOC:
316 puts ("DOC");
317 break;
Lei Wen8f3b9642010-09-13 22:07:28 +0800318 case IF_TYPE_MMC:
319 puts ("MMC");
320 break;
Detlev Zundel726c0f12008-05-05 16:11:22 +0200321 default:
322 puts ("UNKNOWN");
323 break;
wdenkaffae2b2002-08-17 09:36:01 +0000324 }
325 printf (" device %d -- Partition Type: %s\n\n",
326 dev_desc->dev, type);
327}
328
329void print_part (block_dev_desc_t * dev_desc)
330{
331
332 switch (dev_desc->part_type) {
333#ifdef CONFIG_MAC_PARTITION
334 case PART_TYPE_MAC:
335 PRINTF ("## Testing for valid MAC partition ##\n");
336 print_part_header ("MAC", dev_desc);
337 print_part_mac (dev_desc);
338 return;
339#endif
340#ifdef CONFIG_DOS_PARTITION
341 case PART_TYPE_DOS:
342 PRINTF ("## Testing for valid DOS partition ##\n");
343 print_part_header ("DOS", dev_desc);
344 print_part_dos (dev_desc);
345 return;
346#endif
347
348#ifdef CONFIG_ISO_PARTITION
349 case PART_TYPE_ISO:
350 PRINTF ("## Testing for valid ISO Boot partition ##\n");
351 print_part_header ("ISO", dev_desc);
352 print_part_iso (dev_desc);
353 return;
354#endif
wdenkc7de8292002-11-19 11:04:11 +0000355
356#ifdef CONFIG_AMIGA_PARTITION
357 case PART_TYPE_AMIGA:
358 PRINTF ("## Testing for a valid Amiga partition ##\n");
359 print_part_header ("AMIGA", dev_desc);
360 print_part_amiga (dev_desc);
361 return;
362#endif
richardretanubun07f3d782008-09-26 11:13:22 -0400363
364#ifdef CONFIG_EFI_PARTITION
365 case PART_TYPE_EFI:
366 PRINTF ("## Testing for valid EFI partition ##\n");
367 print_part_header ("EFI", dev_desc);
368 print_part_efi (dev_desc);
369 return;
370#endif
wdenkaffae2b2002-08-17 09:36:01 +0000371 }
372 puts ("## Unknown partition table\n");
373}
374
375
richardretanubun07f3d782008-09-26 11:13:22 -0400376#else /* neither MAC nor DOS nor ISO nor AMIGA nor EFI partition configured */
Wolfgang Denk725671c2007-06-06 16:26:56 +0200377# error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION
richardretanubun07f3d782008-09-26 11:13:22 -0400378# error nor CONFIG_ISO_PARTITION nor CONFIG_AMIGA_PARTITION
379# error nor CONFIG_EFI_PARTITION configured!
wdenkaffae2b2002-08-17 09:36:01 +0000380#endif
381
Jon Loeligercde5c642007-07-09 17:22:37 -0500382#endif
Stephen Warren2f501642012-09-21 12:46:54 +0000383
384int get_partition_info(block_dev_desc_t *dev_desc, int part
385 , disk_partition_t *info)
386{
387#if defined(CONFIG_CMD_IDE) || \
388 defined(CONFIG_CMD_SATA) || \
389 defined(CONFIG_CMD_SCSI) || \
390 defined(CONFIG_CMD_USB) || \
391 defined(CONFIG_MMC) || \
392 defined(CONFIG_SYSTEMACE)
393
Stephen Warren894bfbb2012-09-21 09:50:59 +0000394#ifdef CONFIG_PARTITION_UUIDS
395 /* The common case is no UUID support */
396 info->uuid[0] = 0;
397#endif
398
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 }
447#endif
448
449 return -1;
450}
Rob Herring99d2c202012-09-21 04:08:17 +0000451
Stephen Warren2023e602012-09-21 09:50:56 +0000452int get_device(const char *ifname, const char *dev_str,
453 block_dev_desc_t **dev_desc)
454{
455 char *ep;
456 int dev;
457
458 dev = simple_strtoul(dev_str, &ep, 16);
459 if (*ep) {
460 printf("** Bad device specification %s %s **\n",
461 ifname, dev_str);
462 return -1;
463 }
464
465 *dev_desc = get_dev(ifname, dev);
466 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
467 printf("** Bad device %s %s **\n", ifname, dev_str);
468 return -1;
469 }
470
471 return dev;
472}
473
Stephen Warren10a37fd2012-09-21 09:50:57 +0000474#define PART_UNSPECIFIED -2
475#define PART_AUTO -1
476#define MAX_SEARCH_PARTITIONS 16
477int get_device_and_partition(const char *ifname, const char *dev_part_str,
Rob Herring99d2c202012-09-21 04:08:17 +0000478 block_dev_desc_t **dev_desc,
Stephen Warren10a37fd2012-09-21 09:50:57 +0000479 disk_partition_t *info, int allow_whole_dev)
Rob Herring99d2c202012-09-21 04:08:17 +0000480{
Stephen Warren10a37fd2012-09-21 09:50:57 +0000481 int ret = -1;
482 const char *part_str;
483 char *dup_str = NULL;
484 const char *dev_str;
Rob Herring99d2c202012-09-21 04:08:17 +0000485 int dev;
Stephen Warren10a37fd2012-09-21 09:50:57 +0000486 char *ep;
487 int p;
488 int part;
489 disk_partition_t tmpinfo;
Rob Herring99d2c202012-09-21 04:08:17 +0000490
Stephen Warren10a37fd2012-09-21 09:50:57 +0000491 /* If no dev_part_str, use bootdevice environment variable */
492 if (!dev_part_str)
493 dev_part_str = getenv("bootdevice");
Rob Herring99d2c202012-09-21 04:08:17 +0000494
Stephen Warren10a37fd2012-09-21 09:50:57 +0000495 /* If still no dev_part_str, it's an error */
496 if (!dev_part_str) {
497 printf("** No device specified **\n");
498 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000499 }
500
Stephen Warren10a37fd2012-09-21 09:50:57 +0000501 /* Separate device and partition ID specification */
502 part_str = strchr(dev_part_str, ':');
503 if (part_str) {
504 dup_str = strdup(dev_part_str);
505 dup_str[part_str - dev_part_str] = 0;
506 dev_str = dup_str;
507 part_str++;
508 } else {
509 dev_str = dev_part_str;
510 }
Rob Herring99d2c202012-09-21 04:08:17 +0000511
Stephen Warren10a37fd2012-09-21 09:50:57 +0000512 /* Look up the device */
513 dev = get_device(ifname, dev_str, dev_desc);
514 if (dev < 0)
515 goto cleanup;
516
517 /* Convert partition ID string to number */
518 if (!part_str || !*part_str) {
519 part = PART_UNSPECIFIED;
520 } else if (!strcmp(part_str, "auto")) {
521 part = PART_AUTO;
522 } else {
523 /* Something specified -> use exactly that */
524 part = (int)simple_strtoul(part_str, &ep, 16);
525 /*
526 * Less than whole string converted,
527 * or request for whole device, but caller requires partition.
528 */
529 if (*ep || (part == 0 && !allow_whole_dev)) {
530 printf("** Bad partition specification %s %s **\n",
531 ifname, dev_part_str);
532 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000533 }
Rob Herring99d2c202012-09-21 04:08:17 +0000534 }
535
Stephen Warren10a37fd2012-09-21 09:50:57 +0000536 /*
537 * No partition table on device,
538 * or user requested partition 0 (entire device).
539 */
540 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
541 (part == 0)) {
542 if (!(*dev_desc)->lba) {
543 printf("** Bad device size - %s %s **\n", ifname,
544 dev_str);
545 goto cleanup;
546 }
Rob Herring99d2c202012-09-21 04:08:17 +0000547
Stephen Warren10a37fd2012-09-21 09:50:57 +0000548 /*
549 * If user specified a partition ID other than 0,
550 * or the calling command only accepts partitions,
551 * it's an error.
552 */
553 if ((part > 0) || (!allow_whole_dev)) {
554 printf("** No partition table - %s %s **\n", ifname,
555 dev_str);
556 goto cleanup;
557 }
558
559 info->start = 0;
560 info->size = (*dev_desc)->lba;
561 info->blksz = (*dev_desc)->blksz;
562 info->bootable = 0;
563#ifdef CONFIG_PARTITION_UUIDS
564 info->uuid[0] = 0;
565#endif
566
567 ret = 0;
568 goto cleanup;
569 }
570
571 /*
572 * Now there's known to be a partition table,
573 * not specifying a partition means to pick partition 1.
574 */
575 if (part == PART_UNSPECIFIED)
576 part = 1;
577
578 /*
579 * If user didn't specify a partition number, or did specify something
580 * other than "auto", use that partition number directly.
581 */
582 if (part != PART_AUTO) {
583 ret = get_partition_info(*dev_desc, part, info);
584 if (ret) {
585 printf("** Invalid partition %d **\n", part);
586 goto cleanup;
587 }
588 } else {
589 /*
590 * Find the first bootable partition.
591 * If none are bootable, fall back to the first valid partition.
592 */
593 part = 0;
594 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
595 ret = get_partition_info(*dev_desc, p, info);
596 if (ret)
597 continue;
598
599 /*
600 * First valid partition, or new better partition?
601 * If so, save partition ID.
602 */
603 if (!part || info->bootable)
604 part = p;
605
606 /* Best possible partition? Stop searching. */
607 if (info->bootable)
608 break;
609
610 /*
611 * We now need to search further for best possible.
612 * If we what we just queried was the best so far,
613 * save the info since we over-write it next loop.
614 */
615 if (part == p)
616 tmpinfo = *info;
617 }
618 /* If we found any acceptable partition */
619 if (part) {
620 /*
621 * If we searched all possible partition IDs,
622 * return the first valid partition we found.
623 */
624 if (p == MAX_SEARCH_PARTITIONS + 1)
625 *info = tmpinfo;
626 ret = 0;
627 } else {
628 printf("** No valid partitions found **\n");
629 goto cleanup;
630 }
Rob Herring99d2c202012-09-21 04:08:17 +0000631 }
632 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
633 printf("** Invalid partition type \"%.32s\""
634 " (expect \"" BOOT_PART_TYPE "\")\n",
635 info->type);
Stephen Warren10a37fd2012-09-21 09:50:57 +0000636 ret = -1;
637 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000638 }
639
Stephen Warren10a37fd2012-09-21 09:50:57 +0000640 ret = part;
641 goto cleanup;
Rob Herring99d2c202012-09-21 04:08:17 +0000642
Stephen Warren10a37fd2012-09-21 09:50:57 +0000643cleanup:
644 free(dup_str);
645 return ret;
Rob Herring99d2c202012-09-21 04:08:17 +0000646}