Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 3 | * Bootmethod for extlinux boot from a block device |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2021 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #define LOG_CATEGORY UCLASS_BOOTSTD |
| 10 | |
Nam Cao | c39c353 | 2024-11-07 16:01:05 +0100 | [diff] [blame] | 11 | #include <asm/cache.h> |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 12 | #include <bootdev.h> |
| 13 | #include <bootflow.h> |
| 14 | #include <bootmeth.h> |
| 15 | #include <bootstd.h> |
| 16 | #include <command.h> |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 17 | #include <dm.h> |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 18 | #include <extlinux.h> |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 19 | #include <fs.h> |
| 20 | #include <malloc.h> |
| 21 | #include <mapmem.h> |
| 22 | #include <mmc.h> |
| 23 | #include <pxe_utils.h> |
| 24 | |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 25 | struct extlinux_plat { |
| 26 | bool use_fallback; |
| 27 | }; |
| 28 | |
| 29 | enum extlinux_option_type { |
| 30 | EO_FALLBACK, |
| 31 | EO_INVALID |
| 32 | }; |
| 33 | |
| 34 | struct extlinux_option { |
| 35 | char *name; |
| 36 | enum extlinux_option_type option; |
| 37 | }; |
| 38 | |
| 39 | static const struct extlinux_option options[] = { |
| 40 | {"fallback", EO_FALLBACK}, |
| 41 | {NULL, EO_INVALID} |
| 42 | }; |
| 43 | |
| 44 | static enum extlinux_option_type get_option(const char *option) |
| 45 | { |
| 46 | int i = 0; |
| 47 | |
| 48 | while (options[i].name) { |
| 49 | if (!strcmp(options[i].name, option)) |
| 50 | return options[i].option; |
| 51 | |
| 52 | i++; |
| 53 | } |
| 54 | |
| 55 | return EO_INVALID; |
| 56 | }; |
| 57 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 58 | static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize) |
Simon Glass | f6d71a8 | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 59 | { |
| 60 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
| 61 | int len; |
| 62 | |
| 63 | len = snprintf(buf, maxsize, "OK"); |
| 64 | |
| 65 | return len + 1 < maxsize ? 0 : -ENOSPC; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 71 | static int extlinux_getfile(struct pxe_context *ctx, const char *file_path, |
Simon Glass | a686ba6 | 2024-11-15 16:19:19 -0700 | [diff] [blame] | 72 | char *file_addr, enum bootflow_img_t type, |
| 73 | ulong *sizep) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 74 | { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 75 | struct extlinux_info *info = ctx->userdata; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 76 | ulong addr; |
| 77 | int ret; |
| 78 | |
| 79 | addr = simple_strtoul(file_addr, NULL, 16); |
| 80 | |
| 81 | /* Allow up to 1GB */ |
| 82 | *sizep = 1 << 30; |
| 83 | ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr, |
Simon Glass | a686ba6 | 2024-11-15 16:19:19 -0700 | [diff] [blame] | 84 | type, sizep); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 85 | if (ret) |
| 86 | return log_msg_ret("read", ret); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 91 | static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 92 | { |
| 93 | int ret; |
| 94 | |
| 95 | /* This only works on block devices */ |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 96 | ret = bootflow_iter_check_blk(iter); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 97 | if (ret) |
| 98 | return log_msg_ret("blk", ret); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 103 | /** |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 104 | * extlinux_fill_info() - Decode the extlinux file to find out its info |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 105 | * |
| 106 | * @bflow: Bootflow to process |
| 107 | * @return 0 if OK, -ve on error |
| 108 | */ |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 109 | static int extlinux_fill_info(struct bootflow *bflow) |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 110 | { |
Simon Glass | f7e87f9 | 2025-03-18 16:20:44 +0100 | [diff] [blame] | 111 | struct membuf mb; |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 112 | char line[200]; |
| 113 | char *data; |
| 114 | int len; |
| 115 | |
| 116 | log_debug("parsing bflow file size %x\n", bflow->size); |
Simon Glass | ceefc78 | 2025-03-18 16:20:42 +0100 | [diff] [blame] | 117 | membuf_init(&mb, bflow->buf, bflow->size); |
| 118 | membuf_putraw(&mb, bflow->size, true, &data); |
| 119 | while (len = membuf_readline(&mb, line, sizeof(line) - 1, ' ', true), len) { |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 120 | char *tok, *p = line; |
| 121 | |
| 122 | tok = strsep(&p, " "); |
| 123 | if (p) { |
| 124 | if (!strcmp("label", tok)) { |
| 125 | bflow->os_name = strdup(p); |
| 126 | if (!bflow->os_name) |
| 127 | return log_msg_ret("os", -ENOMEM); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 135 | static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 136 | { |
| 137 | struct blk_desc *desc; |
| 138 | const char *const *prefixes; |
| 139 | struct udevice *bootstd; |
| 140 | const char *prefix; |
| 141 | loff_t size; |
| 142 | int ret, i; |
| 143 | |
| 144 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 145 | if (ret) |
| 146 | return log_msg_ret("std", ret); |
| 147 | |
| 148 | /* If a block device, we require a partition table */ |
| 149 | if (bflow->blk && !bflow->part) |
| 150 | return -ENOENT; |
| 151 | |
| 152 | prefixes = bootstd_get_prefixes(bootstd); |
| 153 | i = 0; |
| 154 | desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL; |
| 155 | do { |
| 156 | prefix = prefixes ? prefixes[i] : NULL; |
| 157 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 158 | ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 159 | } while (ret && prefixes && prefixes[++i]); |
| 160 | if (ret) |
| 161 | return log_msg_ret("try", ret); |
| 162 | size = bflow->size; |
| 163 | |
Tom Rini | 0160b2c | 2025-01-15 17:34:26 -0600 | [diff] [blame] | 164 | ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN, |
| 165 | BFI_EXTLINUX_CFG); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 166 | if (ret) |
| 167 | return log_msg_ret("read", ret); |
| 168 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 169 | ret = extlinux_fill_info(bflow); |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 170 | if (ret) |
| 171 | return log_msg_ret("inf", ret); |
| 172 | |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 176 | static int extlinux_boot(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 177 | { |
| 178 | struct cmd_tbl cmdtp = {}; /* dummy */ |
| 179 | struct pxe_context ctx; |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 180 | struct extlinux_info info; |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 181 | struct extlinux_plat *plat; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 182 | ulong addr; |
| 183 | int ret; |
| 184 | |
| 185 | addr = map_to_sysmem(bflow->buf); |
| 186 | info.dev = dev; |
| 187 | info.bflow = bflow; |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 188 | |
| 189 | plat = dev_get_plat(dev); |
| 190 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 191 | ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true, |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 192 | bflow->fname, false, plat->use_fallback); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 193 | if (ret) |
| 194 | return log_msg_ret("ctx", -EINVAL); |
| 195 | |
| 196 | ret = pxe_process(&ctx, addr, false); |
| 197 | if (ret) |
| 198 | return log_msg_ret("bread", -EINVAL); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 203 | static int extlinux_set_property(struct udevice *dev, const char *property, const char *value) |
| 204 | { |
| 205 | struct extlinux_plat *plat; |
| 206 | static enum extlinux_option_type option; |
| 207 | |
| 208 | plat = dev_get_plat(dev); |
| 209 | |
| 210 | option = get_option(property); |
| 211 | if (option == EO_INVALID) { |
| 212 | printf("Invalid option\n"); |
| 213 | return -EINVAL; |
| 214 | } |
| 215 | |
| 216 | switch (option) { |
| 217 | case EO_FALLBACK: |
| 218 | if (!strcmp(value, "1")) { |
| 219 | plat->use_fallback = true; |
| 220 | } else if (!strcmp(value, "0")) { |
| 221 | plat->use_fallback = false; |
| 222 | } else { |
| 223 | printf("Unexpected value '%s'\n", value); |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | break; |
| 227 | default: |
| 228 | printf("Unrecognised property '%s'\n", property); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 235 | static int extlinux_bootmeth_bind(struct udevice *dev) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 236 | { |
| 237 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 238 | |
| 239 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 240 | "Extlinux boot from a block device" : "extlinux"; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 245 | static struct bootmeth_ops extlinux_bootmeth_ops = { |
| 246 | .get_state_desc = extlinux_get_state_desc, |
| 247 | .check = extlinux_check, |
| 248 | .read_bootflow = extlinux_read_bootflow, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 249 | .read_file = bootmeth_common_read_file, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 250 | .boot = extlinux_boot, |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 251 | .set_property = extlinux_set_property, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 252 | }; |
| 253 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 254 | static const struct udevice_id extlinux_bootmeth_ids[] = { |
| 255 | { .compatible = "u-boot,extlinux" }, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 256 | { } |
| 257 | }; |
| 258 | |
Simon Glass | e5cb018 | 2024-07-17 09:30:59 +0100 | [diff] [blame] | 259 | /* Put a number before 'extlinux' to provide a default ordering */ |
Simon Glass | 2f27e47 | 2023-08-19 16:49:35 -0600 | [diff] [blame] | 260 | U_BOOT_DRIVER(bootmeth_1extlinux) = { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 261 | .name = "bootmeth_extlinux", |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 262 | .id = UCLASS_BOOTMETH, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 263 | .of_match = extlinux_bootmeth_ids, |
| 264 | .ops = &extlinux_bootmeth_ops, |
| 265 | .bind = extlinux_bootmeth_bind, |
Martyn Welch | 93a0d16 | 2024-10-09 14:15:40 +0100 | [diff] [blame] | 266 | .plat_auto = sizeof(struct extlinux_plat) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 267 | }; |