]> Git Repo - J-u-boot.git/blame - disk/part.c
disk: part_efi: add support for the Backup GPT
[J-u-boot.git] / disk / part.c
CommitLineData
affae2bf
WD
1/*
2 * (C) Copyright 2001
3 * Wolfgang Denk, DENX Software Engineering, [email protected].
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
affae2bf
WD
6 */
7
8#include <common.h>
9#include <command.h>
10#include <ide.h>
10a37fd7 11#include <malloc.h>
735dd97b 12#include <part.h>
affae2bf
WD
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
735dd97b
GL
22struct block_drvr {
23 char *name;
24 block_dev_desc_t* (*get_dev)(int dev);
25};
26
27static const struct block_drvr block_drvr[] = {
cde5c64d 28#if defined(CONFIG_CMD_IDE)
735dd97b
GL
29 { .name = "ide", .get_dev = ide_get_dev, },
30#endif
c7057b52
DL
31#if defined(CONFIG_CMD_SATA)
32 {.name = "sata", .get_dev = sata_get_dev, },
33#endif
cde5c64d 34#if defined(CONFIG_CMD_SCSI)
735dd97b
GL
35 { .name = "scsi", .get_dev = scsi_get_dev, },
36#endif
cde5c64d 37#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
735dd97b
GL
38 { .name = "usb", .get_dev = usb_stor_get_dev, },
39#endif
40#if defined(CONFIG_MMC)
41 { .name = "mmc", .get_dev = mmc_get_dev, },
42#endif
43#if defined(CONFIG_SYSTEMACE)
44 { .name = "ace", .get_dev = systemace_get_dev, },
f4d8de48
HN
45#endif
46#if defined(CONFIG_SANDBOX)
47 { .name = "host", .get_dev = host_get_dev, },
735dd97b
GL
48#endif
49 { },
50};
51
751bb571 52DECLARE_GLOBAL_DATA_PTR;
751bb571 53
0c9c8fb5 54#ifdef HAVE_BLOCK_DEVICE
99d2c205 55block_dev_desc_t *get_dev(const char *ifname, int dev)
735dd97b
GL
56{
57 const struct block_drvr *drvr = block_drvr;
6c7cac8c 58 block_dev_desc_t* (*reloc_get_dev)(int dev);
23090dac 59 char *name;
735dd97b 60
7e71dc68
TK
61 if (!ifname)
62 return NULL;
63
23090dac 64 name = drvr->name;
2e5167cc 65#ifdef CONFIG_NEEDS_MANUAL_RELOC
23090dac
HS
66 name += gd->reloc_off;
67#endif
b16aadf4 68 while (drvr->name) {
23090dac 69 name = drvr->name;
521af04d 70 reloc_get_dev = drvr->get_dev;
2e5167cc 71#ifdef CONFIG_NEEDS_MANUAL_RELOC
23090dac 72 name += gd->reloc_off;
521af04d
PT
73 reloc_get_dev += gd->reloc_off;
74#endif
23090dac 75 if (strncmp(ifname, name, strlen(name)) == 0)
751bb571 76 return reloc_get_dev(dev);
735dd97b
GL
77 drvr++;
78 }
79 return NULL;
80}
81#else
99d2c205 82block_dev_desc_t *get_dev(const char *ifname, int dev)
735dd97b
GL
83{
84 return NULL;
85}
86#endif
87
0c9c8fb5 88#ifdef HAVE_BLOCK_DEVICE
affae2bf
WD
89
90/* ------------------------------------------------------------------------- */
91/*
92 * reports device info to the user
93 */
69a2a4d9 94
42dfe7a1 95#ifdef CONFIG_LBA48
69a2a4d9 96typedef uint64_t lba512_t;
c40b2956 97#else
69a2a4d9 98typedef lbaint_t lba512_t;
c40b2956 99#endif
affae2bf 100
69a2a4d9
ST
101/*
102 * Overflowless variant of (block_count * mul_by / div_by)
103 * when div_by > mul_by
104 */
105static 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
115void dev_print (block_dev_desc_t *dev_desc)
116{
117 lba512_t lba512; /* number of blocks if 512bytes block size */
118
af75a45d
WD
119 if (dev_desc->type == DEV_TYPE_UNKNOWN) {
120 puts ("not available\n");
121 return;
122 }
123
8ec6e332 124 switch (dev_desc->if_type) {
574b3195
DZ
125 case IF_TYPE_SCSI:
126 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
127 dev_desc->target,dev_desc->lun,
affae2bf 128 dev_desc->vendor,
574b3195
DZ
129 dev_desc->product,
130 dev_desc->revision);
131 break;
6e24a1eb 132 case IF_TYPE_ATAPI:
574b3195
DZ
133 case IF_TYPE_IDE:
134 case IF_TYPE_SATA:
c7057b52
DL
135 printf ("Model: %s Firm: %s Ser#: %s\n",
136 dev_desc->vendor,
137 dev_desc->revision,
138 dev_desc->product);
574b3195 139 break;
6e24a1eb
RB
140 case IF_TYPE_SD:
141 case IF_TYPE_MMC:
47bebe34
NCL
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;
6e24a1eb
RB
148 case IF_TYPE_DOC:
149 puts("device type DOC\n");
150 return;
8ec6e332 151 case IF_TYPE_UNKNOWN:
6e24a1eb
RB
152 puts("device type unknown\n");
153 return;
574b3195 154 default:
6e24a1eb 155 printf("Unhandled device type: %i\n", dev_desc->if_type);
574b3195 156 return;
affae2bf
WD
157 }
158 puts (" Type: ");
159 if (dev_desc->removable)
160 puts ("Removable ");
161 switch (dev_desc->type & 0x1F) {
726c0f1e
DZ
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;
affae2bf
WD
177 }
178 puts ("\n");
33699df1 179 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
affae2bf 180 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
c40b2956 181 lbaint_t lba;
6e592385
WD
182
183 lba = dev_desc->lba;
affae2bf 184
c40b2956 185 lba512 = (lba * (dev_desc->blksz/512));
affae2bf 186 /* round to 1 digit */
69a2a4d9
ST
187 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
188
affae2bf
WD
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);
42dfe7a1 195#ifdef CONFIG_LBA48
6e592385 196 if (dev_desc->lba48)
c40b2956
WD
197 printf (" Supports 48-bit addressing\n");
198#endif
4b142feb 199#if defined(CONFIG_SYS_64BIT_LBA)
6c6166f5 200 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
c40b2956
WD
201 mb_quot, mb_rem,
202 gb_quot, gb_rem,
203 lba,
204 dev_desc->blksz);
205#else
affae2bf
WD
206 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
207 mb_quot, mb_rem,
208 gb_quot, gb_rem,
c40b2956 209 (ulong)lba,
affae2bf 210 dev_desc->blksz);
c40b2956 211#endif
affae2bf
WD
212 } else {
213 puts (" Capacity: not available\n");
214 }
215}
b3aff0cb 216#endif
affae2bf 217
0c9c8fb5 218#ifdef HAVE_BLOCK_DEVICE
affae2bf
WD
219
220void 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
07f3d789 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
affae2bf
WD
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
c7de829c
WD
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
99d2c205 257 dev_desc->part_type = PART_TYPE_UNKNOWN;
affae2bf
WD
258}
259
260
0c9c8fb5
GB
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
affae2bf
WD
267static 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) {
726c0f1e
DZ
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;
8f3b9642
LW
289 case IF_TYPE_MMC:
290 puts ("MMC");
291 break;
f4d8de48
HN
292 case IF_TYPE_HOST:
293 puts("HOST");
294 break;
726c0f1e
DZ
295 default:
296 puts ("UNKNOWN");
297 break;
affae2bf
WD
298 }
299 printf (" device %d -- Partition Type: %s\n\n",
300 dev_desc->dev, type);
301}
302
0c9c8fb5
GB
303#endif /* any CONFIG_..._PARTITION */
304
affae2bf
WD
305void print_part (block_dev_desc_t * dev_desc)
306{
307
308 switch (dev_desc->part_type) {
309#ifdef CONFIG_MAC_PARTITION
310 case PART_TYPE_MAC:
311 PRINTF ("## Testing for valid MAC partition ##\n");
312 print_part_header ("MAC", dev_desc);
313 print_part_mac (dev_desc);
314 return;
315#endif
316#ifdef CONFIG_DOS_PARTITION
317 case PART_TYPE_DOS:
318 PRINTF ("## Testing for valid DOS partition ##\n");
319 print_part_header ("DOS", dev_desc);
320 print_part_dos (dev_desc);
321 return;
322#endif
323
324#ifdef CONFIG_ISO_PARTITION
325 case PART_TYPE_ISO:
326 PRINTF ("## Testing for valid ISO Boot partition ##\n");
327 print_part_header ("ISO", dev_desc);
328 print_part_iso (dev_desc);
329 return;
330#endif
c7de829c
WD
331
332#ifdef CONFIG_AMIGA_PARTITION
333 case PART_TYPE_AMIGA:
334 PRINTF ("## Testing for a valid Amiga partition ##\n");
335 print_part_header ("AMIGA", dev_desc);
336 print_part_amiga (dev_desc);
337 return;
338#endif
07f3d789 339
340#ifdef CONFIG_EFI_PARTITION
341 case PART_TYPE_EFI:
342 PRINTF ("## Testing for valid EFI partition ##\n");
343 print_part_header ("EFI", dev_desc);
344 print_part_efi (dev_desc);
345 return;
346#endif
affae2bf
WD
347 }
348 puts ("## Unknown partition table\n");
349}
350
0c9c8fb5 351#endif /* HAVE_BLOCK_DEVICE */
2f501646
SW
352
353int get_partition_info(block_dev_desc_t *dev_desc, int part
354 , disk_partition_t *info)
355{
0c9c8fb5 356#ifdef HAVE_BLOCK_DEVICE
2f501646 357
894bfbbf
SW
358#ifdef CONFIG_PARTITION_UUIDS
359 /* The common case is no UUID support */
360 info->uuid[0] = 0;
361#endif
362
2f501646
SW
363 switch (dev_desc->part_type) {
364#ifdef CONFIG_MAC_PARTITION
365 case PART_TYPE_MAC:
366 if (get_partition_info_mac(dev_desc, part, info) == 0) {
367 PRINTF("## Valid MAC partition found ##\n");
368 return 0;
369 }
370 break;
371#endif
372
373#ifdef CONFIG_DOS_PARTITION
374 case PART_TYPE_DOS:
375 if (get_partition_info_dos(dev_desc, part, info) == 0) {
376 PRINTF("## Valid DOS partition found ##\n");
377 return 0;
378 }
379 break;
380#endif
381
382#ifdef CONFIG_ISO_PARTITION
383 case PART_TYPE_ISO:
384 if (get_partition_info_iso(dev_desc, part, info) == 0) {
385 PRINTF("## Valid ISO boot partition found ##\n");
386 return 0;
387 }
388 break;
389#endif
390
391#ifdef CONFIG_AMIGA_PARTITION
392 case PART_TYPE_AMIGA:
393 if (get_partition_info_amiga(dev_desc, part, info) == 0) {
394 PRINTF("## Valid Amiga partition found ##\n");
395 return 0;
396 }
397 break;
398#endif
399
400#ifdef CONFIG_EFI_PARTITION
401 case PART_TYPE_EFI:
402 if (get_partition_info_efi(dev_desc, part, info) == 0) {
403 PRINTF("## Valid EFI partition found ##\n");
404 return 0;
405 }
406 break;
407#endif
408 default:
409 break;
410 }
0c9c8fb5 411#endif /* HAVE_BLOCK_DEVICE */
2f501646
SW
412
413 return -1;
414}
99d2c205 415
2023e608
SW
416int get_device(const char *ifname, const char *dev_str,
417 block_dev_desc_t **dev_desc)
418{
419 char *ep;
420 int dev;
421
422 dev = simple_strtoul(dev_str, &ep, 16);
423 if (*ep) {
424 printf("** Bad device specification %s %s **\n",
425 ifname, dev_str);
426 return -1;
427 }
428
429 *dev_desc = get_dev(ifname, dev);
430 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
431 printf("** Bad device %s %s **\n", ifname, dev_str);
432 return -1;
433 }
434
435 return dev;
436}
437
10a37fd7
SW
438#define PART_UNSPECIFIED -2
439#define PART_AUTO -1
440#define MAX_SEARCH_PARTITIONS 16
441int get_device_and_partition(const char *ifname, const char *dev_part_str,
99d2c205 442 block_dev_desc_t **dev_desc,
10a37fd7 443 disk_partition_t *info, int allow_whole_dev)
99d2c205 444{
10a37fd7
SW
445 int ret = -1;
446 const char *part_str;
447 char *dup_str = NULL;
448 const char *dev_str;
99d2c205 449 int dev;
10a37fd7
SW
450 char *ep;
451 int p;
452 int part;
453 disk_partition_t tmpinfo;
454
455 /* If no dev_part_str, use bootdevice environment variable */
a10973e7
SW
456 if (!dev_part_str || !strlen(dev_part_str) ||
457 !strcmp(dev_part_str, "-"))
10a37fd7
SW
458 dev_part_str = getenv("bootdevice");
459
460 /* If still no dev_part_str, it's an error */
461 if (!dev_part_str) {
462 printf("** No device specified **\n");
463 goto cleanup;
464 }
465
466 /* Separate device and partition ID specification */
467 part_str = strchr(dev_part_str, ':');
468 if (part_str) {
469 dup_str = strdup(dev_part_str);
470 dup_str[part_str - dev_part_str] = 0;
471 dev_str = dup_str;
472 part_str++;
473 } else {
474 dev_str = dev_part_str;
99d2c205
RH
475 }
476
10a37fd7
SW
477 /* Look up the device */
478 dev = get_device(ifname, dev_str, dev_desc);
479 if (dev < 0)
480 goto cleanup;
481
482 /* Convert partition ID string to number */
483 if (!part_str || !*part_str) {
484 part = PART_UNSPECIFIED;
485 } else if (!strcmp(part_str, "auto")) {
486 part = PART_AUTO;
487 } else {
488 /* Something specified -> use exactly that */
489 part = (int)simple_strtoul(part_str, &ep, 16);
490 /*
491 * Less than whole string converted,
492 * or request for whole device, but caller requires partition.
493 */
494 if (*ep || (part == 0 && !allow_whole_dev)) {
495 printf("** Bad partition specification %s %s **\n",
496 ifname, dev_part_str);
497 goto cleanup;
498 }
499 }
500
501 /*
502 * No partition table on device,
503 * or user requested partition 0 (entire device).
504 */
505 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
506 (part == 0)) {
507 if (!(*dev_desc)->lba) {
508 printf("** Bad device size - %s %s **\n", ifname,
509 dev_str);
510 goto cleanup;
511 }
99d2c205 512
10a37fd7
SW
513 /*
514 * If user specified a partition ID other than 0,
515 * or the calling command only accepts partitions,
516 * it's an error.
517 */
518 if ((part > 0) || (!allow_whole_dev)) {
519 printf("** No partition table - %s %s **\n", ifname,
520 dev_str);
521 goto cleanup;
99d2c205 522 }
10a37fd7 523
50ffc3b6
LY
524 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
525
99d2c205 526 info->start = 0;
10a37fd7
SW
527 info->size = (*dev_desc)->lba;
528 info->blksz = (*dev_desc)->blksz;
529 info->bootable = 0;
6ab6a650
SW
530 strcpy((char *)info->type, BOOT_PART_TYPE);
531 strcpy((char *)info->name, "Whole Disk");
10a37fd7
SW
532#ifdef CONFIG_PARTITION_UUIDS
533 info->uuid[0] = 0;
534#endif
99d2c205 535
10a37fd7
SW
536 ret = 0;
537 goto cleanup;
99d2c205
RH
538 }
539
10a37fd7
SW
540 /*
541 * Now there's known to be a partition table,
542 * not specifying a partition means to pick partition 1.
543 */
544 if (part == PART_UNSPECIFIED)
545 part = 1;
546
547 /*
548 * If user didn't specify a partition number, or did specify something
549 * other than "auto", use that partition number directly.
550 */
551 if (part != PART_AUTO) {
552 ret = get_partition_info(*dev_desc, part, info);
553 if (ret) {
554 printf("** Invalid partition %d **\n", part);
555 goto cleanup;
556 }
557 } else {
558 /*
559 * Find the first bootable partition.
560 * If none are bootable, fall back to the first valid partition.
561 */
562 part = 0;
563 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
564 ret = get_partition_info(*dev_desc, p, info);
565 if (ret)
566 continue;
567
568 /*
569 * First valid partition, or new better partition?
570 * If so, save partition ID.
571 */
572 if (!part || info->bootable)
573 part = p;
574
575 /* Best possible partition? Stop searching. */
576 if (info->bootable)
577 break;
578
579 /*
580 * We now need to search further for best possible.
581 * If we what we just queried was the best so far,
582 * save the info since we over-write it next loop.
583 */
584 if (part == p)
585 tmpinfo = *info;
586 }
587 /* If we found any acceptable partition */
588 if (part) {
589 /*
590 * If we searched all possible partition IDs,
591 * return the first valid partition we found.
592 */
593 if (p == MAX_SEARCH_PARTITIONS + 1)
594 *info = tmpinfo;
10a37fd7
SW
595 } else {
596 printf("** No valid partitions found **\n");
71bba424 597 ret = -1;
10a37fd7
SW
598 goto cleanup;
599 }
99d2c205
RH
600 }
601 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
602 printf("** Invalid partition type \"%.32s\""
603 " (expect \"" BOOT_PART_TYPE "\")\n",
604 info->type);
10a37fd7
SW
605 ret = -1;
606 goto cleanup;
99d2c205
RH
607 }
608
50ffc3b6
LY
609 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
610
10a37fd7
SW
611 ret = part;
612 goto cleanup;
99d2c205 613
10a37fd7
SW
614cleanup:
615 free(dup_str);
616 return ret;
99d2c205 617}
This page took 0.339919 seconds and 4 git commands to generate.