]>
Commit | Line | Data |
---|---|---|
d9409244 SG |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Bootmethod for booting via a U-Boot script | |
4 | * | |
5 | * Copyright 2021 Google LLC | |
6 | * Written by Simon Glass <[email protected]> | |
7 | */ | |
8 | ||
9 | #define LOG_CATEGORY UCLASS_BOOTSTD | |
10 | ||
d9409244 SG |
11 | #include <blk.h> |
12 | #include <bootflow.h> | |
13 | #include <bootmeth.h> | |
14 | #include <bootstd.h> | |
15 | #include <dm.h> | |
16 | #include <env.h> | |
17 | #include <fs.h> | |
18 | #include <image.h> | |
19 | #include <malloc.h> | |
20 | #include <mapmem.h> | |
5143a41e | 21 | #include <net.h> |
d9409244 SG |
22 | |
23 | #define SCRIPT_FNAME1 "boot.scr.uimg" | |
24 | #define SCRIPT_FNAME2 "boot.scr" | |
25 | ||
26 | static int script_check(struct udevice *dev, struct bootflow_iter *iter) | |
27 | { | |
5143a41e | 28 | /* This works on block devices, network devices and SPI Flash */ |
d9f48579 SG |
29 | if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY) |
30 | return log_msg_ret("pxe", -ENOTSUPP); | |
d9409244 SG |
31 | |
32 | return 0; | |
33 | } | |
34 | ||
2175e76a SG |
35 | /** |
36 | * script_fill_info() - Decode the U-Boot script to find out distro info | |
37 | * | |
38 | * @bflow: Bootflow to process | |
39 | * @return 0 if OK, -ve on error | |
40 | */ | |
41 | static int script_fill_info(struct bootflow *bflow) | |
42 | { | |
43 | char *name = NULL; | |
44 | char *data; | |
45 | uint len; | |
46 | int ret; | |
47 | ||
48 | log_debug("parsing bflow file size %x\n", bflow->size); | |
49 | ||
50 | ret = image_locate_script(bflow->buf, bflow->size, NULL, NULL, &data, &len); | |
51 | if (!ret) { | |
52 | if (strstr(data, "armbianEnv")) | |
53 | name = "Armbian"; | |
54 | } | |
55 | ||
56 | if (name) { | |
57 | bflow->os_name = strdup(name); | |
58 | if (!bflow->os_name) | |
59 | return log_msg_ret("os", -ENOMEM); | |
60 | } | |
61 | ||
62 | return 0; | |
63 | } | |
64 | ||
5143a41e SG |
65 | static int script_read_bootflow_file(struct udevice *bootstd, |
66 | struct bootflow *bflow) | |
d9409244 SG |
67 | { |
68 | struct blk_desc *desc = NULL; | |
69 | const char *const *prefixes; | |
d9409244 SG |
70 | const char *prefix; |
71 | int ret, i; | |
72 | ||
73 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); | |
74 | if (ret) | |
75 | return log_msg_ret("std", ret); | |
76 | ||
5143a41e SG |
77 | if (bflow->blk) { |
78 | /* We require a partition table */ | |
79 | if (!bflow->part) | |
80 | return -ENOENT; | |
d9409244 | 81 | desc = dev_get_uclass_plat(bflow->blk); |
5143a41e | 82 | } |
d9409244 SG |
83 | |
84 | prefixes = bootstd_get_prefixes(bootstd); | |
85 | i = 0; | |
86 | do { | |
87 | prefix = prefixes ? prefixes[i] : NULL; | |
88 | ||
89 | ret = bootmeth_try_file(bflow, desc, prefix, SCRIPT_FNAME1); | |
90 | if (ret) | |
91 | ret = bootmeth_try_file(bflow, desc, prefix, | |
92 | SCRIPT_FNAME2); | |
93 | } while (ret && prefixes && prefixes[++i]); | |
94 | if (ret) | |
95 | return log_msg_ret("try", ret); | |
96 | ||
97 | bflow->subdir = strdup(prefix ? prefix : ""); | |
98 | if (!bflow->subdir) | |
99 | return log_msg_ret("prefix", -ENOMEM); | |
100 | ||
deb746e0 | 101 | ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN); |
d9409244 SG |
102 | if (ret) |
103 | return log_msg_ret("read", ret); | |
104 | ||
2175e76a SG |
105 | ret = script_fill_info(bflow); |
106 | if (ret) | |
107 | return log_msg_ret("inf", ret); | |
108 | ||
24d8e1b3 SG |
109 | ret = bootmeth_alloc_other(bflow, "boot.bmp", &bflow->logo, |
110 | &bflow->logo_size); | |
111 | /* ignore error */ | |
112 | ||
d9409244 SG |
113 | return 0; |
114 | } | |
115 | ||
5143a41e SG |
116 | static int script_read_bootflow_net(struct bootflow *bflow) |
117 | { | |
118 | const char *addr_str; | |
119 | int size, ret; | |
120 | char *fname; | |
121 | ulong addr; | |
122 | ||
123 | /* figure out the load address */ | |
124 | addr_str = env_get("scriptaddr"); | |
125 | addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr; | |
126 | ||
127 | fname = env_get("boot_script_dhcp"); | |
128 | if (!fname) | |
129 | return log_msg_ret("dhc", -EINVAL); | |
130 | ||
131 | ret = dhcp_run(addr, fname, true); | |
132 | if (ret) | |
133 | return log_msg_ret("dhc", ret); | |
134 | ||
135 | size = env_get_hex("filesize", 0); | |
136 | if (!size) | |
137 | return log_msg_ret("sz", -EINVAL); | |
138 | ||
139 | bflow->buf = malloc(size + 1); | |
140 | if (!bflow->buf) | |
141 | return log_msg_ret("buf", -ENOMEM); | |
142 | memcpy(bflow->buf, map_sysmem(addr, size), size); | |
143 | bflow->buf[size] = '\0'; | |
144 | bflow->size = size; | |
145 | bflow->state = BOOTFLOWST_READY; | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static int script_read_bootflow(struct udevice *dev, struct bootflow *bflow) | |
151 | { | |
152 | const struct udevice *media = dev_get_parent(bflow->dev); | |
153 | struct udevice *bootstd; | |
154 | int ret; | |
155 | ||
156 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); | |
157 | if (ret) | |
158 | return log_msg_ret("std", ret); | |
159 | ||
160 | if (IS_ENABLED(CONFIG_CMD_DHCP) && | |
161 | device_get_uclass_id(media) == UCLASS_ETH) { | |
162 | /* we only support reading from one device, so ignore 'dev' */ | |
163 | ret = script_read_bootflow_net(bflow); | |
164 | if (ret) | |
165 | return log_msg_ret("net", ret); | |
166 | } else { | |
167 | ret = script_read_bootflow_file(bootstd, bflow); | |
168 | if (ret) | |
169 | return log_msg_ret("blk", ret); | |
170 | } | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | static int script_set_bootflow(struct udevice *dev, struct bootflow *bflow, | |
176 | char *buf, int size) | |
177 | { | |
178 | buf[size] = '\0'; | |
179 | bflow->buf = buf; | |
180 | bflow->size = size; | |
181 | bflow->state = BOOTFLOWST_READY; | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
d9409244 SG |
186 | static int script_boot(struct udevice *dev, struct bootflow *bflow) |
187 | { | |
60b180fd | 188 | struct blk_desc *desc; |
d9409244 | 189 | ulong addr; |
a7527fbb | 190 | int ret = 0; |
d9409244 | 191 | |
60b180fd SG |
192 | if (bflow->blk) { |
193 | desc = dev_get_uclass_plat(bflow->blk); | |
194 | if (desc->uclass_id == UCLASS_USB) { | |
195 | ret = env_set("devtype", "usb"); | |
196 | } else { | |
197 | /* | |
198 | * If the uclass is AHCI, but the driver is ATA | |
199 | * (not scsi), set devtype to sata | |
200 | */ | |
201 | if (IS_ENABLED(CONFIG_SATA) && | |
202 | desc->uclass_id == UCLASS_AHCI) | |
203 | ret = env_set("devtype", "sata"); | |
204 | else | |
205 | ret = env_set("devtype", blk_get_devtype(bflow->blk)); | |
206 | } | |
207 | if (!ret) | |
208 | ret = env_set_hex("devnum", desc->devnum); | |
209 | if (!ret) | |
210 | ret = env_set_hex("distro_bootpart", bflow->part); | |
211 | if (!ret) | |
212 | ret = env_set("prefix", bflow->subdir); | |
213 | if (!ret && IS_ENABLED(CONFIG_ARCH_SUNXI) && | |
214 | !strcmp("mmc", blk_get_devtype(bflow->blk))) | |
215 | ret = env_set_hex("mmc_bootdev", desc->devnum); | |
a7527fbb | 216 | } else { |
60b180fd SG |
217 | const struct udevice *media = dev_get_parent(bflow->dev); |
218 | ||
219 | ret = env_set("devtype", | |
220 | uclass_get_name(device_get_uclass_id(media))); | |
221 | if (!ret) | |
222 | ret = env_set_hex("devnum", dev_seq(media)); | |
a7527fbb | 223 | } |
d9409244 SG |
224 | if (ret) |
225 | return log_msg_ret("env", ret); | |
226 | ||
227 | log_debug("devtype: %s\n", env_get("devtype")); | |
228 | log_debug("devnum: %s\n", env_get("devnum")); | |
25365879 | 229 | log_debug("distro_bootpart: %s\n", env_get("distro_bootpart")); |
d9409244 SG |
230 | log_debug("prefix: %s\n", env_get("prefix")); |
231 | log_debug("mmc_bootdev: %s\n", env_get("mmc_bootdev")); | |
232 | ||
233 | addr = map_to_sysmem(bflow->buf); | |
30f3333d | 234 | ret = cmd_source_script(addr, NULL, NULL); |
d9409244 SG |
235 | if (ret) |
236 | return log_msg_ret("boot", ret); | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | static int script_bootmeth_bind(struct udevice *dev) | |
242 | { | |
243 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); | |
244 | ||
245 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? | |
246 | "Script boot from a block device" : "script"; | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | static struct bootmeth_ops script_bootmeth_ops = { | |
252 | .check = script_check, | |
253 | .read_bootflow = script_read_bootflow, | |
5143a41e | 254 | .set_bootflow = script_set_bootflow, |
d9409244 SG |
255 | .read_file = bootmeth_common_read_file, |
256 | .boot = script_boot, | |
257 | }; | |
258 | ||
259 | static const struct udevice_id script_bootmeth_ids[] = { | |
260 | { .compatible = "u-boot,script" }, | |
261 | { } | |
262 | }; | |
263 | ||
cfed2219 | 264 | /* Put a number before 'script' to provide a default ordering */ |
5986d46f | 265 | U_BOOT_DRIVER(bootmeth_2script) = { |
d9409244 SG |
266 | .name = "bootmeth_script", |
267 | .id = UCLASS_BOOTMETH, | |
268 | .of_match = script_bootmeth_ids, | |
269 | .ops = &script_bootmeth_ops, | |
270 | .bind = script_bootmeth_bind, | |
271 | }; |