Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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 <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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> |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 21 | #include <net.h> |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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 | { |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 28 | /* This works on block devices, network devices and SPI Flash */ |
Simon Glass | e22fe92 | 2023-01-17 10:48:05 -0700 | [diff] [blame] | 29 | if (iter->method_flags & BOOTFLOW_METHF_PXE_ONLY) |
| 30 | return log_msg_ret("pxe", -ENOTSUPP); |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 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 | |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 65 | static int script_read_bootflow_file(struct udevice *bootstd, |
| 66 | struct bootflow *bflow) |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 67 | { |
| 68 | struct blk_desc *desc = NULL; |
| 69 | const char *const *prefixes; |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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 | |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 77 | if (bflow->blk) { |
| 78 | /* We require a partition table */ |
| 79 | if (!bflow->part) |
| 80 | return -ENOENT; |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 81 | desc = dev_get_uclass_plat(bflow->blk); |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 82 | } |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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 | |
Tony Dinh | d6833e9 | 2023-09-19 14:27:21 -0700 | [diff] [blame] | 101 | ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN); |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 102 | if (ret) |
| 103 | return log_msg_ret("read", ret); |
| 104 | |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 105 | ret = script_fill_info(bflow); |
| 106 | if (ret) |
| 107 | return log_msg_ret("inf", ret); |
| 108 | |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 109 | ret = bootmeth_alloc_other(bflow, "boot.bmp", &bflow->logo, |
| 110 | &bflow->logo_size); |
| 111 | /* ignore error */ |
| 112 | |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 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 | |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 186 | static int script_boot(struct udevice *dev, struct bootflow *bflow) |
| 187 | { |
Simon Glass | d2d7fc8 | 2024-07-17 09:31:00 +0100 | [diff] [blame] | 188 | struct blk_desc *desc; |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 189 | ulong addr; |
Tony Dinh | a87cbb8 | 2023-10-11 13:26:42 -0700 | [diff] [blame] | 190 | int ret = 0; |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 191 | |
Simon Glass | d2d7fc8 | 2024-07-17 09:31:00 +0100 | [diff] [blame] | 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); |
Tony Dinh | a87cbb8 | 2023-10-11 13:26:42 -0700 | [diff] [blame] | 216 | } else { |
Simon Glass | d2d7fc8 | 2024-07-17 09:31:00 +0100 | [diff] [blame] | 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)); |
Tony Dinh | a87cbb8 | 2023-10-11 13:26:42 -0700 | [diff] [blame] | 223 | } |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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")); |
Simon Glass | 30fbeb5 | 2023-01-17 10:47:58 -0700 | [diff] [blame] | 229 | log_debug("distro_bootpart: %s\n", env_get("distro_bootpart")); |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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); |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 234 | ret = cmd_source_script(addr, NULL, NULL); |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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, |
Simon Glass | d393c4f | 2023-01-17 10:48:04 -0700 | [diff] [blame] | 254 | .set_bootflow = script_set_bootflow, |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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 | |
Simon Glass | e5cb018 | 2024-07-17 09:30:59 +0100 | [diff] [blame] | 264 | /* Put a number before 'script' to provide a default ordering */ |
Simon Glass | 2f27e47 | 2023-08-19 16:49:35 -0600 | [diff] [blame] | 265 | U_BOOT_DRIVER(bootmeth_2script) = { |
Simon Glass | 7e03e74 | 2022-04-24 23:31:22 -0600 | [diff] [blame] | 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 | }; |