blob: 6aa3d1fff8d30c6a55af97234a0d209309c1d364 [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
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)
55 */
56struct bootflow {
57 struct list_head bm_node;
58 struct list_head glob_node;
59 struct udevice *dev;
60 struct udevice *blk;
61 int part;
62 int fs_type;
63 struct udevice *method;
64 char *name;
65 enum bootflow_state_t state;
66 char *subdir;
67 char *fname;
68 char *buf;
69 int size;
70 int err;
71};
72
73/**
74 * enum bootflow_flags_t - flags for the bootflow iterator
75 *
76 * @BOOTFLOWF_FIXED: Only used fixed/internal media
77 * @BOOTFLOWF_SHOW: Show each bootdev before scanning it
78 * @BOOTFLOWF_ALL: Return bootflows with errors as well
79 * @BOOTFLOWF_SINGLE_DEV: Just scan one bootmeth
Simon Glass73fcf512022-07-30 15:52:25 -060080 * @BOOTFLOWF_SKIP_GLOBAL: Don't scan global bootmeths
Simon Glassa16d87d2022-04-24 23:31:05 -060081 */
82enum bootflow_flags_t {
83 BOOTFLOWF_FIXED = 1 << 0,
84 BOOTFLOWF_SHOW = 1 << 1,
85 BOOTFLOWF_ALL = 1 << 2,
86 BOOTFLOWF_SINGLE_DEV = 1 << 3,
Simon Glass73fcf512022-07-30 15:52:25 -060087 BOOTFLOWF_SKIP_GLOBAL = 1 << 4,
Simon Glassa16d87d2022-04-24 23:31:05 -060088};
89
90/**
91 * struct bootflow_iter - state for iterating through bootflows
92 *
93 * This starts at with the first bootdev/partition/bootmeth and can be used to
94 * iterate through all of them.
95 *
96 * Iteration starts with the bootdev. The first partition (0, i.e. whole device)
97 * is scanned first. For partition 0, it iterates through all the available
98 * bootmeths to see which one(s) can provide a bootflow. Then it moves to
99 * parition 1 (if there is one) and the process continues. Once all partitions
100 * are examined, it moves to the next bootdev.
101 *
102 * Initially @max_part is 0, meaning that only the whole device (@part=0) can be
103 * used. During scanning, if a partition table is found, then @max_part is
104 * updated to a larger value, no less than the number of available partitions.
105 * This ensures that iteration works through all partitions on the bootdev.
106 *
Simon Glass73fcf512022-07-30 15:52:25 -0600107 * @flags: Flags to use (see enum bootflow_flags_t). If BOOTFLOWF_GLOBAL_FIRST is
108 * enabled then the global bootmeths are being scanned, otherwise we have
109 * moved onto the bootdevs
110 * @dev: Current bootdev, NULL if none
Simon Glassa16d87d2022-04-24 23:31:05 -0600111 * @part: Current partition number (0 for whole device)
112 * @method: Current bootmeth
113 * @max_part: Maximum hardware partition number in @dev, 0 if there is no
114 * partition table
115 * @err: Error obtained from checking the last iteration. This is used to skip
116 * forward (e.g. to skip the current partition because it is not valid)
117 * -ESHUTDOWN: try next bootdev
118 * @num_devs: Number of bootdevs in @dev_order
119 * @cur_dev: Current bootdev number, an index into @dev_order[]
120 * @dev_order: List of bootdevs to scan, in order of priority. The scan starts
121 * with the first one on the list
122 * @num_methods: Number of bootmeth devices in @method_order
123 * @cur_method: Current method number, an index into @method_order
Simon Glass73fcf512022-07-30 15:52:25 -0600124 * @first_glob_method: First global method, if any, else -1
125 * @method_order: List of bootmeth devices to use, in order. The normal methods
126 * appear first, then the global ones, if any
127 * @doing_global: true if we are iterating through the global bootmeths (which
128 * happens before the normal ones)
Simon Glassa16d87d2022-04-24 23:31:05 -0600129 */
130struct bootflow_iter {
131 int flags;
132 struct udevice *dev;
133 int part;
134 struct udevice *method;
135 int max_part;
136 int err;
137 int num_devs;
138 int cur_dev;
139 struct udevice **dev_order;
140 int num_methods;
141 int cur_method;
Simon Glass73fcf512022-07-30 15:52:25 -0600142 int first_glob_method;
Simon Glassa16d87d2022-04-24 23:31:05 -0600143 struct udevice **method_order;
Simon Glass73fcf512022-07-30 15:52:25 -0600144 bool doing_global;
Simon Glassa16d87d2022-04-24 23:31:05 -0600145};
146
147/**
148 * bootflow_iter_init() - Reset a bootflow iterator
149 *
150 * This sets everything to the starting point, ready for use.
151 *
152 * @iter: Place to store private info (inited by this call)
153 * @flags: Flags to use (see enum bootflow_flags_t)
154 */
155void bootflow_iter_init(struct bootflow_iter *iter, int flags);
156
157/**
158 * bootflow_iter_uninit() - Free memory used by an interator
159 *
160 * @iter: Iterator to free
161 */
162void bootflow_iter_uninit(struct bootflow_iter *iter);
163
164/**
Simon Glass03fcbf92022-04-24 23:31:09 -0600165 * bootflow_iter_drop_bootmeth() - Remove a bootmeth from an iterator
166 *
167 * Update the iterator so that the bootmeth will not be used again while this
168 * iterator is in use
169 *
170 * @iter: Iterator to update
171 * @bmeth: Boot method to remove
172 */
173int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
174 const struct udevice *bmeth);
175
176/**
Simon Glassa16d87d2022-04-24 23:31:05 -0600177 * bootflow_scan_bootdev() - find the first bootflow in a bootdev
178 *
179 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
180 *
181 * @dev: Boot device to scan, NULL to work through all of them until it
Simon Glass943d38c2022-07-30 15:52:24 -0600182 * finds one that can supply a bootflow
Simon Glassa16d87d2022-04-24 23:31:05 -0600183 * @iter: Place to store private info (inited by this call)
Simon Glass943d38c2022-07-30 15:52:24 -0600184 * @flags: Flags for iterator (enum bootflow_flags_t)
Simon Glassa16d87d2022-04-24 23:31:05 -0600185 * @bflow: Place to put the bootflow if found
186 * Return: 0 if found, -ENODEV if no device, other -ve on other error
187 * (iteration can continue)
188 */
189int bootflow_scan_bootdev(struct udevice *dev, struct bootflow_iter *iter,
190 int flags, struct bootflow *bflow);
191
192/**
193 * bootflow_scan_first() - find the first bootflow
194 *
195 * This works through the available bootdev devices until it finds one that
196 * can supply a bootflow. It then returns that
197 *
198 * If @flags includes BOOTFLOWF_ALL then bootflows with errors are returned too
199 *
200 * @iter: Place to store private info (inited by this call), with
201 * @flags: Flags for bootdev (enum bootflow_flags_t)
202 * @bflow: Place to put the bootflow if found
203 * Return: 0 if found, -ENODEV if no device, other -ve on other error (iteration
204 * can continue)
205 */
206int bootflow_scan_first(struct bootflow_iter *iter, int flags,
207 struct bootflow *bflow);
208
209/**
210 * bootflow_scan_next() - find the next bootflow
211 *
212 * This works through the available bootdev devices until it finds one that
213 * can supply a bootflow. It then returns that bootflow
214 *
215 * @iter: Private info (as set up by bootflow_scan_first())
216 * @bflow: Place to put the bootflow if found
217 * Return: 0 if found, -ENODEV if no device, -ESHUTDOWN if no more bootflows,
218 * other -ve on other error (iteration can continue)
219 */
220int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow);
221
222/**
223 * bootflow_first_glob() - Get the first bootflow from the global list
224 *
225 * Returns the first bootflow in the global list, no matter what bootflow it is
226 * attached to
227 *
228 * @bflowp: Returns a pointer to the bootflow
229 * Return: 0 if found, -ENOENT if there are no bootflows
230 */
231int bootflow_first_glob(struct bootflow **bflowp);
232
233/**
234 * bootflow_next_glob() - Get the next bootflow from the global list
235 *
236 * Returns the next bootflow in the global list, no matter what bootflow it is
237 * attached to
238 *
239 * @bflowp: On entry, the last bootflow returned , e.g. from
240 * bootflow_first_glob()
241 * Return: 0 if found, -ENOENT if there are no more bootflows
242 */
243int bootflow_next_glob(struct bootflow **bflowp);
244
245/**
246 * bootflow_free() - Free memory used by a bootflow
247 *
248 * This frees fields within @bflow, but not the @bflow pointer itself
249 */
250void bootflow_free(struct bootflow *bflow);
251
252/**
253 * bootflow_boot() - boot a bootflow
254 *
255 * @bflow: Bootflow to boot
256 * Return: -EPROTO if bootflow has not been loaded, -ENOSYS if the bootflow
257 * type is not supported, -EFAULT if the boot returned without an error
258 * when we are expecting it to boot, -ENOTSUPP if trying method resulted in
259 * finding out that is not actually supported for this boot and should not
260 * be tried again unless something changes
261 */
262int bootflow_boot(struct bootflow *bflow);
263
264/**
265 * bootflow_run_boot() - Try to boot a bootflow
266 *
267 * @iter: Current iteration (or NULL if none). Used to disable a bootmeth if the
268 * boot returns -ENOTSUPP
269 * @bflow: Bootflow to boot
270 * Return: result of trying to boot
271 */
272int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow);
273
274/**
275 * bootflow_state_get_name() - Get the name of a bootflow state
276 *
277 * @state: State to check
278 * Return: name, or "?" if invalid
279 */
280const char *bootflow_state_get_name(enum bootflow_state_t state);
281
Simon Glass03fcbf92022-04-24 23:31:09 -0600282/**
283 * bootflow_remove() - Remove a bootflow and free its memory
284 *
285 * This updates the linked lists containing the bootflow then frees it.
286 *
287 * @bflow: Bootflow to remove
288 */
289void bootflow_remove(struct bootflow *bflow);
290
291/**
292 * bootflow_iter_uses_blk_dev() - Check that a bootflow uses a block device
293 *
294 * This checks the bootdev in the bootflow to make sure it uses a block device
295 *
296 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. ethernet)
297 */
298int bootflow_iter_uses_blk_dev(const struct bootflow_iter *iter);
299
300/**
301 * bootflow_iter_uses_network() - Check that a bootflow uses a network device
302 *
303 * This checks the bootdev in the bootflow to make sure it uses a network
304 * device
305 *
306 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
307 */
308int bootflow_iter_uses_network(const struct bootflow_iter *iter);
309
310/**
311 * bootflow_iter_uses_system() - Check that a bootflow uses the bootstd device
312 *
313 * This checks the bootdev in the bootflow to make sure it uses the bootstd
314 * device
315 *
316 * Return: 0 if OK, -ENOTSUPP if some other device is used (e.g. MMC)
317 */
318int bootflow_iter_uses_system(const struct bootflow_iter *iter);
319
Simon Glassa16d87d2022-04-24 23:31:05 -0600320#endif