blob: 26de593a9a462145b4de28585e2bef343f7e7987 [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
Simon Glass8db2a382024-11-15 16:19:14 -070010#include <bootflow.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <linux/bitops.h>
12
Simon Glass4b508b82022-04-24 23:31:08 -060013struct blk_desc;
Simon Glass4b508b82022-04-24 23:31:08 -060014struct udevice;
15
16/**
Simon Glass4f8633d2022-07-30 15:52:21 -060017 * enum bootmeth_flags - Flags for bootmeths
18 *
19 * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically
Simon Glass0fca7e82023-08-24 13:55:43 -060020 * @BOOTMETHF_ANY_PART: bootmeth is willing to check any partition, even if it
21 * has no filesystem
Simon Glass4f8633d2022-07-30 15:52:21 -060022 */
23enum bootmeth_flags {
24 BOOTMETHF_GLOBAL = BIT(0),
Simon Glass0fca7e82023-08-24 13:55:43 -060025 BOOTMETHF_ANY_PART = BIT(1),
Simon Glass4f8633d2022-07-30 15:52:21 -060026};
27
28/**
Simon Glass4b508b82022-04-24 23:31:08 -060029 * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth
30 *
31 * @desc: A long description of the bootmeth
Simon Glass4f8633d2022-07-30 15:52:21 -060032 * @flags: Flags for this bootmeth (enum bootmeth_flags)
Simon Glass4b508b82022-04-24 23:31:08 -060033 */
34struct bootmeth_uc_plat {
35 const char *desc;
Simon Glass4f8633d2022-07-30 15:52:21 -060036 int flags;
Simon Glass4b508b82022-04-24 23:31:08 -060037};
38
39/** struct bootmeth_ops - Operations for boot methods */
40struct bootmeth_ops {
41 /**
Simon Glassf6d71a82022-07-30 15:52:19 -060042 * get_state_desc() - get detailed state information
43 *
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +020044 * Produces a textual description of the state of the boot method. This
Simon Glassf6d71a82022-07-30 15:52:19 -060045 * can include newline characters if it extends to multiple lines. It
46 * must be a nul-terminated string.
47 *
48 * This may involve reading state from the system, e.g. some data in
49 * the firmware area.
50 *
51 * @dev: Bootmethod device to check
52 * @buf: Buffer to place the info in (terminator must fit)
53 * @maxsize: Size of buffer
54 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
55 * something else went wrong
56 */
57 int (*get_state_desc)(struct udevice *dev, char *buf, int maxsize);
58
59 /**
60 * check_supported() - check if a bootmeth supports this bootdev
Simon Glass4b508b82022-04-24 23:31:08 -060061 *
62 * This is optional. If not provided, the bootdev is assumed to be
63 * supported
64 *
65 * The bootmeth can check the bootdev (e.g. to make sure it is a
66 * network device) or the partition information. The following fields
67 * in @iter are available:
68 *
69 * name, dev, state, part
70 * max_part may be set if part != 0 (i.e. there is a valid partition
71 * table). Otherwise max_part is 0
72 * method is available but is the same as @dev
73 * the partition has not yet been read, nor has the filesystem been
74 * checked
75 *
76 * It may update only the flags in @iter
77 *
78 * @dev: Bootmethod device to check against
79 * @iter: On entry, provides bootdev, hwpart, part
80 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
81 */
82 int (*check)(struct udevice *dev, struct bootflow_iter *iter);
83
84 /**
85 * read_bootflow() - read a bootflow for a device
86 *
87 * @dev: Bootmethod device to use
88 * @bflow: On entry, provides dev, hwpart, part and method.
89 * Returns updated bootflow if found
90 * Return: 0 if OK, -ve on error
91 */
92 int (*read_bootflow)(struct udevice *dev, struct bootflow *bflow);
93
94 /**
Simon Glassa63bda62023-01-17 10:48:01 -070095 * set_bootflow() - set the bootflow for a device
96 *
97 * This provides a bootflow file to the bootmeth, to see if it is valid.
98 * If it is, the bootflow is set up accordingly.
99 *
100 * @dev: Bootmethod device to use
101 * @bflow: On entry, provides bootdev.
102 * Returns updated bootflow if found
103 * @buf: Buffer containing the possible bootflow file
104 * @size: Size of file
105 * Return: 0 if OK, -ve on error
106 */
107 int (*set_bootflow)(struct udevice *dev, struct bootflow *bflow,
108 char *buf, int size);
109
110 /**
Simon Glass4b508b82022-04-24 23:31:08 -0600111 * read_file() - read a file needed for a bootflow
112 *
113 * Read a file from the same place as the bootflow came from
114 *
115 * @dev: Bootmethod device to use
116 * @bflow: Bootflow providing info on where to read from
117 * @file_path: Path to file (may be absolute or relative)
118 * @addr: Address to load file
Simon Glassf39b5592024-11-15 16:19:17 -0700119 * @type: File type (IH_TYPE_...)
Simon Glass4b508b82022-04-24 23:31:08 -0600120 * @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,
Simon Glassf39b5592024-11-15 16:19:17 -0700126 const char *file_path, ulong addr,
127 enum bootflow_img_t type, ulong *sizep);
Simon Glass6d8f95b2023-08-10 19:33:18 -0600128#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
129 /**
130 * readall() - read all files for a bootflow
131 *
132 * @dev: Bootmethod device to boot
133 * @bflow: Bootflow to read
134 * Return: 0 if OK, -EIO on I/O error, other -ve on other error
135 */
136 int (*read_all)(struct udevice *dev, struct bootflow *bflow);
137#endif /* BOOTSTD_FULL */
Simon Glass4b508b82022-04-24 23:31:08 -0600138 /**
139 * boot() - boot a bootflow
140 *
141 * @dev: Bootmethod device to boot
142 * @bflow: Bootflow to boot
143 * Return: does not return on success, since it should boot the
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200144 * operating system. Returns -EFAULT if that fails, -ENOTSUPP if
Simon Glass4b508b82022-04-24 23:31:08 -0600145 * trying method resulted in finding out that is not actually
146 * supported for this boot and should not be tried again unless
147 * something changes, other -ve on other error
148 */
149 int (*boot)(struct udevice *dev, struct bootflow *bflow);
Martyn Welch93a0d162024-10-09 14:15:40 +0100150
151 /**
152 * set_property() - set the bootmeth property
153 *
154 * This allows the setting of boot method specific properties to enable
155 * automated finer grain control of the boot process
156 *
157 * @name: String containing the name of the relevant boot method
158 * @property: String containing the name of the property to set
159 * @value: String containing the value to be set for the specified
160 * property
161 * Return: 0 if OK, -ENODEV if an unknown bootmeth or property is
162 * provided, -ENOENT if there are no bootmeth devices
163 */
164 int (*set_property)(struct udevice *dev, const char *property,
165 const char *value);
Simon Glass4b508b82022-04-24 23:31:08 -0600166};
167
168#define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops)
169
170/**
Simon Glassf6d71a82022-07-30 15:52:19 -0600171 * bootmeth_get_state_desc() - get detailed state information
172 *
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200173 * Produces a textual description of the state of the boot method. This
Simon Glassf6d71a82022-07-30 15:52:19 -0600174 * can include newline characters if it extends to multiple lines. It
175 * must be a nul-terminated string.
176 *
177 * This may involve reading state from the system, e.g. some data in
178 * the firmware area.
179 *
180 * @dev: Bootmethod device to check
181 * @buf: Buffer to place the info in (terminator must fit)
182 * @maxsize: Size of buffer
183 * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if
184 * something else went wrong
185 */
186int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize);
187
188/**
Simon Glass4b508b82022-04-24 23:31:08 -0600189 * bootmeth_check() - check if a bootmeth supports this bootflow
190 *
191 * This is optional. If not provided, the bootdev is assumed to be
192 * supported
193 *
194 * The bootmeth can check the bootdev (e.g. to make sure it is a
195 * network device) or the partition information. The following fields
196 * in @iter are available:
197 *
198 * name, dev, state, part
199 * max_part may be set if part != 0 (i.e. there is a valid partition
200 * table). Otherwise max_part is 0
201 * method is available but is the same as @dev
202 * the partition has not yet been read, nor has the filesystem been
203 * checked
204 *
205 * It may update only the flags in @iter
206 *
207 * @dev: Bootmethod device to check against
208 * @iter: On entry, provides bootdev, hwpart, part
209 * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported
210 */
211int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter);
212
213/**
214 * bootmeth_read_bootflow() - set up a bootflow for a device
215 *
216 * @dev: Bootmethod device to check
217 * @bflow: On entry, provides dev, hwpart, part and method.
218 * Returns updated bootflow if found
219 * Return: 0 if OK, -ve on error
220 */
221int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow);
222
223/**
Simon Glassa63bda62023-01-17 10:48:01 -0700224 * bootmeth_set_bootflow() - set the bootflow for a device
225 *
226 * This provides a bootflow file to the bootmeth, to see if it is valid.
227 * If it is, the bootflow is set up accordingly.
228 *
229 * @dev: Bootmethod device to use
230 * @bflow: On entry, provides bootdev.
231 * Returns updated bootflow if found
232 * @buf: Buffer containing the possible bootflow file (must be allocated
233 * by caller to @size + 1 bytes)
234 * @size: Size of file
235 * Return: 0 if OK, -ve on error
236 */
237int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow,
238 char *buf, int size);
239
240/**
Simon Glass4b508b82022-04-24 23:31:08 -0600241 * bootmeth_read_file() - read a file needed for a bootflow
242 *
243 * Read a file from the same place as the bootflow came from
244 *
245 * @dev: Bootmethod device to use
246 * @bflow: Bootflow providing info on where to read from
247 * @file_path: Path to file (may be absolute or relative)
248 * @addr: Address to load file
Simon Glassf39b5592024-11-15 16:19:17 -0700249 * @type: File type (IH_TYPE_...)
Simon Glass4b508b82022-04-24 23:31:08 -0600250 * @sizep: On entry provides the maximum permitted size; on exit
251 * returns the size of the file
252 * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other
253 * -ve value if something else goes wrong
254 */
255int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -0700256 const char *file_path, ulong addr,
257 enum bootflow_img_t type, ulong *sizep);
Simon Glass4b508b82022-04-24 23:31:08 -0600258
259/**
Simon Glass6d8f95b2023-08-10 19:33:18 -0600260 * bootmeth_read_all() - read all bootflow files
261 *
262 * Some bootmeths delay reading of large files until booting is requested. This
263 * causes those files to be read.
264 *
265 * @dev: Bootmethod device to use
266 * @bflow: Bootflow to read
267 * Return: does not return on success, since it should boot the
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200268 * operating system. Returns -EFAULT if that fails, other -ve on
Simon Glass6d8f95b2023-08-10 19:33:18 -0600269 * other error
270 */
271int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow);
272
273/**
Simon Glass4b508b82022-04-24 23:31:08 -0600274 * bootmeth_boot() - boot a bootflow
275 *
276 * @dev: Bootmethod device to boot
277 * @bflow: Bootflow to boot
278 * Return: does not return on success, since it should boot the
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200279 * operating system. Returns -EFAULT if that fails, other -ve on
Simon Glass4b508b82022-04-24 23:31:08 -0600280 * other error
281 */
282int bootmeth_boot(struct udevice *dev, struct bootflow *bflow);
283
284/**
285 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
286 *
287 * This sets up the ordering information in @iter, based on the selected
Mattijs Korpershoekf2b610a2024-06-04 17:15:21 +0200288 * ordering of the boot methods in bootstd_priv->bootmeth_order. If there is no
Simon Glass4b508b82022-04-24 23:31:08 -0600289 * ordering there, then all bootmethods are added
290 *
291 * @iter: Iterator to update with the order
Simon Glasscc15e142022-07-30 15:52:27 -0600292 * @include_global: true to add the global bootmeths, in which case they appear
293 * first
Simon Glass4b508b82022-04-24 23:31:08 -0600294 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
295 * on other error
296 */
Simon Glasscc15e142022-07-30 15:52:27 -0600297int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global);
Simon Glass4b508b82022-04-24 23:31:08 -0600298
299/**
300 * bootmeth_set_order() - Set the bootmeth order
301 *
302 * This selects the ordering to use for bootmeths
303 *
304 * @order_str: String containing the ordering. This is a comma-separate list of
Simon Glassb71d7f72023-05-10 16:34:46 -0600305 * bootmeth-device names, e.g. "extlinux,efi". If empty then a default ordering
Simon Glass4b508b82022-04-24 23:31:08 -0600306 * is used, based on the sequence number of devices (i.e. using aliases)
307 * Return: 0 if OK, -ENODEV if an unknown bootmeth is mentioned, -ENOMEM if
308 * out of memory, -ENOENT if there are no bootmeth devices
309 */
310int bootmeth_set_order(const char *order_str);
311
312/**
Martyn Welch93a0d162024-10-09 14:15:40 +0100313 * bootmeth_set_property() - Set the bootmeth property
314 *
315 * This allows the setting of boot method specific properties to enable
316 * automated finer grain control of the boot process
317 *
318 * @name: String containing the name of the relevant boot method
319 * @property: String containing the name of the property to set
320 * @value: String containing the value to be set for the specified property
321 * Return: 0 if OK, -ENODEV if an unknown bootmeth or property is provided,
322 * -ENOENT if there are no bootmeth devices
323 */
324int bootmeth_set_property(const char *name, const char *property,
325 const char *value);
326
327/**
Simon Glassf41c4452023-07-26 21:01:21 -0600328 * bootmeth_setup_fs() - Set up read to read a file
329 *
330 * We must redo the setup before each filesystem operation. This function
331 * handles that, including setting the filesystem type if a block device is not
332 * being used
333 *
334 * @bflow: Information about file to try
335 * @desc: Block descriptor to read from (NULL if not a block device)
336 * Return: 0 if OK, -ve on error
337 */
338int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc);
339
340/**
Simon Glass4b508b82022-04-24 23:31:08 -0600341 * bootmeth_try_file() - See we can access a given file
342 *
343 * Check for a file with a given name. If found, the filename is allocated in
344 * @bflow
345 *
346 * Sets the state to BOOTFLOWST_FILE on success. It also calls
347 * fs_set_blk_dev_with_part() so that this does not need to be done by the
348 * caller before reading the file.
349 *
350 * @bflow: Information about file to try
Simon Glass4adeeb82023-01-17 10:47:59 -0700351 * @desc: Block descriptor to read from (NULL for sandbox host)
Simon Glass4b508b82022-04-24 23:31:08 -0600352 * @prefix: Filename prefix to prepend to @fname (NULL for none)
353 * @fname: Filename to read
354 * Return: 0 if OK, -ENOMEM if not enough memory to allocate bflow->fname,
355 * other -ve value on other error
356 */
357int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc,
358 const char *prefix, const char *fname);
359
360/**
361 * bootmeth_alloc_file() - Allocate and read a bootflow file
362 *
363 * Allocates memory for a bootflow file and reads it in. Sets the state to
364 * BOOTFLOWST_READY on success
365 *
366 * Note that fs_set_blk_dev_with_part() must have been called previously.
367 *
368 * @bflow: Information about file to read
369 * @size_limit: Maximum file size to permit
370 * @align: Allocation alignment (1 for unaligned)
Simon Glass8db2a382024-11-15 16:19:14 -0700371 * @type: File type (IH_TYPE_...)
Simon Glass4b508b82022-04-24 23:31:08 -0600372 * Return: 0 if OK, -E2BIG if file is too large, -ENOMEM if out of memory,
373 * other -ve on other error
374 */
Simon Glass8db2a382024-11-15 16:19:14 -0700375int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align,
376 enum bootflow_img_t type);
Simon Glass4b508b82022-04-24 23:31:08 -0600377
378/**
Simon Glass612b9cc2023-01-06 08:52:34 -0600379 * bootmeth_alloc_other() - Allocate and read a file for a bootflow
380 *
381 * This reads an arbitrary file in the same directory as the bootflow,
382 * allocating memory for it. The buffer is one byte larger than the file length,
383 * so that it can be nul-terminated.
384 *
385 * @bflow: Information about file to read
386 * @fname: Filename to read from (within bootflow->subdir)
Simon Glass62632b22024-11-15 16:19:21 -0700387 * @type: File type (IH_TYPE_...)
Simon Glass612b9cc2023-01-06 08:52:34 -0600388 * @bufp: Returns a pointer to the allocated buffer
389 * @sizep: Returns the size of the buffer
390 * Return: 0 if OK, -ENOMEM if out of memory, other -ve on other error
391 */
392int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
Simon Glass62632b22024-11-15 16:19:21 -0700393 enum bootflow_img_t type, void **bufp, uint *sizep);
Simon Glass612b9cc2023-01-06 08:52:34 -0600394
395/**
Simon Glass4b508b82022-04-24 23:31:08 -0600396 * bootmeth_common_read_file() - Common handler for reading a file
397 *
398 * Reads a named file from the same location as the bootflow file.
399 *
400 * @dev: bootmeth device to read from
401 * @bflow: Bootflow information
402 * @file_path: Path to file
403 * @addr: Address to load file to
Simon Glassf39b5592024-11-15 16:19:17 -0700404 * @type: File type (IH_TYPE_...)
Simon Glass4b508b82022-04-24 23:31:08 -0600405 * @sizep: On entry, the maximum file size to accept, on exit the actual file
406 * size read
407 */
408int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow,
Simon Glassf39b5592024-11-15 16:19:17 -0700409 const char *file_path, ulong addr,
410 enum bootflow_img_t type, ulong *sizep);
Simon Glass4b508b82022-04-24 23:31:08 -0600411
Simon Glass4f8633d2022-07-30 15:52:21 -0600412/**
413 * bootmeth_get_bootflow() - Get a bootflow from a global bootmeth
414 *
415 * Check the bootmeth for a bootflow which can be used. In this case the
416 * bootmeth handles all bootdev selection, etc.
417 *
418 * @dev: bootmeth device to read from
419 * @bflow: Bootflow information
420 * @return 0 on success, -ve if a bootflow could not be found or had an error
421 */
422int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow);
423
Simon Glass4b508b82022-04-24 23:31:08 -0600424#endif