1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2011
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 #include <common.h>
7 #include <bootstage.h>
8 #include <command.h>
9 #include <cpu_func.h>
10 #include <image.h>
11 #include <log.h>
12 #include <part.h>
13 
common_diskboot(struct cmd_tbl * cmdtp,const char * intf,int argc,char * const argv[])14 int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
15 		    char *const argv[])
16 {
17 	__maybe_unused int dev;
18 	int part;
19 	ulong addr = CONFIG_SYS_LOAD_ADDR;
20 	ulong cnt;
21 	struct disk_partition info;
22 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
23 	image_header_t *hdr;
24 #endif
25 	struct blk_desc *dev_desc;
26 
27 #if CONFIG_IS_ENABLED(FIT)
28 	const void *fit_hdr = NULL;
29 #endif
30 
31 	bootstage_mark(BOOTSTAGE_ID_IDE_START);
32 	if (argc > 3) {
33 		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
34 		return CMD_RET_USAGE;
35 	}
36 	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
37 
38 	if (argc > 1)
39 		addr = simple_strtoul(argv[1], NULL, 16);
40 
41 	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
42 
43 	part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL,
44 					&dev_desc, &info, 1);
45 	if (part < 0) {
46 		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
47 		return 1;
48 	}
49 
50 	dev = dev_desc->devnum;
51 	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
52 
53 	printf("\nLoading from %s device %d, partition %d: "
54 	       "Name: %.32s  Type: %.32s\n", intf, dev, part, info.name,
55 	       info.type);
56 
57 	debug("First Block: " LBAFU ",  # of blocks: " LBAFU
58 	      ", Block Size: %ld\n",
59 	      info.start, info.size, info.blksz);
60 
61 	if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) {
62 		printf("** Read error on %d:%d\n", dev, part);
63 		bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
64 		return 1;
65 	}
66 	bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
67 
68 	switch (genimg_get_format((void *) addr)) {
69 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
70 	case IMAGE_FORMAT_LEGACY:
71 		hdr = (image_header_t *) addr;
72 
73 		bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
74 
75 		if (!image_check_hcrc(hdr)) {
76 			puts("\n** Bad Header Checksum **\n");
77 			bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
78 			return 1;
79 		}
80 		bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
81 
82 		image_print_contents(hdr);
83 
84 		cnt = image_get_image_size(hdr);
85 		break;
86 #endif
87 #if CONFIG_IS_ENABLED(FIT)
88 	case IMAGE_FORMAT_FIT:
89 		fit_hdr = (const void *) addr;
90 		puts("Fit image detected...\n");
91 
92 		cnt = fit_get_size(fit_hdr);
93 		break;
94 #endif
95 	default:
96 		bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
97 		puts("** Unknown image type\n");
98 		return 1;
99 	}
100 
101 	cnt += info.blksz - 1;
102 	cnt /= info.blksz;
103 	cnt -= 1;
104 
105 	if (blk_dread(dev_desc, info.start + 1, cnt,
106 		      (ulong *)(addr + info.blksz)) != cnt) {
107 		printf("** Read error on %d:%d\n", dev, part);
108 		bootstage_error(BOOTSTAGE_ID_IDE_READ);
109 		return 1;
110 	}
111 	bootstage_mark(BOOTSTAGE_ID_IDE_READ);
112 
113 #if CONFIG_IS_ENABLED(FIT)
114 	/* This cannot be done earlier,
115 	 * we need complete FIT image in RAM first */
116 	if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
117 		if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
118 			bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
119 			puts("** Bad FIT image format\n");
120 			return 1;
121 		}
122 		bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
123 	}
124 #endif
125 
126 	flush_cache(addr, (cnt+1)*info.blksz);
127 
128 	/* Loading ok, update default load address */
129 	image_load_addr = addr;
130 
131 	return bootm_maybe_autostart(cmdtp, argv[0]);
132 }
133