blob: 921d721a27b216bea630c26e8fa310028c4d776c [file] [log] [blame]
Simon Glassad8ec372022-04-24 23:31:13 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
Simon Glassb71d7f72023-05-10 16:34:46 -06003 * Bootmethod for extlinux boot from a block device
Simon Glassad8ec372022-04-24 23:31:13 -06004 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY UCLASS_BOOTSTD
10
Nam Caoc39c3532024-11-07 16:01:05 +010011#include <asm/cache.h>
Simon Glassad8ec372022-04-24 23:31:13 -060012#include <bootdev.h>
13#include <bootflow.h>
14#include <bootmeth.h>
15#include <bootstd.h>
16#include <command.h>
Simon Glassad8ec372022-04-24 23:31:13 -060017#include <dm.h>
Simon Glassb71d7f72023-05-10 16:34:46 -060018#include <extlinux.h>
Simon Glassad8ec372022-04-24 23:31:13 -060019#include <fs.h>
20#include <malloc.h>
21#include <mapmem.h>
22#include <mmc.h>
23#include <pxe_utils.h>
24
Martyn Welch93a0d162024-10-09 14:15:40 +010025struct extlinux_plat {
26 bool use_fallback;
27};
28
29enum extlinux_option_type {
30 EO_FALLBACK,
31 EO_INVALID
32};
33
34struct extlinux_option {
35 char *name;
36 enum extlinux_option_type option;
37};
38
39static const struct extlinux_option options[] = {
40 {"fallback", EO_FALLBACK},
41 {NULL, EO_INVALID}
42};
43
44static 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 Glassb71d7f72023-05-10 16:34:46 -060058static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
Simon Glassf6d71a82022-07-30 15:52:19 -060059{
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 Glassb71d7f72023-05-10 16:34:46 -060071static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
Simon Glassa686ba62024-11-15 16:19:19 -070072 char *file_addr, enum bootflow_img_t type,
73 ulong *sizep)
Simon Glassad8ec372022-04-24 23:31:13 -060074{
Simon Glassb71d7f72023-05-10 16:34:46 -060075 struct extlinux_info *info = ctx->userdata;
Simon Glassad8ec372022-04-24 23:31:13 -060076 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 Glassa686ba62024-11-15 16:19:19 -070084 type, sizep);
Simon Glassad8ec372022-04-24 23:31:13 -060085 if (ret)
86 return log_msg_ret("read", ret);
87
88 return 0;
89}
90
Simon Glassb71d7f72023-05-10 16:34:46 -060091static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glassad8ec372022-04-24 23:31:13 -060092{
93 int ret;
94
95 /* This only works on block devices */
Simon Glass18c50402023-01-17 10:47:54 -070096 ret = bootflow_iter_check_blk(iter);
Simon Glassad8ec372022-04-24 23:31:13 -060097 if (ret)
98 return log_msg_ret("blk", ret);
99
100 return 0;
101}
102
Simon Glass72b7b192023-01-06 08:52:33 -0600103/**
Simon Glassb71d7f72023-05-10 16:34:46 -0600104 * extlinux_fill_info() - Decode the extlinux file to find out its info
Simon Glass72b7b192023-01-06 08:52:33 -0600105 *
106 * @bflow: Bootflow to process
107 * @return 0 if OK, -ve on error
108 */
Simon Glassb71d7f72023-05-10 16:34:46 -0600109static int extlinux_fill_info(struct bootflow *bflow)
Simon Glass72b7b192023-01-06 08:52:33 -0600110{
Simon Glassf7e87f92025-03-18 16:20:44 +0100111 struct membuf mb;
Simon Glass72b7b192023-01-06 08:52:33 -0600112 char line[200];
113 char *data;
114 int len;
115
116 log_debug("parsing bflow file size %x\n", bflow->size);
Simon Glassceefc782025-03-18 16:20:42 +0100117 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 Glass72b7b192023-01-06 08:52:33 -0600120 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 Glassb71d7f72023-05-10 16:34:46 -0600135static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600136{
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 Glassb71d7f72023-05-10 16:34:46 -0600158 ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
Simon Glassad8ec372022-04-24 23:31:13 -0600159 } while (ret && prefixes && prefixes[++i]);
160 if (ret)
161 return log_msg_ret("try", ret);
162 size = bflow->size;
163
Tom Rini0160b2c2025-01-15 17:34:26 -0600164 ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN,
165 BFI_EXTLINUX_CFG);
Simon Glassad8ec372022-04-24 23:31:13 -0600166 if (ret)
167 return log_msg_ret("read", ret);
168
Simon Glassb71d7f72023-05-10 16:34:46 -0600169 ret = extlinux_fill_info(bflow);
Simon Glass72b7b192023-01-06 08:52:33 -0600170 if (ret)
171 return log_msg_ret("inf", ret);
172
Simon Glassad8ec372022-04-24 23:31:13 -0600173 return 0;
174}
175
Simon Glassb71d7f72023-05-10 16:34:46 -0600176static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600177{
178 struct cmd_tbl cmdtp = {}; /* dummy */
179 struct pxe_context ctx;
Simon Glassb71d7f72023-05-10 16:34:46 -0600180 struct extlinux_info info;
Martyn Welch93a0d162024-10-09 14:15:40 +0100181 struct extlinux_plat *plat;
Simon Glassad8ec372022-04-24 23:31:13 -0600182 ulong addr;
183 int ret;
184
185 addr = map_to_sysmem(bflow->buf);
186 info.dev = dev;
187 info.bflow = bflow;
Martyn Welch93a0d162024-10-09 14:15:40 +0100188
189 plat = dev_get_plat(dev);
190
Simon Glassb71d7f72023-05-10 16:34:46 -0600191 ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
Martyn Welch93a0d162024-10-09 14:15:40 +0100192 bflow->fname, false, plat->use_fallback);
Simon Glassad8ec372022-04-24 23:31:13 -0600193 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 Welch93a0d162024-10-09 14:15:40 +0100203static 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 Glassb71d7f72023-05-10 16:34:46 -0600235static int extlinux_bootmeth_bind(struct udevice *dev)
Simon Glassad8ec372022-04-24 23:31:13 -0600236{
237 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
238
239 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
Simon Glassb71d7f72023-05-10 16:34:46 -0600240 "Extlinux boot from a block device" : "extlinux";
Simon Glassad8ec372022-04-24 23:31:13 -0600241
242 return 0;
243}
244
Simon Glassb71d7f72023-05-10 16:34:46 -0600245static 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 Glassad8ec372022-04-24 23:31:13 -0600249 .read_file = bootmeth_common_read_file,
Simon Glassb71d7f72023-05-10 16:34:46 -0600250 .boot = extlinux_boot,
Martyn Welch93a0d162024-10-09 14:15:40 +0100251 .set_property = extlinux_set_property,
Simon Glassad8ec372022-04-24 23:31:13 -0600252};
253
Simon Glassb71d7f72023-05-10 16:34:46 -0600254static const struct udevice_id extlinux_bootmeth_ids[] = {
255 { .compatible = "u-boot,extlinux" },
Simon Glassad8ec372022-04-24 23:31:13 -0600256 { }
257};
258
Simon Glasse5cb0182024-07-17 09:30:59 +0100259/* Put a number before 'extlinux' to provide a default ordering */
Simon Glass2f27e472023-08-19 16:49:35 -0600260U_BOOT_DRIVER(bootmeth_1extlinux) = {
Simon Glassb71d7f72023-05-10 16:34:46 -0600261 .name = "bootmeth_extlinux",
Simon Glassad8ec372022-04-24 23:31:13 -0600262 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600263 .of_match = extlinux_bootmeth_ids,
264 .ops = &extlinux_bootmeth_ops,
265 .bind = extlinux_bootmeth_bind,
Martyn Welch93a0d162024-10-09 14:15:40 +0100266 .plat_auto = sizeof(struct extlinux_plat)
Simon Glassad8ec372022-04-24 23:31:13 -0600267};