1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Bootmethod for extlinux boot from a block device
4 *
5 * Copyright 2021 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9 #define LOG_CATEGORY UCLASS_BOOTSTD
10
11 #include <asm/cache.h>
12 #include <bootdev.h>
13 #include <bootflow.h>
14 #include <bootmeth.h>
15 #include <bootstd.h>
16 #include <command.h>
17 #include <dm.h>
18 #include <extlinux.h>
19 #include <fs.h>
20 #include <malloc.h>
21 #include <mapmem.h>
22 #include <mmc.h>
23 #include <pxe_utils.h>
24
25 struct extlinux_plat {
26 bool use_fallback;
27 };
28
29 enum extlinux_option_type {
30 EO_FALLBACK,
31 EO_INVALID
32 };
33
34 struct extlinux_option {
35 char *name;
36 enum extlinux_option_type option;
37 };
38
39 static const struct extlinux_option options[] = {
40 {"fallback", EO_FALLBACK},
41 {NULL, EO_INVALID}
42 };
43
get_option(const char * option)44 static 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
extlinux_get_state_desc(struct udevice * dev,char * buf,int maxsize)58 static int extlinux_get_state_desc(struct udevice *dev, char *buf, int maxsize)
59 {
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
extlinux_getfile(struct pxe_context * ctx,const char * file_path,char * file_addr,enum bootflow_img_t type,ulong * sizep)71 static int extlinux_getfile(struct pxe_context *ctx, const char *file_path,
72 char *file_addr, enum bootflow_img_t type,
73 ulong *sizep)
74 {
75 struct extlinux_info *info = ctx->userdata;
76 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,
84 type, sizep);
85 if (ret)
86 return log_msg_ret("read", ret);
87
88 return 0;
89 }
90
extlinux_check(struct udevice * dev,struct bootflow_iter * iter)91 static int extlinux_check(struct udevice *dev, struct bootflow_iter *iter)
92 {
93 int ret;
94
95 /* This only works on block devices */
96 ret = bootflow_iter_check_blk(iter);
97 if (ret)
98 return log_msg_ret("blk", ret);
99
100 return 0;
101 }
102
103 /**
104 * extlinux_fill_info() - Decode the extlinux file to find out its info
105 *
106 * @bflow: Bootflow to process
107 * @return 0 if OK, -ve on error
108 */
extlinux_fill_info(struct bootflow * bflow)109 static int extlinux_fill_info(struct bootflow *bflow)
110 {
111 struct membuf mb;
112 char line[200];
113 char *data;
114 int len;
115
116 log_debug("parsing bflow file size %x\n", bflow->size);
117 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) {
120 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
extlinux_read_bootflow(struct udevice * dev,struct bootflow * bflow)135 static int extlinux_read_bootflow(struct udevice *dev, struct bootflow *bflow)
136 {
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
158 ret = bootmeth_try_file(bflow, desc, prefix, EXTLINUX_FNAME);
159 } while (ret && prefixes && prefixes[++i]);
160 if (ret)
161 return log_msg_ret("try", ret);
162 size = bflow->size;
163
164 ret = bootmeth_alloc_file(bflow, 0x10000, ARCH_DMA_MINALIGN,
165 BFI_EXTLINUX_CFG);
166 if (ret)
167 return log_msg_ret("read", ret);
168
169 ret = extlinux_fill_info(bflow);
170 if (ret)
171 return log_msg_ret("inf", ret);
172
173 return 0;
174 }
175
extlinux_boot(struct udevice * dev,struct bootflow * bflow)176 static int extlinux_boot(struct udevice *dev, struct bootflow *bflow)
177 {
178 struct cmd_tbl cmdtp = {}; /* dummy */
179 struct pxe_context ctx;
180 struct extlinux_info info;
181 struct extlinux_plat *plat;
182 ulong addr;
183 int ret;
184
185 addr = map_to_sysmem(bflow->buf);
186 info.dev = dev;
187 info.bflow = bflow;
188
189 plat = dev_get_plat(dev);
190
191 ret = pxe_setup_ctx(&ctx, &cmdtp, extlinux_getfile, &info, true,
192 bflow->fname, false, plat->use_fallback);
193 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
extlinux_set_property(struct udevice * dev,const char * property,const char * value)203 static 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
extlinux_bootmeth_bind(struct udevice * dev)235 static int extlinux_bootmeth_bind(struct udevice *dev)
236 {
237 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
238
239 plat->desc = IS_ENABLED(CONFIG_BOOTSTD_FULL) ?
240 "Extlinux boot from a block device" : "extlinux";
241
242 return 0;
243 }
244
245 static struct bootmeth_ops extlinux_bootmeth_ops = {
246 .get_state_desc = extlinux_get_state_desc,
247 .check = extlinux_check,
248 .read_bootflow = extlinux_read_bootflow,
249 .read_file = bootmeth_common_read_file,
250 .boot = extlinux_boot,
251 .set_property = extlinux_set_property,
252 };
253
254 static const struct udevice_id extlinux_bootmeth_ids[] = {
255 { .compatible = "u-boot,extlinux" },
256 { }
257 };
258
259 /* Put a number before 'extlinux' to provide a default ordering */
260 U_BOOT_DRIVER(bootmeth_1extlinux) = {
261 .name = "bootmeth_extlinux",
262 .id = UCLASS_BOOTMETH,
263 .of_match = extlinux_bootmeth_ids,
264 .ops = &extlinux_bootmeth_ops,
265 .bind = extlinux_bootmeth_bind,
266 .plat_auto = sizeof(struct extlinux_plat)
267 };
268