Simon Glass | a950d31 | 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 | |
| 10 | struct blk_desc; |
| 11 | struct bootflow; |
| 12 | struct bootflow_iter; |
| 13 | struct udevice; |
| 14 | |
| 15 | /** |
Simon Glass | bc06aa0 | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 16 | * enum bootmeth_flags - Flags for bootmeths |
| 17 | * |
| 18 | * @BOOTMETHF_GLOBAL: bootmeth handles bootdev selection automatically |
| 19 | */ |
| 20 | enum bootmeth_flags { |
| 21 | BOOTMETHF_GLOBAL = BIT(0), |
| 22 | }; |
| 23 | |
| 24 | /** |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 25 | * struct bootmeth_uc_plat - information the uclass keeps about each bootmeth |
| 26 | * |
| 27 | * @desc: A long description of the bootmeth |
Simon Glass | bc06aa0 | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 28 | * @flags: Flags for this bootmeth (enum bootmeth_flags) |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 29 | */ |
| 30 | struct bootmeth_uc_plat { |
| 31 | const char *desc; |
Simon Glass | bc06aa0 | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 32 | int flags; |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /** struct bootmeth_ops - Operations for boot methods */ |
| 36 | struct bootmeth_ops { |
| 37 | /** |
Simon Glass | 988caca | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 38 | * get_state_desc() - get detailed state information |
| 39 | * |
| 40 | * Prodecues a textual description of the state of the bootmeth. This |
| 41 | * can include newline characters if it extends to multiple lines. It |
| 42 | * must be a nul-terminated string. |
| 43 | * |
| 44 | * This may involve reading state from the system, e.g. some data in |
| 45 | * the firmware area. |
| 46 | * |
| 47 | * @dev: Bootmethod device to check |
| 48 | * @buf: Buffer to place the info in (terminator must fit) |
| 49 | * @maxsize: Size of buffer |
| 50 | * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if |
| 51 | * something else went wrong |
| 52 | */ |
| 53 | int (*get_state_desc)(struct udevice *dev, char *buf, int maxsize); |
| 54 | |
| 55 | /** |
| 56 | * check_supported() - check if a bootmeth supports this bootdev |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 57 | * |
| 58 | * This is optional. If not provided, the bootdev is assumed to be |
| 59 | * supported |
| 60 | * |
| 61 | * The bootmeth can check the bootdev (e.g. to make sure it is a |
| 62 | * network device) or the partition information. The following fields |
| 63 | * in @iter are available: |
| 64 | * |
| 65 | * name, dev, state, part |
| 66 | * max_part may be set if part != 0 (i.e. there is a valid partition |
| 67 | * table). Otherwise max_part is 0 |
| 68 | * method is available but is the same as @dev |
| 69 | * the partition has not yet been read, nor has the filesystem been |
| 70 | * checked |
| 71 | * |
| 72 | * It may update only the flags in @iter |
| 73 | * |
| 74 | * @dev: Bootmethod device to check against |
| 75 | * @iter: On entry, provides bootdev, hwpart, part |
| 76 | * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported |
| 77 | */ |
| 78 | int (*check)(struct udevice *dev, struct bootflow_iter *iter); |
| 79 | |
| 80 | /** |
| 81 | * read_bootflow() - read a bootflow for a device |
| 82 | * |
| 83 | * @dev: Bootmethod device to use |
| 84 | * @bflow: On entry, provides dev, hwpart, part and method. |
| 85 | * Returns updated bootflow if found |
| 86 | * Return: 0 if OK, -ve on error |
| 87 | */ |
| 88 | int (*read_bootflow)(struct udevice *dev, struct bootflow *bflow); |
| 89 | |
| 90 | /** |
| 91 | * read_file() - read a file needed for a bootflow |
| 92 | * |
| 93 | * Read a file from the same place as the bootflow came from |
| 94 | * |
| 95 | * @dev: Bootmethod device to use |
| 96 | * @bflow: Bootflow providing info on where to read from |
| 97 | * @file_path: Path to file (may be absolute or relative) |
| 98 | * @addr: Address to load file |
| 99 | * @sizep: On entry provides the maximum permitted size; on exit |
| 100 | * returns the size of the file |
| 101 | * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other |
| 102 | * -ve value if something else goes wrong |
| 103 | */ |
| 104 | int (*read_file)(struct udevice *dev, struct bootflow *bflow, |
| 105 | const char *file_path, ulong addr, ulong *sizep); |
| 106 | |
| 107 | /** |
| 108 | * boot() - boot a bootflow |
| 109 | * |
| 110 | * @dev: Bootmethod device to boot |
| 111 | * @bflow: Bootflow to boot |
| 112 | * Return: does not return on success, since it should boot the |
| 113 | * Operating Systemn. Returns -EFAULT if that fails, -ENOTSUPP if |
| 114 | * trying method resulted in finding out that is not actually |
| 115 | * supported for this boot and should not be tried again unless |
| 116 | * something changes, other -ve on other error |
| 117 | */ |
| 118 | int (*boot)(struct udevice *dev, struct bootflow *bflow); |
| 119 | }; |
| 120 | |
| 121 | #define bootmeth_get_ops(dev) ((struct bootmeth_ops *)(dev)->driver->ops) |
| 122 | |
| 123 | /** |
Simon Glass | 988caca | 2022-07-30 15:52:19 -0600 | [diff] [blame] | 124 | * bootmeth_get_state_desc() - get detailed state information |
| 125 | * |
| 126 | * Prodecues a textual description of the state of the bootmeth. This |
| 127 | * can include newline characters if it extends to multiple lines. It |
| 128 | * must be a nul-terminated string. |
| 129 | * |
| 130 | * This may involve reading state from the system, e.g. some data in |
| 131 | * the firmware area. |
| 132 | * |
| 133 | * @dev: Bootmethod device to check |
| 134 | * @buf: Buffer to place the info in (terminator must fit) |
| 135 | * @maxsize: Size of buffer |
| 136 | * Returns: 0 if OK, -ENOSPC is buffer is too small, other -ve error if |
| 137 | * something else went wrong |
| 138 | */ |
| 139 | int bootmeth_get_state_desc(struct udevice *dev, char *buf, int maxsize); |
| 140 | |
| 141 | /** |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 142 | * bootmeth_check() - check if a bootmeth supports this bootflow |
| 143 | * |
| 144 | * This is optional. If not provided, the bootdev is assumed to be |
| 145 | * supported |
| 146 | * |
| 147 | * The bootmeth can check the bootdev (e.g. to make sure it is a |
| 148 | * network device) or the partition information. The following fields |
| 149 | * in @iter are available: |
| 150 | * |
| 151 | * name, dev, state, part |
| 152 | * max_part may be set if part != 0 (i.e. there is a valid partition |
| 153 | * table). Otherwise max_part is 0 |
| 154 | * method is available but is the same as @dev |
| 155 | * the partition has not yet been read, nor has the filesystem been |
| 156 | * checked |
| 157 | * |
| 158 | * It may update only the flags in @iter |
| 159 | * |
| 160 | * @dev: Bootmethod device to check against |
| 161 | * @iter: On entry, provides bootdev, hwpart, part |
| 162 | * Return: 0 if OK, -ENOTSUPP if this bootdev is not supported |
| 163 | */ |
| 164 | int bootmeth_check(struct udevice *dev, struct bootflow_iter *iter); |
| 165 | |
| 166 | /** |
| 167 | * bootmeth_read_bootflow() - set up a bootflow for a device |
| 168 | * |
| 169 | * @dev: Bootmethod device to check |
| 170 | * @bflow: On entry, provides dev, hwpart, part and method. |
| 171 | * Returns updated bootflow if found |
| 172 | * Return: 0 if OK, -ve on error |
| 173 | */ |
| 174 | int bootmeth_read_bootflow(struct udevice *dev, struct bootflow *bflow); |
| 175 | |
| 176 | /** |
| 177 | * bootmeth_read_file() - read a file needed for a bootflow |
| 178 | * |
| 179 | * Read a file from the same place as the bootflow came from |
| 180 | * |
| 181 | * @dev: Bootmethod device to use |
| 182 | * @bflow: Bootflow providing info on where to read from |
| 183 | * @file_path: Path to file (may be absolute or relative) |
| 184 | * @addr: Address to load file |
| 185 | * @sizep: On entry provides the maximum permitted size; on exit |
| 186 | * returns the size of the file |
| 187 | * Return: 0 if OK, -ENOSPC if the file is too large for @sizep, other |
| 188 | * -ve value if something else goes wrong |
| 189 | */ |
| 190 | int bootmeth_read_file(struct udevice *dev, struct bootflow *bflow, |
| 191 | const char *file_path, ulong addr, ulong *sizep); |
| 192 | |
| 193 | /** |
| 194 | * bootmeth_boot() - boot a bootflow |
| 195 | * |
| 196 | * @dev: Bootmethod device to boot |
| 197 | * @bflow: Bootflow to boot |
| 198 | * Return: does not return on success, since it should boot the |
| 199 | * Operating Systemn. Returns -EFAULT if that fails, other -ve on |
| 200 | * other error |
| 201 | */ |
| 202 | int bootmeth_boot(struct udevice *dev, struct bootflow *bflow); |
| 203 | |
| 204 | /** |
| 205 | * bootmeth_setup_iter_order() - Set up the ordering of bootmeths to scan |
| 206 | * |
| 207 | * This sets up the ordering information in @iter, based on the selected |
| 208 | * ordering of the bootmethds in bootstd_priv->bootmeth_order. If there is no |
| 209 | * ordering there, then all bootmethods are added |
| 210 | * |
| 211 | * @iter: Iterator to update with the order |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 212 | * @include_global: true to add the global bootmeths, in which case they appear |
| 213 | * first |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 214 | * Return: 0 if OK, -ENOENT if no bootdevs, -ENOMEM if out of memory, other -ve |
| 215 | * on other error |
| 216 | */ |
Simon Glass | c627cfc | 2022-07-30 15:52:27 -0600 | [diff] [blame] | 217 | int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global); |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * bootmeth_set_order() - Set the bootmeth order |
| 221 | * |
| 222 | * This selects the ordering to use for bootmeths |
| 223 | * |
| 224 | * @order_str: String containing the ordering. This is a comma-separate list of |
| 225 | * bootmeth-device names, e.g. "syslinux,efi". If empty then a default ordering |
| 226 | * is used, based on the sequence number of devices (i.e. using aliases) |
| 227 | * Return: 0 if OK, -ENODEV if an unknown bootmeth is mentioned, -ENOMEM if |
| 228 | * out of memory, -ENOENT if there are no bootmeth devices |
| 229 | */ |
| 230 | int bootmeth_set_order(const char *order_str); |
| 231 | |
| 232 | /** |
| 233 | * bootmeth_try_file() - See we can access a given file |
| 234 | * |
| 235 | * Check for a file with a given name. If found, the filename is allocated in |
| 236 | * @bflow |
| 237 | * |
| 238 | * Sets the state to BOOTFLOWST_FILE on success. It also calls |
| 239 | * fs_set_blk_dev_with_part() so that this does not need to be done by the |
| 240 | * caller before reading the file. |
| 241 | * |
| 242 | * @bflow: Information about file to try |
Simon Glass | a58e7bb | 2023-01-17 10:47:59 -0700 | [diff] [blame^] | 243 | * @desc: Block descriptor to read from (NULL for sandbox host) |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 244 | * @prefix: Filename prefix to prepend to @fname (NULL for none) |
| 245 | * @fname: Filename to read |
| 246 | * Return: 0 if OK, -ENOMEM if not enough memory to allocate bflow->fname, |
| 247 | * other -ve value on other error |
| 248 | */ |
| 249 | int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc, |
| 250 | const char *prefix, const char *fname); |
| 251 | |
| 252 | /** |
| 253 | * bootmeth_alloc_file() - Allocate and read a bootflow file |
| 254 | * |
| 255 | * Allocates memory for a bootflow file and reads it in. Sets the state to |
| 256 | * BOOTFLOWST_READY on success |
| 257 | * |
| 258 | * Note that fs_set_blk_dev_with_part() must have been called previously. |
| 259 | * |
| 260 | * @bflow: Information about file to read |
| 261 | * @size_limit: Maximum file size to permit |
| 262 | * @align: Allocation alignment (1 for unaligned) |
| 263 | * Return: 0 if OK, -E2BIG if file is too large, -ENOMEM if out of memory, |
| 264 | * other -ve on other error |
| 265 | */ |
| 266 | int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align); |
| 267 | |
| 268 | /** |
Simon Glass | 24d8e1b | 2023-01-06 08:52:34 -0600 | [diff] [blame] | 269 | * bootmeth_alloc_other() - Allocate and read a file for a bootflow |
| 270 | * |
| 271 | * This reads an arbitrary file in the same directory as the bootflow, |
| 272 | * allocating memory for it. The buffer is one byte larger than the file length, |
| 273 | * so that it can be nul-terminated. |
| 274 | * |
| 275 | * @bflow: Information about file to read |
| 276 | * @fname: Filename to read from (within bootflow->subdir) |
| 277 | * @bufp: Returns a pointer to the allocated buffer |
| 278 | * @sizep: Returns the size of the buffer |
| 279 | * Return: 0 if OK, -ENOMEM if out of memory, other -ve on other error |
| 280 | */ |
| 281 | int bootmeth_alloc_other(struct bootflow *bflow, const char *fname, |
| 282 | void **bufp, uint *sizep); |
| 283 | |
| 284 | /** |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 285 | * bootmeth_common_read_file() - Common handler for reading a file |
| 286 | * |
| 287 | * Reads a named file from the same location as the bootflow file. |
| 288 | * |
| 289 | * @dev: bootmeth device to read from |
| 290 | * @bflow: Bootflow information |
| 291 | * @file_path: Path to file |
| 292 | * @addr: Address to load file to |
| 293 | * @sizep: On entry, the maximum file size to accept, on exit the actual file |
| 294 | * size read |
| 295 | */ |
| 296 | int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow, |
| 297 | const char *file_path, ulong addr, ulong *sizep); |
| 298 | |
Simon Glass | bc06aa0 | 2022-07-30 15:52:21 -0600 | [diff] [blame] | 299 | /** |
| 300 | * bootmeth_get_bootflow() - Get a bootflow from a global bootmeth |
| 301 | * |
| 302 | * Check the bootmeth for a bootflow which can be used. In this case the |
| 303 | * bootmeth handles all bootdev selection, etc. |
| 304 | * |
| 305 | * @dev: bootmeth device to read from |
| 306 | * @bflow: Bootflow information |
| 307 | * @return 0 on success, -ve if a bootflow could not be found or had an error |
| 308 | */ |
| 309 | int bootmeth_get_bootflow(struct udevice *dev, struct bootflow *bflow); |
| 310 | |
Simon Glass | a950d31 | 2022-04-24 23:31:08 -0600 | [diff] [blame] | 311 | #endif |