blob: c6ae6dffcb7e3d03b4cc80655c63e542a6ef243b [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,
72 char *file_addr, ulong *sizep)
Simon Glassad8ec372022-04-24 23:31:13 -060073{
Simon Glassb71d7f72023-05-10 16:34:46 -060074 struct extlinux_info *info = ctx->userdata;
Simon Glassad8ec372022-04-24 23:31:13 -060075 ulong addr;
76 int ret;
77
78 addr = simple_strtoul(file_addr, NULL, 16);
79
80 /* Allow up to 1GB */
81 *sizep = 1 << 30;
82 ret = bootmeth_read_file(info->dev, info->bflow, file_path, addr,
83 sizep);
84 if (ret)
85 return log_msg_ret("read", ret);
86
87 return 0;
88}
89
Simon Glassb71d7f72023-05-10 16:34:46 -060090static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
Simon Glassad8ec372022-04-24 23:31:13 -060091{
92 int ret;
93
94 /* This only works on block devices */
Simon Glass18c50402023-01-17 10:47:54 -070095 ret = bootflow_iter_check_blk(iter);
Simon Glassad8ec372022-04-24 23:31:13 -060096 if (ret)
97 return log_msg_ret("blk", ret);
98
99 return 0;
100}
101
Simon Glass72b7b192023-01-06 08:52:33 -0600102/**
Simon Glassb71d7f72023-05-10 16:34:46 -0600103 * extlinux_fill_info() - Decode the extlinux file to find out its info
Simon Glass72b7b192023-01-06 08:52:33 -0600104 *
105 * @bflow: Bootflow to process
106 * @return 0 if OK, -ve on error
107 */
Simon Glassb71d7f72023-05-10 16:34:46 -0600108static int extlinux_fill_info(struct bootflow *bflow)
Simon Glass72b7b192023-01-06 08:52:33 -0600109{
110 struct membuff mb;
111 char line[200];
112 char *data;
113 int len;
114
115 log_debug("parsing bflow file size %x\n", bflow->size);
116 membuff_init(&mb, bflow->buf, bflow->size);
117 membuff_putraw(&mb, bflow->size, true, &data);
Ion Agorria7e06c1f2024-01-05 09:22:10 +0200118 while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' ', true), len) {
Simon Glass72b7b192023-01-06 08:52:33 -0600119 char *tok, *p = line;
120
121 tok = strsep(&p, " ");
122 if (p) {
123 if (!strcmp("label", tok)) {
124 bflow->os_name = strdup(p);
125 if (!bflow->os_name)
126 return log_msg_ret("os", -ENOMEM);
127 }
128 }
129 }
130
131 return 0;
132}
133
Simon Glassb71d7f72023-05-10 16:34:46 -0600134static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600135{
136 struct blk_desc *desc;
137 const char *const *prefixes;
138 struct udevice *bootstd;
139 const char *prefix;
140 loff_t size;
141 int ret, i;
142
143 ret = uclass_first_device_err(UCLASS_BOOTSTD, &bootstd);
144 if (ret)
145 return log_msg_ret("std", ret);
146
147 /* If a block device, we require a partition table */
148 if (bflow->blk && !bflow->part)
149 return -ENOENT;
150
151 prefixes = bootstd_get_prefixes(bootstd);
152 i = 0;
153 desc = bflow->blk ? dev_get_uclass_plat(bflow->blk) : NULL;
154 do {
155 prefix = prefixes ? prefixes[i] : NULL;
156
Simon Glassb71d7f72023-05-10 16:34:46 -0600157 ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
Simon Glassad8ec372022-04-24 23:31:13 -0600158 } while (ret && prefixes && prefixes[++i]);
159 if (ret)
160 return log_msg_ret("try", ret);
161 size = bflow->size;
162
Nam Caoc39c3532024-11-07 16:01:05 +0100163 ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN);
Simon Glassad8ec372022-04-24 23:31:13 -0600164 if (ret)
165 return log_msg_ret("read", ret);
166
Simon Glassb71d7f72023-05-10 16:34:46 -0600167 ret = extlinux_fill_info(bflow);
Simon Glass72b7b192023-01-06 08:52:33 -0600168 if (ret)
169 return log_msg_ret("inf", ret);
170
Simon Glassad8ec372022-04-24 23:31:13 -0600171 return 0;
172}
173
Simon Glassb71d7f72023-05-10 16:34:46 -0600174static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
Simon Glassad8ec372022-04-24 23:31:13 -0600175{
176 struct cmd_tbl cmdtp = {}; /* dummy */
177 struct pxe_context ctx;
Simon Glassb71d7f72023-05-10 16:34:46 -0600178 struct extlinux_info info;
Martyn Welch93a0d162024-10-09 14:15:40 +0100179 struct extlinux_plat *plat;
Simon Glassad8ec372022-04-24 23:31:13 -0600180 ulong addr;
181 int ret;
182
183 addr = map_to_sysmem(bflow->buf);
184 info.dev = dev;
185 info.bflow = bflow;
Martyn Welch93a0d162024-10-09 14:15:40 +0100186
187 plat = dev_get_plat(dev);
188
Simon Glassb71d7f72023-05-10 16:34:46 -0600189 ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
Martyn Welch93a0d162024-10-09 14:15:40 +0100190 bflow->fname, false, plat->use_fallback);
Simon Glassad8ec372022-04-24 23:31:13 -0600191 if (ret)
192 return log_msg_ret("ctx", -EINVAL);
193
194 ret = pxe_process(&ctx, addr, false);
195 if (ret)
196 return log_msg_ret("bread", -EINVAL);
197
198 return 0;
199}
200
Martyn Welch93a0d162024-10-09 14:15:40 +0100201static int extlinux_set_property(struct udevice *dev, const char *property, const char *value)
202{
203 struct extlinux_plat *plat;
204 static enum extlinux_option_type option;
205
206 plat = dev_get_plat(dev);
207
208 option = get_option(property);
209 if (option == EO_INVALID) {
210 printf("Invalid option\n");
211 return -EINVAL;
212 }
213
214 switch (option) {
215 case EO_FALLBACK:
216 if (!strcmp(value, "1")) {
217 plat->use_fallback = true;
218 } else if (!strcmp(value, "0")) {
219 plat->use_fallback = false;
220 } else {
221 printf("Unexpected value '%s'\n", value);
222 return -EINVAL;
223 }
224 break;
225 default:
226 printf("Unrecognised property '%s'\n", property);
227 return -EINVAL;
228 }
229
230 return 0;
231}
232
Simon Glassb71d7f72023-05-10 16:34:46 -0600233static int extlinux_bootmeth_bind(struct udevice *dev)
Simon Glassad8ec372022-04-24 23:31:13 -0600234{
235 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
236
237 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
Simon Glassb71d7f72023-05-10 16:34:46 -0600238 "Extlinux boot from a block device" : "extlinux";
Simon Glassad8ec372022-04-24 23:31:13 -0600239
240 return 0;
241}
242
Simon Glassb71d7f72023-05-10 16:34:46 -0600243static struct bootmeth_ops extlinux_bootmeth_ops = {
244 .get_state_desc = extlinux_get_state_desc,
245 .check = extlinux_check,
246 .read_bootflow = extlinux_read_bootflow,
Simon Glassad8ec372022-04-24 23:31:13 -0600247 .read_file = bootmeth_common_read_file,
Simon Glassb71d7f72023-05-10 16:34:46 -0600248 .boot = extlinux_boot,
Martyn Welch93a0d162024-10-09 14:15:40 +0100249 .set_property = extlinux_set_property,
Simon Glassad8ec372022-04-24 23:31:13 -0600250};
251
Simon Glassb71d7f72023-05-10 16:34:46 -0600252static const struct udevice_id extlinux_bootmeth_ids[] = {
253 { .compatible = "u-boot,extlinux" },
Simon Glassad8ec372022-04-24 23:31:13 -0600254 { }
255};
256
Simon Glasse5cb0182024-07-17 09:30:59 +0100257/* Put a number before 'extlinux' to provide a default ordering */
Simon Glass2f27e472023-08-19 16:49:35 -0600258U_BOOT_DRIVER(bootmeth_1extlinux) = {
Simon Glassb71d7f72023-05-10 16:34:46 -0600259 .name = "bootmeth_extlinux",
Simon Glassad8ec372022-04-24 23:31:13 -0600260 .id = UCLASS_BOOTMETH,
Simon Glassb71d7f72023-05-10 16:34:46 -0600261 .of_match = extlinux_bootmeth_ids,
262 .ops = &extlinux_bootmeth_ops,
263 .bind = extlinux_bootmeth_bind,
Martyn Welch93a0d162024-10-09 14:15:40 +0100264 .plat_auto = sizeof(struct extlinux_plat)
Simon Glassad8ec372022-04-24 23:31:13 -0600265};