blob: fdcfeddc1a63b6d827906cf5c88da63851fe82d6 [file] [log] [blame]
Simon Glassa16d87d2022-04-24 23:31:05 -06001/* 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
Simon Glass660a9952023-01-17 10:48:11 -070010#include <bootdev.h>
Simon Glassd92bcc42023-01-06 08:52:42 -060011#include <dm/ofnode_decl.h>
Simon Glassa16d87d2022-04-24 23:31:05 -060012#include <linux/list.h>
13
Simon Glass0a2f6a32023-01-06 08:52:40 -060014struct bootstd_priv;
15struct expo;
16
Simon Glasscb2a5cd2023-01-17 10:48:17 -070017enum {
18 BOOTFLOW_MAX_USED_DEVS = 16,
19};
20
Simon Glassa16d87d2022-04-24 23:31:05 -060021/**
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 */
28enum 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/**
Simon Glassca84dc82023-02-22 12:17:04 -070040 * 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 */
47enum bootflow_flags_t {
48 BOOTFLOWF_USE_PRIOR_FDT = 1 << 0,
49};
50
51/**
Simon Glassa16d87d2022-04-24 23:31:05 -060052 * 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)
Simon Glass41571582023-07-12 09:04:32 -060061 * @dev: Bootdev device which produced this bootflow
Simon Glassa16d87d2022-04-24 23:31:05 -060062 * @blk: Block device which contains this bootflow, NULL if this is a network
Simon Glass4adeeb82023-01-17 10:47:59 -070063 * device or sandbox 'host' device
Simon Glassa16d87d2022-04-24 23:31:05 -060064 * @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)
Simon Glass612b9cc2023-01-06 08:52:34 -060073 * @logo: Logo to display for this bootflow (BMP format)
74 * @logo_size: Size of the logo in bytes
Simon Glassa16d87d2022-04-24 23:31:05 -060075 * @buf: Bootflow file contents (allocated)
76 * @size: Size of bootflow file in bytes
77 * @err: Error number received (0 if OK)
Simon Glass72b7b192023-01-06 08:52:33 -060078 * @os_name: Name of the OS / distro being booted, or NULL if not known
79 * (allocated)
Simon Glass7b8c6342023-01-17 10:47:56 -070080 * @fdt_fname: Filename of FDT file
81 * @fdt_size: Size of FDT file
82 * @fdt_addr: Address of loaded fdt
Simon Glassca84dc82023-02-22 12:17:04 -070083 * @flags: Flags for the bootflow (see enum bootflow_flags_t)
Simon Glass33927522023-07-12 09:04:34 -060084 * @cmdline: OS command line, or NULL if not known (allocated)
Simon Glass63398b02023-07-12 09:04:36 -060085 * @x86_setup: Pointer to x86 setup block inside @buf, NULL if not present
Simon Glass7f75f232023-07-30 11:16:56 -060086 * @bootmeth_priv: Private data for the bootmeth
Simon Glassa16d87d2022-04-24 23:31:05 -060087 */
88struct bootflow {
89 struct list_head bm_node;
90 struct list_head glob_node;
91 struct udevice *dev;
92 struct udevice *blk;
93 int part;
94 int fs_type;
95 struct udevice *method;
96 char *name;
97 enum bootflow_state_t state;
98 char *subdir;
99 char *fname;
Simon Glass612b9cc2023-01-06 08:52:34 -0600100 void *logo;
101 uint logo_size;
Simon Glassa16d87d2022-04-24 23:31:05 -0600102 char *buf;
103 int size;
104 int err;
Simon Glass72b7b192023-01-06 08:52:33 -0600105 char *os_name;
Simon Glass7b8c6342023-01-17 10:47:56 -0700106 char *fdt_fname;
107 int fdt_size;
108 ulong fdt_addr;
Simon Glassca84dc82023-02-22 12:17:04 -0700109 int flags;
Simon Glass33927522023-07-12 09:04:34 -0600110 char *cmdline;
Simon Glass5495aaf2023-07-30 11:17:00 -0600111 void *x86_setup;
Simon Glass7f75f232023-07-30 11:16:56 -0600112 void *bootmeth_priv;
Simon Glassa16d87d2022-04-24 23:31:05 -0600113};
114
115/**
Simon Glass99e68182023-02-22 12:17:03 -0700116 * enum bootflow_iter_flags_t - flags for the bootflow iterator
Simon Glassa16d87d2022-04-24 23:31:05 -0600117 *
Simon Glass99e68182023-02-22 12:17:03 -0700118 * @BOOTFLOWIF_FIXED: Only used fixed/internal media
119 * @BOOTFLOWIF_SHOW: Show each bootdev before scanning it; show each hunter
Simon Glassa21e7752023-01-17 10:48:06 -0700120 * before using it
Simon Glass99e68182023-02-22 12:17:03 -0700121 * @BOOTFLOWIF_ALL: Return bootflows with errors as well
122 * @BOOTFLOWIF_HUNT: Hunt for new bootdevs using the bootdrv hunters
Simon Glassa21e7752023-01-17 10:48:06 -0700123 *
124 * Internal flags:
Simon Glass99e68182023-02-22 12:17:03 -0700125 * @BOOTFLOWIF_SINGLE_DEV: (internal) Just scan one bootdev
126 * @BOOTFLOWIF_SKIP_GLOBAL: (internal) Don't scan global bootmeths
127 * @BOOTFLOWIF_SINGLE_UCLASS: (internal) Keep scanning through all devices in
Simon Glassde567b12023-01-17 10:48:09 -0700128 * this uclass (used with things like "mmc")
Simon Glass99e68182023-02-22 12:17:03 -0700129 * @BOOTFLOWIF_SINGLE_MEDIA: (internal) Scan one media device in the uclass (used
Simon Glassde567b12023-01-17 10:48:09 -0700130 * with things like "mmc1")
Simon Glassa16d87d2022-04-24 23:31:05 -0600131 */
Simon Glass99e68182023-02-22 12:17:03 -0700132enum bootflow_iter_flags_t {
133 BOOTFLOWIF_FIXED = 1 << 0,
134 BOOTFLOWIF_SHOW = 1 << 1,
135 BOOTFLOWIF_ALL = 1 << 2,
136 BOOTFLOWIF_HUNT = 1 << 3,
Simon Glassa21e7752023-01-17 10:48:06 -0700137
138 /*
139 * flags used internally by standard boot - do not set these when
140 * calling bootflow_scan_bootdev() etc.
141 */
Simon Glass99e68182023-02-22 12:17:03 -0700142 BOOTFLOWIF_SINGLE_DEV = 1 << 16,
143 BOOTFLOWIF_SKIP_GLOBAL = 1 << 17,
144 BOOTFLOWIF_SINGLE_UCLASS = 1 << 18,
145 BOOTFLOWIF_SINGLE_MEDIA = 1 << 19,
Simon Glassa16d87d2022-04-24 23:31:05 -0600146};
147
148/**
Simon Glasse22fe922023-01-17 10:48:05 -0700149 * enum bootflow_meth_flags_t - flags controlling which bootmeths are used
150 *
151 * Used during iteration, e.g. by bootdev_find_by_label(), to determine which
152 * bootmeths are used for the current bootdev. The flags reset when the bootdev
153 * changes
154 *
155 * @BOOTFLOW_METHF_DHCP_ONLY: Only use dhcp (scripts and EFI)
156 * @BOOTFLOW_METHF_PXE_ONLY: Only use pxe (PXE boot)
Simon Glassde567b12023-01-17 10:48:09 -0700157 * @BOOTFLOW_METHF_SINGLE_DEV: Scan only a single bootdev (used for labels like
158 * "3"). This is used if a sequence number is provided instead of a label
159 * @BOOTFLOW_METHF_SINGLE_UCLASS: Scan all bootdevs in this one uclass (used
160 * with things like "mmc"). If this is not set, then the bootdev has an integer
161 * value in the label (like "mmc2")
Simon Glasse22fe922023-01-17 10:48:05 -0700162 */
163enum bootflow_meth_flags_t {
164 BOOTFLOW_METHF_DHCP_ONLY = 1 << 0,
165 BOOTFLOW_METHF_PXE_ONLY = 1 << 1,
Simon Glassde567b12023-01-17 10:48:09 -0700166 BOOTFLOW_METHF_SINGLE_DEV = 1 << 2,
167 BOOTFLOW_METHF_SINGLE_UCLASS = 1 << 3,
Simon Glasse22fe922023-01-17 10:48:05 -0700168};
169
170/**
Simon Glassa16d87d2022-04-24 23:31:05 -0600171 * struct bootflow_iter - state for iterating through bootflows
172 *
173 * This starts at with the first bootdev/partition/bootmeth and can be used to
174 * iterate through all of them.
175 *
176 * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
177 * is scanned first. For partition 0, it iterates through all the available
178 * bootmeths to see which one(s) can provide a bootflow. Then it moves to
179 * parition 1 (if there is one) and the process continues. Once all partitions
180 * are examined, it moves to the next bootdev.
181 *
182 * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
183 * used. During scanning, if a partition table is found, then @max_part is
184 * updated to a larger value, no less than the number of available partitions.
185 * This ensures that iteration works through all partitions on the bootdev.
186 *
Simon Glass99e68182023-02-22 12:17:03 -0700187 * @flags: Flags to use (see enum bootflow_iter_flags_t). If
188 * BOOTFLOWIF_GLOBAL_FIRST is enabled then the global bootmeths are being
189 * scanned, otherwise we have moved onto the bootdevs
Simon Glass484e4072023-01-17 10:48:14 -0700190 * @dev: Current bootdev, NULL if none. This is only ever updated in
191 * bootflow_iter_set_dev()
Simon Glassa16d87d2022-04-24 23:31:05 -0600192 * @part: Current partition number (0 for whole device)
193 * @method: Current bootmeth
194 * @max_part: Maximum hardware partition number in @dev, 0 if there is no
195 * partition table
Simon Glass64cbc5c2023-01-17 10:47:42 -0700196 * @first_bootable: First bootable partition, or 0 if none
Simon Glassa16d87d2022-04-24 23:31:05 -0600197 * @err: Error obtained from checking the last iteration. This is used to skip
198 * forward (e.g. to skip the current partition because it is not valid)
199 * -ESHUTDOWN: try next bootdev
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700200 * @num_devs: Number of bootdevs in @dev_used
201 * @max_devs: Maximum number of entries in @dev_used
202 * @dev_used: List of bootdevs used during iteration
Simon Glassac06b26a2023-01-17 10:48:10 -0700203 * @labels: List of labels to scan for bootdevs
204 * @cur_label: Current label being processed
Simon Glassa16d87d2022-04-24 23:31:05 -0600205 * @num_methods: Number of bootmeth devices in @method_order
206 * @cur_method: Current method number, an index into @method_order
Simon Glass73fcf512022-07-30 15:52:25 -0600207 * @first_glob_method: First global method, if any, else -1
Simon Glass660a9952023-01-17 10:48:11 -0700208 * @cur_prio: Current priority being scanned
Simon Glass73fcf512022-07-30 15:52:25 -0600209 * @method_order: List of bootmeth devices to use, in order. The normal methods
210 * appear first, then the global ones, if any
211 * @doing_global: true if we are iterating through the global bootmeths (which
212 * happens before the normal ones)
Simon Glass30fbeb52023-01-17 10:47:58 -0700213 * @method_flags: flags controlling which methods should be used for this @dev
214 * (enum bootflow_meth_flags_t)
Simon Glassa16d87d2022-04-24 23:31:05 -0600215 */
216struct bootflow_iter {
217 int flags;
218 struct udevice *dev;
219 int part;
220 struct udevice *method;
221 int max_part;
Simon Glass64cbc5c2023-01-17 10:47:42 -0700222 int first_bootable;
Simon Glassa16d87d2022-04-24 23:31:05 -0600223 int err;
224 int num_devs;
Simon Glasscb2a5cd2023-01-17 10:48:17 -0700225 int max_devs;
226 struct udevice *dev_used[BOOTFLOW_MAX_USED_DEVS];
Simon Glassac06b26a2023-01-17 10:48:10 -0700227 const char *const *labels;
228 int cur_label;
Simon Glassa16d87d2022-04-24 23:31:05 -0600229 int num_methods;
230 int cur_method;
Simon Glass73fcf512022-07-30 15:52:25 -0600231 int first_glob_method;
Simon Glass660a9952023-01-17 10:48:11 -0700232 enum bootdev_prio_t cur_prio;
Simon Glassa16d87d2022-04-24 23:31:05 -0600233 struct udevice **method_order;
Simon Glass73fcf512022-07-30 15:52:25 -0600234 bool doing_global;
Simon Glass30fbeb52023-01-17 10:47:58 -0700235 int method_flags;
Simon Glassa16d87d2022-04-24 23:31:05 -0600236};
237
238/**
Simon Glass00501fc2022-10-20 18:22:51 -0600239 * bootflow_init() - Set up a bootflow struct
240 *
241 * The bootflow is zeroed and set to state BOOTFLOWST_BASE
242 *
243 * @bflow: Struct to set up
244 * @bootdev: Bootdev to use
245 * @meth: Bootmeth to use
246 */
247void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
248 struct udevice *meth);
249
250/**
Simon Glassa16d87d2022-04-24 23:31:05 -0600251 * bootflow_iter_init() - Reset a bootflow iterator
252 *
253 * This sets everything to the starting point, ready for use.
254 *
255 * @iter: Place to store private info (inited by this call)
Simon Glass99e68182023-02-22 12:17:03 -0700256 * @flags: Flags to use (see enum bootflow_iter_flags_t)
Simon Glassa16d87d2022-04-24 23:31:05 -0600257 */
258void bootflow_iter_init(struct bootflow_iter *iter, int flags);
259
260/**
261 * bootflow_iter_uninit() - Free memory used by an interator
262 *
263 * @iter: Iterator to free
264 */
265void bootflow_iter_uninit(struct bootflow_iter *iter);
266
267/**
Simon Glass03fcbf92022-04-24 23:31:09 -0600268 * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
269 *
270 * Update the iterator so that the bootmeth will not be used again while this
271 * iterator is in use
272 *
273 * @iter: Iterator to update
274 * @bmeth: Boot method to remove
275 */
276int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
277 const struct udevice *bmeth);
278
279/**
Simon Glass5d3d44f2023-01-17 10:48:16 -0700280 * bootflow_scan_first() - find the first bootflow for a device or label
Simon Glassa16d87d2022-04-24 23:31:05 -0600281 *
Simon Glass99e68182023-02-22 12:17:03 -0700282 * If @flags includes BOOTFLOWIF_ALL then bootflows with errors are returned too
Simon Glassa16d87d2022-04-24 23:31:05 -0600283 *
284 * @dev: Boot device to scan, NULL to work through all of them until it
Simon Glass943d38c2022-07-30 15:52:24 -0600285 * finds one that can supply a bootflow
Simon Glassba3d5372023-01-17 10:48:15 -0700286 * @label: Label to control the scan, NULL to work through all devices
287 * until it finds one that can supply a bootflow
Simon Glassa16d87d2022-04-24 23:31:05 -0600288 * @iter: Place to store private info (inited by this call)
Simon Glass99e68182023-02-22 12:17:03 -0700289 * @flags: Flags for iterator (enum bootflow_iter_flags_t). Note that if
290 * @dev is NULL, then BOOTFLOWIF_SKIP_GLOBAL is set automatically by this
291 * function
Simon Glassa16d87d2022-04-24 23:31:05 -0600292 * @bflow: Place to put the bootflow if found
293 * Return: 0 if found, -ENODEV if no device, other -ve on other error
294 * (iteration can continue)
295 */
Simon Glass5d3d44f2023-01-17 10:48:16 -0700296int bootflow_scan_first(struct udevice *dev, const char *label,
297 struct bootflow_iter *iter, int flags,
Simon Glassa16d87d2022-04-24 23:31:05 -0600298 struct bootflow *bflow);
299
300/**
301 * bootflow_scan_next() - find the next bootflow
302 *
303 * This works through the available bootdev devices until it finds one that
304 * can supply a bootflow. It then returns that bootflow
305 *
306 * @iter: Private info (as set up by bootflow_scan_first())
307 * @bflow: Place to put the bootflow if found
308 * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
309 * other -ve on other error (iteration can continue)
310 */
311int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
312
313/**
314 * bootflow_first_glob() - Get the first bootflow from the global list
315 *
316 * Returns the first bootflow in the global list, no matter what bootflow it is
317 * attached to
318 *
319 * @bflowp: Returns a pointer to the bootflow
320 * Return: 0 if found, -ENOENT if there are no bootflows
321 */
322int bootflow_first_glob(struct bootflow **bflowp);
323
324/**
325 * bootflow_next_glob() - Get the next bootflow from the global list
326 *
327 * Returns the next bootflow in the global list, no matter what bootflow it is
328 * attached to
329 *
330 * @bflowp: On entry, the last bootflow returned , e.g. from
331 * bootflow_first_glob()
332 * Return: 0 if found, -ENOENT if there are no more bootflows
333 */
334int bootflow_next_glob(struct bootflow **bflowp);
335
336/**
337 * bootflow_free() - Free memory used by a bootflow
338 *
339 * This frees fields within @bflow, but not the @bflow pointer itself
340 */
341void bootflow_free(struct bootflow *bflow);
342
343/**
344 * bootflow_boot() - boot a bootflow
345 *
346 * @bflow: Bootflow to boot
347 * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
348 * type is not supported, -EFAULT if the boot returned without an error
349 * when we are expecting it to boot, -ENOTSUPP if trying method resulted in
350 * finding out that is not actually supported for this boot and should not
351 * be tried again unless something changes
352 */
353int bootflow_boot(struct bootflow *bflow);
354
355/**
356 * bootflow_run_boot() - Try to boot a bootflow
357 *
358 * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
359 * boot returns -ENOTSUPP
360 * @bflow: Bootflow to boot
361 * Return: result of trying to boot
362 */
363int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
364
365/**
366 * bootflow_state_get_name() - Get the name of a bootflow state
367 *
368 * @state: State to check
369 * Return: name, or "?" if invalid
370 */
371const char *bootflow_state_get_name(enum bootflow_state_t state);
372
Simon Glass03fcbf92022-04-24 23:31:09 -0600373/**
374 * bootflow_remove() - Remove a bootflow and free its memory
375 *
376 * This updates the linked lists containing the bootflow then frees it.
377 *
378 * @bflow: Bootflow to remove
379 */
380void bootflow_remove(struct bootflow *bflow);
381
382/**
Simon Glass18c50402023-01-17 10:47:54 -0700383 * bootflow_iter_check_blk() - Check that a bootflow uses a block device
Simon Glass03fcbf92022-04-24 23:31:09 -0600384 *
385 * This checks the bootdev in the bootflow to make sure it uses a block device
386 *
387 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
388 */
Simon Glass18c50402023-01-17 10:47:54 -0700389int bootflow_iter_check_blk(const struct bootflow_iter *iter);
Simon Glass03fcbf92022-04-24 23:31:09 -0600390
391/**
Simon Glass215be682023-01-17 10:48:03 -0700392 * bootflow_iter_check_sf() - Check that a bootflow uses SPI FLASH
393 *
394 * This checks the bootdev in the bootflow to make sure it uses SPI flash
395 *
396 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
397 */
398int bootflow_iter_check_sf(const struct bootflow_iter *iter);
399
400/**
Simon Glass18c50402023-01-17 10:47:54 -0700401 * bootflow_iter_check_net() - Check that a bootflow uses a network device
Simon Glass03fcbf92022-04-24 23:31:09 -0600402 *
403 * This checks the bootdev in the bootflow to make sure it uses a network
404 * device
405 *
406 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
407 */
Simon Glass18c50402023-01-17 10:47:54 -0700408int bootflow_iter_check_net(const struct bootflow_iter *iter);
Simon Glass03fcbf92022-04-24 23:31:09 -0600409
410/**
Simon Glass18c50402023-01-17 10:47:54 -0700411 * bootflow_iter_check_system() - Check that a bootflow uses the bootstd device
Simon Glass03fcbf92022-04-24 23:31:09 -0600412 *
413 * This checks the bootdev in the bootflow to make sure it uses the bootstd
414 * device
415 *
416 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
417 */
Simon Glass18c50402023-01-17 10:47:54 -0700418int bootflow_iter_check_system(const struct bootflow_iter *iter);
Simon Glass03fcbf92022-04-24 23:31:09 -0600419
Simon Glass0a2f6a32023-01-06 08:52:40 -0600420/**
421 * bootflow_menu_new() - Create a new bootflow menu
422 *
423 * @expp: Returns the expo created
424 * Returns 0 on success, -ve on error
425 */
426int bootflow_menu_new(struct expo **expp);
427
428/**
Simon Glassd92bcc42023-01-06 08:52:42 -0600429 * bootflow_menu_apply_theme() - Apply a theme to a bootmenu
430 *
431 * @exp: Expo to update
432 * @node: Node containing the theme information
433 * Returns 0 on success, -ve on error
434 */
435int bootflow_menu_apply_theme(struct expo *exp, ofnode node);
436
437/**
Simon Glass0a2f6a32023-01-06 08:52:40 -0600438 * bootflow_menu_run() - Create and run a menu of available bootflows
439 *
440 * @std: Bootstd information
441 * @text_mode: Uses a text-based menu suitable for a serial port
442 * @bflowp: Returns chosen bootflow (set to NULL if nothing is chosen)
443 * @return 0 if an option was chosen, -EAGAIN if nothing was chosen, -ve on
444 * error
445 */
446int bootflow_menu_run(struct bootstd_priv *std, bool text_mode,
447 struct bootflow **bflowp);
448
Simon Glassa08adca2023-07-12 09:04:38 -0600449#define BOOTFLOWCL_EMPTY ((void *)1)
450
451/**
452 * cmdline_set_arg() - Update or read an argument in a cmdline string
453 *
454 * Handles updating a single arg in a cmdline string, returning it in a supplied
455 * buffer; also reading an arg from a cmdline string
456 *
457 * When updating, consecutive spaces are squashed as are spaces at the start and
458 * end.
459 *
460 * @buf: Working buffer to use (initial contents are ignored). Use NULL when
461 * reading
462 * @maxlen: Length of working buffer. Use 0 when reading
463 * @cmdline: Command line to update, in the form:
464 *
465 * fred mary= jane=123 john="has spaces"
466 *
467 * @set_arg: Argument to set or read (may or may not exist)
468 * @new_val: Value for the new argument. May not include quotes (") but may
469 * include embedded spaces, in which case it will be quoted when added to the
470 * command line. Use NULL to delete the argument from @cmdline, BOOTFLOWCL_EMPTY
471 * to set it to an empty value (no '=' sign after arg), "" to add an '=' sign
472 * but with an empty value. Use NULL when reading.
473 * @posp: Ignored when setting an argument; when getting an argument, returns
474 * the start position of its value in @cmdline, after the first quote, if any
475 *
476 * Return:
477 * For updating:
478 * length of new buffer (including \0 terminator) on success, -ENOENT if
479 * @new_val is NULL and @set_arg does not exist in @from, -EINVAL if a
480 * quoted arg-value in @from is not terminated with a quote, -EBADF if
481 * @new_val has spaces but does not start and end with quotes (or it has
482 * quotes in the middle of the string), -E2BIG if @maxlen is too small
483 * For reading:
484 * length of arg value (excluding quotes), -ENOENT if not found
485 */
486int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
487 const char *set_arg, const char *new_val, int *posp);
488
Simon Glass55a2da32023-07-12 09:04:39 -0600489/**
490 * bootflow_cmdline_set_arg() - Set a single argument for a bootflow
491 *
492 * Update the allocated cmdline and set the bootargs variable
493 *
494 * @bflow: Bootflow to update
495 * @arg: Argument to update (e.g. "console")
496 * @val: Value to set (e.g. "ttyS2") or NULL to delete the argument if present,
497 * "" to set it to an empty value (e.g. "console=") and BOOTFLOWCL_EMPTY to add
498 * it without any value ("initrd")
499 * @set_env: true to set the "bootargs" environment variable too
500 *
501 * Return: 0 if OK, -ENOMEM if out of memory
502 */
503int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *arg,
504 const char *val, bool set_env);
505
506/**
507 * cmdline_get_arg() - Read an argument from a cmdline
508 *
509 * @cmdline: Command line to read, in the form:
510 *
511 * fred mary= jane=123 john="has spaces"
512 * @arg: Argument to read (may or may not exist)
513 * @posp: Returns position of argument (after any leading quote) if present
514 * Return: Length of argument value excluding quotes if found, -ENOENT if not
515 * found
516 */
517int cmdline_get_arg(const char *cmdline, const char *arg, int *posp);
518
519/**
520 * bootflow_cmdline_get_arg() - Read an argument from a cmdline
521 *
522 * @bootflow: Bootflow to read from
523 * @arg: Argument to read (may or may not exist)
524 * @valp: Returns a pointer to the argument (after any leading quote) if present
525 * Return: Length of argument value excluding quotes if found, -ENOENT if not
526 * found
527 */
528int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
529 const char **val);
530
Simon Glasscd91e992023-07-12 09:04:42 -0600531/**
532 * bootflow_cmdline_auto() - Automatically set a value for a known argument
533 *
534 * This handles a small number of known arguments, for Linux in particular. It
535 * adds suitable kernel parameters automatically, e.g. to enable the console.
536 *
537 * @bflow: Bootflow to update
538 * @arg: Name of argument to set (e.g. "earlycon" or "console")
539 * Return: 0 if OK -ve on error
540 */
541int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg);
542
Simon Glassa16d87d2022-04-24 23:31:05 -0600543#endif