1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
10 #include <linux/bitops.h>
18 * enum bootmeth_flags - Flags for bootmeths
20 * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically
21 * @BOOTMETHF_ANY_PART: bootmeth is willing to check any partition, even if it
25 BOOTMETHF_GLOBAL
= BIT(0),
26 BOOTMETHF_ANY_PART
= BIT(1),
30 * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth
32 * @desc: A long description of the bootmeth
33 * @flags: Flags for this bootmeth (enum bootmeth_flags)
35 struct bootmeth_uc_plat
{
40 /** struct bootmeth_ops - Operations for boot methods */
43 * get_state_desc() - get detailed state information
45 * Produces a textual description of the state of the boot method. This
46 * can include newline characters if it extends to multiple lines. It
47 * must be a nul-terminated string.
49 * This may involve reading state from the system, e.g. some data in
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
58 int (*get_state_desc
)(struct udevice
*dev
, char *buf
, int maxsize
);
61 * check_supported() - check if a bootmeth supports this bootdev
63 * This is optional. If not provided, the bootdev is assumed to be
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:
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
77 * It may update only the flags in @iter
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
83 int (*check
)(struct udevice
*dev
, struct bootflow_iter
*iter
);
86 * read_bootflow() - read a bootflow for a device
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
93 int (*read_bootflow
)(struct udevice
*dev
, struct bootflow
*bflow
);
96 * set_bootflow() - set the bootflow for a device
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.
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
108 int (*set_bootflow
)(struct udevice
*dev
, struct bootflow
*bflow
,
109 char *buf
, int size
);
112 * read_file() - read a file needed for a bootflow
114 * Read a file from the same place as the bootflow came from
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
125 int (*read_file
)(struct udevice
*dev
, struct bootflow
*bflow
,
126 const char *file_path
, ulong addr
, ulong
*sizep
);
127 #if CONFIG_IS_ENABLED(BOOTSTD_FULL)
129 * readall() - read all files for a bootflow
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
135 int (*read_all
)(struct udevice
*dev
, struct bootflow
*bflow
);
136 #endif /* BOOTSTD_FULL */
138 * boot() - boot a bootflow
140 * @dev: Bootmethod device to boot
141 * @bflow: Bootflow to boot
142 * Return: does not return on success, since it should boot the
143 * operating system. Returns -EFAULT if that fails, -ENOTSUPP if
144 * 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
148 int (*boot
)(struct udevice
*dev
, struct bootflow
*bflow
);
151 * set_property() - set the bootmeth property
153 * This allows the setting of boot method specific properties to enable
154 * automated finer grain control of the boot process
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
160 * Return: 0 if OK, -ENODEV if an unknown bootmeth or property is
161 * provided, -ENOENT if there are no bootmeth devices
163 int (*set_property
)(struct udevice
*dev
, const char *property
,
167 #define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops)
170 * bootmeth_get_state_desc() - get detailed state information
172 * Produces a textual description of the state of the boot method. This
173 * can include newline characters if it extends to multiple lines. It
174 * must be a nul-terminated string.
176 * This may involve reading state from the system, e.g. some data in
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
185 int bootmeth_get_state_desc(struct udevice
*dev
, char *buf
, int maxsize
);
188 * bootmeth_check() - check if a bootmeth supports this bootflow
190 * This is optional. If not provided, the bootdev is assumed to be
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:
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
204 * It may update only the flags in @iter
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
210 int bootmeth_check(struct udevice
*dev
, struct bootflow_iter
*iter
);
213 * bootmeth_read_bootflow() - set up a bootflow for a device
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
220 int bootmeth_read_bootflow(struct udevice
*dev
, struct bootflow
*bflow
);
223 * bootmeth_set_bootflow() - set the bootflow for a device
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.
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
236 int bootmeth_set_bootflow(struct udevice
*dev
, struct bootflow
*bflow
,
237 char *buf
, int size
);
240 * bootmeth_read_file() - read a file needed for a bootflow
242 * Read a file from the same place as the bootflow came from
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
253 int bootmeth_read_file(struct udevice
*dev
, struct bootflow
*bflow
,
254 const char *file_path
, ulong addr
, ulong
*sizep
);
257 * bootmeth_read_all() - read all bootflow files
259 * Some bootmeths delay reading of large files until booting is requested. This
260 * causes those files to be read.
262 * @dev: Bootmethod device to use
263 * @bflow: Bootflow to read
264 * Return: does not return on success, since it should boot the
265 * operating system. Returns -EFAULT if that fails, other -ve on
268 int bootmeth_read_all(struct udevice
*dev
, struct bootflow
*bflow
);
271 * bootmeth_boot() - boot a bootflow
273 * @dev: Bootmethod device to boot
274 * @bflow: Bootflow to boot
275 * Return: does not return on success, since it should boot the
276 * operating system. Returns -EFAULT if that fails, other -ve on
279 int bootmeth_boot(struct udevice
*dev
, struct bootflow
*bflow
);
282 * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan
284 * This sets up the ordering information in @iter, based on the selected
285 * ordering of the boot methods in bootstd_priv->bootmeth_order. If there is no
286 * ordering there, then all bootmethods are added
288 * @iter: Iterator to update with the order
289 * @include_global: true to add the global bootmeths, in which case they appear
291 * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve
294 int bootmeth_setup_iter_order(struct bootflow_iter
*iter
, bool include_global
);
297 * bootmeth_set_order() - Set the bootmeth order
299 * This selects the ordering to use for bootmeths
301 * @order_str: String containing the ordering. This is a comma-separate list of
302 * bootmeth-device names, e.g. "extlinux,efi". If empty then a default ordering
303 * 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
307 int bootmeth_set_order(const char *order_str
);
310 * bootmeth_set_property() - Set the bootmeth property
312 * This allows the setting of boot method specific properties to enable
313 * automated finer grain control of the boot process
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
321 int bootmeth_set_property(const char *name
, const char *property
,
325 * bootmeth_setup_fs() - Set up read to read a file
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
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
335 int bootmeth_setup_fs(struct bootflow
*bflow
, struct blk_desc
*desc
);
338 * bootmeth_try_file() - See we can access a given file
340 * Check for a file with a given name. If found, the filename is allocated in
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.
347 * @bflow: Information about file to try
348 * @desc: Block descriptor to read from (NULL for sandbox host)
349 * @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
354 int bootmeth_try_file(struct bootflow
*bflow
, struct blk_desc
*desc
,
355 const char *prefix
, const char *fname
);
358 * bootmeth_alloc_file() - Allocate and read a bootflow file
360 * Allocates memory for a bootflow file and reads it in. Sets the state to
361 * BOOTFLOWST_READY on success
363 * Note that fs_set_blk_dev_with_part() must have been called previously.
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
371 int bootmeth_alloc_file(struct bootflow
*bflow
, uint size_limit
, uint align
);
374 * bootmeth_alloc_other() - Allocate and read a file for a bootflow
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.
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
386 int bootmeth_alloc_other(struct bootflow
*bflow
, const char *fname
,
387 void **bufp
, uint
*sizep
);
390 * bootmeth_common_read_file() - Common handler for reading a file
392 * Reads a named file from the same location as the bootflow file.
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
401 int bootmeth_common_read_file(struct udevice
*dev
, struct bootflow
*bflow
,
402 const char *file_path
, ulong addr
, ulong
*sizep
);
405 * bootmeth_get_bootflow() - Get a bootflow from a global bootmeth
407 * Check the bootmeth for a bootflow which can be used. In this case the
408 * bootmeth handles all bootdev selection, etc.
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
414 int bootmeth_get_bootflow(struct udevice
*dev
, struct bootflow
*bflow
);