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 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 24 | 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] | 25 | { |
| 26 | if (IS_ENABLED(CONFIG_SANDBOX)) { |
| 27 | int len; |
| 28 | |
| 29 | len = snprintf(buf, maxsize, "OK"); |
| 30 | |
| 31 | return len + 1 < maxsize ? 0 : -ENOSPC; |
| 32 | } |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 37 | static int extlinux_getfile(struct pxe_context *ctx, const char *file_path, |
| 38 | char *file_addr, ulong *sizep) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 39 | { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 40 | struct extlinux_info *info = ctx->userdata; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 41 | ulong addr; |
| 42 | int ret; |
| 43 | |
| 44 | addr = simple_strtoul(file_addr, NULL, 16); |
| 45 | |
| 46 | /* Allow up to 1GB */ |
| 47 | *sizep = 1 << 30; |
| 48 | ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr, |
| 49 | sizep); |
| 50 | if (ret) |
| 51 | return log_msg_ret("read", ret); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 56 | static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | /* This only works on block devices */ |
Simon Glass | 18c5040 | 2023-01-17 10:47:54 -0700 | [diff] [blame] | 61 | ret = bootflow_iter_check_blk(iter); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 62 | if (ret) |
| 63 | return log_msg_ret("blk", ret); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 68 | /** |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 69 | * extlinux_fill_info() - Decode the extlinux file to find out its info |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 70 | * |
| 71 | * @bflow: Bootflow to process |
| 72 | * @return 0 if OK, -ve on error |
| 73 | */ |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 74 | static int extlinux_fill_info(struct bootflow *bflow) |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 75 | { |
| 76 | struct membuff mb; |
| 77 | char line[200]; |
| 78 | char *data; |
| 79 | int len; |
| 80 | |
| 81 | log_debug("parsing bflow file size %x\n", bflow->size); |
| 82 | membuff_init(&mb, bflow->buf, bflow->size); |
| 83 | membuff_putraw(&mb, bflow->size, true, &data); |
Ion Agorria | 7e06c1f | 2024-01-05 09:22:10 +0200 | [diff] [blame] | 84 | while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) { |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 85 | char *tok, *p = line; |
| 86 | |
| 87 | tok = strsep(&p, " "); |
| 88 | if (p) { |
| 89 | if (!strcmp("label", tok)) { |
| 90 | bflow->os_name = strdup(p); |
| 91 | if (!bflow->os_name) |
| 92 | return log_msg_ret("os", -ENOMEM); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 100 | static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 101 | { |
| 102 | struct blk_desc *desc; |
| 103 | const char *const *prefixes; |
| 104 | struct udevice *bootstd; |
| 105 | const char *prefix; |
| 106 | loff_t size; |
| 107 | int ret, i; |
| 108 | |
| 109 | ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd); |
| 110 | if (ret) |
| 111 | return log_msg_ret("std", ret); |
| 112 | |
| 113 | /* If a block device, we require a partition table */ |
| 114 | if (bflow->blk && !bflow->part) |
| 115 | return -ENOENT; |
| 116 | |
| 117 | prefixes = bootstd_get_prefixes(bootstd); |
| 118 | i = 0; |
| 119 | desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL; |
| 120 | do { |
| 121 | prefix = prefixes ? prefixes[i] : NULL; |
| 122 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 123 | ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 124 | } while (ret && prefixes && prefixes[++i]); |
| 125 | if (ret) |
| 126 | return log_msg_ret("try", ret); |
| 127 | size = bflow->size; |
| 128 | |
| 129 | ret = bootmeth_alloc_file(bflow, 0x10000, 1); |
| 130 | if (ret) |
| 131 | return log_msg_ret("read", ret); |
| 132 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 133 | ret = extlinux_fill_info(bflow); |
Simon Glass | 72b7b19 | 2023-01-06 08:52:33 -0600 | [diff] [blame] | 134 | if (ret) |
| 135 | return log_msg_ret("inf", ret); |
| 136 | |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 140 | static int extlinux_boot(struct udevice *dev, struct bootflow *bflow) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 141 | { |
| 142 | struct cmd_tbl cmdtp = {}; /* dummy */ |
| 143 | struct pxe_context ctx; |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 144 | struct extlinux_info info; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 145 | ulong addr; |
| 146 | int ret; |
| 147 | |
| 148 | addr = map_to_sysmem(bflow->buf); |
| 149 | info.dev = dev; |
| 150 | info.bflow = bflow; |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 151 | ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true, |
Jonas Karlman | f99af49 | 2023-06-09 14:59:01 +0000 | [diff] [blame] | 152 | bflow->fname, false); |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 153 | if (ret) |
| 154 | return log_msg_ret("ctx", -EINVAL); |
| 155 | |
| 156 | ret = pxe_process(&ctx, addr, false); |
| 157 | if (ret) |
| 158 | return log_msg_ret("bread", -EINVAL); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 163 | static int extlinux_bootmeth_bind(struct udevice *dev) |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 164 | { |
| 165 | struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev); |
| 166 | |
| 167 | plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ? |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 168 | "Extlinux boot from a block device" : "extlinux"; |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 173 | static struct bootmeth_ops extlinux_bootmeth_ops = { |
| 174 | .get_state_desc = extlinux_get_state_desc, |
| 175 | .check = extlinux_check, |
| 176 | .read_bootflow = extlinux_read_bootflow, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 177 | .read_file = bootmeth_common_read_file, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 178 | .boot = extlinux_boot, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 179 | }; |
| 180 | |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 181 | static const struct udevice_id extlinux_bootmeth_ids[] = { |
| 182 | { .compatible = "u-boot,extlinux" }, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 183 | { } |
| 184 | }; |
| 185 | |
Simon Glass | e5cb018 | 2024-07-17 09:30:59 +0100 | [diff] [blame] | 186 | /* Put a number before 'extlinux' to provide a default ordering */ |
Simon Glass | 2f27e47 | 2023-08-19 16:49:35 -0600 | [diff] [blame] | 187 | U_BOOT_DRIVER(bootmeth_1extlinux) = { |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 188 | .name = "bootmeth_extlinux", |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 189 | .id = UCLASS_BOOTMETH, |
Simon Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 190 | .of_match = extlinux_bootmeth_ids, |
| 191 | .ops = &extlinux_bootmeth_ops, |
| 192 | .bind = extlinux_bootmeth_bind, |
Simon Glass | ad8ec37 | 2022-04-24 23:31:13 -0600 | [diff] [blame] | 193 | }; |