blob: b8ba4eca7abd10a96fb33506497e442c07ebc8cb [file] [log] [blame]
Simon Glassa950d312022-04-24 23:31:08 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#define LOG_CATEGORY UCLASS_BOOTSTD
8
9#include <common.h>
10#include <blk.h>
11#include <bootflow.h>
12#include <bootmeth.h>
13#include <bootstd.h>
14#include <dm.h>
15#include <env_internal.h>
16#include <fs.h>
17#include <malloc.h>
18#include <mapmem.h>
19#include <dm/uclass-internal.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
24{
25 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
26
27 if (!ops->check)
28 return 0;
29
30 return ops->check(dev, iter);
31}
32
33int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
34{
35 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
36
37 if (!ops->read_bootflow)
38 return -ENOSYS;
39
40 return ops->read_bootflow(dev, bflow);
41}
42
43int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
44{
45 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
46
47 if (!ops->boot)
48 return -ENOSYS;
49
50 return ops->boot(dev, bflow);
51}
52
53int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
54 const char *file_path, ulong addr, ulong *sizep)
55{
56 const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
57
58 if (!ops->read_file)
59 return -ENOSYS;
60
61 return ops->read_file(dev, bflow, file_path, addr, sizep);
62}
63
64/**
65 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
66 *
67 * This sets up the ordering information in @iter, based on the selected
68 * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no
69 * ordering there, then all bootmethods are added
70 *
71 * @iter: Iterator to update with the order
72 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
73 * on other error
74 */
75int bootmeth_setup_iter_order(struct bootflow_iter *iter)
76{
77 struct bootstd_priv *std;
78 struct udevice **order;
79 int count;
80 int ret;
81
82 ret = bootstd_get_priv(&std);
83 if (ret)
84 return ret;
85
86 /* Create an array large enough */
87 count = std->bootmeth_count ? std->bootmeth_count :
88 uclass_id_count(UCLASS_BOOTMETH);
89 if (!count)
90 return log_msg_ret("count", -ENOENT);
91
92 order = calloc(count, sizeof(struct udevice *));
93 if (!order)
94 return log_msg_ret("order", -ENOMEM);
95
96 /* If we have an ordering, copy it */
97 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
98 memcpy(order, std->bootmeth_order,
99 count * sizeof(struct bootmeth *));
100 } else {
101 struct udevice *dev;
102 int i, upto;
103
104 /*
105 * Get a list of bootmethods, in seq order (i.e. using aliases).
106 * There may be gaps so try to count up high enough to find them
107 * all.
108 */
109 for (i = 0, upto = 0; upto < count && i < 20 + count * 2; i++) {
110 ret = uclass_get_device_by_seq(UCLASS_BOOTMETH, i,
111 &dev);
112 if (!ret)
113 order[upto++] = dev;
114 }
115 count = upto;
116 }
Simon Glass10d16fa2022-07-30 15:52:18 -0600117 if (!count)
118 return log_msg_ret("count2", -ENOENT);
Simon Glassa950d312022-04-24 23:31:08 -0600119
120 iter->method_order = order;
121 iter->num_methods = count;
122 iter->cur_method = 0;
123
124 return 0;
125}
126
127int bootmeth_set_order(const char *order_str)
128{
129 struct bootstd_priv *std;
130 struct udevice **order;
131 int count, ret, i, len;
132 const char *s, *p;
133
134 ret = bootstd_get_priv(&std);
135 if (ret)
136 return ret;
137
138 if (!order_str) {
139 free(std->bootmeth_order);
140 std->bootmeth_order = NULL;
141 std->bootmeth_count = 0;
142 return 0;
143 }
144
145 /* Create an array large enough */
146 count = uclass_id_count(UCLASS_BOOTMETH);
147 if (!count)
148 return log_msg_ret("count", -ENOENT);
149
150 order = calloc(count + 1, sizeof(struct udevice *));
151 if (!order)
152 return log_msg_ret("order", -ENOMEM);
153
154 for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
155 struct udevice *dev;
156
157 p = strchrnul(s, ' ');
158 len = p - s;
159 ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
160 &dev);
161 if (ret) {
162 printf("Unknown bootmeth '%.*s'\n", len, s);
163 free(order);
164 return ret;
165 }
166 order[i] = dev;
167 }
168 order[i] = NULL;
169 free(std->bootmeth_order);
170 std->bootmeth_order = order;
171 std->bootmeth_count = i;
172
173 return 0;
174}
175
176/**
177 * setup_fs() - Set up read to read a file
178 *
179 * We must redo the setup before each filesystem operation. This function
180 * handles that, including setting the filesystem type if a block device is not
181 * being used
182 *
183 * @bflow: Information about file to try
184 * @desc: Block descriptor to read from (NULL if not a block device)
185 * Return: 0 if OK, -ve on error
186 */
187static int setup_fs(struct bootflow *bflow, struct blk_desc *desc)
188{
189 int ret;
190
191 if (desc) {
192 ret = fs_set_blk_dev_with_part(desc, bflow->part);
193 if (ret)
194 return log_msg_ret("set", ret);
195 } else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
196 fs_set_type(bflow->fs_type);
197 }
198
199 return 0;
200}
201
202int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
203 const char *prefix, const char *fname)
204{
205 char path[200];
206 loff_t size;
207 int ret, ret2;
208
209 snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
210 log_debug("trying: %s\n", path);
211
212 free(bflow->fname);
213 bflow->fname = strdup(path);
214 if (!bflow->fname)
215 return log_msg_ret("name", -ENOMEM);
216
217 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
218 fs_set_type(bflow->fs_type);
219
220 ret = fs_size(path, &size);
221 log_debug(" %s - err=%d\n", path, ret);
222
223 /* Sadly FS closes the file after fs_size() so we must redo this */
224 ret2 = setup_fs(bflow, desc);
225 if (ret2)
226 return log_msg_ret("fs", ret2);
227
228 if (ret)
229 return log_msg_ret("size", ret);
230
231 bflow->size = size;
232 bflow->state = BOOTFLOWST_FILE;
233
234 return 0;
235}
236
237int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
238{
239 loff_t bytes_read;
240 ulong addr;
241 char *buf;
242 uint size;
243 int ret;
244
245 size = bflow->size;
246 log_debug(" - script file size %x\n", size);
247 if (size > size_limit)
248 return log_msg_ret("chk", -E2BIG);
249
250 buf = memalign(align, size + 1);
251 if (!buf)
252 return log_msg_ret("buf", -ENOMEM);
253 addr = map_to_sysmem(buf);
254
255 ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read);
256 if (ret) {
257 free(buf);
258 return log_msg_ret("read", ret);
259 }
260 if (size != bytes_read)
261 return log_msg_ret("bread", -EINVAL);
262 buf[size] = '\0';
263 bflow->state = BOOTFLOWST_READY;
264 bflow->buf = buf;
265
266 return 0;
267}
268
269int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
270 const char *file_path, ulong addr, ulong *sizep)
271{
272 struct blk_desc *desc = NULL;
273 loff_t len_read;
274 loff_t size;
275 int ret;
276
277 if (bflow->blk)
278 desc = dev_get_uclass_plat(bflow->blk);
279
280 ret = setup_fs(bflow, desc);
281 if (ret)
282 return log_msg_ret("fs", ret);
283
284 ret = fs_size(file_path, &size);
285 if (ret)
286 return log_msg_ret("size", ret);
287 if (size > *sizep)
288 return log_msg_ret("spc", -ENOSPC);
289
290 ret = setup_fs(bflow, desc);
291 if (ret)
292 return log_msg_ret("fs", ret);
293
294 ret = fs_read(file_path, addr, 0, 0, &len_read);
295 if (ret)
296 return ret;
297 *sizep = len_read;
298
299 return 0;
300}
301
302#ifdef CONFIG_BOOTSTD_FULL
303/**
304 * on_bootmeths() - Update the bootmeth order
305 *
306 * This will check for a valid baudrate and only apply it if valid.
307 */
308static int on_bootmeths(const char *name, const char *value, enum env_op op,
309 int flags)
310{
311 int ret;
312
313 switch (op) {
314 case env_op_create:
315 case env_op_overwrite:
316 ret = bootmeth_set_order(value);
317 if (ret)
318 return 1;
319 return 0;
320 case env_op_delete:
321 bootmeth_set_order(NULL);
322 fallthrough;
323 default:
324 return 0;
325 }
326}
327U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
328#endif /* CONFIG_BOOTSTD_FULL */
329
330UCLASS_DRIVER(bootmeth) = {
331 .id = UCLASS_BOOTMETH,
332 .name = "bootmeth",
333 .flags = DM_UC_FLAG_SEQ_ALIAS,
334 .per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
335};