blob: a08ebf005ada534335ea844e6b652e4b71453dc3 [file] [log] [blame]
Simon Glass4b508b82022-04-24 23:31:08 -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 __bootmeth_h
8#define __bootmeth_h
9
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <linux/bitops.h>
11
Simon Glass4b508b82022-04-24 23:31:08 -060012struct blk_desc;
13struct bootflow;
14struct bootflow_iter;
15struct udevice;
16
17/**
Simon Glass4f8633d2022-07-30 15:52:21 -060018 * enum bootmeth_flags - Flags for bootmeths
19 *
20 * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically
Simon Glass0fca7e82023-08-24 13:55:43 -060021 * @BOOTMETHF_ANY_PART: bootmeth is willing to check any partition, even if it
22 * has no filesystem
Simon Glass4f8633d2022-07-30 15:52:21 -060023 */
24enum bootmeth_flags {
25 BOOTMETHF_GLOBAL = BIT(0),
Simon Glass0fca7e82023-08-24 13:55:43 -060026 BOOTMETHF_ANY_PART = BIT(1),
Simon Glass4f8633d2022-07-30 15:52:21 -060027};
28
29/**
Simon Glass4b508b82022-04-24 23:31:08 -060030 * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth
31 *
32 * @desc: A long description of the bootmeth
Simon Glass4f8633d2022-07-30 15:52:21 -060033 * @flags: Flags for this bootmeth (enum bootmeth_flags)
Simon Glass4b508b82022-04-24 23:31:08 -060034 */
35struct bootmeth_uc_plat {
36 const char *desc;
Simon Glass4f8633d2022-07-30 15:52:21 -060037 int flags;
Simon Glass4b508b82022-04-24 23:31:08 -060038};
39
40/** struct bootmeth_ops - Operations for boot methods */
41struct bootmeth_ops {
42 /**
Simon Glassf6d71a82022-07-30 15:52:19 -060043 * get_state_desc() - get detailed state information
44 *
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +020045 * Produces a textual description of the state of the boot method. This
Simon Glassf6d71a82022-07-30 15:52:19 -060046 * can include newline characters if it extends to multiple lines. It
47 * must be a nul-terminated string.
48 *
49 * This may involve reading state from the system, e.g. some data in
50 * the firmware area.
51 *
52 * @dev: Bootmethod device to check
53 * @buf: Buffer to place the info in (terminator must fit)
54 * @maxsize: Size of buffer
55 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
56 * something else went wrong
57 */
58 int (*get_state_desc)(struct udevice *dev, char *buf, int maxsize);
59
60 /**
61 * check_supported() - check if a bootmeth supports this bootdev
Simon Glass4b508b82022-04-24 23:31:08 -060062 *
63 * This is optional. If not provided, the bootdev is assumed to be
64 * supported
65 *
66 * The bootmeth can check the bootdev (e.g. to make sure it is a
67 * network device) or the partition information. The following fields
68 * in @iter are available:
69 *
70 * name, dev, state, part
71 * max_part may be set if part != 0 (i.e. there is a valid partition
72 * table). Otherwise max_part is 0
73 * method is available but is the same as @dev
74 * the partition has not yet been read, nor has the filesystem been
75 * checked
76 *
77 * It may update only the flags in @iter
78 *
79 * @dev: Bootmethod device to check against
80 * @iter: On entry, provides bootdev, hwpart, part
81 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
82 */
83 int (*check)(struct udevice *dev, struct bootflow_iter *iter);
84
85 /**
86 * read_bootflow() - read a bootflow for a device
87 *
88 * @dev: Bootmethod device to use
89 * @bflow: On entry, provides dev, hwpart, part and method.
90 * Returns updated bootflow if found
91 * Return: 0 if OK, -ve on error
92 */
93 int (*read_bootflow)(struct udevice *dev, struct bootflow *bflow);
94
95 /**
Simon Glassa63bda62023-01-17 10:48:01 -070096 * set_bootflow() - set the bootflow for a device
97 *
98 * This provides a bootflow file to the bootmeth, to see if it is valid.
99 * If it is, the bootflow is set up accordingly.
100 *
101 * @dev: Bootmethod device to use
102 * @bflow: On entry, provides bootdev.
103 * Returns updated bootflow if found
104 * @buf: Buffer containing the possible bootflow file
105 * @size: Size of file
106 * Return: 0 if OK, -ve on error
107 */
108 int (*set_bootflow)(struct udevice *dev, struct bootflow *bflow,
109 char *buf, int size);
110
111 /**
Simon Glass4b508b82022-04-24 23:31:08 -0600112 * read_file() - read a file needed for a bootflow
113 *
114 * Read a file from the same place as the bootflow came from
115 *
116 * @dev: Bootmethod device to use
117 * @bflow: Bootflow providing info on where to read from
118 * @file_path: Path to file (may be absolute or relative)
119 * @addr: Address to load file
120 * @sizep: On entry provides the maximum permitted size; on exit
121 * returns the size of the file
122 * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other
123 * -ve value if something else goes wrong
124 */
125 int (*read_file)(struct udevice *dev, struct bootflow *bflow,
126 const char *file_path, ulong addr, ulong *sizep);
Simon Glass6d8f95b2023-08-10 19:33:18 -0600127#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
128 /**
129 * readall() - read all files for a bootflow
130 *
131 * @dev: Bootmethod device to boot
132 * @bflow: Bootflow to read
133 * Return: 0 if OK, -EIO on I/O error, other -ve on other error
134 */
135 int (*read_all)(struct udevice *dev, struct bootflow *bflow);
136#endif /* BOOTSTD_FULL */
Simon Glass4b508b82022-04-24 23:31:08 -0600137 /**
138 * boot() - boot a bootflow
139 *
140 * @dev: Bootmethod device to boot
141 * @bflow: Bootflow to boot
142 * Return: does not return on success, since it should boot the
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200143 * operating system. Returns -EFAULT if that fails, -ENOTSUPP if
Simon Glass4b508b82022-04-24 23:31:08 -0600144 * trying method resulted in finding out that is not actually
145 * supported for this boot and should not be tried again unless
146 * something changes, other -ve on other error
147 */
148 int (*boot)(struct udevice *dev, struct bootflow *bflow);
Martyn Welch93a0d162024-10-09 14:15:40 +0100149
150 /**
151 * set_property() - set the bootmeth property
152 *
153 * This allows the setting of boot method specific properties to enable
154 * automated finer grain control of the boot process
155 *
156 * @name: String containing the name of the relevant boot method
157 * @property: String containing the name of the property to set
158 * @value: String containing the value to be set for the specified
159 * property
160 * Return: 0 if OK, -ENODEV if an unknown bootmeth or property is
161 * provided, -ENOENT if there are no bootmeth devices
162 */
163 int (*set_property)(struct udevice *dev, const char *property,
164 const char *value);
Simon Glass4b508b82022-04-24 23:31:08 -0600165};
166
167#define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops)
168
169/**
Simon Glassf6d71a82022-07-30 15:52:19 -0600170 * bootmeth_get_state_desc() - get detailed state information
171 *
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200172 * Produces a textual description of the state of the boot method. This
Simon Glassf6d71a82022-07-30 15:52:19 -0600173 * can include newline characters if it extends to multiple lines. It
174 * must be a nul-terminated string.
175 *
176 * This may involve reading state from the system, e.g. some data in
177 * the firmware area.
178 *
179 * @dev: Bootmethod device to check
180 * @buf: Buffer to place the info in (terminator must fit)
181 * @maxsize: Size of buffer
182 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
183 * something else went wrong
184 */
185int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize);
186
187/**
Simon Glass4b508b82022-04-24 23:31:08 -0600188 * bootmeth_check() - check if a bootmeth supports this bootflow
189 *
190 * This is optional. If not provided, the bootdev is assumed to be
191 * supported
192 *
193 * The bootmeth can check the bootdev (e.g. to make sure it is a
194 * network device) or the partition information. The following fields
195 * in @iter are available:
196 *
197 * name, dev, state, part
198 * max_part may be set if part != 0 (i.e. there is a valid partition
199 * table). Otherwise max_part is 0
200 * method is available but is the same as @dev
201 * the partition has not yet been read, nor has the filesystem been
202 * checked
203 *
204 * It may update only the flags in @iter
205 *
206 * @dev: Bootmethod device to check against
207 * @iter: On entry, provides bootdev, hwpart, part
208 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
209 */
210int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter);
211
212/**
213 * bootmeth_read_bootflow() - set up a bootflow for a device
214 *
215 * @dev: Bootmethod device to check
216 * @bflow: On entry, provides dev, hwpart, part and method.
217 * Returns updated bootflow if found
218 * Return: 0 if OK, -ve on error
219 */
220int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow);
221
222/**
Simon Glassa63bda62023-01-17 10:48:01 -0700223 * bootmeth_set_bootflow() - set the bootflow for a device
224 *
225 * This provides a bootflow file to the bootmeth, to see if it is valid.
226 * If it is, the bootflow is set up accordingly.
227 *
228 * @dev: Bootmethod device to use
229 * @bflow: On entry, provides bootdev.
230 * Returns updated bootflow if found
231 * @buf: Buffer containing the possible bootflow file (must be allocated
232 * by caller to @size + 1 bytes)
233 * @size: Size of file
234 * Return: 0 if OK, -ve on error
235 */
236int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
237 char *buf, int size);
238
239/**
Simon Glass4b508b82022-04-24 23:31:08 -0600240 * bootmeth_read_file() - read a file needed for a bootflow
241 *
242 * Read a file from the same place as the bootflow came from
243 *
244 * @dev: Bootmethod device to use
245 * @bflow: Bootflow providing info on where to read from
246 * @file_path: Path to file (may be absolute or relative)
247 * @addr: Address to load file
248 * @sizep: On entry provides the maximum permitted size; on exit
249 * returns the size of the file
250 * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other
251 * -ve value if something else goes wrong
252 */
253int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
254 const char *file_path, ulong addr, ulong *sizep);
255
256/**
Simon Glass6d8f95b2023-08-10 19:33:18 -0600257 * bootmeth_read_all() - read all bootflow files
258 *
259 * Some bootmeths delay reading of large files until booting is requested. This
260 * causes those files to be read.
261 *
262 * @dev: Bootmethod device to use
263 * @bflow: Bootflow to read
264 * Return: does not return on success, since it should boot the
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200265 * operating system. Returns -EFAULT if that fails, other -ve on
Simon Glass6d8f95b2023-08-10 19:33:18 -0600266 * other error
267 */
268int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow);
269
270/**
Simon Glass4b508b82022-04-24 23:31:08 -0600271 * bootmeth_boot() - boot a bootflow
272 *
273 * @dev: Bootmethod device to boot
274 * @bflow: Bootflow to boot
275 * Return: does not return on success, since it should boot the
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200276 * operating system. Returns -EFAULT if that fails, other -ve on
Simon Glass4b508b82022-04-24 23:31:08 -0600277 * other error
278 */
279int bootmeth_boot(struct udevice *dev, struct bootflow *bflow);
280
281/**
282 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
283 *
284 * This sets up the ordering information in @iter, based on the selected
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200285 * ordering of the boot methods in bootstd_priv->bootmeth_order. If there is no
Simon Glass4b508b82022-04-24 23:31:08 -0600286 * ordering there, then all bootmethods are added
287 *
288 * @iter: Iterator to update with the order
Simon Glasscc15e142022-07-30 15:52:27 -0600289 * @include_global: true to add the global bootmeths, in which case they appear
290 * first
Simon Glass4b508b82022-04-24 23:31:08 -0600291 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
292 * on other error
293 */
Simon Glasscc15e142022-07-30 15:52:27 -0600294int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global);
Simon Glass4b508b82022-04-24 23:31:08 -0600295
296/**
297 * bootmeth_set_order() - Set the bootmeth order
298 *
299 * This selects the ordering to use for bootmeths
300 *
301 * @order_str: String containing the ordering. This is a comma-separate list of
Simon Glassb71d7f72023-05-10 16:34:46 -0600302 * bootmeth-device names, e.g. "extlinux,efi". If empty then a default ordering
Simon Glass4b508b82022-04-24 23:31:08 -0600303 * is used, based on the sequence number of devices (i.e. using aliases)
304 * Return: 0 if OK, -ENODEV if an unknown bootmeth is mentioned, -ENOMEM if
305 * out of memory, -ENOENT if there are no bootmeth devices
306 */
307int bootmeth_set_order(const char *order_str);
308
309/**
Martyn Welch93a0d162024-10-09 14:15:40 +0100310 * bootmeth_set_property() - Set the bootmeth property
311 *
312 * This allows the setting of boot method specific properties to enable
313 * automated finer grain control of the boot process
314 *
315 * @name: String containing the name of the relevant boot method
316 * @property: String containing the name of the property to set
317 * @value: String containing the value to be set for the specified property
318 * Return: 0 if OK, -ENODEV if an unknown bootmeth or property is provided,
319 * -ENOENT if there are no bootmeth devices
320 */
321int bootmeth_set_property(const char *name, const char *property,
322 const char *value);
323
324/**
Simon Glassf41c4452023-07-26 21:01:21 -0600325 * bootmeth_setup_fs() - Set up read to read a file
326 *
327 * We must redo the setup before each filesystem operation. This function
328 * handles that, including setting the filesystem type if a block device is not
329 * being used
330 *
331 * @bflow: Information about file to try
332 * @desc: Block descriptor to read from (NULL if not a block device)
333 * Return: 0 if OK, -ve on error
334 */
335int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc);
336
337/**
Simon Glass4b508b82022-04-24 23:31:08 -0600338 * bootmeth_try_file() - See we can access a given file
339 *
340 * Check for a file with a given name. If found, the filename is allocated in
341 * @bflow
342 *
343 * Sets the state to BOOTFLOWST_FILE on success. It also calls
344 * fs_set_blk_dev_with_part() so that this does not need to be done by the
345 * caller before reading the file.
346 *
347 * @bflow: Information about file to try
Simon Glass4adeeb82023-01-17 10:47:59 -0700348 * @desc: Block descriptor to read from (NULL for sandbox host)
Simon Glass4b508b82022-04-24 23:31:08 -0600349 * @prefix: Filename prefix to prepend to @fname (NULL for none)
350 * @fname: Filename to read
351 * Return: 0 if OK, -ENOMEM if not enough memory to allocate bflow->fname,
352 * other -ve value on other error
353 */
354int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
355 const char *prefix, const char *fname);
356
357/**
358 * bootmeth_alloc_file() - Allocate and read a bootflow file
359 *
360 * Allocates memory for a bootflow file and reads it in. Sets the state to
361 * BOOTFLOWST_READY on success
362 *
363 * Note that fs_set_blk_dev_with_part() must have been called previously.
364 *
365 * @bflow: Information about file to read
366 * @size_limit: Maximum file size to permit
367 * @align: Allocation alignment (1 for unaligned)
368 * Return: 0 if OK, -E2BIG if file is too large, -ENOMEM if out of memory,
369 * other -ve on other error
370 */
371int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align);
372
373/**
Simon Glass612b9cc2023-01-06 08:52:34 -0600374 * bootmeth_alloc_other() - Allocate and read a file for a bootflow
375 *
376 * This reads an arbitrary file in the same directory as the bootflow,
377 * allocating memory for it. The buffer is one byte larger than the file length,
378 * so that it can be nul-terminated.
379 *
380 * @bflow: Information about file to read
381 * @fname: Filename to read from (within bootflow->subdir)
382 * @bufp: Returns a pointer to the allocated buffer
383 * @sizep: Returns the size of the buffer
384 * Return: 0 if OK, -ENOMEM if out of memory, other -ve on other error
385 */
386int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
387 void **bufp, uint *sizep);
388
389/**
Simon Glass4b508b82022-04-24 23:31:08 -0600390 * bootmeth_common_read_file() - Common handler for reading a file
391 *
392 * Reads a named file from the same location as the bootflow file.
393 *
394 * @dev: bootmeth device to read from
395 * @bflow: Bootflow information
396 * @file_path: Path to file
397 * @addr: Address to load file to
398 * @sizep: On entry, the maximum file size to accept, on exit the actual file
399 * size read
400 */
401int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
402 const char *file_path, ulong addr, ulong *sizep);
403
Simon Glass4f8633d2022-07-30 15:52:21 -0600404/**
405 * bootmeth_get_bootflow() - Get a bootflow from a global bootmeth
406 *
407 * Check the bootmeth for a bootflow which can be used. In this case the
408 * bootmeth handles all bootdev selection, etc.
409 *
410 * @dev: bootmeth device to read from
411 * @bflow: Bootflow information
412 * @return 0 on success, -ve if a bootflow could not be found or had an error
413 */
414int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow);
415
Simon Glass4b508b82022-04-24 23:31:08 -0600416#endif