blob: 4d8ca48efd47cd95e81f3deeda2b712592d91436 [file] [log] [blame]
Simon Glassa950d312022-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 Rini03de3052024-05-20 13:35:03 -060010#include <linux/bitops.h>
11
Simon Glassa950d312022-04-24 23:31:08 -060012struct blk_desc;
13struct bootflow;
14struct bootflow_iter;
15struct udevice;
16
17/**
Simon Glassbc06aa02022-07-30 15:52:21 -060018 * enum bootmeth_flags - Flags for bootmeths
19 *
20 * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically
Simon Glass831405f2023-08-24 13:55:43 -060021 * @BOOTMETHF_ANY_PART: bootmeth is willing to check any partition, even if it
22 * has no filesystem
Simon Glassbc06aa02022-07-30 15:52:21 -060023 */
24enum bootmeth_flags {
25 BOOTMETHF_GLOBAL = BIT(0),
Simon Glass831405f2023-08-24 13:55:43 -060026 BOOTMETHF_ANY_PART = BIT(1),
Simon Glassbc06aa02022-07-30 15:52:21 -060027};
28
29/**
Simon Glassa950d312022-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 Glassbc06aa02022-07-30 15:52:21 -060033 * @flags: Flags for this bootmeth (enum bootmeth_flags)
Simon Glassa950d312022-04-24 23:31:08 -060034 */
35struct bootmeth_uc_plat {
36 const char *desc;
Simon Glassbc06aa02022-07-30 15:52:21 -060037 int flags;
Simon Glassa950d312022-04-24 23:31:08 -060038};
39
40/** struct bootmeth_ops - Operations for boot methods */
41struct bootmeth_ops {
42 /**
Simon Glass988caca2022-07-30 15:52:19 -060043 * get_state_desc() - get detailed state information
44 *
Mattijs Korpershoek06eb1aa2024-06-04 17:15:21 +020045 * Produces a textual description of the state of the boot method. This
Simon Glass988caca2022-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 Glassa950d312022-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 Glass22061d32023-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 Glassa950d312022-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 Glassc2792242023-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 Glassa950d312022-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 Korpershoek06eb1aa2024-06-04 17:15:21 +0200143 * operating system. Returns -EFAULT if that fails, -ENOTSUPP if
Simon Glassa950d312022-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);
149};
150
151#define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops)
152
153/**
Simon Glass988caca2022-07-30 15:52:19 -0600154 * bootmeth_get_state_desc() - get detailed state information
155 *
Mattijs Korpershoek06eb1aa2024-06-04 17:15:21 +0200156 * Produces a textual description of the state of the boot method. This
Simon Glass988caca2022-07-30 15:52:19 -0600157 * can include newline characters if it extends to multiple lines. It
158 * must be a nul-terminated string.
159 *
160 * This may involve reading state from the system, e.g. some data in
161 * the firmware area.
162 *
163 * @dev: Bootmethod device to check
164 * @buf: Buffer to place the info in (terminator must fit)
165 * @maxsize: Size of buffer
166 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
167 * something else went wrong
168 */
169int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize);
170
171/**
Simon Glassa950d312022-04-24 23:31:08 -0600172 * bootmeth_check() - check if a bootmeth supports this bootflow
173 *
174 * This is optional. If not provided, the bootdev is assumed to be
175 * supported
176 *
177 * The bootmeth can check the bootdev (e.g. to make sure it is a
178 * network device) or the partition information. The following fields
179 * in @iter are available:
180 *
181 * name, dev, state, part
182 * max_part may be set if part != 0 (i.e. there is a valid partition
183 * table). Otherwise max_part is 0
184 * method is available but is the same as @dev
185 * the partition has not yet been read, nor has the filesystem been
186 * checked
187 *
188 * It may update only the flags in @iter
189 *
190 * @dev: Bootmethod device to check against
191 * @iter: On entry, provides bootdev, hwpart, part
192 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
193 */
194int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter);
195
196/**
197 * bootmeth_read_bootflow() - set up a bootflow for a device
198 *
199 * @dev: Bootmethod device to check
200 * @bflow: On entry, provides dev, hwpart, part and method.
201 * Returns updated bootflow if found
202 * Return: 0 if OK, -ve on error
203 */
204int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow);
205
206/**
Simon Glass22061d32023-01-17 10:48:01 -0700207 * bootmeth_set_bootflow() - set the bootflow for a device
208 *
209 * This provides a bootflow file to the bootmeth, to see if it is valid.
210 * If it is, the bootflow is set up accordingly.
211 *
212 * @dev: Bootmethod device to use
213 * @bflow: On entry, provides bootdev.
214 * Returns updated bootflow if found
215 * @buf: Buffer containing the possible bootflow file (must be allocated
216 * by caller to @size + 1 bytes)
217 * @size: Size of file
218 * Return: 0 if OK, -ve on error
219 */
220int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
221 char *buf, int size);
222
223/**
Simon Glassa950d312022-04-24 23:31:08 -0600224 * bootmeth_read_file() - read a file needed for a bootflow
225 *
226 * Read a file from the same place as the bootflow came from
227 *
228 * @dev: Bootmethod device to use
229 * @bflow: Bootflow providing info on where to read from
230 * @file_path: Path to file (may be absolute or relative)
231 * @addr: Address to load file
232 * @sizep: On entry provides the maximum permitted size; on exit
233 * returns the size of the file
234 * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other
235 * -ve value if something else goes wrong
236 */
237int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
238 const char *file_path, ulong addr, ulong *sizep);
239
240/**
Simon Glassc2792242023-08-10 19:33:18 -0600241 * bootmeth_read_all() - read all bootflow files
242 *
243 * Some bootmeths delay reading of large files until booting is requested. This
244 * causes those files to be read.
245 *
246 * @dev: Bootmethod device to use
247 * @bflow: Bootflow to read
248 * Return: does not return on success, since it should boot the
Mattijs Korpershoek06eb1aa2024-06-04 17:15:21 +0200249 * operating system. Returns -EFAULT if that fails, other -ve on
Simon Glassc2792242023-08-10 19:33:18 -0600250 * other error
251 */
252int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow);
253
254/**
Simon Glassa950d312022-04-24 23:31:08 -0600255 * bootmeth_boot() - boot a bootflow
256 *
257 * @dev: Bootmethod device to boot
258 * @bflow: Bootflow to boot
259 * Return: does not return on success, since it should boot the
Mattijs Korpershoek06eb1aa2024-06-04 17:15:21 +0200260 * operating system. Returns -EFAULT if that fails, other -ve on
Simon Glassa950d312022-04-24 23:31:08 -0600261 * other error
262 */
263int bootmeth_boot(struct udevice *dev, struct bootflow *bflow);
264
265/**
266 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
267 *
268 * This sets up the ordering information in @iter, based on the selected
Mattijs Korpershoek06eb1aa2024-06-04 17:15:21 +0200269 * ordering of the boot methods in bootstd_priv->bootmeth_order. If there is no
Simon Glassa950d312022-04-24 23:31:08 -0600270 * ordering there, then all bootmethods are added
271 *
272 * @iter: Iterator to update with the order
Simon Glassc627cfc2022-07-30 15:52:27 -0600273 * @include_global: true to add the global bootmeths, in which case they appear
274 * first
Simon Glassa950d312022-04-24 23:31:08 -0600275 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
276 * on other error
277 */
Simon Glassc627cfc2022-07-30 15:52:27 -0600278int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global);
Simon Glassa950d312022-04-24 23:31:08 -0600279
280/**
281 * bootmeth_set_order() - Set the bootmeth order
282 *
283 * This selects the ordering to use for bootmeths
284 *
285 * @order_str: String containing the ordering. This is a comma-separate list of
Simon Glass79f66352023-05-10 16:34:46 -0600286 * bootmeth-device names, e.g. "extlinux,efi". If empty then a default ordering
Simon Glassa950d312022-04-24 23:31:08 -0600287 * is used, based on the sequence number of devices (i.e. using aliases)
288 * Return: 0 if OK, -ENODEV if an unknown bootmeth is mentioned, -ENOMEM if
289 * out of memory, -ENOENT if there are no bootmeth devices
290 */
291int bootmeth_set_order(const char *order_str);
292
293/**
Simon Glass0c0c82b2023-07-26 21:01:21 -0600294 * bootmeth_setup_fs() - Set up read to read a file
295 *
296 * We must redo the setup before each filesystem operation. This function
297 * handles that, including setting the filesystem type if a block device is not
298 * being used
299 *
300 * @bflow: Information about file to try
301 * @desc: Block descriptor to read from (NULL if not a block device)
302 * Return: 0 if OK, -ve on error
303 */
304int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc);
305
306/**
Simon Glassa950d312022-04-24 23:31:08 -0600307 * bootmeth_try_file() - See we can access a given file
308 *
309 * Check for a file with a given name. If found, the filename is allocated in
310 * @bflow
311 *
312 * Sets the state to BOOTFLOWST_FILE on success. It also calls
313 * fs_set_blk_dev_with_part() so that this does not need to be done by the
314 * caller before reading the file.
315 *
316 * @bflow: Information about file to try
Simon Glassa58e7bb2023-01-17 10:47:59 -0700317 * @desc: Block descriptor to read from (NULL for sandbox host)
Simon Glassa950d312022-04-24 23:31:08 -0600318 * @prefix: Filename prefix to prepend to @fname (NULL for none)
319 * @fname: Filename to read
320 * Return: 0 if OK, -ENOMEM if not enough memory to allocate bflow->fname,
321 * other -ve value on other error
322 */
323int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
324 const char *prefix, const char *fname);
325
326/**
327 * bootmeth_alloc_file() - Allocate and read a bootflow file
328 *
329 * Allocates memory for a bootflow file and reads it in. Sets the state to
330 * BOOTFLOWST_READY on success
331 *
332 * Note that fs_set_blk_dev_with_part() must have been called previously.
333 *
334 * @bflow: Information about file to read
335 * @size_limit: Maximum file size to permit
336 * @align: Allocation alignment (1 for unaligned)
337 * Return: 0 if OK, -E2BIG if file is too large, -ENOMEM if out of memory,
338 * other -ve on other error
339 */
340int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align);
341
342/**
Simon Glass24d8e1b2023-01-06 08:52:34 -0600343 * bootmeth_alloc_other() - Allocate and read a file for a bootflow
344 *
345 * This reads an arbitrary file in the same directory as the bootflow,
346 * allocating memory for it. The buffer is one byte larger than the file length,
347 * so that it can be nul-terminated.
348 *
349 * @bflow: Information about file to read
350 * @fname: Filename to read from (within bootflow->subdir)
351 * @bufp: Returns a pointer to the allocated buffer
352 * @sizep: Returns the size of the buffer
353 * Return: 0 if OK, -ENOMEM if out of memory, other -ve on other error
354 */
355int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
356 void **bufp, uint *sizep);
357
358/**
Simon Glassa950d312022-04-24 23:31:08 -0600359 * bootmeth_common_read_file() - Common handler for reading a file
360 *
361 * Reads a named file from the same location as the bootflow file.
362 *
363 * @dev: bootmeth device to read from
364 * @bflow: Bootflow information
365 * @file_path: Path to file
366 * @addr: Address to load file to
367 * @sizep: On entry, the maximum file size to accept, on exit the actual file
368 * size read
369 */
370int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
371 const char *file_path, ulong addr, ulong *sizep);
372
Simon Glassbc06aa02022-07-30 15:52:21 -0600373/**
374 * bootmeth_get_bootflow() - Get a bootflow from a global bootmeth
375 *
376 * Check the bootmeth for a bootflow which can be used. In this case the
377 * bootmeth handles all bootdev selection, etc.
378 *
379 * @dev: bootmeth device to read from
380 * @bflow: Bootflow information
381 * @return 0 on success, -ve if a bootflow could not be found or had an error
382 */
383int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow);
384
Simon Glassa950d312022-04-24 23:31:08 -0600385#endif