blob: 69ac90483c6f9ee45a0ac612754438d95e98d443 [file] [log] [blame]
Simon Glass9d260252022-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 Glass43e89a32023-01-17 10:48:11 -070010#include <bootdev.h>
Simon Glasse64c2952023-01-06 08:52:42 -060011#include <dm/ofnode_decl.h>
Simon Glass9d260252022-04-24 23:31:05 -060012#include <linux/list.h>
13
Simon Glass02d929b2023-01-06 08:52:40 -060014struct bootstd_priv;
15struct expo;
16
Simon Glass9d260252022-04-24 23:31:05 -060017/**
18 * enum bootflow_state_t - states that a particular bootflow can be in
19 *
20 * Only bootflows in state BOOTFLOWST_READY can be used to boot.
21 *
22 * See bootflow_state[] for the names for each of these
23 */
24enum bootflow_state_t {
25 BOOTFLOWST_BASE, /**< Nothing known yet */
26 BOOTFLOWST_MEDIA, /**< Media exists */
27 BOOTFLOWST_PART, /**< Partition exists */
28 BOOTFLOWST_FS, /**< Filesystem exists */
29 BOOTFLOWST_FILE, /**< Bootflow file exists */
30 BOOTFLOWST_READY, /**< Bootflow file loaded */
31
32 BOOTFLOWST_COUNT
33};
34
35/**
36 * struct bootflow - information about a bootflow
37 *
38 * This is connected into two separate linked lists:
39 *
40 * bm_sibling - links all bootflows in the same bootdev
41 * glob_sibling - links all bootflows in all bootdevs
42 *
43 * @bm_node: Points to siblings in the same bootdev
44 * @glob_node: Points to siblings in the global list (all bootdev)
45 * @dev: Bootdevice device which produced this bootflow
46 * @blk: Block device which contains this bootflow, NULL if this is a network
Simon Glassa58e7bb2023-01-17 10:47:59 -070047 * device or sandbox 'host' device
Simon Glass9d260252022-04-24 23:31:05 -060048 * @part: Partition number (0 for whole device)
49 * @fs_type: Filesystem type (FS_TYPE...) if this is fixed by the media, else 0.
50 * For example, the sandbox host-filesystem bootdev sets this to
51 * FS_TYPE_SANDBOX
52 * @method: Bootmethod device used to perform the boot and read files
53 * @name: Name of bootflow (allocated)
54 * @state: Current state (enum bootflow_state_t)
55 * @subdir: Subdirectory to fetch files from (with trailing /), or NULL if none
56 * @fname: Filename of bootflow file (allocated)
Simon Glass24d8e1b2023-01-06 08:52:34 -060057 * @logo: Logo to display for this bootflow (BMP format)
58 * @logo_size: Size of the logo in bytes
Simon Glass9d260252022-04-24 23:31:05 -060059 * @buf: Bootflow file contents (allocated)
60 * @size: Size of bootflow file in bytes
61 * @err: Error number received (0 if OK)
Simon Glass2175e762023-01-06 08:52:33 -060062 * @os_name: Name of the OS / distro being booted, or NULL if not known
63 * (allocated)
Simon Glass7638c852023-01-17 10:47:56 -070064 * @fdt_fname: Filename of FDT file
65 * @fdt_size: Size of FDT file
66 * @fdt_addr: Address of loaded fdt
Simon Glass9d260252022-04-24 23:31:05 -060067 */
68struct bootflow {
69 struct list_head bm_node;
70 struct list_head glob_node;
71 struct udevice *dev;
72 struct udevice *blk;
73 int part;
74 int fs_type;
75 struct udevice *method;
76 char *name;
77 enum bootflow_state_t state;
78 char *subdir;
79 char *fname;
Simon Glass24d8e1b2023-01-06 08:52:34 -060080 void *logo;
81 uint logo_size;
Simon Glass9d260252022-04-24 23:31:05 -060082 char *buf;
83 int size;
84 int err;
Simon Glass2175e762023-01-06 08:52:33 -060085 char *os_name;
Simon Glass7638c852023-01-17 10:47:56 -070086 char *fdt_fname;
87 int fdt_size;
88 ulong fdt_addr;
Simon Glass9d260252022-04-24 23:31:05 -060089};
90
91/**
92 * enum bootflow_flags_t - flags for the bootflow iterator
93 *
94 * @BOOTFLOWF_FIXED: Only used fixed/internal media
Simon Glassd73420e2023-01-17 10:48:06 -070095 * @BOOTFLOWF_SHOW: Show each bootdev before scanning it; show each hunter
96 * before using it
Simon Glass9d260252022-04-24 23:31:05 -060097 * @BOOTFLOWF_ALL: Return bootflows with errors as well
Simon Glassd73420e2023-01-17 10:48:06 -070098 * @BOOTFLOWF_HUNT: Hunt for new bootdevs using the bootdrv hunters
99 *
100 * Internal flags:
101 * @BOOTFLOWF_SINGLE_DEV: (internal) Just scan one bootdev
102 * @BOOTFLOWF_SKIP_GLOBAL: (internal) Don't scan global bootmeths
Simon Glass66e3dce2023-01-17 10:48:09 -0700103 * @BOOTFLOWF_SINGLE_UCLASS: (internal) Keep scanning through all devices in
104 * this uclass (used with things like "mmc")
105 * @BOOTFLOWF_SINGLE_MEDIA: (internal) Scan one media device in the uclass (used
106 * with things like "mmc1")
Simon Glass9d260252022-04-24 23:31:05 -0600107 */
108enum bootflow_flags_t {
109 BOOTFLOWF_FIXED = 1 << 0,
110 BOOTFLOWF_SHOW = 1 << 1,
111 BOOTFLOWF_ALL = 1 << 2,
Simon Glassd73420e2023-01-17 10:48:06 -0700112 BOOTFLOWF_HUNT = 1 << 3,
113
114 /*
115 * flags used internally by standard boot - do not set these when
116 * calling bootflow_scan_bootdev() etc.
117 */
118 BOOTFLOWF_SINGLE_DEV = 1 << 16,
119 BOOTFLOWF_SKIP_GLOBAL = 1 << 17,
Simon Glass66e3dce2023-01-17 10:48:09 -0700120 BOOTFLOWF_SINGLE_UCLASS = 1 << 18,
121 BOOTFLOWF_SINGLE_MEDIA = 1 << 19,
Simon Glass9d260252022-04-24 23:31:05 -0600122};
123
124/**
Simon Glassd9f48572023-01-17 10:48:05 -0700125 * enum bootflow_meth_flags_t - flags controlling which bootmeths are used
126 *
127 * Used during iteration, e.g. by bootdev_find_by_label(), to determine which
128 * bootmeths are used for the current bootdev. The flags reset when the bootdev
129 * changes
130 *
131 * @BOOTFLOW_METHF_DHCP_ONLY: Only use dhcp (scripts and EFI)
132 * @BOOTFLOW_METHF_PXE_ONLY: Only use pxe (PXE boot)
Simon Glass66e3dce2023-01-17 10:48:09 -0700133 * @BOOTFLOW_METHF_SINGLE_DEV: Scan only a single bootdev (used for labels like
134 * "3"). This is used if a sequence number is provided instead of a label
135 * @BOOTFLOW_METHF_SINGLE_UCLASS: Scan all bootdevs in this one uclass (used
136 * with things like "mmc"). If this is not set, then the bootdev has an integer
137 * value in the label (like "mmc2")
Simon Glassd9f48572023-01-17 10:48:05 -0700138 */
139enum bootflow_meth_flags_t {
140 BOOTFLOW_METHF_DHCP_ONLY = 1 << 0,
141 BOOTFLOW_METHF_PXE_ONLY = 1 << 1,
Simon Glass66e3dce2023-01-17 10:48:09 -0700142 BOOTFLOW_METHF_SINGLE_DEV = 1 << 2,
143 BOOTFLOW_METHF_SINGLE_UCLASS = 1 << 3,
Simon Glassd9f48572023-01-17 10:48:05 -0700144};
145
146/**
Simon Glass9d260252022-04-24 23:31:05 -0600147 * struct bootflow_iter - state for iterating through bootflows
148 *
149 * This starts at with the first bootdev/partition/bootmeth and can be used to
150 * iterate through all of them.
151 *
152 * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
153 * is scanned first. For partition 0, it iterates through all the available
154 * bootmeths to see which one(s) can provide a bootflow. Then it moves to
155 * parition 1 (if there is one) and the process continues. Once all partitions
156 * are examined, it moves to the next bootdev.
157 *
158 * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
159 * used. During scanning, if a partition table is found, then @max_part is
160 * updated to a larger value, no less than the number of available partitions.
161 * This ensures that iteration works through all partitions on the bootdev.
162 *
Simon Glass2b80bc12022-07-30 15:52:25 -0600163 * @flags: Flags to use (see enum bootflow_flags_t). If BOOTFLOWF_GLOBAL_FIRST is
164 * enabled then the global bootmeths are being scanned, otherwise we have
165 * moved onto the bootdevs
166 * @dev: Current bootdev, NULL if none
Simon Glass9d260252022-04-24 23:31:05 -0600167 * @part: Current partition number (0 for whole device)
168 * @method: Current bootmeth
169 * @max_part: Maximum hardware partition number in @dev, 0 if there is no
170 * partition table
Simon Glassf0e358f2023-01-17 10:47:42 -0700171 * @first_bootable: First bootable partition, or 0 if none
Simon Glass9d260252022-04-24 23:31:05 -0600172 * @err: Error obtained from checking the last iteration. This is used to skip
173 * forward (e.g. to skip the current partition because it is not valid)
174 * -ESHUTDOWN: try next bootdev
175 * @num_devs: Number of bootdevs in @dev_order
176 * @cur_dev: Current bootdev number, an index into @dev_order[]
177 * @dev_order: List of bootdevs to scan, in order of priority. The scan starts
178 * with the first one on the list
Simon Glasse4b69482023-01-17 10:48:10 -0700179 * @labels: List of labels to scan for bootdevs
180 * @cur_label: Current label being processed
Simon Glass9d260252022-04-24 23:31:05 -0600181 * @num_methods: Number of bootmeth devices in @method_order
182 * @cur_method: Current method number, an index into @method_order
Simon Glass2b80bc12022-07-30 15:52:25 -0600183 * @first_glob_method: First global method, if any, else -1
Simon Glass43e89a32023-01-17 10:48:11 -0700184 * @cur_prio: Current priority being scanned
Simon Glass2b80bc12022-07-30 15:52:25 -0600185 * @method_order: List of bootmeth devices to use, in order. The normal methods
186 * appear first, then the global ones, if any
187 * @doing_global: true if we are iterating through the global bootmeths (which
188 * happens before the normal ones)
Simon Glass25365872023-01-17 10:47:58 -0700189 * @method_flags: flags controlling which methods should be used for this @dev
190 * (enum bootflow_meth_flags_t)
Simon Glass9d260252022-04-24 23:31:05 -0600191 */
192struct bootflow_iter {
193 int flags;
194 struct udevice *dev;
195 int part;
196 struct udevice *method;
197 int max_part;
Simon Glassf0e358f2023-01-17 10:47:42 -0700198 int first_bootable;
Simon Glass9d260252022-04-24 23:31:05 -0600199 int err;
200 int num_devs;
201 int cur_dev;
202 struct udevice **dev_order;
Simon Glasse4b69482023-01-17 10:48:10 -0700203 const char *const *labels;
204 int cur_label;
Simon Glass9d260252022-04-24 23:31:05 -0600205 int num_methods;
206 int cur_method;
Simon Glass2b80bc12022-07-30 15:52:25 -0600207 int first_glob_method;
Simon Glass43e89a32023-01-17 10:48:11 -0700208 enum bootdev_prio_t cur_prio;
Simon Glass9d260252022-04-24 23:31:05 -0600209 struct udevice **method_order;
Simon Glass2b80bc12022-07-30 15:52:25 -0600210 bool doing_global;
Simon Glass25365872023-01-17 10:47:58 -0700211 int method_flags;
Simon Glass9d260252022-04-24 23:31:05 -0600212};
213
214/**
Simon Glassb190deb2022-10-20 18:22:51 -0600215 * bootflow_init() - Set up a bootflow struct
216 *
217 * The bootflow is zeroed and set to state BOOTFLOWST_BASE
218 *
219 * @bflow: Struct to set up
220 * @bootdev: Bootdev to use
221 * @meth: Bootmeth to use
222 */
223void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
224 struct udevice *meth);
225
226/**
Simon Glass9d260252022-04-24 23:31:05 -0600227 * bootflow_iter_init() - Reset a bootflow iterator
228 *
229 * This sets everything to the starting point, ready for use.
230 *
231 * @iter: Place to store private info (inited by this call)
232 * @flags: Flags to use (see enum bootflow_flags_t)
233 */
234void bootflow_iter_init(struct bootflow_iter *iter, int flags);
235
236/**
237 * bootflow_iter_uninit() - Free memory used by an interator
238 *
239 * @iter: Iterator to free
240 */
241void bootflow_iter_uninit(struct bootflow_iter *iter);
242
243/**
Simon Glassa8f5be12022-04-24 23:31:09 -0600244 * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
245 *
246 * Update the iterator so that the bootmeth will not be used again while this
247 * iterator is in use
248 *
249 * @iter: Iterator to update
250 * @bmeth: Boot method to remove
251 */
252int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
253 const struct udevice *bmeth);
254
255/**
Simon Glass9d260252022-04-24 23:31:05 -0600256 * bootflow_scan_bootdev() - find the first bootflow in a bootdev
257 *
258 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
259 *
260 * @dev: Boot device to scan, NULL to work through all of them until it
Simon Glassee47d4a2022-07-30 15:52:24 -0600261 * finds one that can supply a bootflow
Simon Glass9d260252022-04-24 23:31:05 -0600262 * @iter: Place to store private info (inited by this call)
Simon Glassee47d4a2022-07-30 15:52:24 -0600263 * @flags: Flags for iterator (enum bootflow_flags_t)
Simon Glass9d260252022-04-24 23:31:05 -0600264 * @bflow: Place to put the bootflow if found
265 * Return: 0 if found, -ENODEV if no device, other -ve on other error
266 * (iteration can continue)
267 */
268int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
269 int flags, struct bootflow *bflow);
270
271/**
272 * bootflow_scan_first() - find the first bootflow
273 *
274 * This works through the available bootdev devices until it finds one that
275 * can supply a bootflow. It then returns that
276 *
277 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
278 *
279 * @iter: Place to store private info (inited by this call), with
280 * @flags: Flags for bootdev (enum bootflow_flags_t)
281 * @bflow: Place to put the bootflow if found
282 * Return: 0 if found, -ENODEV if no device, other -ve on other error (iteration
283 * can continue)
284 */
285int bootflow_scan_first(struct bootflow_iter *iter, int flags,
286 struct bootflow *bflow);
287
288/**
289 * bootflow_scan_next() - find the next bootflow
290 *
291 * This works through the available bootdev devices until it finds one that
292 * can supply a bootflow. It then returns that bootflow
293 *
294 * @iter: Private info (as set up by bootflow_scan_first())
295 * @bflow: Place to put the bootflow if found
296 * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
297 * other -ve on other error (iteration can continue)
298 */
299int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
300
301/**
302 * bootflow_first_glob() - Get the first bootflow from the global list
303 *
304 * Returns the first bootflow in the global list, no matter what bootflow it is
305 * attached to
306 *
307 * @bflowp: Returns a pointer to the bootflow
308 * Return: 0 if found, -ENOENT if there are no bootflows
309 */
310int bootflow_first_glob(struct bootflow **bflowp);
311
312/**
313 * bootflow_next_glob() - Get the next bootflow from the global list
314 *
315 * Returns the next bootflow in the global list, no matter what bootflow it is
316 * attached to
317 *
318 * @bflowp: On entry, the last bootflow returned , e.g. from
319 * bootflow_first_glob()
320 * Return: 0 if found, -ENOENT if there are no more bootflows
321 */
322int bootflow_next_glob(struct bootflow **bflowp);
323
324/**
325 * bootflow_free() - Free memory used by a bootflow
326 *
327 * This frees fields within @bflow, but not the @bflow pointer itself
328 */
329void bootflow_free(struct bootflow *bflow);
330
331/**
332 * bootflow_boot() - boot a bootflow
333 *
334 * @bflow: Bootflow to boot
335 * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
336 * type is not supported, -EFAULT if the boot returned without an error
337 * when we are expecting it to boot, -ENOTSUPP if trying method resulted in
338 * finding out that is not actually supported for this boot and should not
339 * be tried again unless something changes
340 */
341int bootflow_boot(struct bootflow *bflow);
342
343/**
344 * bootflow_run_boot() - Try to boot a bootflow
345 *
346 * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
347 * boot returns -ENOTSUPP
348 * @bflow: Bootflow to boot
349 * Return: result of trying to boot
350 */
351int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
352
353/**
354 * bootflow_state_get_name() - Get the name of a bootflow state
355 *
356 * @state: State to check
357 * Return: name, or "?" if invalid
358 */
359const char *bootflow_state_get_name(enum bootflow_state_t state);
360
Simon Glassa8f5be12022-04-24 23:31:09 -0600361/**
362 * bootflow_remove() - Remove a bootflow and free its memory
363 *
364 * This updates the linked lists containing the bootflow then frees it.
365 *
366 * @bflow: Bootflow to remove
367 */
368void bootflow_remove(struct bootflow *bflow);
369
370/**
Simon Glass865328c2023-01-17 10:47:54 -0700371 * bootflow_iter_check_blk() - Check that a bootflow uses a block device
Simon Glassa8f5be12022-04-24 23:31:09 -0600372 *
373 * This checks the bootdev in the bootflow to make sure it uses a block device
374 *
375 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
376 */
Simon Glass865328c2023-01-17 10:47:54 -0700377int bootflow_iter_check_blk(const struct bootflow_iter *iter);
Simon Glassa8f5be12022-04-24 23:31:09 -0600378
379/**
Simon Glass0c1f4a92023-01-17 10:48:03 -0700380 * bootflow_iter_check_sf() - Check that a bootflow uses SPI FLASH
381 *
382 * This checks the bootdev in the bootflow to make sure it uses SPI flash
383 *
384 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
385 */
386int bootflow_iter_check_sf(const struct bootflow_iter *iter);
387
388/**
Simon Glass865328c2023-01-17 10:47:54 -0700389 * bootflow_iter_check_net() - Check that a bootflow uses a network device
Simon Glassa8f5be12022-04-24 23:31:09 -0600390 *
391 * This checks the bootdev in the bootflow to make sure it uses a network
392 * device
393 *
394 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
395 */
Simon Glass865328c2023-01-17 10:47:54 -0700396int bootflow_iter_check_net(const struct bootflow_iter *iter);
Simon Glassa8f5be12022-04-24 23:31:09 -0600397
398/**
Simon Glass865328c2023-01-17 10:47:54 -0700399 * bootflow_iter_check_system() - Check that a bootflow uses the bootstd device
Simon Glassa8f5be12022-04-24 23:31:09 -0600400 *
401 * This checks the bootdev in the bootflow to make sure it uses the bootstd
402 * device
403 *
404 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
405 */
Simon Glass865328c2023-01-17 10:47:54 -0700406int bootflow_iter_check_system(const struct bootflow_iter *iter);
Simon Glassa8f5be12022-04-24 23:31:09 -0600407
Simon Glass02d929b2023-01-06 08:52:40 -0600408/**
409 * bootflow_menu_new() - Create a new bootflow menu
410 *
411 * @expp: Returns the expo created
412 * Returns 0 on success, -ve on error
413 */
414int bootflow_menu_new(struct expo **expp);
415
416/**
Simon Glasse64c2952023-01-06 08:52:42 -0600417 * bootflow_menu_apply_theme() - Apply a theme to a bootmenu
418 *
419 * @exp: Expo to update
420 * @node: Node containing the theme information
421 * Returns 0 on success, -ve on error
422 */
423int bootflow_menu_apply_theme(struct expo *exp, ofnode node);
424
425/**
Simon Glass02d929b2023-01-06 08:52:40 -0600426 * bootflow_menu_run() - Create and run a menu of available bootflows
427 *
428 * @std: Bootstd information
429 * @text_mode: Uses a text-based menu suitable for a serial port
430 * @bflowp: Returns chosen bootflow (set to NULL if nothing is chosen)
431 * @return 0 if an option was chosen, -EAGAIN if nothing was chosen, -ve on
432 * error
433 */
434int bootflow_menu_run(struct bootstd_priv *std, bool text_mode,
435 struct bootflow **bflowp);
436
Simon Glass9d260252022-04-24 23:31:05 -0600437#endif