blob: 81dbcd6754b71996f652874e10d8d2e3a300cfd9 [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 Glasse64c2952023-01-06 08:52:42 -060010#include <dm/ofnode_decl.h>
Simon Glass9d260252022-04-24 23:31:05 -060011#include <linux/list.h>
12
Simon Glass02d929b2023-01-06 08:52:40 -060013struct bootstd_priv;
14struct expo;
15
Simon Glass9d260252022-04-24 23:31:05 -060016/**
17 * enum bootflow_state_t - states that a particular bootflow can be in
18 *
19 * Only bootflows in state BOOTFLOWST_READY can be used to boot.
20 *
21 * See bootflow_state[] for the names for each of these
22 */
23enum bootflow_state_t {
24 BOOTFLOWST_BASE, /**< Nothing known yet */
25 BOOTFLOWST_MEDIA, /**< Media exists */
26 BOOTFLOWST_PART, /**< Partition exists */
27 BOOTFLOWST_FS, /**< Filesystem exists */
28 BOOTFLOWST_FILE, /**< Bootflow file exists */
29 BOOTFLOWST_READY, /**< Bootflow file loaded */
30
31 BOOTFLOWST_COUNT
32};
33
34/**
35 * struct bootflow - information about a bootflow
36 *
37 * This is connected into two separate linked lists:
38 *
39 * bm_sibling - links all bootflows in the same bootdev
40 * glob_sibling - links all bootflows in all bootdevs
41 *
42 * @bm_node: Points to siblings in the same bootdev
43 * @glob_node: Points to siblings in the global list (all bootdev)
44 * @dev: Bootdevice device which produced this bootflow
45 * @blk: Block device which contains this bootflow, NULL if this is a network
Simon Glassa58e7bb2023-01-17 10:47:59 -070046 * device or sandbox 'host' device
Simon Glass9d260252022-04-24 23:31:05 -060047 * @part: Partition number (0 for whole device)
48 * @fs_type: Filesystem type (FS_TYPE...) if this is fixed by the media, else 0.
49 * For example, the sandbox host-filesystem bootdev sets this to
50 * FS_TYPE_SANDBOX
51 * @method: Bootmethod device used to perform the boot and read files
52 * @name: Name of bootflow (allocated)
53 * @state: Current state (enum bootflow_state_t)
54 * @subdir: Subdirectory to fetch files from (with trailing /), or NULL if none
55 * @fname: Filename of bootflow file (allocated)
Simon Glass24d8e1b2023-01-06 08:52:34 -060056 * @logo: Logo to display for this bootflow (BMP format)
57 * @logo_size: Size of the logo in bytes
Simon Glass9d260252022-04-24 23:31:05 -060058 * @buf: Bootflow file contents (allocated)
59 * @size: Size of bootflow file in bytes
60 * @err: Error number received (0 if OK)
Simon Glass2175e762023-01-06 08:52:33 -060061 * @os_name: Name of the OS / distro being booted, or NULL if not known
62 * (allocated)
Simon Glass7638c852023-01-17 10:47:56 -070063 * @fdt_fname: Filename of FDT file
64 * @fdt_size: Size of FDT file
65 * @fdt_addr: Address of loaded fdt
Simon Glass9d260252022-04-24 23:31:05 -060066 */
67struct bootflow {
68 struct list_head bm_node;
69 struct list_head glob_node;
70 struct udevice *dev;
71 struct udevice *blk;
72 int part;
73 int fs_type;
74 struct udevice *method;
75 char *name;
76 enum bootflow_state_t state;
77 char *subdir;
78 char *fname;
Simon Glass24d8e1b2023-01-06 08:52:34 -060079 void *logo;
80 uint logo_size;
Simon Glass9d260252022-04-24 23:31:05 -060081 char *buf;
82 int size;
83 int err;
Simon Glass2175e762023-01-06 08:52:33 -060084 char *os_name;
Simon Glass7638c852023-01-17 10:47:56 -070085 char *fdt_fname;
86 int fdt_size;
87 ulong fdt_addr;
Simon Glass9d260252022-04-24 23:31:05 -060088};
89
90/**
91 * enum bootflow_flags_t - flags for the bootflow iterator
92 *
93 * @BOOTFLOWF_FIXED: Only used fixed/internal media
Simon Glassd73420e2023-01-17 10:48:06 -070094 * @BOOTFLOWF_SHOW: Show each bootdev before scanning it; show each hunter
95 * before using it
Simon Glass9d260252022-04-24 23:31:05 -060096 * @BOOTFLOWF_ALL: Return bootflows with errors as well
Simon Glassd73420e2023-01-17 10:48:06 -070097 * @BOOTFLOWF_HUNT: Hunt for new bootdevs using the bootdrv hunters
98 *
99 * Internal flags:
100 * @BOOTFLOWF_SINGLE_DEV: (internal) Just scan one bootdev
101 * @BOOTFLOWF_SKIP_GLOBAL: (internal) Don't scan global bootmeths
Simon Glass66e3dce2023-01-17 10:48:09 -0700102 * @BOOTFLOWF_SINGLE_UCLASS: (internal) Keep scanning through all devices in
103 * this uclass (used with things like "mmc")
104 * @BOOTFLOWF_SINGLE_MEDIA: (internal) Scan one media device in the uclass (used
105 * with things like "mmc1")
Simon Glass9d260252022-04-24 23:31:05 -0600106 */
107enum bootflow_flags_t {
108 BOOTFLOWF_FIXED = 1 << 0,
109 BOOTFLOWF_SHOW = 1 << 1,
110 BOOTFLOWF_ALL = 1 << 2,
Simon Glassd73420e2023-01-17 10:48:06 -0700111 BOOTFLOWF_HUNT = 1 << 3,
112
113 /*
114 * flags used internally by standard boot - do not set these when
115 * calling bootflow_scan_bootdev() etc.
116 */
117 BOOTFLOWF_SINGLE_DEV = 1 << 16,
118 BOOTFLOWF_SKIP_GLOBAL = 1 << 17,
Simon Glass66e3dce2023-01-17 10:48:09 -0700119 BOOTFLOWF_SINGLE_UCLASS = 1 << 18,
120 BOOTFLOWF_SINGLE_MEDIA = 1 << 19,
Simon Glass9d260252022-04-24 23:31:05 -0600121};
122
123/**
Simon Glassd9f48572023-01-17 10:48:05 -0700124 * enum bootflow_meth_flags_t - flags controlling which bootmeths are used
125 *
126 * Used during iteration, e.g. by bootdev_find_by_label(), to determine which
127 * bootmeths are used for the current bootdev. The flags reset when the bootdev
128 * changes
129 *
130 * @BOOTFLOW_METHF_DHCP_ONLY: Only use dhcp (scripts and EFI)
131 * @BOOTFLOW_METHF_PXE_ONLY: Only use pxe (PXE boot)
Simon Glass66e3dce2023-01-17 10:48:09 -0700132 * @BOOTFLOW_METHF_SINGLE_DEV: Scan only a single bootdev (used for labels like
133 * "3"). This is used if a sequence number is provided instead of a label
134 * @BOOTFLOW_METHF_SINGLE_UCLASS: Scan all bootdevs in this one uclass (used
135 * with things like "mmc"). If this is not set, then the bootdev has an integer
136 * value in the label (like "mmc2")
Simon Glassd9f48572023-01-17 10:48:05 -0700137 */
138enum bootflow_meth_flags_t {
139 BOOTFLOW_METHF_DHCP_ONLY = 1 << 0,
140 BOOTFLOW_METHF_PXE_ONLY = 1 << 1,
Simon Glass66e3dce2023-01-17 10:48:09 -0700141 BOOTFLOW_METHF_SINGLE_DEV = 1 << 2,
142 BOOTFLOW_METHF_SINGLE_UCLASS = 1 << 3,
Simon Glassd9f48572023-01-17 10:48:05 -0700143};
144
145/**
Simon Glass9d260252022-04-24 23:31:05 -0600146 * struct bootflow_iter - state for iterating through bootflows
147 *
148 * This starts at with the first bootdev/partition/bootmeth and can be used to
149 * iterate through all of them.
150 *
151 * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
152 * is scanned first. For partition 0, it iterates through all the available
153 * bootmeths to see which one(s) can provide a bootflow. Then it moves to
154 * parition 1 (if there is one) and the process continues. Once all partitions
155 * are examined, it moves to the next bootdev.
156 *
157 * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
158 * used. During scanning, if a partition table is found, then @max_part is
159 * updated to a larger value, no less than the number of available partitions.
160 * This ensures that iteration works through all partitions on the bootdev.
161 *
Simon Glass2b80bc12022-07-30 15:52:25 -0600162 * @flags: Flags to use (see enum bootflow_flags_t). If BOOTFLOWF_GLOBAL_FIRST is
163 * enabled then the global bootmeths are being scanned, otherwise we have
164 * moved onto the bootdevs
165 * @dev: Current bootdev, NULL if none
Simon Glass9d260252022-04-24 23:31:05 -0600166 * @part: Current partition number (0 for whole device)
167 * @method: Current bootmeth
168 * @max_part: Maximum hardware partition number in @dev, 0 if there is no
169 * partition table
Simon Glassf0e358f2023-01-17 10:47:42 -0700170 * @first_bootable: First bootable partition, or 0 if none
Simon Glass9d260252022-04-24 23:31:05 -0600171 * @err: Error obtained from checking the last iteration. This is used to skip
172 * forward (e.g. to skip the current partition because it is not valid)
173 * -ESHUTDOWN: try next bootdev
174 * @num_devs: Number of bootdevs in @dev_order
175 * @cur_dev: Current bootdev number, an index into @dev_order[]
176 * @dev_order: List of bootdevs to scan, in order of priority. The scan starts
177 * with the first one on the list
178 * @num_methods: Number of bootmeth devices in @method_order
179 * @cur_method: Current method number, an index into @method_order
Simon Glass2b80bc12022-07-30 15:52:25 -0600180 * @first_glob_method: First global method, if any, else -1
181 * @method_order: List of bootmeth devices to use, in order. The normal methods
182 * appear first, then the global ones, if any
183 * @doing_global: true if we are iterating through the global bootmeths (which
184 * happens before the normal ones)
Simon Glass25365872023-01-17 10:47:58 -0700185 * @method_flags: flags controlling which methods should be used for this @dev
186 * (enum bootflow_meth_flags_t)
Simon Glass9d260252022-04-24 23:31:05 -0600187 */
188struct bootflow_iter {
189 int flags;
190 struct udevice *dev;
191 int part;
192 struct udevice *method;
193 int max_part;
Simon Glassf0e358f2023-01-17 10:47:42 -0700194 int first_bootable;
Simon Glass9d260252022-04-24 23:31:05 -0600195 int err;
196 int num_devs;
197 int cur_dev;
198 struct udevice **dev_order;
199 int num_methods;
200 int cur_method;
Simon Glass2b80bc12022-07-30 15:52:25 -0600201 int first_glob_method;
Simon Glass9d260252022-04-24 23:31:05 -0600202 struct udevice **method_order;
Simon Glass2b80bc12022-07-30 15:52:25 -0600203 bool doing_global;
Simon Glass25365872023-01-17 10:47:58 -0700204 int method_flags;
Simon Glass9d260252022-04-24 23:31:05 -0600205};
206
207/**
Simon Glassb190deb2022-10-20 18:22:51 -0600208 * bootflow_init() - Set up a bootflow struct
209 *
210 * The bootflow is zeroed and set to state BOOTFLOWST_BASE
211 *
212 * @bflow: Struct to set up
213 * @bootdev: Bootdev to use
214 * @meth: Bootmeth to use
215 */
216void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
217 struct udevice *meth);
218
219/**
Simon Glass9d260252022-04-24 23:31:05 -0600220 * bootflow_iter_init() - Reset a bootflow iterator
221 *
222 * This sets everything to the starting point, ready for use.
223 *
224 * @iter: Place to store private info (inited by this call)
225 * @flags: Flags to use (see enum bootflow_flags_t)
226 */
227void bootflow_iter_init(struct bootflow_iter *iter, int flags);
228
229/**
230 * bootflow_iter_uninit() - Free memory used by an interator
231 *
232 * @iter: Iterator to free
233 */
234void bootflow_iter_uninit(struct bootflow_iter *iter);
235
236/**
Simon Glassa8f5be12022-04-24 23:31:09 -0600237 * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
238 *
239 * Update the iterator so that the bootmeth will not be used again while this
240 * iterator is in use
241 *
242 * @iter: Iterator to update
243 * @bmeth: Boot method to remove
244 */
245int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
246 const struct udevice *bmeth);
247
248/**
Simon Glass9d260252022-04-24 23:31:05 -0600249 * bootflow_scan_bootdev() - find the first bootflow in a bootdev
250 *
251 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
252 *
253 * @dev: Boot device to scan, NULL to work through all of them until it
Simon Glassee47d4a2022-07-30 15:52:24 -0600254 * finds one that can supply a bootflow
Simon Glass9d260252022-04-24 23:31:05 -0600255 * @iter: Place to store private info (inited by this call)
Simon Glassee47d4a2022-07-30 15:52:24 -0600256 * @flags: Flags for iterator (enum bootflow_flags_t)
Simon Glass9d260252022-04-24 23:31:05 -0600257 * @bflow: Place to put the bootflow if found
258 * Return: 0 if found, -ENODEV if no device, other -ve on other error
259 * (iteration can continue)
260 */
261int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
262 int flags, struct bootflow *bflow);
263
264/**
265 * bootflow_scan_first() - find the first bootflow
266 *
267 * This works through the available bootdev devices until it finds one that
268 * can supply a bootflow. It then returns that
269 *
270 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
271 *
272 * @iter: Place to store private info (inited by this call), with
273 * @flags: Flags for bootdev (enum bootflow_flags_t)
274 * @bflow: Place to put the bootflow if found
275 * Return: 0 if found, -ENODEV if no device, other -ve on other error (iteration
276 * can continue)
277 */
278int bootflow_scan_first(struct bootflow_iter *iter, int flags,
279 struct bootflow *bflow);
280
281/**
282 * bootflow_scan_next() - find the next bootflow
283 *
284 * This works through the available bootdev devices until it finds one that
285 * can supply a bootflow. It then returns that bootflow
286 *
287 * @iter: Private info (as set up by bootflow_scan_first())
288 * @bflow: Place to put the bootflow if found
289 * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
290 * other -ve on other error (iteration can continue)
291 */
292int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
293
294/**
295 * bootflow_first_glob() - Get the first bootflow from the global list
296 *
297 * Returns the first bootflow in the global list, no matter what bootflow it is
298 * attached to
299 *
300 * @bflowp: Returns a pointer to the bootflow
301 * Return: 0 if found, -ENOENT if there are no bootflows
302 */
303int bootflow_first_glob(struct bootflow **bflowp);
304
305/**
306 * bootflow_next_glob() - Get the next bootflow from the global list
307 *
308 * Returns the next bootflow in the global list, no matter what bootflow it is
309 * attached to
310 *
311 * @bflowp: On entry, the last bootflow returned , e.g. from
312 * bootflow_first_glob()
313 * Return: 0 if found, -ENOENT if there are no more bootflows
314 */
315int bootflow_next_glob(struct bootflow **bflowp);
316
317/**
318 * bootflow_free() - Free memory used by a bootflow
319 *
320 * This frees fields within @bflow, but not the @bflow pointer itself
321 */
322void bootflow_free(struct bootflow *bflow);
323
324/**
325 * bootflow_boot() - boot a bootflow
326 *
327 * @bflow: Bootflow to boot
328 * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
329 * type is not supported, -EFAULT if the boot returned without an error
330 * when we are expecting it to boot, -ENOTSUPP if trying method resulted in
331 * finding out that is not actually supported for this boot and should not
332 * be tried again unless something changes
333 */
334int bootflow_boot(struct bootflow *bflow);
335
336/**
337 * bootflow_run_boot() - Try to boot a bootflow
338 *
339 * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
340 * boot returns -ENOTSUPP
341 * @bflow: Bootflow to boot
342 * Return: result of trying to boot
343 */
344int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
345
346/**
347 * bootflow_state_get_name() - Get the name of a bootflow state
348 *
349 * @state: State to check
350 * Return: name, or "?" if invalid
351 */
352const char *bootflow_state_get_name(enum bootflow_state_t state);
353
Simon Glassa8f5be12022-04-24 23:31:09 -0600354/**
355 * bootflow_remove() - Remove a bootflow and free its memory
356 *
357 * This updates the linked lists containing the bootflow then frees it.
358 *
359 * @bflow: Bootflow to remove
360 */
361void bootflow_remove(struct bootflow *bflow);
362
363/**
Simon Glass865328c2023-01-17 10:47:54 -0700364 * bootflow_iter_check_blk() - Check that a bootflow uses a block device
Simon Glassa8f5be12022-04-24 23:31:09 -0600365 *
366 * This checks the bootdev in the bootflow to make sure it uses a block device
367 *
368 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
369 */
Simon Glass865328c2023-01-17 10:47:54 -0700370int bootflow_iter_check_blk(const struct bootflow_iter *iter);
Simon Glassa8f5be12022-04-24 23:31:09 -0600371
372/**
Simon Glass0c1f4a92023-01-17 10:48:03 -0700373 * bootflow_iter_check_sf() - Check that a bootflow uses SPI FLASH
374 *
375 * This checks the bootdev in the bootflow to make sure it uses SPI flash
376 *
377 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
378 */
379int bootflow_iter_check_sf(const struct bootflow_iter *iter);
380
381/**
Simon Glass865328c2023-01-17 10:47:54 -0700382 * bootflow_iter_check_net() - Check that a bootflow uses a network device
Simon Glassa8f5be12022-04-24 23:31:09 -0600383 *
384 * This checks the bootdev in the bootflow to make sure it uses a network
385 * device
386 *
387 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
388 */
Simon Glass865328c2023-01-17 10:47:54 -0700389int bootflow_iter_check_net(const struct bootflow_iter *iter);
Simon Glassa8f5be12022-04-24 23:31:09 -0600390
391/**
Simon Glass865328c2023-01-17 10:47:54 -0700392 * bootflow_iter_check_system() - Check that a bootflow uses the bootstd device
Simon Glassa8f5be12022-04-24 23:31:09 -0600393 *
394 * This checks the bootdev in the bootflow to make sure it uses the bootstd
395 * device
396 *
397 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
398 */
Simon Glass865328c2023-01-17 10:47:54 -0700399int bootflow_iter_check_system(const struct bootflow_iter *iter);
Simon Glassa8f5be12022-04-24 23:31:09 -0600400
Simon Glass02d929b2023-01-06 08:52:40 -0600401/**
402 * bootflow_menu_new() - Create a new bootflow menu
403 *
404 * @expp: Returns the expo created
405 * Returns 0 on success, -ve on error
406 */
407int bootflow_menu_new(struct expo **expp);
408
409/**
Simon Glasse64c2952023-01-06 08:52:42 -0600410 * bootflow_menu_apply_theme() - Apply a theme to a bootmenu
411 *
412 * @exp: Expo to update
413 * @node: Node containing the theme information
414 * Returns 0 on success, -ve on error
415 */
416int bootflow_menu_apply_theme(struct expo *exp, ofnode node);
417
418/**
Simon Glass02d929b2023-01-06 08:52:40 -0600419 * bootflow_menu_run() - Create and run a menu of available bootflows
420 *
421 * @std: Bootstd information
422 * @text_mode: Uses a text-based menu suitable for a serial port
423 * @bflowp: Returns chosen bootflow (set to NULL if nothing is chosen)
424 * @return 0 if an option was chosen, -EAGAIN if nothing was chosen, -ve on
425 * error
426 */
427int bootflow_menu_run(struct bootstd_priv *std, bool text_mode,
428 struct bootflow **bflowp);
429
Simon Glass9d260252022-04-24 23:31:05 -0600430#endif