Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0+: |
| 2 | |
| 3 | U-Boot Standard Boot |
| 4 | ==================== |
| 5 | |
| 6 | Introduction |
| 7 | ------------ |
| 8 | |
| 9 | Standard boot provides a built-in way for U-Boot to automatically boot |
| 10 | an Operating System without custom scripting and other customisation. It |
| 11 | introduces the following concepts: |
| 12 | |
| 13 | - bootdev - a device which can hold or access a distro (e.g. MMC, Ethernet) |
| 14 | - bootmeth - a method to scan a bootdev to find bootflows (e.g. distro boot) |
| 15 | - bootflow - a description of how to boot (provided by the distro) |
| 16 | |
| 17 | For Linux, the distro (Linux distribution, e.g. Debian, Fedora) is responsible |
| 18 | for creating a bootflow for each kernel combination that it wants to offer. |
| 19 | These bootflows are stored on media so they can be discovered by U-Boot. This |
| 20 | feature is typically called `distro boot` (see :doc:`distro`) because it is |
| 21 | a way for distributions to boot on any hardware. |
| 22 | |
| 23 | Traditionally U-Boot has relied on scripts to implement this feature. See |
Paul Barker | 6c55d0d | 2022-07-29 14:31:58 +0100 | [diff] [blame] | 24 | distro_bootcmd_ for details. This is done because U-Boot has no native support |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 25 | for scanning devices. While the scripts work remarkably well, they can be hard |
| 26 | to understand and extend, and the feature does not include tests. They are also |
| 27 | making it difficult to move away from ad-hoc CONFIGs, since they are implemented |
| 28 | using the environment and a lot of #defines. |
| 29 | |
| 30 | Standard boot is a generalisation of distro boot. It provides a more built-in |
| 31 | way to boot with U-Boot. The feature is extensible to different Operating |
| 32 | Systems (such as Chromium OS) and devices (beyond just block and network |
| 33 | devices). It supports EFI boot and EFI bootmgr too. |
| 34 | |
Simon Glass | c08a992 | 2022-07-30 15:52:03 -0600 | [diff] [blame] | 35 | Finally, standard boot supports the operation of :doc:`vbe`. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 36 | |
| 37 | Bootflow |
| 38 | -------- |
| 39 | |
| 40 | A bootflow is a file that describes how to boot a distro. Conceptually there can |
| 41 | be different formats for that file but at present U-Boot only supports the |
| 42 | BootLoaderSpec_ format. which looks something like this:: |
| 43 | |
| 44 | menu autoboot Welcome to Fedora-Workstation-armhfp-31-1.9. Automatic boot in # second{,s}. Press a key for options. |
| 45 | menu title Fedora-Workstation-armhfp-31-1.9 Boot Options. |
| 46 | menu hidden |
| 47 | |
| 48 | label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) |
| 49 | kernel /vmlinuz-5.3.7-301.fc31.armv7hl |
| 50 | append ro root=UUID=9732b35b-4cd5-458b-9b91-80f7047e0b8a rhgb quiet LANG=en_US.UTF-8 cma=192MB cma=256MB |
| 51 | fdtdir /dtb-5.3.7-301.fc31.armv7hl/ |
| 52 | initrd /initramfs-5.3.7-301.fc31.armv7hl.img |
| 53 | |
| 54 | As you can see it specifies a kernel, a ramdisk (initrd) and a directory from |
Paul Barker | 6c55d0d | 2022-07-29 14:31:58 +0100 | [diff] [blame] | 55 | which to load devicetree files. The details are described in distro_bootcmd_. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 56 | |
| 57 | The bootflow is provided by the distro. It is not part of U-Boot. U-Boot's job |
| 58 | is simply to interpret the file and carry out the instructions. This allows |
| 59 | distros to boot on essentially any device supported by U-Boot. |
| 60 | |
| 61 | Typically the first available bootflow is selected and booted. If that fails, |
| 62 | then the next one is tried. |
| 63 | |
| 64 | |
| 65 | Bootdev |
| 66 | ------- |
| 67 | |
| 68 | Where does U-Boot find the media that holds the operating systems? That is the |
| 69 | job of bootdev. A bootdev is simply a layer on top of a media device (such as |
| 70 | MMC, NVMe). The bootdev accesses the device, including partitions and |
| 71 | filesystems that might contain things related to an operating system. |
| 72 | |
| 73 | For example, an MMC bootdev provides access to the individual partitions on the |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 74 | MMC device. It scans through these to find filesystems with the boot flag set, |
| 75 | then provides a list of these for consideration. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 76 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 77 | Some bootdevs are not visible until a bus is enumerated, e.g. flash sticks |
| 78 | attached via USB. To deal with this, each bootdev has an associated 'hunter' |
| 79 | which can hunt for bootdevs of a particular uclass type. For example, the SCSI |
| 80 | bootdev scans the SCSI bus looking for devices, creating a bootdev for each |
| 81 | Logical Unit Number (LUN) that it finds. |
| 82 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 83 | |
| 84 | Bootmeth |
| 85 | -------- |
| 86 | |
| 87 | Once the list of filesystems is provided, how does U-Boot find the bootflow |
| 88 | files in these filesystems. That is the job of bootmeth. Each boot method has |
| 89 | its own way of doing this. |
| 90 | |
| 91 | For example, the distro bootmeth simply looks through the provided filesystem |
| 92 | for a file called `extlinux/extlinux.conf`. This files constitutes a bootflow. |
| 93 | If the distro bootmeth is used on multiple partitions it may produce multiple |
| 94 | bootflows. |
| 95 | |
| 96 | Note: it is possible to have a bootmeth that uses a partition or a whole device |
| 97 | directly, but it is more common to use a filesystem. |
| 98 | |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 99 | Note that some bootmeths are 'global', meaning that they select the bootdev |
| 100 | themselves. Examples include VBE and EFI boot manager. In this case, they |
| 101 | provide a `read_bootflow()` method which checks whatever bootdevs it likes, then |
| 102 | returns the bootflow, if found. Some of these bootmeths may be very slow, if |
| 103 | they scan a lot of devices. |
| 104 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 105 | |
| 106 | Boot process |
| 107 | ------------ |
| 108 | |
| 109 | U-Boot tries to use the 'lazy init' approach whereever possible and distro boot |
| 110 | is no exception. The algorithm is:: |
| 111 | |
| 112 | while (get next bootdev) |
| 113 | while (get next bootmeth) |
| 114 | while (get next bootflow) |
| 115 | try to boot it |
| 116 | |
| 117 | So U-Boot works its way through the bootdevs, trying each bootmeth in turn to |
| 118 | obtain bootflows, until it either boots or exhausts the available options. |
| 119 | |
| 120 | Instead of 500 lines of #defines and a 4KB boot script, all that is needed is |
| 121 | the following command:: |
| 122 | |
| 123 | bootflow scan -lb |
| 124 | |
| 125 | which scans for available bootflows, optionally listing each find it finds (-l) |
| 126 | and trying to boot it (-b). |
| 127 | |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 128 | When global bootmeths are available, these are typically checked before the |
| 129 | above bootdev scanning. |
| 130 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 131 | |
| 132 | Controlling ordering |
| 133 | -------------------- |
| 134 | |
| 135 | Several options are available to control the ordering of boot scanning: |
| 136 | |
| 137 | |
| 138 | boot_targets |
| 139 | ~~~~~~~~~~~~ |
| 140 | |
| 141 | This environment variable can be used to control the list of bootdevs searched |
| 142 | and their ordering, for example:: |
| 143 | |
| 144 | setenv boot_targets "mmc0 mmc1 usb pxe" |
| 145 | |
| 146 | Entries may be removed or re-ordered in this list to affect the boot order. If |
| 147 | the variable is empty, the default ordering is used, based on the priority of |
| 148 | bootdevs and their sequence numbers. |
| 149 | |
| 150 | |
| 151 | bootmeths |
| 152 | ~~~~~~~~~ |
| 153 | |
| 154 | This environment variable can be used to control the list of bootmeths used and |
| 155 | their ordering for example:: |
| 156 | |
| 157 | setenv bootmeths "syslinux efi" |
| 158 | |
| 159 | Entries may be removed or re-ordered in this list to affect the order the |
| 160 | bootmeths are tried on each bootdev. If the variable is empty, the default |
| 161 | ordering is used, based on the bootmeth sequence numbers, which can be |
| 162 | controlled by aliases. |
| 163 | |
| 164 | The :ref:`usage/cmd/bootmeth:bootmeth command` (`bootmeth order`) operates in |
| 165 | the same way as setting this variable. |
| 166 | |
| 167 | |
| 168 | Bootdev uclass |
| 169 | -------------- |
| 170 | |
| 171 | The bootdev uclass provides an simple API call to obtain a bootflows from a |
| 172 | device:: |
| 173 | |
| 174 | int bootdev_get_bootflow(struct udevice *dev, struct bootflow_iter *iter, |
| 175 | struct bootflow *bflow); |
| 176 | |
| 177 | This takes a iterator which indicates the bootdev, partition and bootmeth to |
| 178 | use. It returns a bootflow. This is the core of the bootdev implementation. The |
| 179 | bootdev drivers that implement this differ depending on the media they are |
| 180 | reading from, but each is responsible for returning a valid bootflow if |
| 181 | available. |
| 182 | |
| 183 | A helper called `bootdev_find_in_blk()` makes it fairly easy to implement this |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 184 | function for each media device uclass, in a few lines of code. For many types |
| 185 | ot bootdevs, the `get_bootflow` member can be NULL, indicating that the default |
| 186 | handler is used. This is called `default_get_bootflow()` and it only works with |
| 187 | block devices. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 188 | |
| 189 | |
| 190 | Bootdev drivers |
| 191 | --------------- |
| 192 | |
| 193 | A bootdev driver is typically fairly simple. Here is one for mmc:: |
| 194 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 195 | static int mmc_bootdev_bind(struct udevice *dev) |
| 196 | { |
| 197 | struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev); |
| 198 | |
Simon Glass | 7e1f6a4 | 2023-01-17 10:48:08 -0700 | [diff] [blame] | 199 | ucp->prio = BOOTDEVP_2_INTERNAL_FAST; |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | struct bootdev_ops mmc_bootdev_ops = { |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static const struct udevice_id mmc_bootdev_ids[] = { |
| 208 | { .compatible = "u-boot,bootdev-mmc" }, |
| 209 | { } |
| 210 | }; |
| 211 | |
| 212 | U_BOOT_DRIVER(mmc_bootdev) = { |
| 213 | .name = "mmc_bootdev", |
| 214 | .id = UCLASS_BOOTDEV, |
| 215 | .ops = &mmc_bootdev_ops, |
| 216 | .bind = mmc_bootdev_bind, |
| 217 | .of_match = mmc_bootdev_ids, |
| 218 | }; |
| 219 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 220 | You may notice that the `get_bootflow` memory is not provided, so is NULL. This |
| 221 | means that `default_get_bootflow()` is used. This simply obtains the |
| 222 | block device and calls a bootdev helper function to do the rest. The |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 223 | implementation of `bootdev_find_in_blk()` checks the partition table, and |
| 224 | attempts to read a file from a filesystem on the partition number given by the |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 225 | `@iter->part` parameter. If there are any bootable partitions in the table, |
| 226 | then only bootable partitions are considered. |
| 227 | |
| 228 | Each bootdev has a priority, which indicates the order in which it is used, |
| 229 | if `boot_targets` is not used. Faster bootdevs are used first, since they are |
| 230 | more likely to be able to boot the device quickly. |
| 231 | |
| 232 | |
| 233 | Environment Variables |
| 234 | --------------------- |
| 235 | |
| 236 | Various environment variables are used by standard boot. These allow the board |
| 237 | to control where things are placed when booting the OS. You should ensure that |
| 238 | your boards sets values for these. |
| 239 | |
| 240 | fdtfile |
| 241 | Name of the flattened device tree (FDT) file to load, e.g. |
| 242 | "rockchip/rk3399-rockpro64.dtb" |
| 243 | |
| 244 | fdtaddr_addr_r |
| 245 | Address at which to load the FDT, e.g. 0x01f00000 |
| 246 | |
| 247 | fdtoverlay_addr_r (needed if overlays are used) |
| 248 | Address at which to load the overlay for the FDT, e.g. 0x02000000 |
| 249 | |
| 250 | kernel_addr_r |
| 251 | Address at which to load the kernel, e.g. 0x02080000 |
| 252 | |
| 253 | kernel_comp_addr_r |
| 254 | Address to which to decompress the kernel, e.g. 0x08000000 |
| 255 | |
| 256 | kernel_comp_size |
| 257 | Size of available space for decompressed kernel, e.g. 0x2000000 |
| 258 | |
| 259 | pxefile_addr_r |
| 260 | Address at which to load the PXE file, e.g. 0x00600000 |
| 261 | |
| 262 | ramdisk_addr_r |
| 263 | Address at which to load the ramdisk, e.g. 0x06000000 |
| 264 | |
| 265 | scriptaddr |
| 266 | Address at which to load the U-Boot script, e.g. 0x00500000 |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 267 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 268 | script_offset_f |
| 269 | SPI flash offset from which to load the U-Boot script, e.g. 0xffe000 |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 270 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 271 | script_size_f |
| 272 | Size of the script to load, e.g. 0x2000 |
| 273 | |
| 274 | Some variables are set by script bootmeth: |
| 275 | |
| 276 | devtype |
| 277 | Device type being used for boot, e.g. mmc |
| 278 | |
| 279 | devnum |
| 280 | Device number being used for boot, e.g. 1 |
| 281 | |
| 282 | distro_bootpart |
| 283 | Partition being used for boot, e.g. 2 |
| 284 | |
| 285 | prefix |
| 286 | Directory containing the script |
| 287 | |
| 288 | mmc_bootdev |
| 289 | Device number being used for boot (e.g. 1). This is only used by MMC on |
| 290 | sunxi boards. |
| 291 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 292 | |
| 293 | Device hierarchy |
| 294 | ---------------- |
| 295 | |
| 296 | A bootdev device is a child of the media device. In this example, you can see |
| 297 | that the bootdev is a sibling of the block device and both are children of |
| 298 | media device:: |
| 299 | |
| 300 | mmc 0 [ + ] bcm2835-sdhost | |-- mmc@7e202000 |
| 301 | blk 0 [ + ] mmc_blk | | |-- mmc@7e202000.blk |
| 302 | bootdev 0 [ ] mmc_bootdev | | `-- mmc@7e202000.bootdev |
| 303 | mmc 1 [ + ] sdhci-bcm2835 | |-- sdhci@7e300000 |
| 304 | blk 1 [ ] mmc_blk | | |-- sdhci@7e300000.blk |
| 305 | bootdev 1 [ ] mmc_bootdev | | `-- sdhci@7e300000.bootdev |
| 306 | |
| 307 | The bootdev device is typically created automatically in the media uclass' |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 308 | `post_bind()` method by calling `bootdev_setup_for_dev()` or |
| 309 | `bootdev_setup_sibling_blk()`. The code typically something like this:: |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 310 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 311 | /* dev is the Ethernet device */ |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 312 | ret = bootdev_setup_for_dev(dev, "eth_bootdev"); |
| 313 | if (ret) |
| 314 | return log_msg_ret("bootdev", ret); |
| 315 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 316 | or:: |
| 317 | |
| 318 | /* blk is the block device (child of MMC device) |
| 319 | ret = bootdev_setup_sibling_blk(blk, "mmc_bootdev"); |
| 320 | if (ret) |
| 321 | return log_msg_ret("bootdev", ret); |
| 322 | |
| 323 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 324 | Here, `eth_bootdev` is the name of the Ethernet bootdev driver and `dev` |
| 325 | is the ethernet device. This function is safe to call even if standard boot is |
| 326 | not enabled, since it does nothing in that case. It can be added to all uclasses |
| 327 | which implement suitable media. |
| 328 | |
| 329 | |
| 330 | The bootstd device |
| 331 | ------------------ |
| 332 | |
| 333 | Standard boot requires a single instance of the bootstd device to make things |
| 334 | work. This includes global information about the state of standard boot. See |
| 335 | `struct bootstd_priv` for this structure, accessed with `bootstd_get_priv()`. |
| 336 | |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 337 | Within the devicetree, if you add bootmeth devices, they should be children of |
| 338 | the bootstd device. See `arch/sandbox/dts/test.dts` for an example of this. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 339 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 340 | |
| 341 | .. _`Automatic Devices`: |
| 342 | |
| 343 | Automatic devices |
| 344 | ----------------- |
| 345 | |
| 346 | It is possible to define all the required devices in the devicetree manually, |
| 347 | but it is not necessary. The bootstd uclass includes a `dm_scan_other()` |
| 348 | function which creates the bootstd device if not found. If no bootmeth devices |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 349 | are found at all, it creates one for each available bootmeth driver. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 350 | |
| 351 | If your devicetree has any bootmeth device it must have all of them that you |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 352 | want to use, since no bootmeth devices will be created automatically in that |
| 353 | case. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 354 | |
| 355 | |
| 356 | Using devicetree |
| 357 | ---------------- |
| 358 | |
| 359 | If a bootdev is complicated or needs configuration information, it can be |
| 360 | added to the devicetree as a child of the media device. For example, imagine a |
| 361 | bootdev which reads a bootflow from SPI flash. The devicetree fragment might |
| 362 | look like this:: |
| 363 | |
| 364 | spi@0 { |
| 365 | flash@0 { |
| 366 | reg = <0>; |
| 367 | compatible = "spansion,m25p16", "jedec,spi-nor"; |
| 368 | spi-max-frequency = <40000000>; |
| 369 | |
| 370 | bootdev { |
| 371 | compatible = "u-boot,sf-bootdev"; |
| 372 | offset = <0x2000>; |
| 373 | size = <0x1000>; |
| 374 | }; |
| 375 | }; |
| 376 | }; |
| 377 | |
| 378 | The `sf-bootdev` driver can implement a way to read from the SPI flash, using |
| 379 | the offset and size provided, and return that bootflow file back to the caller. |
Dario Binacchi | 3c9c6d7 | 2022-08-26 15:15:41 +0200 | [diff] [blame] | 380 | When distro boot wants to read the kernel it calls distro_getfile() which must |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 381 | provide a way to read from the SPI flash. See `distro_boot()` at distro_boot_ |
| 382 | for more details. |
| 383 | |
| 384 | Of course this is all internal to U-Boot. All the distro sees is another way |
| 385 | to boot. |
| 386 | |
| 387 | |
| 388 | Configuration |
| 389 | ------------- |
| 390 | |
| 391 | Standard boot is enabled with `CONFIG_BOOTSTD`. Each bootmeth has its own CONFIG |
| 392 | option also. For example, `CONFIG_BOOTMETH_DISTRO` enables support for distro |
| 393 | boot from a disk. |
| 394 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 395 | To enable all feature sof standard boot, use `CONFIG_BOOTSTD_FULL`. This |
| 396 | includes the full set of commands, more error messages when things go wrong and |
| 397 | bootmeth ordering with the bootmeths environment variable. |
| 398 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 399 | |
| 400 | Available bootmeth drivers |
| 401 | -------------------------- |
| 402 | |
| 403 | Bootmeth drivers are provided for: |
| 404 | |
| 405 | - distro boot from a disk (syslinux) |
| 406 | - distro boot from a network (PXE) |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 407 | - U-Boot scripts from disk, network or SPI flash |
| 408 | - EFI boot using bootefi from disk |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 409 | - VBE |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 410 | - EFI boot using boot manager |
| 411 | |
| 412 | |
| 413 | Command interface |
| 414 | ----------------- |
| 415 | |
| 416 | Three commands are available: |
| 417 | |
| 418 | `bootdev` |
| 419 | Allows listing of available bootdevs, selecting a particular one and |
| 420 | getting information about it. See :doc:`../usage/cmd/bootdev` |
| 421 | |
| 422 | `bootflow` |
| 423 | Allows scanning one or more bootdevs for bootflows, listing available |
| 424 | bootflows, selecting one, obtaining information about it and booting it. |
| 425 | See :doc:`../usage/cmd/bootflow` |
| 426 | |
| 427 | `bootmeth` |
| 428 | Allow listing of available bootmethds and setting the order in which they |
| 429 | are tried. See :doc:`../usage/cmd/bootmeth` |
| 430 | |
| 431 | .. _BootflowStates: |
| 432 | |
| 433 | Bootflow states |
| 434 | --------------- |
| 435 | |
| 436 | Here is a list of states that a bootflow can be in: |
| 437 | |
| 438 | ======= ======================================================================= |
| 439 | State Meaning |
| 440 | ======= ======================================================================= |
| 441 | base Starting-out state, indicates that no media/partition was found. For an |
| 442 | SD card socket it may indicate that the card is not inserted. |
| 443 | media Media was found (e.g. SD card is inserted) but no partition information |
| 444 | was found. It might lack a partition table or have a read error. |
| 445 | part Partition was found but a filesystem could not be read. This could be |
| 446 | because the partition does not hold a filesystem or the filesystem is |
| 447 | very corrupted. |
| 448 | fs Filesystem was found but the file could not be read. It could be |
| 449 | missing or in the wrong subdirectory. |
| 450 | file File was found and its size detected, but it could not be read. This |
| 451 | could indicate filesystem corruption. |
| 452 | ready File was loaded and is ready for use. In this state the bootflow is |
| 453 | ready to be booted. |
| 454 | ======= ======================================================================= |
| 455 | |
| 456 | |
| 457 | Theory of operation |
| 458 | ------------------- |
| 459 | |
| 460 | This describes how standard boot progresses through to booting an operating |
| 461 | system. |
| 462 | |
| 463 | To start. all the necessary devices must be bound, including bootstd, which |
| 464 | provides the top-level `struct bootstd_priv` containing optional configuration |
| 465 | information. The bootstd device is also holds the various lists used while |
| 466 | scanning. This step is normally handled automatically by driver model, as |
| 467 | described in `Automatic Devices`_. |
| 468 | |
| 469 | Bootdevs are also required, to provide access to the media to use. These are not |
| 470 | useful by themselves: bootmeths are needed to provide the means of scanning |
| 471 | those bootdevs. So, all up, we need a single bootstd device, one or more bootdev |
| 472 | devices and one or more bootmeth devices. |
| 473 | |
| 474 | Once these are ready, typically a `bootflow scan` command is issued. This kicks |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 475 | of the iteration process, which involves hunting for bootdevs and looking |
| 476 | through the bootdevs and their partitions one by one to find bootflows. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 477 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 478 | Iteration is kicked off using `bootflow_scan_first()`. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 479 | |
| 480 | The iterator is set up with `bootflow_iter_init()`. This simply creates an |
| 481 | empty one with the given flags. Flags are used to control whether each |
| 482 | iteration is displayed, whether to return iterations even if they did not result |
| 483 | in a valid bootflow, whether to iterate through just a single bootdev, etc. |
| 484 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 485 | Then the iterator is set up to according to the parameters given: |
| 486 | |
| 487 | - When `dev` is provided, then a single bootdev is scanned. In this case, |
| 488 | `BOOTFLOWF_SKIP_GLOBAL` and `BOOTFLOWF_SINGLE_DEV` are set. No hunters are |
| 489 | used in this case |
| 490 | |
| 491 | - Otherwise, when `label` is provided, then a single label or named bootdev is |
| 492 | scanned. In this case `BOOTFLOWF_SKIP_GLOBAL` is set and there are three |
| 493 | options (with an effect on the `iter_incr()` function described later): |
| 494 | |
| 495 | - If `label` indicates a numeric bootdev number (e.g. "2") then |
| 496 | `BOOTFLOW_METHF_SINGLE_DEV` is set. In this case, moving to the next bootdev |
| 497 | simple stops, since there is only one. No hunters are used. |
| 498 | - If `label` indicates a particular media device (e.g. "mmc1") then |
| 499 | `BOOTFLOWF_SINGLE_MEDIA` is set. In this case, moving to the next bootdev |
| 500 | processes just the children of the media device. Hunters are used, in this |
| 501 | example just the "mmc" hunter. |
| 502 | - If `label` indicates a media uclass (e.g. "mmc") then |
| 503 | `BOOTFLOWF_SINGLE_UCLASS` is set. In this case, all bootdevs in that uclass |
| 504 | are used. Hunters are used, in this example just the "mmc" hunter |
| 505 | |
| 506 | - Otherwise, none of the above flags is set and iteration is set up to work |
| 507 | through `boot_targets` environment variable (or `bootdev-order` device tree |
| 508 | property) in order, running the relevant hunter first. In this case |
| 509 | `cur_label` is used to indicate the label being processed. If there is no list |
| 510 | of labels, then all bootdevs are processed in order of priority, running the |
| 511 | hunters as it goes. |
| 512 | |
| 513 | With the above it is therefore possible to iterate in a variety of ways. |
| 514 | |
| 515 | No attempt is made to determine the ordering of bootdevs, since this cannot be |
| 516 | known in advance if we are using the hunters. Any hunter might discover a new |
| 517 | bootdev and disturb the original ordering. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 518 | |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 519 | Next, the ordering of bootmeths is determined, by `bootmeth_setup_iter_order()`. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 520 | By default the ordering is again by sequence number, i.e. the `/aliases` node, |
| 521 | or failing that the order in the devicetree. But the `bootmeth order` command |
| 522 | or `bootmeths` environment variable can be used to set up an ordering. If that |
| 523 | has been done, the ordering is in `struct bootstd_priv`, so that ordering is |
| 524 | simply copied into the iterator. Either way, the `method_order` array it set up, |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 525 | along with `num_methods`. |
| 526 | |
| 527 | Note that global bootmeths are always put at the end of the ordering. If any are |
| 528 | present, `cur_method` is set to the first one, so that global bootmeths are done |
| 529 | first. Once all have been used, these bootmeths are dropped from the iteration. |
| 530 | When there are no global bootmeths, `cur_method` is set to 0. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 531 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 532 | At this point the iterator is ready to use, with the first bootmeth selected. |
| 533 | Most of the other fields are 0. This means that the current partition |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 534 | is 0, which is taken to mean the whole device, since partition numbers start at |
| 535 | 1. It also means that `max_part` is 0, i.e. the maximum partition number we know |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 536 | about is 0, meaning that, as far as we know, there is no partition table on this |
| 537 | bootdev. |
| 538 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 539 | With the iterator ready, `bootflow_scan_first()` checks whether the current |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 540 | settings produce a valid bootflow. This is handled by `bootflow_check()`, which |
| 541 | either returns 0 (if it got something) or an error if not (more on that later). |
| 542 | If the `BOOTFLOWF_ALL` iterator flag is set, even errors are returned as |
| 543 | incomplete bootflows, but normally an error results in moving onto the next |
| 544 | iteration. |
| 545 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 546 | Note that `bootflow_check()` handles global bootmeths explicitly, by calling |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 547 | `bootmeth_get_bootflow()` on each one. The `doing_global` flag indicates when |
| 548 | the iterator is in that state. |
| 549 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 550 | The `bootflow_scan_next()` function handles moving onto the next iteration and |
| 551 | checking it. In fact it sits in a loop doing that repeatedly until it finds |
| 552 | something it wants to return. |
| 553 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 554 | The actual 'moving on' part is implemented in `iter_incr()`. This is a fairly |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 555 | simple function. It increments the first counter. If that hits its maximum, it |
| 556 | sets it to zero and increments the second counter. You can think of all the |
| 557 | counters together as a number with three digits which increment in order, with |
| 558 | the least-sigificant digit on the right, counting like this: |
| 559 | |
| 560 | ======== ======= ======= |
| 561 | bootdev part method |
| 562 | ======== ======= ======= |
| 563 | 0 0 0 |
| 564 | 0 0 1 |
| 565 | 0 0 2 |
| 566 | 0 1 0 |
| 567 | 0 1 1 |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 568 | 0 1 2 |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 569 | 1 0 0 |
| 570 | 1 0 1 |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 571 | ... |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 572 | ======== ======= ======= |
| 573 | |
| 574 | The maximum value for `method` is `num_methods - 1` so when it exceeds that, it |
| 575 | goes back to 0 and the next `part` is considered. The maximum value for that is |
| 576 | `max_part`, which is initially zero for all bootdevs. If we find a partition |
| 577 | table on that bootdev, `max_part` can be updated during the iteration to a |
| 578 | higher value - see `bootdev_find_in_blk()` for that, described later. If that |
| 579 | exceeds its maximum, then the next bootdev is used. In this way, iter_incr() |
| 580 | works its way through all possibilities, moving forward one each time it is |
| 581 | called. |
| 582 | |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 583 | Note that global bootmeths introduce a subtlety into the above description. |
| 584 | When `doing_global` is true, the iteration takes place only among the bootmeths, |
| 585 | i.e. the last column above. The global bootmeths are at the end of the list. |
| 586 | Assuming that they are entries 3 and 4 in the list, the iteration then looks |
| 587 | like this: |
| 588 | |
| 589 | ======== ======= ======= ======================================= |
| 590 | bootdev part method notes |
| 591 | ======== ======= ======= ======================================= |
| 592 | . . 3 doing_global = true, method_count = 5 |
| 593 | . . 4 |
| 594 | 0 0 0 doing_global = false, method_count = 3 |
| 595 | 0 0 1 |
| 596 | 0 0 2 |
| 597 | 0 1 0 |
| 598 | 0 1 1 |
| 599 | 0 1 2 |
| 600 | 1 0 0 |
| 601 | 1 0 1 |
| 602 | ... |
| 603 | ======== ======= ======= ======================================= |
| 604 | |
| 605 | The changeover of the value of `doing_global` from true to false is handled in |
| 606 | `iter_incr()` as well. |
| 607 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 608 | Note that the value in the `bootdev` column above is not actually stored - it is |
| 609 | just for illustration. In practice, `iter_incr()` uses the flags to determine |
| 610 | whether to move to the next bootdev in the uclass, the next child of the media |
| 611 | device, the next label, or the next priority level, depending on the flag |
| 612 | settings (see `BOOTFLOW_METHF_SINGLE_DEV`, etc. above). |
| 613 | |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 614 | There is no expectation that iteration will actually finish. Quite often a |
| 615 | valid bootflow is found early on. With `bootflow scan -b`, that causes the |
| 616 | bootflow to be immediately booted. Assuming it is successful, the iteration never |
| 617 | completes. |
| 618 | |
| 619 | Also note that the iterator hold the **current** combination being considered. |
| 620 | So when `iter_incr()` is called, it increments to the next one and returns it, |
| 621 | the new **current** combination. |
| 622 | |
| 623 | Note also the `err` field in `struct bootflow_iter`. This is normally 0 and has |
| 624 | thus has no effect on `iter_inc()`. But if it is non-zero, signalling an error, |
| 625 | it indicates to the iterator what it should do when called. It can force moving |
| 626 | to the next partition, or bootdev, for example. The special values |
| 627 | `BF_NO_MORE_PARTS` and `BF_NO_MORE_DEVICES` handle this. When `iter_incr` sees |
| 628 | `BF_NO_MORE_PARTS` it knows that it should immediately move to the next bootdev. |
| 629 | When it sees `BF_NO_MORE_DEVICES` it knows that there is nothing more it can do |
| 630 | so it should immediately return. The caller of `iter_incr()` is responsible for |
| 631 | updating the `err` field, based on the return value it sees. |
| 632 | |
| 633 | The above describes the iteration process at a high level. It is basically a |
| 634 | very simple increment function with a checker called `bootflow_check()` that |
| 635 | checks the result of each iteration generated, to determine whether it can |
| 636 | produce a bootflow. |
| 637 | |
| 638 | So what happens inside of `bootflow_check()`? It simply calls the uclass |
| 639 | method `bootdev_get_bootflow()` to ask the bootdev to return a bootflow. It |
| 640 | passes the iterator to the bootdev method, so that function knows what we are |
| 641 | talking about. At first, the bootflow is set up in the state `BOOTFLOWST_BASE`, |
| 642 | with just the `method` and `dev` intiialised. But the bootdev may fill in more, |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 643 | e.g. updating the state, depending on what it finds. For global bootmeths the |
| 644 | `bootmeth_get_bootflow()` function is called instead of |
| 645 | `bootdev_get_bootflow()`. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 646 | |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 647 | Based on what the bootdev or bootmeth responds with, `bootflow_check()` either |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 648 | returns a valid bootflow, or a partial one with an error. A partial bootflow |
| 649 | is one that has some fields set up, but did not reach the `BOOTFLOWST_READY` |
| 650 | state. As noted before, if the `BOOTFLOWF_ALL` iterator flag is set, then all |
| 651 | bootflows are returned, even partial ones. This can help with debugging. |
| 652 | |
| 653 | So at this point you can see that total control over whether a bootflow can |
Simon Glass | afaeb77 | 2022-07-30 15:52:35 -0600 | [diff] [blame] | 654 | be generated from a particular iteration, or not, rests with the bootdev (or |
| 655 | global bootmeth). Each one can adopt its own approach. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 656 | |
| 657 | Going down a level, what does the bootdev do in its `get_bootflow()` method? |
| 658 | Let us consider the MMC bootdev. In that case the call to |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 659 | `bootdev_get_bootflow()` ends up in `default_get_bootflow()`. It locates the |
| 660 | parent device of the bootdev, i.e. the `UCLASS_MMC` device itself, then finds |
| 661 | the block device associated with it. It then calls the helper function |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 662 | `bootdev_find_in_blk()` to do all the work. This is common with just about any |
| 663 | bootdev that is based on a media device. |
| 664 | |
| 665 | The `bootdev_find_in_blk()` helper is implemented in the bootdev uclass. It |
| 666 | names the bootflow and copies the partition number in from the iterator. Then it |
| 667 | calls the bootmeth device to check if it can support this device. This is |
| 668 | important since some bootmeths only work with network devices, for example. If |
| 669 | that check fails, it stops. |
| 670 | |
| 671 | Assuming the bootmeth is happy, or at least indicates that it is willing to try |
| 672 | (by returning 0 from its `check()` method), the next step is to try the |
| 673 | partition. If that works it tries to detect a file system. If that works then it |
| 674 | calls the bootmeth device once more, this time to read the bootflow. |
| 675 | |
| 676 | Note: At present a filesystem is needed for the bootmeth to be called on block |
| 677 | devices, simply because we don't have any examples where this is not the case. |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 678 | This feature can be added as needed. Note that sandbox is a special case, since |
| 679 | in that case the host filesystem can be accessed even though the block device |
| 680 | is NULL. |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 681 | |
| 682 | If we take the example of the `bootmeth_distro` driver, this call ends up at |
| 683 | `distro_read_bootflow()`. It has the filesystem ready, so tries various |
| 684 | filenames to try to find the `extlinux.conf` file, reading it if possible. If |
| 685 | all goes well the bootflow ends up in the `BOOTFLOWST_READY` state. |
| 686 | |
| 687 | At this point, we fall back from the bootmeth driver, to |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 688 | `bootdev_find_in_blk()`, then back to `default_get_bootflow()`, then to |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 689 | `bootdev_get_bootflow()`, then to `bootflow_check()` and finally to its caller, |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 690 | either `bootflow_scan_first()` or `bootflow_scan_next()`. In either case, |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 691 | the bootflow is returned as the result of this iteration, assuming it made it to |
| 692 | the `BOOTFLOWST_READY` state. |
| 693 | |
| 694 | That is the basic operation of scanning for bootflows. The process of booting a |
| 695 | bootflow is handled by the bootmeth driver for that bootflow. In the case of |
| 696 | distro boot, this parses and processes the `extlinux.conf` file that was read. |
| 697 | See `distro_boot()` for how that works. The processing may involve reading |
| 698 | additional files, which is handled by the `read_file()` method, which is |
| 699 | `distro_read_file()` in this case. All bootmethds should support reading files, |
| 700 | since the bootflow is typically only the basic instructions and does not include |
| 701 | the operating system itself, ramdisk, device tree, etc. |
| 702 | |
| 703 | The vast majority of the bootstd code is concerned with iterating through |
| 704 | partitions on bootdevs and using bootmethds to find bootflows. |
| 705 | |
| 706 | How about bootdevs which are not block devices? They are handled by the same |
| 707 | methods as above, but with a different implementation. For example, the bootmeth |
| 708 | for PXE boot (over a network) uses `tftp` to read files rather than `fs_read()`. |
| 709 | But other than that it is very similar. |
| 710 | |
| 711 | |
| 712 | Tests |
| 713 | ----- |
| 714 | |
| 715 | Tests are located in `test/boot` and cover the core functionality as well as |
| 716 | the commands. All tests use sandbox so can be run on a standard Linux computer |
| 717 | and in U-Boot's CI. |
| 718 | |
Simon Glass | 736612e | 2023-01-17 10:48:19 -0700 | [diff] [blame] | 719 | For testing, a DOS-formatted disk image is used with a FAT partition on it and |
| 720 | a second unused partition. This is created in `setup_bootflow_image()`, with a |
| 721 | canned one from the source tree used if it cannot be created (e.g. in CI). |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 722 | |
| 723 | |
| 724 | Bootflow internals |
| 725 | ------------------ |
| 726 | |
| 727 | The bootstd device holds a linked list of scanned bootflows as well as the |
| 728 | currently selected bootdev and bootflow (for use by commands). This is in |
| 729 | `struct bootstd_priv`. |
| 730 | |
| 731 | Each bootdev device has its own `struct bootdev_uc_plat` which holds a |
| 732 | list of scanned bootflows just for that device. |
| 733 | |
| 734 | The bootflow itself is documented in bootflow_h_. It includes various bits of |
| 735 | information about the bootflow and a buffer to hold the file. |
| 736 | |
| 737 | |
| 738 | Future |
| 739 | ------ |
| 740 | |
| 741 | Apart from the to-do items below, different types of bootflow files may be |
| 742 | implemented in future, e.g. Chromium OS support which is currently only |
| 743 | available as a script in chromebook_coral. |
| 744 | |
| 745 | |
| 746 | To do |
| 747 | ----- |
| 748 | |
| 749 | Some things that need to be done to completely replace the distro-boot scripts: |
| 750 | |
| 751 | - add bootdev drivers for dhcp, sata, scsi, ide, virtio |
| 752 | - PXE boot for EFI |
| 753 | - support for loading U-Boot scripts |
| 754 | |
| 755 | Other ideas: |
| 756 | |
| 757 | - `bootflow prep` to load everything preparing for boot, so that `bootflow boot` |
| 758 | can just do the boot. |
| 759 | - automatically load kernel, FDT, etc. to suitable addresses so the board does |
| 760 | not need to specify things like `pxefile_addr_r` |
| 761 | |
| 762 | |
Paul Barker | 6c55d0d | 2022-07-29 14:31:58 +0100 | [diff] [blame] | 763 | .. _distro_bootcmd: https://github.com/u-boot/u-boot/blob/master/include/config_distro_bootcmd.h |
Simon Glass | 83b9be6 | 2022-04-24 23:31:26 -0600 | [diff] [blame] | 764 | .. _BootLoaderSpec: http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec/ |
| 765 | .. _distro_boot: https://github.com/u-boot/u-boot/blob/master/boot/distro.c |
| 766 | .. _bootflow_h: https://github.com/u-boot/u-boot/blob/master/include/bootflow.h |