blob: 776158c65df59c60a2535cd01802563a47164ce6 [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)
52 * @buf: Bootflow file contents (allocated)
53 * @size: Size of bootflow file in bytes
54 * @err: Error number received (0 if OK)
Simon Glass2175e762023-01-06 08:52:33 -060055 * @os_name: Name of the OS / distro being booted, or NULL if not known
56 * (allocated)
Simon Glass9d260252022-04-24 23:31:05 -060057 */
58struct bootflow {
59 struct list_head bm_node;
60 struct list_head glob_node;
61 struct udevice *dev;
62 struct udevice *blk;
63 int part;
64 int fs_type;
65 struct udevice *method;
66 char *name;
67 enum bootflow_state_t state;
68 char *subdir;
69 char *fname;
70 char *buf;
71 int size;
72 int err;
Simon Glass2175e762023-01-06 08:52:33 -060073 char *os_name;
Simon Glass9d260252022-04-24 23:31:05 -060074};
75
76/**
77 * enum bootflow_flags_t - flags for the bootflow iterator
78 *
79 * @BOOTFLOWF_FIXED: Only used fixed/internal media
80 * @BOOTFLOWF_SHOW: Show each bootdev before scanning it
81 * @BOOTFLOWF_ALL: Return bootflows with errors as well
82 * @BOOTFLOWF_SINGLE_DEV: Just scan one bootmeth
Simon Glass2b80bc12022-07-30 15:52:25 -060083 * @BOOTFLOWF_SKIP_GLOBAL: Don't scan global bootmeths
Simon Glass9d260252022-04-24 23:31:05 -060084 */
85enum bootflow_flags_t {
86 BOOTFLOWF_FIXED = 1 << 0,
87 BOOTFLOWF_SHOW = 1 << 1,
88 BOOTFLOWF_ALL = 1 << 2,
89 BOOTFLOWF_SINGLE_DEV = 1 << 3,
Simon Glass2b80bc12022-07-30 15:52:25 -060090 BOOTFLOWF_SKIP_GLOBAL = 1 << 4,
Simon Glass9d260252022-04-24 23:31:05 -060091};
92
93/**
94 * struct bootflow_iter - state for iterating through bootflows
95 *
96 * This starts at with the first bootdev/partition/bootmeth and can be used to
97 * iterate through all of them.
98 *
99 * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
100 * is scanned first. For partition 0, it iterates through all the available
101 * bootmeths to see which one(s) can provide a bootflow. Then it moves to
102 * parition 1 (if there is one) and the process continues. Once all partitions
103 * are examined, it moves to the next bootdev.
104 *
105 * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
106 * used. During scanning, if a partition table is found, then @max_part is
107 * updated to a larger value, no less than the number of available partitions.
108 * This ensures that iteration works through all partitions on the bootdev.
109 *
Simon Glass2b80bc12022-07-30 15:52:25 -0600110 * @flags: Flags to use (see enum bootflow_flags_t). If BOOTFLOWF_GLOBAL_FIRST is
111 * enabled then the global bootmeths are being scanned, otherwise we have
112 * moved onto the bootdevs
113 * @dev: Current bootdev, NULL if none
Simon Glass9d260252022-04-24 23:31:05 -0600114 * @part: Current partition number (0 for whole device)
115 * @method: Current bootmeth
116 * @max_part: Maximum hardware partition number in @dev, 0 if there is no
117 * partition table
118 * @err: Error obtained from checking the last iteration. This is used to skip
119 * forward (e.g. to skip the current partition because it is not valid)
120 * -ESHUTDOWN: try next bootdev
121 * @num_devs: Number of bootdevs in @dev_order
122 * @cur_dev: Current bootdev number, an index into @dev_order[]
123 * @dev_order: List of bootdevs to scan, in order of priority. The scan starts
124 * with the first one on the list
125 * @num_methods: Number of bootmeth devices in @method_order
126 * @cur_method: Current method number, an index into @method_order
Simon Glass2b80bc12022-07-30 15:52:25 -0600127 * @first_glob_method: First global method, if any, else -1
128 * @method_order: List of bootmeth devices to use, in order. The normal methods
129 * appear first, then the global ones, if any
130 * @doing_global: true if we are iterating through the global bootmeths (which
131 * happens before the normal ones)
Simon Glass9d260252022-04-24 23:31:05 -0600132 */
133struct bootflow_iter {
134 int flags;
135 struct udevice *dev;
136 int part;
137 struct udevice *method;
138 int max_part;
139 int err;
140 int num_devs;
141 int cur_dev;
142 struct udevice **dev_order;
143 int num_methods;
144 int cur_method;
Simon Glass2b80bc12022-07-30 15:52:25 -0600145 int first_glob_method;
Simon Glass9d260252022-04-24 23:31:05 -0600146 struct udevice **method_order;
Simon Glass2b80bc12022-07-30 15:52:25 -0600147 bool doing_global;
Simon Glass9d260252022-04-24 23:31:05 -0600148};
149
150/**
Simon Glassb190deb2022-10-20 18:22:51 -0600151 * bootflow_init() - Set up a bootflow struct
152 *
153 * The bootflow is zeroed and set to state BOOTFLOWST_BASE
154 *
155 * @bflow: Struct to set up
156 * @bootdev: Bootdev to use
157 * @meth: Bootmeth to use
158 */
159void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
160 struct udevice *meth);
161
162/**
Simon Glass9d260252022-04-24 23:31:05 -0600163 * bootflow_iter_init() - Reset a bootflow iterator
164 *
165 * This sets everything to the starting point, ready for use.
166 *
167 * @iter: Place to store private info (inited by this call)
168 * @flags: Flags to use (see enum bootflow_flags_t)
169 */
170void bootflow_iter_init(struct bootflow_iter *iter, int flags);
171
172/**
173 * bootflow_iter_uninit() - Free memory used by an interator
174 *
175 * @iter: Iterator to free
176 */
177void bootflow_iter_uninit(struct bootflow_iter *iter);
178
179/**
Simon Glassa8f5be12022-04-24 23:31:09 -0600180 * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
181 *
182 * Update the iterator so that the bootmeth will not be used again while this
183 * iterator is in use
184 *
185 * @iter: Iterator to update
186 * @bmeth: Boot method to remove
187 */
188int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
189 const struct udevice *bmeth);
190
191/**
Simon Glass9d260252022-04-24 23:31:05 -0600192 * bootflow_scan_bootdev() - find the first bootflow in a bootdev
193 *
194 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
195 *
196 * @dev: Boot device to scan, NULL to work through all of them until it
Simon Glassee47d4a2022-07-30 15:52:24 -0600197 * finds one that can supply a bootflow
Simon Glass9d260252022-04-24 23:31:05 -0600198 * @iter: Place to store private info (inited by this call)
Simon Glassee47d4a2022-07-30 15:52:24 -0600199 * @flags: Flags for iterator (enum bootflow_flags_t)
Simon Glass9d260252022-04-24 23:31:05 -0600200 * @bflow: Place to put the bootflow if found
201 * Return: 0 if found, -ENODEV if no device, other -ve on other error
202 * (iteration can continue)
203 */
204int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
205 int flags, struct bootflow *bflow);
206
207/**
208 * bootflow_scan_first() - find the first bootflow
209 *
210 * This works through the available bootdev devices until it finds one that
211 * can supply a bootflow. It then returns that
212 *
213 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
214 *
215 * @iter: Place to store private info (inited by this call), with
216 * @flags: Flags for bootdev (enum bootflow_flags_t)
217 * @bflow: Place to put the bootflow if found
218 * Return: 0 if found, -ENODEV if no device, other -ve on other error (iteration
219 * can continue)
220 */
221int bootflow_scan_first(struct bootflow_iter *iter, int flags,
222 struct bootflow *bflow);
223
224/**
225 * bootflow_scan_next() - find the next bootflow
226 *
227 * This works through the available bootdev devices until it finds one that
228 * can supply a bootflow. It then returns that bootflow
229 *
230 * @iter: Private info (as set up by bootflow_scan_first())
231 * @bflow: Place to put the bootflow if found
232 * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
233 * other -ve on other error (iteration can continue)
234 */
235int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
236
237/**
238 * bootflow_first_glob() - Get the first bootflow from the global list
239 *
240 * Returns the first bootflow in the global list, no matter what bootflow it is
241 * attached to
242 *
243 * @bflowp: Returns a pointer to the bootflow
244 * Return: 0 if found, -ENOENT if there are no bootflows
245 */
246int bootflow_first_glob(struct bootflow **bflowp);
247
248/**
249 * bootflow_next_glob() - Get the next bootflow from the global list
250 *
251 * Returns the next bootflow in the global list, no matter what bootflow it is
252 * attached to
253 *
254 * @bflowp: On entry, the last bootflow returned , e.g. from
255 * bootflow_first_glob()
256 * Return: 0 if found, -ENOENT if there are no more bootflows
257 */
258int bootflow_next_glob(struct bootflow **bflowp);
259
260/**
261 * bootflow_free() - Free memory used by a bootflow
262 *
263 * This frees fields within @bflow, but not the @bflow pointer itself
264 */
265void bootflow_free(struct bootflow *bflow);
266
267/**
268 * bootflow_boot() - boot a bootflow
269 *
270 * @bflow: Bootflow to boot
271 * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
272 * type is not supported, -EFAULT if the boot returned without an error
273 * when we are expecting it to boot, -ENOTSUPP if trying method resulted in
274 * finding out that is not actually supported for this boot and should not
275 * be tried again unless something changes
276 */
277int bootflow_boot(struct bootflow *bflow);
278
279/**
280 * bootflow_run_boot() - Try to boot a bootflow
281 *
282 * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
283 * boot returns -ENOTSUPP
284 * @bflow: Bootflow to boot
285 * Return: result of trying to boot
286 */
287int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
288
289/**
290 * bootflow_state_get_name() - Get the name of a bootflow state
291 *
292 * @state: State to check
293 * Return: name, or "?" if invalid
294 */
295const char *bootflow_state_get_name(enum bootflow_state_t state);
296
Simon Glassa8f5be12022-04-24 23:31:09 -0600297/**
298 * bootflow_remove() - Remove a bootflow and free its memory
299 *
300 * This updates the linked lists containing the bootflow then frees it.
301 *
302 * @bflow: Bootflow to remove
303 */
304void bootflow_remove(struct bootflow *bflow);
305
306/**
307 * bootflow_iter_uses_blk_dev() - Check that a bootflow uses a block device
308 *
309 * This checks the bootdev in the bootflow to make sure it uses a block device
310 *
311 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
312 */
313int bootflow_iter_uses_blk_dev(const struct bootflow_iter *iter);
314
315/**
316 * bootflow_iter_uses_network() - Check that a bootflow uses a network device
317 *
318 * This checks the bootdev in the bootflow to make sure it uses a network
319 * device
320 *
321 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
322 */
323int bootflow_iter_uses_network(const struct bootflow_iter *iter);
324
325/**
326 * bootflow_iter_uses_system() - Check that a bootflow uses the bootstd device
327 *
328 * This checks the bootdev in the bootflow to make sure it uses the bootstd
329 * device
330 *
331 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
332 */
333int bootflow_iter_uses_system(const struct bootflow_iter *iter);
334
Simon Glass9d260252022-04-24 23:31:05 -0600335#endif