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