Merge tag 'fsl-qoriq-2023-7-13' of https://source.denx.de/u-boot/custodians/u-boot...
[platform/kernel/u-boot.git] / include / bootflow.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright 2021 Google LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #ifndef __bootflow_h
8 #define __bootflow_h
9
10 #include <bootdev.h>
11 #include <dm/ofnode_decl.h>
12 #include <linux/list.h>
13
14 struct bootstd_priv;
15 struct expo;
16
17 enum {
18         BOOTFLOW_MAX_USED_DEVS  = 16,
19 };
20
21 /**
22  * enum bootflow_state_t - states that a particular bootflow can be in
23  *
24  * Only bootflows in state BOOTFLOWST_READY can be used to boot.
25  *
26  * See bootflow_state[] for the names for each of these
27  */
28 enum bootflow_state_t {
29         BOOTFLOWST_BASE,        /**< Nothing known yet */
30         BOOTFLOWST_MEDIA,       /**< Media exists */
31         BOOTFLOWST_PART,        /**< Partition exists */
32         BOOTFLOWST_FS,          /**< Filesystem exists */
33         BOOTFLOWST_FILE,        /**< Bootflow file exists */
34         BOOTFLOWST_READY,       /**< Bootflow file loaded */
35
36         BOOTFLOWST_COUNT
37 };
38
39 /**
40  * enum bootflow_flags_t - flags for bootflows
41  *
42  * @BOOTFLOWF_USE_PRIOR_FDT: Indicates that an FDT was not found by the bootmeth
43  *      and it is using the prior-stage FDT, which is the U-Boot control FDT.
44  *      This is only possible with the EFI bootmeth (distro-efi) and only when
45  *      CONFIG_OF_HAS_PRIOR_STAGE is enabled
46  */
47 enum bootflow_flags_t {
48         BOOTFLOWF_USE_PRIOR_FDT = 1 << 0,
49 };
50
51 /**
52  * struct bootflow - information about a bootflow
53  *
54  * This is connected into two separate linked lists:
55  *
56  *   bm_sibling - links all bootflows in the same bootdev
57  *   glob_sibling - links all bootflows in all bootdevs
58  *
59  * @bm_node: Points to siblings in the same bootdev
60  * @glob_node: Points to siblings in the global list (all bootdev)
61  * @dev: Bootdev device which produced this bootflow
62  * @blk: Block device which contains this bootflow, NULL if this is a network
63  *      device or sandbox 'host' device
64  * @part: Partition number (0 for whole device)
65  * @fs_type: Filesystem type (FS_TYPE...) if this is fixed by the media, else 0.
66  *      For example, the sandbox host-filesystem bootdev sets this to
67  *      FS_TYPE_SANDBOX
68  * @method: Bootmethod device used to perform the boot and read files
69  * @name: Name of bootflow (allocated)
70  * @state: Current state (enum bootflow_state_t)
71  * @subdir: Subdirectory to fetch files from (with trailing /), or NULL if none
72  * @fname: Filename of bootflow file (allocated)
73  * @logo: Logo to display for this bootflow (BMP format)
74  * @logo_size: Size of the logo in bytes
75  * @buf: Bootflow file contents (allocated)
76  * @size: Size of bootflow file in bytes
77  * @err: Error number received (0 if OK)
78  * @os_name: Name of the OS / distro being booted, or NULL if not known
79  *      (allocated)
80  * @fdt_fname: Filename of FDT file
81  * @fdt_size: Size of FDT file
82  * @fdt_addr: Address of loaded fdt
83  * @flags: Flags for the bootflow (see enum bootflow_flags_t)
84  * @cmdline: OS command line, or NULL if not known (allocated)
85  * @x86_setup: Pointer to x86 setup block inside @buf, NULL if not present
86  */
87 struct bootflow {
88         struct list_head bm_node;
89         struct list_head glob_node;
90         struct udevice *dev;
91         struct udevice *blk;
92         int part;
93         int fs_type;
94         struct udevice *method;
95         char *name;
96         enum bootflow_state_t state;
97         char *subdir;
98         char *fname;
99         void *logo;
100         uint logo_size;
101         char *buf;
102         int size;
103         int err;
104         char *os_name;
105         char *fdt_fname;
106         int fdt_size;
107         ulong fdt_addr;
108         int flags;
109         char *cmdline;
110         char *x86_setup;
111 };
112
113 /**
114  * enum bootflow_iter_flags_t - flags for the bootflow iterator
115  *
116  * @BOOTFLOWIF_FIXED: Only used fixed/internal media
117  * @BOOTFLOWIF_SHOW: Show each bootdev before scanning it; show each hunter
118  * before using it
119  * @BOOTFLOWIF_ALL: Return bootflows with errors as well
120  * @BOOTFLOWIF_HUNT: Hunt for new bootdevs using the bootdrv hunters
121  *
122  * Internal flags:
123  * @BOOTFLOWIF_SINGLE_DEV: (internal) Just scan one bootdev
124  * @BOOTFLOWIF_SKIP_GLOBAL: (internal) Don't scan global bootmeths
125  * @BOOTFLOWIF_SINGLE_UCLASS: (internal) Keep scanning through all devices in
126  * this uclass (used with things like "mmc")
127  * @BOOTFLOWIF_SINGLE_MEDIA: (internal) Scan one media device in the uclass (used
128  * with things like "mmc1")
129  */
130 enum bootflow_iter_flags_t {
131         BOOTFLOWIF_FIXED                = 1 << 0,
132         BOOTFLOWIF_SHOW                 = 1 << 1,
133         BOOTFLOWIF_ALL                  = 1 << 2,
134         BOOTFLOWIF_HUNT                 = 1 << 3,
135
136         /*
137          * flags used internally by standard boot - do not set these when
138          * calling bootflow_scan_bootdev() etc.
139          */
140         BOOTFLOWIF_SINGLE_DEV           = 1 << 16,
141         BOOTFLOWIF_SKIP_GLOBAL          = 1 << 17,
142         BOOTFLOWIF_SINGLE_UCLASS        = 1 << 18,
143         BOOTFLOWIF_SINGLE_MEDIA         = 1 << 19,
144 };
145
146 /**
147  * enum bootflow_meth_flags_t - flags controlling which bootmeths are used
148  *
149  * Used during iteration, e.g. by bootdev_find_by_label(), to determine which
150  * bootmeths are used for the current bootdev. The flags reset when the bootdev
151  * changes
152  *
153  * @BOOTFLOW_METHF_DHCP_ONLY: Only use dhcp (scripts and EFI)
154  * @BOOTFLOW_METHF_PXE_ONLY: Only use pxe (PXE boot)
155  * @BOOTFLOW_METHF_SINGLE_DEV: Scan only a single bootdev (used for labels like
156  * "3"). This is used if a sequence number is provided instead of a label
157  * @BOOTFLOW_METHF_SINGLE_UCLASS: Scan all bootdevs in this one uclass (used
158  * with things like "mmc"). If this is not set, then the bootdev has an integer
159  * value in the label (like "mmc2")
160  */
161 enum bootflow_meth_flags_t {
162         BOOTFLOW_METHF_DHCP_ONLY        = 1 << 0,
163         BOOTFLOW_METHF_PXE_ONLY         = 1 << 1,
164         BOOTFLOW_METHF_SINGLE_DEV       = 1 << 2,
165         BOOTFLOW_METHF_SINGLE_UCLASS    = 1 << 3,
166 };
167
168 /**
169  * struct bootflow_iter - state for iterating through bootflows
170  *
171  * This starts at with the first bootdev/partition/bootmeth and can be used to
172  * iterate through all of them.
173  *
174  * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
175  * is scanned first. For partition 0, it iterates through all the available
176  * bootmeths to see which one(s) can provide a bootflow. Then it moves to
177  * parition 1 (if there is one) and the process continues. Once all partitions
178  * are examined, it moves to the next bootdev.
179  *
180  * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
181  * used. During scanning, if a partition table is found, then @max_part is
182  * updated to a larger value, no less than the number of available partitions.
183  * This ensures that iteration works through all partitions on the bootdev.
184  *
185  * @flags: Flags to use (see enum bootflow_iter_flags_t). If
186  *      BOOTFLOWIF_GLOBAL_FIRST is enabled then the global bootmeths are being
187  *      scanned, otherwise we have moved onto the bootdevs
188  * @dev: Current bootdev, NULL if none. This is only ever updated in
189  * bootflow_iter_set_dev()
190  * @part: Current partition number (0 for whole device)
191  * @method: Current bootmeth
192  * @max_part: Maximum hardware partition number in @dev, 0 if there is no
193  *      partition table
194  * @first_bootable: First bootable partition, or 0 if none
195  * @err: Error obtained from checking the last iteration. This is used to skip
196  *      forward (e.g. to skip the current partition because it is not valid)
197  *      -ESHUTDOWN: try next bootdev
198  * @num_devs: Number of bootdevs in @dev_used
199  * @max_devs: Maximum number of entries in @dev_used
200  * @dev_used: List of bootdevs used during iteration
201  * @labels: List of labels to scan for bootdevs
202  * @cur_label: Current label being processed
203  * @num_methods: Number of bootmeth devices in @method_order
204  * @cur_method: Current method number, an index into @method_order
205  * @first_glob_method: First global method, if any, else -1
206  * @cur_prio: Current priority being scanned
207  * @method_order: List of bootmeth devices to use, in order. The normal methods
208  *      appear first, then the global ones, if any
209  * @doing_global: true if we are iterating through the global bootmeths (which
210  *      happens before the normal ones)
211  * @method_flags: flags controlling which methods should be used for this @dev
212  * (enum bootflow_meth_flags_t)
213  */
214 struct bootflow_iter {
215         int flags;
216         struct udevice *dev;
217         int part;
218         struct udevice *method;
219         int max_part;
220         int first_bootable;
221         int err;
222         int num_devs;
223         int max_devs;
224         struct udevice *dev_used[BOOTFLOW_MAX_USED_DEVS];
225         const char *const *labels;
226         int cur_label;
227         int num_methods;
228         int cur_method;
229         int first_glob_method;
230         enum bootdev_prio_t cur_prio;
231         struct udevice **method_order;
232         bool doing_global;
233         int method_flags;
234 };
235
236 /**
237  * bootflow_init() - Set up a bootflow struct
238  *
239  * The bootflow is zeroed and set to state BOOTFLOWST_BASE
240  *
241  * @bflow: Struct to set up
242  * @bootdev: Bootdev to use
243  * @meth: Bootmeth to use
244  */
245 void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
246                    struct udevice *meth);
247
248 /**
249  * bootflow_iter_init() - Reset a bootflow iterator
250  *
251  * This sets everything to the starting point, ready for use.
252  *
253  * @iter: Place to store private info (inited by this call)
254  * @flags: Flags to use (see enum bootflow_iter_flags_t)
255  */
256 void bootflow_iter_init(struct bootflow_iter *iter, int flags);
257
258 /**
259  * bootflow_iter_uninit() - Free memory used by an interator
260  *
261  * @iter:       Iterator to free
262  */
263 void bootflow_iter_uninit(struct bootflow_iter *iter);
264
265 /**
266  * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
267  *
268  * Update the iterator so that the bootmeth will not be used again while this
269  * iterator is in use
270  *
271  * @iter: Iterator to update
272  * @bmeth: Boot method to remove
273  */
274 int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
275                                 const struct udevice *bmeth);
276
277 /**
278  * bootflow_scan_first() - find the first bootflow for a device or label
279  *
280  * If @flags includes BOOTFLOWIF_ALL then bootflows with errors are returned too
281  *
282  * @dev:        Boot device to scan, NULL to work through all of them until it
283  *      finds one that can supply a bootflow
284  * @label:      Label to control the scan, NULL to work through all devices
285  *      until it finds one that can supply a bootflow
286  * @iter:       Place to store private info (inited by this call)
287  * @flags:      Flags for iterator (enum bootflow_iter_flags_t). Note that if
288  *      @dev is NULL, then BOOTFLOWIF_SKIP_GLOBAL is set automatically by this
289  *      function
290  * @bflow:      Place to put the bootflow if found
291  * Return: 0 if found,  -ENODEV if no device, other -ve on other error
292  *      (iteration can continue)
293  */
294 int bootflow_scan_first(struct udevice *dev, const char *label,
295                         struct bootflow_iter *iter, int flags,
296                         struct bootflow *bflow);
297
298 /**
299  * bootflow_scan_next() - find the next bootflow
300  *
301  * This works through the available bootdev devices until it finds one that
302  * can supply a bootflow. It then returns that bootflow
303  *
304  * @iter:       Private info (as set up by bootflow_scan_first())
305  * @bflow:      Place to put the bootflow if found
306  * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
307  *      other -ve on other error (iteration can continue)
308  */
309 int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
310
311 /**
312  * bootflow_first_glob() - Get the first bootflow from the global list
313  *
314  * Returns the first bootflow in the global list, no matter what bootflow it is
315  * attached to
316  *
317  * @bflowp: Returns a pointer to the bootflow
318  * Return: 0 if found, -ENOENT if there are no bootflows
319  */
320 int bootflow_first_glob(struct bootflow **bflowp);
321
322 /**
323  * bootflow_next_glob() - Get the next bootflow from the global list
324  *
325  * Returns the next bootflow in the global list, no matter what bootflow it is
326  * attached to
327  *
328  * @bflowp: On entry, the last bootflow returned , e.g. from
329  *      bootflow_first_glob()
330  * Return: 0 if found, -ENOENT if there are no more bootflows
331  */
332 int bootflow_next_glob(struct bootflow **bflowp);
333
334 /**
335  * bootflow_free() - Free memory used by a bootflow
336  *
337  * This frees fields within @bflow, but not the @bflow pointer itself
338  */
339 void bootflow_free(struct bootflow *bflow);
340
341 /**
342  * bootflow_boot() - boot a bootflow
343  *
344  * @bflow: Bootflow to boot
345  * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
346  *      type is not supported, -EFAULT if the boot returned without an error
347  *      when we are expecting it to boot, -ENOTSUPP if trying method resulted in
348  *      finding out that is not actually supported for this boot and should not
349  *      be tried again unless something changes
350  */
351 int bootflow_boot(struct bootflow *bflow);
352
353 /**
354  * bootflow_run_boot() - Try to boot a bootflow
355  *
356  * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
357  *      boot returns -ENOTSUPP
358  * @bflow: Bootflow to boot
359  * Return: result of trying to boot
360  */
361 int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
362
363 /**
364  * bootflow_state_get_name() - Get the name of a bootflow state
365  *
366  * @state: State to check
367  * Return: name, or "?" if invalid
368  */
369 const char *bootflow_state_get_name(enum bootflow_state_t state);
370
371 /**
372  * bootflow_remove() - Remove a bootflow and free its memory
373  *
374  * This updates the linked lists containing the bootflow then frees it.
375  *
376  * @bflow: Bootflow to remove
377  */
378 void bootflow_remove(struct bootflow *bflow);
379
380 /**
381  * bootflow_iter_check_blk() - Check that a bootflow uses a block device
382  *
383  * This checks the bootdev in the bootflow to make sure it uses a block device
384  *
385  * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
386  */
387 int bootflow_iter_check_blk(const struct bootflow_iter *iter);
388
389 /**
390  * bootflow_iter_check_sf() - Check that a bootflow uses SPI FLASH
391  *
392  * This checks the bootdev in the bootflow to make sure it uses SPI flash
393  *
394  * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
395  */
396 int bootflow_iter_check_sf(const struct bootflow_iter *iter);
397
398 /**
399  * bootflow_iter_check_net() - Check that a bootflow uses a network device
400  *
401  * This checks the bootdev in the bootflow to make sure it uses a network
402  * device
403  *
404  * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
405  */
406 int bootflow_iter_check_net(const struct bootflow_iter *iter);
407
408 /**
409  * bootflow_iter_check_system() - Check that a bootflow uses the bootstd device
410  *
411  * This checks the bootdev in the bootflow to make sure it uses the bootstd
412  * device
413  *
414  * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
415  */
416 int bootflow_iter_check_system(const struct bootflow_iter *iter);
417
418 /**
419  * bootflow_menu_new() - Create a new bootflow menu
420  *
421  * @expp: Returns the expo created
422  * Returns 0 on success, -ve on error
423  */
424 int bootflow_menu_new(struct expo **expp);
425
426 /**
427  * bootflow_menu_apply_theme() - Apply a theme to a bootmenu
428  *
429  * @exp: Expo to update
430  * @node: Node containing the theme information
431  * Returns 0 on success, -ve on error
432  */
433 int bootflow_menu_apply_theme(struct expo *exp, ofnode node);
434
435 /**
436  * bootflow_menu_run() - Create and run a menu of available bootflows
437  *
438  * @std: Bootstd information
439  * @text_mode: Uses a text-based menu suitable for a serial port
440  * @bflowp: Returns chosen bootflow (set to NULL if nothing is chosen)
441  * @return 0 if an option was chosen, -EAGAIN if nothing was chosen, -ve on
442  * error
443  */
444 int bootflow_menu_run(struct bootstd_priv *std, bool text_mode,
445                       struct bootflow **bflowp);
446
447 #define BOOTFLOWCL_EMPTY        ((void *)1)
448
449 /**
450  * cmdline_set_arg() - Update or read an argument in a cmdline string
451  *
452  * Handles updating a single arg in a cmdline string, returning it in a supplied
453  * buffer; also reading an arg from a cmdline string
454  *
455  * When updating, consecutive spaces are squashed as are spaces at the start and
456  * end.
457  *
458  * @buf: Working buffer to use (initial contents are ignored). Use NULL when
459  * reading
460  * @maxlen: Length of working buffer. Use 0 when reading
461  * @cmdline: Command line to update, in the form:
462  *
463  *      fred mary= jane=123 john="has spaces"
464  *
465  * @set_arg: Argument to set or read (may or may not exist)
466  * @new_val: Value for the new argument. May not include quotes (") but may
467  * include embedded spaces, in which case it will be quoted when added to the
468  * command line. Use NULL to delete the argument from @cmdline, BOOTFLOWCL_EMPTY
469  * to set it to an empty value (no '=' sign after arg), "" to add an '=' sign
470  * but with an empty value. Use NULL when reading.
471  * @posp: Ignored when setting an argument; when getting an argument, returns
472  * the start position of its value in @cmdline, after the first quote, if any
473  *
474  * Return:
475  * For updating:
476  *      length of new buffer (including \0 terminator) on success, -ENOENT if
477  *      @new_val is NULL and @set_arg does not exist in @from, -EINVAL if a
478  *      quoted arg-value in @from is not terminated with a quote, -EBADF if
479  *      @new_val has spaces but does not start and end with quotes (or it has
480  *      quotes in the middle of the string), -E2BIG if @maxlen is too small
481  * For reading:
482  *      length of arg value (excluding quotes), -ENOENT if not found
483  */
484 int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
485                     const char *set_arg, const char *new_val, int *posp);
486
487 /**
488  * bootflow_cmdline_set_arg() - Set a single argument for a bootflow
489  *
490  * Update the allocated cmdline and set the bootargs variable
491  *
492  * @bflow: Bootflow to update
493  * @arg: Argument to update (e.g. "console")
494  * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
495  * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
496  * it without any value ("initrd")
497  * @set_env: true to set the "bootargs" environment variable too
498  *
499  * Return: 0 if OK, -ENOMEM if out of memory
500  */
501 int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *arg,
502                              const char *val, bool set_env);
503
504 /**
505  * cmdline_get_arg() - Read an argument from a cmdline
506  *
507  * @cmdline: Command line to read, in the form:
508  *
509  *      fred mary= jane=123 john="has spaces"
510  * @arg: Argument to read (may or may not exist)
511  * @posp: Returns position of argument (after any leading quote) if present
512  * Return: Length of argument value excluding quotes if found, -ENOENT if not
513  * found
514  */
515 int cmdline_get_arg(const char *cmdline, const char *arg, int *posp);
516
517 /**
518  * bootflow_cmdline_get_arg() - Read an argument from a cmdline
519  *
520  * @bootflow: Bootflow to read from
521  * @arg: Argument to read (may or may not exist)
522  * @valp: Returns a pointer to the argument (after any leading quote) if present
523  * Return: Length of argument value excluding quotes if found, -ENOENT if not
524  * found
525  */
526 int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
527                              const char **val);
528
529 /**
530  * bootflow_cmdline_auto() - Automatically set a value for a known argument
531  *
532  * This handles a small number of known arguments, for Linux in particular. It
533  * adds suitable kernel parameters automatically, e.g. to enable the console.
534  *
535  * @bflow: Bootflow to update
536  * @arg: Name of argument to set (e.g. "earlycon" or "console")
537  * Return: 0 if OK -ve on error
538  */
539 int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg);
540
541 #endif