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