blob: be8fbf4df63b6d2f81967d4b718016a32783acdd [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
Simon Glassad8ec372022-04-24 23:31:13 -060011#include <bootdev.h>
12#include <bootflow.h>
13#include <bootmeth.h>
14#include <bootstd.h>
15#include <command.h>
Simon Glassad8ec372022-04-24 23:31:13 -060016#include <dm.h>
Simon Glassb71d7f72023-05-10 16:34:46 -060017#include <extlinux.h>
Simon Glassad8ec372022-04-24 23:31:13 -060018#include <fs.h>
19#include <malloc.h>
20#include <mapmem.h>
21#include <mmc.h>
22#include <pxe_utils.h>
23
Martyn Welch93a0d162024-10-09 14:15:40 +010024struct extlinux_plat {
25 bool use_fallback;
26};
27
28enum extlinux_option_type {
29 EO_FALLBACK,
30 EO_INVALID
31};
32
33struct extlinux_option {
34 char *name;
35 enum extlinux_option_type option;
36};
37
38static const struct extlinux_option options[] = {
39 {"fallback", EO_FALLBACK},
40 {NULL, EO_INVALID}
41};
42
43static 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 Glassb71d7f72023-05-10 16:34:46 -060057static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
Simon Glassf6d71a82022-07-30 15:52:19 -060058{
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 Glassb71d7f72023-05-10 16:34:46 -060070static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
71 char *file_addr, ulong *sizep)
Simon Glassad8ec372022-04-24 23:31:13 -060072{
Simon Glassb71d7f72023-05-10 16:34:46 -060073 struct extlinux_info *info = ctx->userdata;
Simon Glassad8ec372022-04-24 23:31:13 -060074 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 Glassb71d7f72023-05-10 16:34:46 -060089static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glassad8ec372022-04-24 23:31:13 -060090{
91 int ret;
92
93 /* This only works on block devices */
Simon Glass18c50402023-01-17 10:47:54 -070094 ret = bootflow_iter_check_blk(iter);
Simon Glassad8ec372022-04-24 23:31:13 -060095 if (ret)
96 return log_msg_ret("blk", ret);
97
98 return 0;
99}
100
Simon Glass72b7b192023-01-06 08:52:33 -0600101/**
Simon Glassb71d7f72023-05-10 16:34:46 -0600102 * extlinux_fill_info() - Decode the extlinux file to find out its info
Simon Glass72b7b192023-01-06 08:52:33 -0600103 *
104 * @bflow: Bootflow to process
105 * @return 0 if OK, -ve on error
106 */
Simon Glassb71d7f72023-05-10 16:34:46 -0600107static int extlinux_fill_info(struct bootflow *bflow)
Simon Glass72b7b192023-01-06 08:52:33 -0600108{
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 Agorria7e06c1f2024-01-05 09:22:10 +0200117 while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) {
Simon Glass72b7b192023-01-06 08:52:33 -0600118 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 Glassb71d7f72023-05-10 16:34:46 -0600133static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600134{
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 Glassb71d7f72023-05-10 16:34:46 -0600156 ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
Simon Glassad8ec372022-04-24 23:31:13 -0600157 } 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 Glassb71d7f72023-05-10 16:34:46 -0600166 ret = extlinux_fill_info(bflow);
Simon Glass72b7b192023-01-06 08:52:33 -0600167 if (ret)
168 return log_msg_ret("inf", ret);
169
Simon Glassad8ec372022-04-24 23:31:13 -0600170 return 0;
171}
172
Simon Glassb71d7f72023-05-10 16:34:46 -0600173static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600174{
175 struct cmd_tbl cmdtp = {}; /* dummy */
176 struct pxe_context ctx;
Simon Glassb71d7f72023-05-10 16:34:46 -0600177 struct extlinux_info info;
Martyn Welch93a0d162024-10-09 14:15:40 +0100178 struct extlinux_plat *plat;
Simon Glassad8ec372022-04-24 23:31:13 -0600179 ulong addr;
180 int ret;
181
182 addr = map_to_sysmem(bflow->buf);
183 info.dev = dev;
184 info.bflow = bflow;
Martyn Welch93a0d162024-10-09 14:15:40 +0100185
186 plat = dev_get_plat(dev);
187
Simon Glassb71d7f72023-05-10 16:34:46 -0600188 ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
Martyn Welch93a0d162024-10-09 14:15:40 +0100189 bflow->fname, false, plat->use_fallback);
Simon Glassad8ec372022-04-24 23:31:13 -0600190 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 Welch93a0d162024-10-09 14:15:40 +0100200static 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 Glassb71d7f72023-05-10 16:34:46 -0600232static int extlinux_bootmeth_bind(struct udevice *dev)
Simon Glassad8ec372022-04-24 23:31:13 -0600233{
234 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
235
236 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
Simon Glassb71d7f72023-05-10 16:34:46 -0600237 "Extlinux boot from a block device" : "extlinux";
Simon Glassad8ec372022-04-24 23:31:13 -0600238
239 return 0;
240}
241
Simon Glassb71d7f72023-05-10 16:34:46 -0600242static 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 Glassad8ec372022-04-24 23:31:13 -0600246 .read_file = bootmeth_common_read_file,
Simon Glassb71d7f72023-05-10 16:34:46 -0600247 .boot = extlinux_boot,
Martyn Welch93a0d162024-10-09 14:15:40 +0100248 .set_property = extlinux_set_property,
Simon Glassad8ec372022-04-24 23:31:13 -0600249};
250
Simon Glassb71d7f72023-05-10 16:34:46 -0600251static const struct udevice_id extlinux_bootmeth_ids[] = {
252 { .compatible = "u-boot,extlinux" },
Simon Glassad8ec372022-04-24 23:31:13 -0600253 { }
254};
255
Simon Glasse5cb0182024-07-17 09:30:59 +0100256/* Put a number before 'extlinux' to provide a default ordering */
Simon Glass2f27e472023-08-19 16:49:35 -0600257U_BOOT_DRIVER(bootmeth_1extlinux) = {
Simon Glassb71d7f72023-05-10 16:34:46 -0600258 .name = "bootmeth_extlinux",
Simon Glassad8ec372022-04-24 23:31:13 -0600259 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600260 .of_match = extlinux_bootmeth_ids,
261 .ops = &extlinux_bootmeth_ops,
262 .bind = extlinux_bootmeth_bind,
Martyn Welch93a0d162024-10-09 14:15:40 +0100263 .plat_auto = sizeof(struct extlinux_plat)
Simon Glassad8ec372022-04-24 23:31:13 -0600264};