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