1 // SPDX-License-Identifier: GPL-2.0+
3 * Bootmethod for booting via a U-Boot script
5 * Copyright 2021 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
24 #define SCRIPT_FNAME1 "boot.scr.uimg"
25 #define SCRIPT_FNAME2 "boot.scr"
27 static int script_check(struct udevice *dev, struct bootflow_iter *iter)
29 /* This works on block devices, network devices and SPI Flash */
30 if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY)
31 return log_msg_ret("pxe", -ENOTSUPP);
37 * script_fill_info() - Decode the U-Boot script to find out distro info
39 * @bflow: Bootflow to process
40 * @return 0 if OK, -ve on error
42 static int script_fill_info(struct bootflow *bflow)
49 log_debug("parsing bflow file size %x\n", bflow->size);
51 ret = image_locate_script(bflow->buf, bflow->size, NULL, NULL, &data, &len);
53 if (strstr(data, "armbianEnv"))
58 bflow->os_name = strdup(name);
60 return log_msg_ret("os", -ENOMEM);
66 static int script_read_bootflow_file(struct udevice *bootstd,
67 struct bootflow *bflow)
69 struct blk_desc *desc = NULL;
70 const char *const *prefixes;
74 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
76 return log_msg_ret("std", ret);
79 /* We require a partition table */
82 desc = dev_get_uclass_plat(bflow->blk);
85 prefixes = bootstd_get_prefixes(bootstd);
88 prefix = prefixes ? prefixes[i] : NULL;
90 ret = bootmeth_try_file(bflow, desc, prefix, SCRIPT_FNAME1);
92 ret = bootmeth_try_file(bflow, desc, prefix,
94 } while (ret && prefixes && prefixes[++i]);
96 return log_msg_ret("try", ret);
98 bflow->subdir = strdup(prefix ? prefix : "");
100 return log_msg_ret("prefix", -ENOMEM);
102 ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN);
104 return log_msg_ret("read", ret);
106 ret = script_fill_info(bflow);
108 return log_msg_ret("inf", ret);
110 ret = bootmeth_alloc_other(bflow, "boot.bmp", &bflow->logo,
117 static int script_read_bootflow_net(struct bootflow *bflow)
119 const char *addr_str;
124 /* figure out the load address */
125 addr_str = env_get("scriptaddr");
126 addr = addr_str ? hextoul(addr_str, NULL) : image_load_addr;
128 fname = env_get("boot_script_dhcp");
130 return log_msg_ret("dhc", -EINVAL);
132 ret = dhcp_run(addr, fname, true);
134 return log_msg_ret("dhc", ret);
136 size = env_get_hex("filesize", 0);
138 return log_msg_ret("sz", -EINVAL);
140 bflow->buf = malloc(size + 1);
142 return log_msg_ret("buf", -ENOMEM);
143 memcpy(bflow->buf, map_sysmem(addr, size), size);
144 bflow->buf[size] = '\0';
146 bflow->state = BOOTFLOWST_READY;
151 static int script_read_bootflow(struct udevice *dev, struct bootflow *bflow)
153 const struct udevice *media = dev_get_parent(bflow->dev);
154 struct udevice *bootstd;
157 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
159 return log_msg_ret("std", ret);
161 if (IS_ENABLED(CONFIG_CMD_DHCP) &&
162 device_get_uclass_id(media) == UCLASS_ETH) {
163 /* we only support reading from one device, so ignore 'dev' */
164 ret = script_read_bootflow_net(bflow);
166 return log_msg_ret("net", ret);
168 ret = script_read_bootflow_file(bootstd, bflow);
170 return log_msg_ret("blk", ret);
176 static int script_set_bootflow(struct udevice *dev, struct bootflow *bflow,
182 bflow->state = BOOTFLOWST_READY;
187 static int script_boot(struct udevice *dev, struct bootflow *bflow)
189 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
193 if (desc->uclass_id == UCLASS_USB) {
194 ret = env_set("devtype", "usb");
196 /* If the uclass is AHCI, but the driver is ATA
197 * (not scsi), set devtype to sata
199 if (IS_ENABLED(CONFIG_SATA) &&
200 desc->uclass_id == UCLASS_AHCI)
201 ret = env_set("devtype", "sata");
203 ret = env_set("devtype", blk_get_devtype(bflow->blk));
206 ret = env_set_hex("devnum", desc->devnum);
208 ret = env_set_hex("distro_bootpart", bflow->part);
210 ret = env_set("prefix", bflow->subdir);
211 if (!ret && IS_ENABLED(CONFIG_ARCH_SUNXI) &&
212 !strcmp("mmc", blk_get_devtype(bflow->blk)))
213 ret = env_set_hex("mmc_bootdev", desc->devnum);
215 return log_msg_ret("env", ret);
217 log_debug("devtype: %s\n", env_get("devtype"));
218 log_debug("devnum: %s\n", env_get("devnum"));
219 log_debug("distro_bootpart: %s\n", env_get("distro_bootpart"));
220 log_debug("prefix: %s\n", env_get("prefix"));
221 log_debug("mmc_bootdev: %s\n", env_get("mmc_bootdev"));
223 addr = map_to_sysmem(bflow->buf);
224 ret = cmd_source_script(addr, NULL, NULL);
226 return log_msg_ret("boot", ret);
231 static int script_bootmeth_bind(struct udevice *dev)
233 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
235 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
236 "Script boot from a block device" : "script";
241 static struct bootmeth_ops script_bootmeth_ops = {
242 .check = script_check,
243 .read_bootflow = script_read_bootflow,
244 .set_bootflow = script_set_bootflow,
245 .read_file = bootmeth_common_read_file,
249 static const struct udevice_id script_bootmeth_ids[] = {
250 { .compatible = "u-boot,script" },
254 /* Put an number before 'script' to provide a default ordering */
255 U_BOOT_DRIVER(bootmeth_2script) = {
256 .name = "bootmeth_script",
257 .id = UCLASS_BOOTMETH,
258 .of_match = script_bootmeth_ids,
259 .ops = &script_bootmeth_ops,
260 .bind = script_bootmeth_bind,