1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright 2021 Google LLC
* Written by Simon Glass <sjg@chromium.org>
*/
#define LOG_CATEGORY UCLASS_BOOTSTD
#include <common.h>
#include <blk.h>
#include <bootflow.h>
#include <bootmeth.h>
#include <bootstd.h>
#include <dm.h>
#include <env_internal.h>
#include <fs.h>
#include <malloc.h>
#include <mapmem.h>
#include <dm/uclass-internal.h>
DECLARE_GLOBAL_DATA_PTR;
int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->get_state_desc)
return -ENOSYS;
return ops->get_state_desc(dev, buf, maxsize);
}
int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->check)
return 0;
return ops->check(dev, iter);
}
int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->read_bootflow)
return -ENOSYS;
return ops->read_bootflow(dev, bflow);
}
int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
char *buf, int size)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->set_bootflow)
return -ENOSYS;
return ops->set_bootflow(dev, bflow, buf, size);
}
int bootmeth_boot(struct udevice *dev, struct bootflow *bflow)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->boot)
return -ENOSYS;
return ops->boot(dev, bflow);
}
int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
const char *file_path, ulong addr, ulong *sizep)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->read_file)
return -ENOSYS;
return ops->read_file(dev, bflow, file_path, addr, sizep);
}
int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow)
{
const struct bootmeth_ops *ops = bootmeth_get_ops(dev);
if (!ops->read_bootflow)
return -ENOSYS;
bootflow_init(bflow, NULL, dev);
return ops->read_bootflow(dev, bflow);
}
int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
{
struct bootstd_priv *std;
struct udevice **order;
int count;
int ret;
ret = bootstd_get_priv(&std);
if (ret)
return ret;
/* Create an array large enough */
count = std->bootmeth_count ? std->bootmeth_count :
uclass_id_count(UCLASS_BOOTMETH);
if (!count)
return log_msg_ret("count", -ENOENT);
order = calloc(count, sizeof(struct udevice *));
if (!order)
return log_msg_ret("order", -ENOMEM);
/* If we have an ordering, copy it */
if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && std->bootmeth_count) {
int i;
/*
* We don't support skipping global bootmeths. Instead, the user
* should omit them from the ordering
*/
if (!include_global)
return log_msg_ret("glob", -EPERM);
memcpy(order, std->bootmeth_order,
count * sizeof(struct bootmeth *));
if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
for (i = 0; i < count; i++) {
struct udevice *dev = order[i];
struct bootmeth_uc_plat *ucp;
bool is_global;
ucp = dev_get_uclass_plat(dev);
is_global = ucp->flags &
BOOTMETHF_GLOBAL;
if (is_global) {
iter->first_glob_method = i;
break;
}
}
}
} else {
struct udevice *dev;
int i, upto, pass;
/*
* Do two passes, one to find the normal bootmeths and another
* to find the global ones, if required, The global ones go at
* the end.
*/
for (pass = 0, upto = 0; pass < 1 + include_global; pass++) {
if (pass)
iter->first_glob_method = upto;
/*
* Get a list of bootmethods, in seq order (i.e. using
* aliases). There may be gaps so try to count up high
* enough to find them all.
*/
for (i = 0; upto < count && i < 20 + count * 2; i++) {
struct bootmeth_uc_plat *ucp;
bool is_global;
ret = uclass_get_device_by_seq(UCLASS_BOOTMETH,
i, &dev);
if (ret)
continue;
ucp = dev_get_uclass_plat(dev);
is_global =
IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
(ucp->flags & BOOTMETHF_GLOBAL);
if (pass ? is_global : !is_global)
order[upto++] = dev;
}
}
count = upto;
}
if (!count)
return log_msg_ret("count2", -ENOENT);
if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
iter->first_glob_method != -1 && iter->first_glob_method != count) {
iter->cur_method = iter->first_glob_method;
iter->doing_global = true;
}
iter->method_order = order;
iter->num_methods = count;
return 0;
}
int bootmeth_set_order(const char *order_str)
{
struct bootstd_priv *std;
struct udevice **order;
int count, ret, i, len;
const char *s, *p;
ret = bootstd_get_priv(&std);
if (ret)
return ret;
if (!order_str) {
free(std->bootmeth_order);
std->bootmeth_order = NULL;
std->bootmeth_count = 0;
return 0;
}
/* Create an array large enough */
count = uclass_id_count(UCLASS_BOOTMETH);
if (!count)
return log_msg_ret("count", -ENOENT);
order = calloc(count + 1, sizeof(struct udevice *));
if (!order)
return log_msg_ret("order", -ENOMEM);
for (i = 0, s = order_str; *s && i < count; s = p + (*p == ' '), i++) {
struct udevice *dev;
p = strchrnul(s, ' ');
len = p - s;
ret = uclass_find_device_by_namelen(UCLASS_BOOTMETH, s, len,
&dev);
if (ret) {
printf("Unknown bootmeth '%.*s'\n", len, s);
free(order);
return ret;
}
order[i] = dev;
}
order[i] = NULL;
free(std->bootmeth_order);
std->bootmeth_order = order;
std->bootmeth_count = i;
return 0;
}
int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc)
{
int ret;
if (desc) {
ret = fs_set_blk_dev_with_part(desc, bflow->part);
if (ret)
return log_msg_ret("set", ret);
} else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type) {
fs_set_type(bflow->fs_type);
}
return 0;
}
int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
const char *prefix, const char *fname)
{
char path[200];
loff_t size;
int ret, ret2;
snprintf(path, sizeof(path), "%s%s", prefix ? prefix : "", fname);
log_debug("trying: %s\n", path);
free(bflow->fname);
bflow->fname = strdup(path);
if (!bflow->fname)
return log_msg_ret("name", -ENOMEM);
if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && bflow->fs_type)
fs_set_type(bflow->fs_type);
ret = fs_size(path, &size);
log_debug(" %s - err=%d\n", path, ret);
/* Sadly FS closes the file after fs_size() so we must redo this */
ret2 = bootmeth_setup_fs(bflow, desc);
if (ret2)
return log_msg_ret("fs", ret2);
if (ret)
return log_msg_ret("size", ret);
bflow->size = size;
bflow->state = BOOTFLOWST_FILE;
return 0;
}
int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align)
{
void *buf;
uint size;
int ret;
size = bflow->size;
log_debug(" - script file size %x\n", size);
if (size > size_limit)
return log_msg_ret("chk", -E2BIG);
ret = fs_read_alloc(bflow->fname, bflow->size, align, &buf);
if (ret)
return log_msg_ret("all", ret);
bflow->state = BOOTFLOWST_READY;
bflow->buf = buf;
return 0;
}
int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
void **bufp, uint *sizep)
{
struct blk_desc *desc = NULL;
char path[200];
loff_t size;
void *buf;
int ret;
snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
log_debug("trying: %s\n", path);
if (bflow->blk)
desc = dev_get_uclass_plat(bflow->blk);
ret = bootmeth_setup_fs(bflow, desc);
if (ret)
return log_msg_ret("fs", ret);
ret = fs_size(path, &size);
log_debug(" %s - err=%d\n", path, ret);
ret = bootmeth_setup_fs(bflow, desc);
if (ret)
return log_msg_ret("fs", ret);
ret = fs_read_alloc(path, size, 0, &buf);
if (ret)
return log_msg_ret("all", ret);
*bufp = buf;
*sizep = size;
return 0;
}
int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
const char *file_path, ulong addr, ulong *sizep)
{
struct blk_desc *desc = NULL;
loff_t len_read;
loff_t size;
int ret;
if (bflow->blk)
desc = dev_get_uclass_plat(bflow->blk);
ret = bootmeth_setup_fs(bflow, desc);
if (ret)
return log_msg_ret("fs", ret);
ret = fs_size(file_path, &size);
if (ret)
return log_msg_ret("size", ret);
if (size > *sizep)
return log_msg_ret("spc", -ENOSPC);
ret = bootmeth_setup_fs(bflow, desc);
if (ret)
return log_msg_ret("fs", ret);
ret = fs_read(file_path, addr, 0, 0, &len_read);
if (ret)
return ret;
*sizep = len_read;
return 0;
}
#ifdef CONFIG_BOOTSTD_FULL
/**
* on_bootmeths() - Update the bootmeth order
*
* This will check for a valid list of bootmeths and only apply it if valid.
*/
static int on_bootmeths(const char *name, const char *value, enum env_op op,
int flags)
{
int ret;
switch (op) {
case env_op_create:
case env_op_overwrite:
ret = bootmeth_set_order(value);
if (ret)
return 1;
return 0;
case env_op_delete:
bootmeth_set_order(NULL);
fallthrough;
default:
return 0;
}
}
U_BOOT_ENV_CALLBACK(bootmeths, on_bootmeths);
#endif /* CONFIG_BOOTSTD_FULL */
UCLASS_DRIVER(bootmeth) = {
.id = UCLASS_BOOTMETH,
.name = "bootmeth",
.flags = DM_UC_FLAG_SEQ_ALIAS,
.per_device_plat_auto = sizeof(struct bootmeth_uc_plat),
};
|