blob: c5cbf18c2e669c53dbd72be7e225fc84d4a6015a [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{
Simon Glassd2d7fc82024-07-17 09:31:00 +0100188 struct blk_desc *desc;
Simon Glass7e03e742022-04-24 23:31:22 -0600189 ulong addr;
Tony Dinha87cbb82023-10-11 13:26:42 -0700190 int ret = 0;
Simon Glass7e03e742022-04-24 23:31:22 -0600191
Simon Glassd2d7fc82024-07-17 09:31:00 +0100192 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);
Tony Dinha87cbb82023-10-11 13:26:42 -0700216 } else {
Simon Glassd2d7fc82024-07-17 09:31:00 +0100217 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));
Tony Dinha87cbb82023-10-11 13:26:42 -0700223 }
Simon Glass7e03e742022-04-24 23:31:22 -0600224 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"));
Simon Glass30fbeb52023-01-17 10:47:58 -0700229 log_debug("distro_bootpart: %s\n", env_get("distro_bootpart"));
Simon Glass7e03e742022-04-24 23:31:22 -0600230 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);
Simon Glassdaee3ba2023-01-06 08:52:28 -0600234 ret = cmd_source_script(addr, NULL, NULL);
Simon Glass7e03e742022-04-24 23:31:22 -0600235 if (ret)
236 return log_msg_ret("boot", ret);
237
238 return 0;
239}
240
241static 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
251static struct bootmeth_ops script_bootmeth_ops = {
252 .check = script_check,
253 .read_bootflow = script_read_bootflow,
Simon Glassd393c4f2023-01-17 10:48:04 -0700254 .set_bootflow = script_set_bootflow,
Simon Glass7e03e742022-04-24 23:31:22 -0600255 .read_file = bootmeth_common_read_file,
256 .boot = script_boot,
257};
258
259static const struct udevice_id script_bootmeth_ids[] = {
260 { .compatible = "u-boot,script" },
261 { }
262};
263
Simon Glasse5cb0182024-07-17 09:30:59 +0100264/* Put a number before 'script' to provide a default ordering */
Simon Glass2f27e472023-08-19 16:49:35 -0600265U_BOOT_DRIVER(bootmeth_2script) = {
Simon Glass7e03e742022-04-24 23:31:22 -0600266 .name = "bootmeth_script",
267 .id = UCLASS_BOOTMETH,
268 .of_match = script_bootmeth_ids,
269 .ops = &script_bootmeth_ops,
270 .bind = script_bootmeth_bind,
271};