blob: 0e05d28d4d9b81feb2c152a0230be8d557c43e13 [file] [log] [blame]
Simon Glass7e03e742022-04-24 23:31:22 -06001// 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 <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY UCLASS_BOOTSTD
10
Simon Glass7e03e742022-04-24 23:31:22 -060011#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>
Simon Glassd393c4f2023-01-17 10:48:04 -070021#include <net.h>
Simon Glass7e03e742022-04-24 23:31:22 -060022
23#define SCRIPT_FNAME1 "boot.scr.uimg"
24#define SCRIPT_FNAME2 "boot.scr"
25
26static int script_check(struct udevice *dev, struct bootflow_iter *iter)
27{
Simon Glassd393c4f2023-01-17 10:48:04 -070028 /* This works on block devices, network devices and SPI Flash */
Simon Glasse22fe922023-01-17 10:48:05 -070029 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
30 return log_msg_ret("pxe", -ENOTSUPP);
Simon Glass7e03e742022-04-24 23:31:22 -060031
32 return 0;
33}
34
Simon Glass72b7b192023-01-06 08:52:33 -060035/**
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 */
41static 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
Simon Glassd393c4f2023-01-17 10:48:04 -070065static int script_read_bootflow_file(struct udevice *bootstd,
66 struct bootflow *bflow)
Simon Glass7e03e742022-04-24 23:31:22 -060067{
68 struct blk_desc *desc = NULL;
69 const char *const *prefixes;
Simon Glass7e03e742022-04-24 23:31:22 -060070 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
Simon Glassd393c4f2023-01-17 10:48:04 -070077 if (bflow->blk) {
78 /* We require a partition table */
79 if (!bflow->part)
80 return -ENOENT;
Simon Glass7e03e742022-04-24 23:31:22 -060081 desc = dev_get_uclass_plat(bflow->blk);
Simon Glassd393c4f2023-01-17 10:48:04 -070082 }
Simon Glass7e03e742022-04-24 23:31:22 -060083
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
Tony Dinhd6833e92023-09-19 14:27:21 -0700101 ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN);
Simon Glass7e03e742022-04-24 23:31:22 -0600102 if (ret)
103 return log_msg_ret("read", ret);
104
Simon Glass72b7b192023-01-06 08:52:33 -0600105 ret = script_fill_info(bflow);
106 if (ret)
107 return log_msg_ret("inf", ret);
108
Simon Glass612b9cc2023-01-06 08:52:34 -0600109 ret = bootmeth_alloc_other(bflow, "boot.bmp", &bflow->logo,
110 &bflow->logo_size);
111 /* ignore error */
112
Simon Glass7e03e742022-04-24 23:31:22 -0600113 return 0;
114}
115
Simon Glassd393c4f2023-01-17 10:48:04 -0700116static 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
150static 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
175static 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
Simon Glass7e03e742022-04-24 23:31:22 -0600186static int script_boot(struct udevice *dev, struct bootflow *bflow)
187{
188 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
189 ulong addr;
Tony Dinha87cbb82023-10-11 13:26:42 -0700190 int ret = 0;
Simon Glass7e03e742022-04-24 23:31:22 -0600191
Tony Dinha87cbb82023-10-11 13:26:42 -0700192 if (desc->uclass_id == UCLASS_USB) {
John Clark8d7f4622023-06-30 13:12:14 -0400193 ret = env_set("devtype", "usb");
Tony Dinha87cbb82023-10-11 13:26:42 -0700194 } else {
195 /* If the uclass is AHCI, but the driver is ATA
196 * (not scsi), set devtype to sata
197 */
198 if (IS_ENABLED(CONFIG_SATA) &&
199 desc->uclass_id == UCLASS_AHCI)
200 ret = env_set("devtype", "sata");
201 else
202 ret = env_set("devtype", blk_get_devtype(bflow->blk));
203 }
Simon Glass7e03e742022-04-24 23:31:22 -0600204 if (!ret)
205 ret = env_set_hex("devnum", desc->devnum);
206 if (!ret)
Simon Glass30fbeb52023-01-17 10:47:58 -0700207 ret = env_set_hex("distro_bootpart", bflow->part);
208 if (!ret)
Simon Glass7e03e742022-04-24 23:31:22 -0600209 ret = env_set("prefix", bflow->subdir);
210 if (!ret && IS_ENABLED(CONFIG_ARCH_SUNXI) &&
211 !strcmp("mmc", blk_get_devtype(bflow->blk)))
212 ret = env_set_hex("mmc_bootdev", desc->devnum);
213 if (ret)
214 return log_msg_ret("env", ret);
215
216 log_debug("devtype: %s\n", env_get("devtype"));
217 log_debug("devnum: %s\n", env_get("devnum"));
Simon Glass30fbeb52023-01-17 10:47:58 -0700218 log_debug("distro_bootpart: %s\n", env_get("distro_bootpart"));
Simon Glass7e03e742022-04-24 23:31:22 -0600219 log_debug("prefix: %s\n", env_get("prefix"));
220 log_debug("mmc_bootdev: %s\n", env_get("mmc_bootdev"));
221
222 addr = map_to_sysmem(bflow->buf);
Simon Glassdaee3ba2023-01-06 08:52:28 -0600223 ret = cmd_source_script(addr, NULL, NULL);
Simon Glass7e03e742022-04-24 23:31:22 -0600224 if (ret)
225 return log_msg_ret("boot", ret);
226
227 return 0;
228}
229
230static int script_bootmeth_bind(struct udevice *dev)
231{
232 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
233
234 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
235 "Script boot from a block device" : "script";
236
237 return 0;
238}
239
240static struct bootmeth_ops script_bootmeth_ops = {
241 .check = script_check,
242 .read_bootflow = script_read_bootflow,
Simon Glassd393c4f2023-01-17 10:48:04 -0700243 .set_bootflow = script_set_bootflow,
Simon Glass7e03e742022-04-24 23:31:22 -0600244 .read_file = bootmeth_common_read_file,
245 .boot = script_boot,
246};
247
248static const struct udevice_id script_bootmeth_ids[] = {
249 { .compatible = "u-boot,script" },
250 { }
251};
252
Simon Glass2f27e472023-08-19 16:49:35 -0600253/* Put an number before 'script' to provide a default ordering */
254U_BOOT_DRIVER(bootmeth_2script) = {
Simon Glass7e03e742022-04-24 23:31:22 -0600255 .name = "bootmeth_script",
256 .id = UCLASS_BOOTMETH,
257 .of_match = script_bootmeth_ids,
258 .ops = &script_bootmeth_ops,
259 .bind = script_bootmeth_bind,
260};