Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 1 | /* 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 Rini | d22d069 | 2024-04-27 08:10:58 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
| 11 | |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 12 | struct blk_desc; |
| 13 | struct bootflow; |
| 14 | struct bootflow_iter; |
| 15 | struct udevice; |
| 16 | |
| 17 | /** |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 18 | * enum bootmeth_flags - Flags for bootmeths |
| 19 | * |
| 20 | * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 21 | * @BOOTMETHF_ANY_PART: bootmeth is willing to check any partition, even if it |
| 22 | * has no filesystem |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 23 | */ |
| 24 | enum bootmeth_flags { |
| 25 | BOOTMETHF_GLOBAL = BIT(0), |
Simon Glass | 0fca7e8 | 2023-08-24 13:55:43 -0600 | [diff] [blame] | 26 | BOOTMETHF_ANY_PART = BIT(1), |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | /** |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 30 | * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth |
| 31 | * |
| 32 | * @desc: A long description of the bootmeth |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 33 | * @flags: Flags for this bootmeth (enum bootmeth_flags) |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 34 | */ |
| 35 | struct bootmeth_uc_plat { |
| 36 | const char *desc; |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 37 | int flags; |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | /** struct bootmeth_ops - Operations for boot methods */ |
| 41 | struct bootmeth_ops { |
| 42 | /** |
Simon Glass | f6d71a8 | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 43 | * get_state_desc() - get detailed state information |
| 44 | * |
| 45 | * Prodecues a textual description of the state of the bootmeth. This |
| 46 | * 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 Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 62 | * |
| 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 Glass | a63bda6 | 2023-01-17 10:48:01 -0700 | [diff] [blame] | 96 | * 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 Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 112 | * 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 Glass | 6d8f95b | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 127 | #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 Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 137 | /** |
| 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 |
| 143 | * Operating Systemn. 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 |
| 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 Glass | f6d71a8 | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 154 | * bootmeth_get_state_desc() - get detailed state information |
| 155 | * |
| 156 | * Prodecues a textual description of the state of the bootmeth. This |
| 157 | * 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 | */ |
| 169 | int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize); |
| 170 | |
| 171 | /** |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 172 | * 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 | */ |
| 194 | int 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 | */ |
| 204 | int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow); |
| 205 | |
| 206 | /** |
Simon Glass | a63bda6 | 2023-01-17 10:48:01 -0700 | [diff] [blame] | 207 | * 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 | */ |
| 220 | int bootmeth_set_bootflow(struct udevice *dev, struct bootflow *bflow, |
| 221 | char *buf, int size); |
| 222 | |
| 223 | /** |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 224 | * 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 | */ |
| 237 | int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow, |
| 238 | const char *file_path, ulong addr, ulong *sizep); |
| 239 | |
| 240 | /** |
Simon Glass | 6d8f95b | 2023-08-10 19:33:18 -0600 | [diff] [blame] | 241 | * 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 |
| 249 | * Operating Systemn. Returns -EFAULT if that fails, other -ve on |
| 250 | * other error |
| 251 | */ |
| 252 | int bootmeth_read_all(struct udevice *dev, struct bootflow *bflow); |
| 253 | |
| 254 | /** |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 255 | * 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 |
| 260 | * Operating Systemn. Returns -EFAULT if that fails, other -ve on |
| 261 | * other error |
| 262 | */ |
| 263 | int 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 |
| 269 | * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no |
| 270 | * ordering there, then all bootmethods are added |
| 271 | * |
| 272 | * @iter: Iterator to update with the order |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 273 | * @include_global: true to add the global bootmeths, in which case they appear |
| 274 | * first |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 275 | * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve |
| 276 | * on other error |
| 277 | */ |
Simon Glass | cc15e14 | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 278 | int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global); |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 279 | |
| 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 Glass | b71d7f7 | 2023-05-10 16:34:46 -0600 | [diff] [blame] | 286 | * bootmeth-device names, e.g. "extlinux,efi". If empty then a default ordering |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 287 | * 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 | */ |
| 291 | int bootmeth_set_order(const char *order_str); |
| 292 | |
| 293 | /** |
Simon Glass | f41c445 | 2023-07-26 21:01:21 -0600 | [diff] [blame] | 294 | * 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 | */ |
| 304 | int bootmeth_setup_fs(struct bootflow *bflow, struct blk_desc *desc); |
| 305 | |
| 306 | /** |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 307 | * 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 Glass | 4adeeb8 | 2023-01-17 10:47:59 -0700 | [diff] [blame] | 317 | * @desc: Block descriptor to read from (NULL for sandbox host) |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 318 | * @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 | */ |
| 323 | int 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 | */ |
| 340 | int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align); |
| 341 | |
| 342 | /** |
Simon Glass | 612b9cc | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 343 | * 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 | */ |
| 355 | int bootmeth_alloc_other(struct bootflow *bflow, const char *fname, |
| 356 | void **bufp, uint *sizep); |
| 357 | |
| 358 | /** |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 359 | * 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 | */ |
| 370 | int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow, |
| 371 | const char *file_path, ulong addr, ulong *sizep); |
| 372 | |
Simon Glass | 4f8633d | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 373 | /** |
| 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 | */ |
| 383 | int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow); |
| 384 | |
Simon Glass | 4b508b8 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 385 | #endif |