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