]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - disk/part.c
TX6 Release 2013-04-22
[karo-tx-uboot.git] / disk / part.c
1 /*
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>
27 #include <malloc.h>
28 #include <part.h>
29
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
38 /* Rather than repeat this expression each time, add a define for it */
39 #if (defined(CONFIG_CMD_IDE) || \
40      defined(CONFIG_CMD_SATA) || \
41      defined(CONFIG_CMD_SCSI) || \
42      defined(CONFIG_CMD_USB) || \
43      defined(CONFIG_MMC) || \
44      defined(CONFIG_SYSTEMACE) )
45 #define HAVE_BLOCK_DEVICE
46 #endif
47
48 struct block_drvr {
49         char *name;
50         block_dev_desc_t* (*get_dev)(int dev);
51 };
52
53 static const struct block_drvr block_drvr[] = {
54 #if defined(CONFIG_CMD_IDE)
55         { .name = "ide", .get_dev = ide_get_dev, },
56 #endif
57 #if defined(CONFIG_CMD_PATA)
58         { .name = "pata", .get_dev = pata_get_dev, },
59 #endif
60 #if defined(CONFIG_CMD_SATA)
61         {.name = "sata", .get_dev = sata_get_dev, },
62 #endif
63 #if defined(CONFIG_CMD_SCSI)
64         { .name = "scsi", .get_dev = scsi_get_dev, },
65 #endif
66 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
67         { .name = "usb", .get_dev = usb_stor_get_dev, },
68 #endif
69 #if defined(CONFIG_MMC)
70         { .name = "mmc", .get_dev = mmc_get_dev, },
71 #endif
72 #if defined(CONFIG_SYSTEMACE)
73         { .name = "ace", .get_dev = systemace_get_dev, },
74 #endif
75         { },
76 };
77
78 DECLARE_GLOBAL_DATA_PTR;
79
80 #ifdef HAVE_BLOCK_DEVICE
81 block_dev_desc_t *get_dev(const char *ifname, int dev)
82 {
83         const struct block_drvr *drvr = block_drvr;
84         block_dev_desc_t* (*reloc_get_dev)(int dev);
85         char *name;
86
87         if (!ifname)
88                 return NULL;
89
90         name = drvr->name;
91 #ifdef CONFIG_NEEDS_MANUAL_RELOC
92         name += gd->reloc_off;
93 #endif
94         while (drvr->name) {
95                 name = drvr->name;
96                 reloc_get_dev = drvr->get_dev;
97 #ifdef CONFIG_NEEDS_MANUAL_RELOC
98                 name += gd->reloc_off;
99                 reloc_get_dev += gd->reloc_off;
100 #endif
101                 if (strncmp(ifname, name, strlen(name)) == 0)
102                         return reloc_get_dev(dev);
103                 drvr++;
104         }
105         return NULL;
106 }
107 #else
108 block_dev_desc_t *get_dev(const char *ifname, int dev)
109 {
110         return NULL;
111 }
112 #endif
113
114 #ifdef HAVE_BLOCK_DEVICE
115
116 /* ------------------------------------------------------------------------- */
117 /*
118  * reports device info to the user
119  */
120
121 #ifdef CONFIG_LBA48
122 typedef uint64_t lba512_t;
123 #else
124 typedef lbaint_t lba512_t;
125 #endif
126
127 /*
128  * Overflowless variant of (block_count * mul_by / div_by)
129  * when div_by > mul_by
130  */
131 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
132 {
133         lba512_t bc_quot, bc_rem;
134
135         /* x * m / d == x / d * m + (x % d) * m / d */
136         bc_quot = block_count / div_by;
137         bc_rem  = block_count - div_by * bc_quot;
138         return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
139 }
140
141 void dev_print (block_dev_desc_t *dev_desc)
142 {
143         lba512_t lba512; /* number of blocks if 512bytes block size */
144
145         if (dev_desc->type == DEV_TYPE_UNKNOWN) {
146                 puts ("not available\n");
147                 return;
148         }
149
150         switch (dev_desc->if_type) {
151         case IF_TYPE_SCSI:
152                 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
153                         dev_desc->target,dev_desc->lun,
154                         dev_desc->vendor,
155                         dev_desc->product,
156                         dev_desc->revision);
157                 break;
158         case IF_TYPE_ATAPI:
159         case IF_TYPE_IDE:
160         case IF_TYPE_SATA:
161                 printf ("Model: %s Firm: %s Ser#: %s\n",
162                         dev_desc->vendor,
163                         dev_desc->revision,
164                         dev_desc->product);
165                 break;
166         case IF_TYPE_SD:
167         case IF_TYPE_MMC:
168         case IF_TYPE_USB:
169                 printf ("Vendor: %s Rev: %s Prod: %s\n",
170                         dev_desc->vendor,
171                         dev_desc->revision,
172                         dev_desc->product);
173                 break;
174         case IF_TYPE_DOC:
175                 puts("device type DOC\n");
176                 return;
177         case IF_TYPE_UNKNOWN:
178                 puts("device type unknown\n");
179                 return;
180         default:
181                 printf("Unhandled device type: %i\n", dev_desc->if_type);
182                 return;
183         }
184         puts ("            Type: ");
185         if (dev_desc->removable)
186                 puts ("Removable ");
187         switch (dev_desc->type & 0x1F) {
188         case DEV_TYPE_HARDDISK:
189                 puts ("Hard Disk");
190                 break;
191         case DEV_TYPE_CDROM:
192                 puts ("CD ROM");
193                 break;
194         case DEV_TYPE_OPDISK:
195                 puts ("Optical Device");
196                 break;
197         case DEV_TYPE_TAPE:
198                 puts ("Tape");
199                 break;
200         default:
201                 printf ("# %02X #", dev_desc->type & 0x1F);
202                 break;
203         }
204         puts ("\n");
205         if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
206                 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
207                 lbaint_t lba;
208
209                 lba = dev_desc->lba;
210
211                 lba512 = (lba * (dev_desc->blksz/512));
212                 /* round to 1 digit */
213                 mb = lba512_muldiv(lba512, 10, 2048);   /* 2048 = (1024 * 1024) / 512 MB */
214
215                 mb_quot = mb / 10;
216                 mb_rem  = mb - (10 * mb_quot);
217
218                 gb = mb / 1024;
219                 gb_quot = gb / 10;
220                 gb_rem  = gb - (10 * gb_quot);
221 #ifdef CONFIG_LBA48
222                 if (dev_desc->lba48)
223                         printf ("            Supports 48-bit addressing\n");
224 #endif
225 #if defined(CONFIG_SYS_64BIT_LBA)
226                 printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
227                         mb_quot, mb_rem,
228                         gb_quot, gb_rem,
229                         lba,
230                         dev_desc->blksz);
231 #else
232                 printf ("            Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
233                         mb_quot, mb_rem,
234                         gb_quot, gb_rem,
235                         (ulong)lba,
236                         dev_desc->blksz);
237 #endif
238         } else {
239                 puts ("            Capacity: not available\n");
240         }
241 }
242 #endif
243
244 #ifdef HAVE_BLOCK_DEVICE
245
246 void init_part (block_dev_desc_t * dev_desc)
247 {
248 #ifdef CONFIG_ISO_PARTITION
249         if (test_part_iso(dev_desc) == 0) {
250                 dev_desc->part_type = PART_TYPE_ISO;
251                 return;
252         }
253 #endif
254
255 #ifdef CONFIG_MAC_PARTITION
256         if (test_part_mac(dev_desc) == 0) {
257                 dev_desc->part_type = PART_TYPE_MAC;
258                 return;
259         }
260 #endif
261
262 /* must be placed before DOS partition detection */
263 #ifdef CONFIG_EFI_PARTITION
264         if (test_part_efi(dev_desc) == 0) {
265                 dev_desc->part_type = PART_TYPE_EFI;
266                 return;
267         }
268 #endif
269
270 #ifdef CONFIG_DOS_PARTITION
271         if (test_part_dos(dev_desc) == 0) {
272                 dev_desc->part_type = PART_TYPE_DOS;
273                 return;
274         }
275 #endif
276
277 #ifdef CONFIG_AMIGA_PARTITION
278         if (test_part_amiga(dev_desc) == 0) {
279             dev_desc->part_type = PART_TYPE_AMIGA;
280             return;
281         }
282 #endif
283         dev_desc->part_type = PART_TYPE_UNKNOWN;
284 }
285
286
287 #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)
292
293 static void print_part_header (const char *type, block_dev_desc_t * dev_desc)
294 {
295         puts ("\nPartition Map for ");
296         switch (dev_desc->if_type) {
297         case IF_TYPE_IDE:
298                 puts ("IDE");
299                 break;
300         case IF_TYPE_SATA:
301                 puts ("SATA");
302                 break;
303         case IF_TYPE_SCSI:
304                 puts ("SCSI");
305                 break;
306         case IF_TYPE_ATAPI:
307                 puts ("ATAPI");
308                 break;
309         case IF_TYPE_USB:
310                 puts ("USB");
311                 break;
312         case IF_TYPE_DOC:
313                 puts ("DOC");
314                 break;
315         case IF_TYPE_MMC:
316                 puts ("MMC");
317                 break;
318         default:
319                 puts ("UNKNOWN");
320                 break;
321         }
322         printf (" device %d  --   Partition Type: %s\n\n",
323                         dev_desc->dev, type);
324 }
325
326 #endif /* any CONFIG_..._PARTITION */
327
328 void print_part (block_dev_desc_t * dev_desc)
329 {
330
331                 switch (dev_desc->part_type) {
332 #ifdef CONFIG_MAC_PARTITION
333         case PART_TYPE_MAC:
334                 PRINTF ("## Testing for valid MAC partition ##\n");
335                 print_part_header ("MAC", dev_desc);
336                 print_part_mac (dev_desc);
337                 return;
338 #endif
339 #ifdef CONFIG_DOS_PARTITION
340         case PART_TYPE_DOS:
341                 PRINTF ("## Testing for valid DOS partition ##\n");
342                 print_part_header ("DOS", dev_desc);
343                 print_part_dos (dev_desc);
344                 return;
345 #endif
346
347 #ifdef CONFIG_ISO_PARTITION
348         case PART_TYPE_ISO:
349                 PRINTF ("## Testing for valid ISO Boot partition ##\n");
350                 print_part_header ("ISO", dev_desc);
351                 print_part_iso (dev_desc);
352                 return;
353 #endif
354
355 #ifdef CONFIG_AMIGA_PARTITION
356         case PART_TYPE_AMIGA:
357             PRINTF ("## Testing for a valid Amiga partition ##\n");
358             print_part_header ("AMIGA", dev_desc);
359             print_part_amiga (dev_desc);
360             return;
361 #endif
362
363 #ifdef CONFIG_EFI_PARTITION
364         case PART_TYPE_EFI:
365                 PRINTF ("## Testing for valid EFI partition ##\n");
366                 print_part_header ("EFI", dev_desc);
367                 print_part_efi (dev_desc);
368                 return;
369 #endif
370         }
371         puts ("## Unknown partition table\n");
372 }
373
374 #endif /* HAVE_BLOCK_DEVICE */
375
376 int get_partition_info(block_dev_desc_t *dev_desc, int part
377                                         , disk_partition_t *info)
378 {
379 #ifdef HAVE_BLOCK_DEVICE
380
381 #ifdef CONFIG_PARTITION_UUIDS
382         /* The common case is no UUID support */
383         info->uuid[0] = 0;
384 #endif
385
386         switch (dev_desc->part_type) {
387 #ifdef CONFIG_MAC_PARTITION
388         case PART_TYPE_MAC:
389                 if (get_partition_info_mac(dev_desc, part, info) == 0) {
390                         PRINTF("## Valid MAC partition found ##\n");
391                         return 0;
392                 }
393                 break;
394 #endif
395
396 #ifdef CONFIG_DOS_PARTITION
397         case PART_TYPE_DOS:
398                 if (get_partition_info_dos(dev_desc, part, info) == 0) {
399                         PRINTF("## Valid DOS partition found ##\n");
400                         return 0;
401                 }
402                 break;
403 #endif
404
405 #ifdef CONFIG_ISO_PARTITION
406         case PART_TYPE_ISO:
407                 if (get_partition_info_iso(dev_desc, part, info) == 0) {
408                         PRINTF("## Valid ISO boot partition found ##\n");
409                         return 0;
410                 }
411                 break;
412 #endif
413
414 #ifdef CONFIG_AMIGA_PARTITION
415         case PART_TYPE_AMIGA:
416                 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
417                         PRINTF("## Valid Amiga partition found ##\n");
418                         return 0;
419                 }
420                 break;
421 #endif
422
423 #ifdef CONFIG_EFI_PARTITION
424         case PART_TYPE_EFI:
425                 if (get_partition_info_efi(dev_desc, part, info) == 0) {
426                         PRINTF("## Valid EFI partition found ##\n");
427                         return 0;
428                 }
429                 break;
430 #endif
431         default:
432                 break;
433         }
434 #endif /* HAVE_BLOCK_DEVICE */
435
436         return -1;
437 }
438
439 int get_device(const char *ifname, const char *dev_str,
440                block_dev_desc_t **dev_desc)
441 {
442         char *ep;
443         int dev;
444
445         dev = simple_strtoul(dev_str, &ep, 16);
446         if (*ep) {
447                 printf("** Bad device specification %s %s **\n",
448                        ifname, dev_str);
449                 return -1;
450         }
451
452         *dev_desc = get_dev(ifname, dev);
453         if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
454                 printf("** Bad device %s %s **\n", ifname, dev_str);
455                 return -1;
456         }
457
458         return dev;
459 }
460
461 #define PART_UNSPECIFIED -2
462 #define PART_AUTO -1
463 #define MAX_SEARCH_PARTITIONS 16
464 int get_device_and_partition(const char *ifname, const char *dev_part_str,
465                              block_dev_desc_t **dev_desc,
466                              disk_partition_t *info, int allow_whole_dev)
467 {
468         int ret = -1;
469         const char *part_str;
470         char *dup_str = NULL;
471         const char *dev_str;
472         int dev;
473         char *ep;
474         int p;
475         int part;
476         disk_partition_t tmpinfo;
477
478         /* If no dev_part_str, use bootdevice environment variable */
479         if (!dev_part_str || !strlen(dev_part_str) ||
480             !strcmp(dev_part_str, "-"))
481                 dev_part_str = getenv("bootdevice");
482
483         /* If still no dev_part_str, it's an error */
484         if (!dev_part_str) {
485                 printf("** No device specified **\n");
486                 goto cleanup;
487         }
488
489         /* Separate device and partition ID specification */
490         part_str = strchr(dev_part_str, ':');
491         if (part_str) {
492                 dup_str = strdup(dev_part_str);
493                 dup_str[part_str - dev_part_str] = 0;
494                 dev_str = dup_str;
495                 part_str++;
496         } else {
497                 dev_str = dev_part_str;
498         }
499
500         /* Look up the device */
501         dev = get_device(ifname, dev_str, dev_desc);
502         if (dev < 0)
503                 goto cleanup;
504
505         /* Convert partition ID string to number */
506         if (!part_str || !*part_str) {
507                 part = PART_UNSPECIFIED;
508         } else if (!strcmp(part_str, "auto")) {
509                 part = PART_AUTO;
510         } else {
511                 /* Something specified -> use exactly that */
512                 part = (int)simple_strtoul(part_str, &ep, 16);
513                 /*
514                  * Less than whole string converted,
515                  * or request for whole device, but caller requires partition.
516                  */
517                 if (*ep || (part == 0 && !allow_whole_dev)) {
518                         printf("** Bad partition specification %s %s **\n",
519                             ifname, dev_part_str);
520                         goto cleanup;
521                 }
522         }
523
524         /*
525          * No partition table on device,
526          * or user requested partition 0 (entire device).
527          */
528         if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
529             (part == 0)) {
530                 if (!(*dev_desc)->lba) {
531                         printf("** Bad device size - %s %s **\n", ifname,
532                                dev_str);
533                         goto cleanup;
534                 }
535
536                 /*
537                  * If user specified a partition ID other than 0,
538                  * or the calling command only accepts partitions,
539                  * it's an error.
540                  */
541                 if ((part > 0) || (!allow_whole_dev)) {
542                         printf("** No partition table - %s %s **\n", ifname,
543                                dev_str);
544                         goto cleanup;
545                 }
546
547                 info->start = 0;
548                 info->size = (*dev_desc)->lba;
549                 info->blksz = (*dev_desc)->blksz;
550                 info->bootable = 0;
551                 strcpy((char *)info->type, BOOT_PART_TYPE);
552                 strcpy((char *)info->name, "Whole Disk");
553 #ifdef CONFIG_PARTITION_UUIDS
554                 info->uuid[0] = 0;
555 #endif
556
557                 ret = 0;
558                 goto cleanup;
559         }
560
561         /*
562          * Now there's known to be a partition table,
563          * not specifying a partition means to pick partition 1.
564          */
565         if (part == PART_UNSPECIFIED)
566                 part = 1;
567
568         /*
569          * If user didn't specify a partition number, or did specify something
570          * other than "auto", use that partition number directly.
571          */
572         if (part != PART_AUTO) {
573                 ret = get_partition_info(*dev_desc, part, info);
574                 if (ret) {
575                         printf("** Invalid partition %d **\n", part);
576                         goto cleanup;
577                 }
578         } else {
579                 /*
580                  * Find the first bootable partition.
581                  * If none are bootable, fall back to the first valid partition.
582                  */
583                 part = 0;
584                 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
585                         ret = get_partition_info(*dev_desc, p, info);
586                         if (ret)
587                                 continue;
588
589                         /*
590                          * First valid partition, or new better partition?
591                          * If so, save partition ID.
592                          */
593                         if (!part || info->bootable)
594                                 part = p;
595
596                         /* Best possible partition? Stop searching. */
597                         if (info->bootable)
598                                 break;
599
600                         /*
601                          * We now need to search further for best possible.
602                          * If we what we just queried was the best so far,
603                          * save the info since we over-write it next loop.
604                          */
605                         if (part == p)
606                                 tmpinfo = *info;
607                 }
608                 /* If we found any acceptable partition */
609                 if (part) {
610                         /*
611                          * If we searched all possible partition IDs,
612                          * return the first valid partition we found.
613                          */
614                         if (p == MAX_SEARCH_PARTITIONS + 1)
615                                 *info = tmpinfo;
616                 } else {
617                         printf("** No valid partitions found **\n");
618                         ret = -1;
619                         goto cleanup;
620                 }
621         }
622         if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
623                 printf("** Invalid partition type \"%.32s\""
624                         " (expect \"" BOOT_PART_TYPE "\")\n",
625                         info->type);
626                 ret  = -1;
627                 goto cleanup;
628         }
629
630         ret = part;
631         goto cleanup;
632
633 cleanup:
634         free(dup_str);
635         return ret;
636 }