blob: 8a07ab30191e25a8593dad7ceffbfe3fc0ba3e00 [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
10#include <linux/list.h>
11
12/**
13 * enum bootflow_state_t - states that a particular bootflow can be in
14 *
15 * Only bootflows in state BOOTFLOWST_READY can be used to boot.
16 *
17 * See bootflow_state[] for the names for each of these
18 */
19enum bootflow_state_t {
20 BOOTFLOWST_BASE, /**< Nothing known yet */
21 BOOTFLOWST_MEDIA, /**< Media exists */
22 BOOTFLOWST_PART, /**< Partition exists */
23 BOOTFLOWST_FS, /**< Filesystem exists */
24 BOOTFLOWST_FILE, /**< Bootflow file exists */
25 BOOTFLOWST_READY, /**< Bootflow file loaded */
26
27 BOOTFLOWST_COUNT
28};
29
30/**
31 * struct bootflow - information about a bootflow
32 *
33 * This is connected into two separate linked lists:
34 *
35 * bm_sibling - links all bootflows in the same bootdev
36 * glob_sibling - links all bootflows in all bootdevs
37 *
38 * @bm_node: Points to siblings in the same bootdev
39 * @glob_node: Points to siblings in the global list (all bootdev)
40 * @dev: Bootdevice device which produced this bootflow
41 * @blk: Block device which contains this bootflow, NULL if this is a network
42 * device
43 * @part: Partition number (0 for whole device)
44 * @fs_type: Filesystem type (FS_TYPE...) if this is fixed by the media, else 0.
45 * For example, the sandbox host-filesystem bootdev sets this to
46 * FS_TYPE_SANDBOX
47 * @method: Bootmethod device used to perform the boot and read files
48 * @name: Name of bootflow (allocated)
49 * @state: Current state (enum bootflow_state_t)
50 * @subdir: Subdirectory to fetch files from (with trailing /), or NULL if none
51 * @fname: Filename of bootflow file (allocated)
Simon Glass24d8e1b2023-01-06 08:52:34 -060052 * @logo: Logo to display for this bootflow (BMP format)
53 * @logo_size: Size of the logo in bytes
Simon Glass9d260252022-04-24 23:31:05 -060054 * @buf: Bootflow file contents (allocated)
55 * @size: Size of bootflow file in bytes
56 * @err: Error number received (0 if OK)
Simon Glass2175e762023-01-06 08:52:33 -060057 * @os_name: Name of the OS / distro being booted, or NULL if not known
58 * (allocated)
Simon Glass9d260252022-04-24 23:31:05 -060059 */
60struct bootflow {
61 struct list_head bm_node;
62 struct list_head glob_node;
63 struct udevice *dev;
64 struct udevice *blk;
65 int part;
66 int fs_type;
67 struct udevice *method;
68 char *name;
69 enum bootflow_state_t state;
70 char *subdir;
71 char *fname;
Simon Glass24d8e1b2023-01-06 08:52:34 -060072 void *logo;
73 uint logo_size;
Simon Glass9d260252022-04-24 23:31:05 -060074 char *buf;
75 int size;
76 int err;
Simon Glass2175e762023-01-06 08:52:33 -060077 char *os_name;
Simon Glass9d260252022-04-24 23:31:05 -060078};
79
80/**
81 * enum bootflow_flags_t - flags for the bootflow iterator
82 *
83 * @BOOTFLOWF_FIXED: Only used fixed/internal media
84 * @BOOTFLOWF_SHOW: Show each bootdev before scanning it
85 * @BOOTFLOWF_ALL: Return bootflows with errors as well
86 * @BOOTFLOWF_SINGLE_DEV: Just scan one bootmeth
Simon Glass2b80bc12022-07-30 15:52:25 -060087 * @BOOTFLOWF_SKIP_GLOBAL: Don't scan global bootmeths
Simon Glass9d260252022-04-24 23:31:05 -060088 */
89enum bootflow_flags_t {
90 BOOTFLOWF_FIXED = 1 << 0,
91 BOOTFLOWF_SHOW = 1 << 1,
92 BOOTFLOWF_ALL = 1 << 2,
93 BOOTFLOWF_SINGLE_DEV = 1 << 3,
Simon Glass2b80bc12022-07-30 15:52:25 -060094 BOOTFLOWF_SKIP_GLOBAL = 1 << 4,
Simon Glass9d260252022-04-24 23:31:05 -060095};
96
97/**
98 * struct bootflow_iter - state for iterating through bootflows
99 *
100 * This starts at with the first bootdev/partition/bootmeth and can be used to
101 * iterate through all of them.
102 *
103 * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
104 * is scanned first. For partition 0, it iterates through all the available
105 * bootmeths to see which one(s) can provide a bootflow. Then it moves to
106 * parition 1 (if there is one) and the process continues. Once all partitions
107 * are examined, it moves to the next bootdev.
108 *
109 * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
110 * used. During scanning, if a partition table is found, then @max_part is
111 * updated to a larger value, no less than the number of available partitions.
112 * This ensures that iteration works through all partitions on the bootdev.
113 *
Simon Glass2b80bc12022-07-30 15:52:25 -0600114 * @flags: Flags to use (see enum bootflow_flags_t). If BOOTFLOWF_GLOBAL_FIRST is
115 * enabled then the global bootmeths are being scanned, otherwise we have
116 * moved onto the bootdevs
117 * @dev: Current bootdev, NULL if none
Simon Glass9d260252022-04-24 23:31:05 -0600118 * @part: Current partition number (0 for whole device)
119 * @method: Current bootmeth
120 * @max_part: Maximum hardware partition number in @dev, 0 if there is no
121 * partition table
122 * @err: Error obtained from checking the last iteration. This is used to skip
123 * forward (e.g. to skip the current partition because it is not valid)
124 * -ESHUTDOWN: try next bootdev
125 * @num_devs: Number of bootdevs in @dev_order
126 * @cur_dev: Current bootdev number, an index into @dev_order[]
127 * @dev_order: List of bootdevs to scan, in order of priority. The scan starts
128 * with the first one on the list
129 * @num_methods: Number of bootmeth devices in @method_order
130 * @cur_method: Current method number, an index into @method_order
Simon Glass2b80bc12022-07-30 15:52:25 -0600131 * @first_glob_method: First global method, if any, else -1
132 * @method_order: List of bootmeth devices to use, in order. The normal methods
133 * appear first, then the global ones, if any
134 * @doing_global: true if we are iterating through the global bootmeths (which
135 * happens before the normal ones)
Simon Glass9d260252022-04-24 23:31:05 -0600136 */
137struct bootflow_iter {
138 int flags;
139 struct udevice *dev;
140 int part;
141 struct udevice *method;
142 int max_part;
143 int err;
144 int num_devs;
145 int cur_dev;
146 struct udevice **dev_order;
147 int num_methods;
148 int cur_method;
Simon Glass2b80bc12022-07-30 15:52:25 -0600149 int first_glob_method;
Simon Glass9d260252022-04-24 23:31:05 -0600150 struct udevice **method_order;
Simon Glass2b80bc12022-07-30 15:52:25 -0600151 bool doing_global;
Simon Glass9d260252022-04-24 23:31:05 -0600152};
153
154/**
Simon Glassb190deb2022-10-20 18:22:51 -0600155 * bootflow_init() - Set up a bootflow struct
156 *
157 * The bootflow is zeroed and set to state BOOTFLOWST_BASE
158 *
159 * @bflow: Struct to set up
160 * @bootdev: Bootdev to use
161 * @meth: Bootmeth to use
162 */
163void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
164 struct udevice *meth);
165
166/**
Simon Glass9d260252022-04-24 23:31:05 -0600167 * bootflow_iter_init() - Reset a bootflow iterator
168 *
169 * This sets everything to the starting point, ready for use.
170 *
171 * @iter: Place to store private info (inited by this call)
172 * @flags: Flags to use (see enum bootflow_flags_t)
173 */
174void bootflow_iter_init(struct bootflow_iter *iter, int flags);
175
176/**
177 * bootflow_iter_uninit() - Free memory used by an interator
178 *
179 * @iter: Iterator to free
180 */
181void bootflow_iter_uninit(struct bootflow_iter *iter);
182
183/**
Simon Glassa8f5be12022-04-24 23:31:09 -0600184 * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
185 *
186 * Update the iterator so that the bootmeth will not be used again while this
187 * iterator is in use
188 *
189 * @iter: Iterator to update
190 * @bmeth: Boot method to remove
191 */
192int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
193 const struct udevice *bmeth);
194
195/**
Simon Glass9d260252022-04-24 23:31:05 -0600196 * bootflow_scan_bootdev() - find the first bootflow in a bootdev
197 *
198 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
199 *
200 * @dev: Boot device to scan, NULL to work through all of them until it
Simon Glassee47d4a2022-07-30 15:52:24 -0600201 * finds one that can supply a bootflow
Simon Glass9d260252022-04-24 23:31:05 -0600202 * @iter: Place to store private info (inited by this call)
Simon Glassee47d4a2022-07-30 15:52:24 -0600203 * @flags: Flags for iterator (enum bootflow_flags_t)
Simon Glass9d260252022-04-24 23:31:05 -0600204 * @bflow: Place to put the bootflow if found
205 * Return: 0 if found, -ENODEV if no device, other -ve on other error
206 * (iteration can continue)
207 */
208int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
209 int flags, struct bootflow *bflow);
210
211/**
212 * bootflow_scan_first() - find the first bootflow
213 *
214 * This works through the available bootdev devices until it finds one that
215 * can supply a bootflow. It then returns that
216 *
217 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
218 *
219 * @iter: Place to store private info (inited by this call), with
220 * @flags: Flags for bootdev (enum bootflow_flags_t)
221 * @bflow: Place to put the bootflow if found
222 * Return: 0 if found, -ENODEV if no device, other -ve on other error (iteration
223 * can continue)
224 */
225int bootflow_scan_first(struct bootflow_iter *iter, int flags,
226 struct bootflow *bflow);
227
228/**
229 * bootflow_scan_next() - find the next bootflow
230 *
231 * This works through the available bootdev devices until it finds one that
232 * can supply a bootflow. It then returns that bootflow
233 *
234 * @iter: Private info (as set up by bootflow_scan_first())
235 * @bflow: Place to put the bootflow if found
236 * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
237 * other -ve on other error (iteration can continue)
238 */
239int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
240
241/**
242 * bootflow_first_glob() - Get the first bootflow from the global list
243 *
244 * Returns the first bootflow in the global list, no matter what bootflow it is
245 * attached to
246 *
247 * @bflowp: Returns a pointer to the bootflow
248 * Return: 0 if found, -ENOENT if there are no bootflows
249 */
250int bootflow_first_glob(struct bootflow **bflowp);
251
252/**
253 * bootflow_next_glob() - Get the next bootflow from the global list
254 *
255 * Returns the next bootflow in the global list, no matter what bootflow it is
256 * attached to
257 *
258 * @bflowp: On entry, the last bootflow returned , e.g. from
259 * bootflow_first_glob()
260 * Return: 0 if found, -ENOENT if there are no more bootflows
261 */
262int bootflow_next_glob(struct bootflow **bflowp);
263
264/**
265 * bootflow_free() - Free memory used by a bootflow
266 *
267 * This frees fields within @bflow, but not the @bflow pointer itself
268 */
269void bootflow_free(struct bootflow *bflow);
270
271/**
272 * bootflow_boot() - boot a bootflow
273 *
274 * @bflow: Bootflow to boot
275 * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
276 * type is not supported, -EFAULT if the boot returned without an error
277 * when we are expecting it to boot, -ENOTSUPP if trying method resulted in
278 * finding out that is not actually supported for this boot and should not
279 * be tried again unless something changes
280 */
281int bootflow_boot(struct bootflow *bflow);
282
283/**
284 * bootflow_run_boot() - Try to boot a bootflow
285 *
286 * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
287 * boot returns -ENOTSUPP
288 * @bflow: Bootflow to boot
289 * Return: result of trying to boot
290 */
291int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
292
293/**
294 * bootflow_state_get_name() - Get the name of a bootflow state
295 *
296 * @state: State to check
297 * Return: name, or "?" if invalid
298 */
299const char *bootflow_state_get_name(enum bootflow_state_t state);
300
Simon Glassa8f5be12022-04-24 23:31:09 -0600301/**
302 * bootflow_remove() - Remove a bootflow and free its memory
303 *
304 * This updates the linked lists containing the bootflow then frees it.
305 *
306 * @bflow: Bootflow to remove
307 */
308void bootflow_remove(struct bootflow *bflow);
309
310/**
311 * bootflow_iter_uses_blk_dev() - Check that a bootflow uses a block device
312 *
313 * This checks the bootdev in the bootflow to make sure it uses a block device
314 *
315 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
316 */
317int bootflow_iter_uses_blk_dev(const struct bootflow_iter *iter);
318
319/**
320 * bootflow_iter_uses_network() - Check that a bootflow uses a network device
321 *
322 * This checks the bootdev in the bootflow to make sure it uses a network
323 * device
324 *
325 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
326 */
327int bootflow_iter_uses_network(const struct bootflow_iter *iter);
328
329/**
330 * bootflow_iter_uses_system() - Check that a bootflow uses the bootstd device
331 *
332 * This checks the bootdev in the bootflow to make sure it uses the bootstd
333 * device
334 *
335 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
336 */
337int bootflow_iter_uses_system(const struct bootflow_iter *iter);
338
Simon Glass9d260252022-04-24 23:31:05 -0600339#endif