boot-image.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. .. sectionauthor:: Sam Protsenko <joe.skb7@gmail.com>
  3. Android Boot Image
  4. ==================
  5. Overview
  6. --------
  7. Android Boot Image is used to boot Android OS. It usually contains kernel image
  8. (like ``zImage`` file) and ramdisk. Sometimes it can contain additional
  9. binaries. This image is built as a part of AOSP (called ``boot.img``), and being
  10. flashed into ``boot`` partition on eMMC. Bootloader then reads that image from
  11. ``boot`` partition to RAM and boots the kernel from it. Kernel then starts
  12. ``init`` process from the ramdisk. It should be mentioned that recovery image
  13. (``recovery.img``) also has Android Boot Image format.
  14. Android Boot Image format is described at [1]_. At the moment it can have one of
  15. next image headers:
  16. * v0: it's called *legacy* boot image header; used in devices launched before
  17. Android 9; contains kernel image, ramdisk and second stage bootloader
  18. (usually unused)
  19. * v1: used in devices launched with Android 9; adds ``recovery_dtbo`` field,
  20. which should be used for non-A/B devices in ``recovery.img`` (see [2]_ for
  21. details)
  22. * v2: used in devices launched with Android 10; adds ``dtb`` field, which
  23. references payload containing DTB blobs (either concatenated one after the
  24. other, or in Android DTBO image format)
  25. * v3: used in devices launched with Android 11; adds ``vendor_boot`` partition
  26. and removes the second-stage bootloader and recovery image support. The new
  27. ``vendor_boot`` partition holds the device tree blob (DTB) and a vendor ramdisk.
  28. The generic ramdisk in ``boot`` partition is loaded immediately following
  29. the vendor ramdisk.
  30. * v4: used in devices launched with Android 12; provides a boot signature in boot
  31. image header, supports multiple vendor ramdisk fragments in ``vendor_boot``
  32. partition. This version also adds a bootconfig section at the end of the vendor
  33. boot image, this section contains boot configuration parameters known at build time
  34. (see [9]_ for details).
  35. v2, v1 and v0 formats are backward compatible.
  36. The Android Boot Image format is represented by
  37. :c:type:`struct andr_image_data <andr_image_data>` in U-Boot, and can be seen in
  38. ``include/android_image.h``. U-Boot supports booting Android Boot Image and also
  39. has associated command
  40. Booting
  41. -------
  42. U-Boot is able to boot the Android OS from Android Boot Image using ``bootm``
  43. command. In order to use Android Boot Image format support, next option should
  44. be enabled::
  45. CONFIG_ANDROID_BOOT_IMAGE=y
  46. Then one can use next ``bootm`` command call to run Android:
  47. .. code-block:: bash
  48. => bootm $loadaddr $loadaddr $fdtaddr
  49. where ``$loadaddr`` - address in RAM where boot image was loaded; ``$fdtaddr`` -
  50. address in RAM where DTB blob was loaded.
  51. And parameters are, correspondingly:
  52. 1. Where kernel image is located in RAM
  53. 2. Where ramdisk is located in RAM (can be ``"-"`` if not applicable)
  54. 3. Where DTB blob is located in RAM
  55. ``bootm`` command will figure out that image located in ``$loadaddr`` has
  56. Android Boot Image format, will parse that and boot the kernel from it,
  57. providing DTB blob to kernel (from 3rd parameter), passing info about ramdisk to
  58. kernel via DTB.
  59. DTB and DTBO blobs
  60. ------------------
  61. ``bootm`` command can't just use DTB blob from Android Boot Image (``dtb``
  62. field), because:
  63. * there is no DTB area in Android Boot Image before v2
  64. * there may be several DTB blobs in DTB area (e.g. for different SoCs)
  65. * some DTBO blobs may have to be merged in DTB blobs before booting
  66. (e.g. for different boards)
  67. So user has to prepare DTB blob manually and provide it in a 3rd parameter
  68. of ``bootm`` command. Next commands can be used to do so:
  69. 1. ``abootimg``: manipulates Anroid Boot Image, allows one to extract
  70. meta-information and payloads from it
  71. 2. ``adtimg``: manipulates Android DTB/DTBO image [3]_, allows one to extract
  72. DTB/DTBO blobs from it
  73. In order to use those, please enable next config options::
  74. CONFIG_CMD_ABOOTIMG=y
  75. CONFIG_CMD_ADTIMG=y
  76. For example, let's assume we have next Android partitions on eMMC:
  77. * ``boot``: contains Android Boot Image v2 (including DTB blobs)
  78. * ``dtbo``: contains DTBO blobs
  79. Then next command sequence can be used to boot Android:
  80. .. code-block:: bash
  81. => mmc dev 1
  82. # Read boot image to RAM (into $loadaddr)
  83. => part start mmc 1 boot boot_start
  84. => part size mmc 1 boot boot_size
  85. => mmc read $loadaddr $boot_start $boot_size
  86. # Read DTBO image to RAM (into $dtboaddr)
  87. => part start mmc 1 dtbo dtbo_start
  88. => part size mmc 1 dtbo dtbo_size
  89. => mmc read $dtboaddr $dtbo_start $dtbo_size
  90. # Copy required DTB blob (into $fdtaddr)
  91. => abootimg get dtb --index=0 dtb0_start dtb0_size
  92. => cp.b $dtb0_start $fdtaddr $dtb0_size
  93. # Merge required DTBO blobs into DTB blob
  94. => fdt addr $fdtaddr 0x100000
  95. => adtimg addr $dtboaddr
  96. => adtimg get dt --index=0 $dtbo0_addr
  97. => fdt apply $dtbo0_addr
  98. # Boot Android
  99. => bootm $loadaddr $loadaddr $fdtaddr
  100. This sequence should be used for Android 10 boot. Of course, the whole Android
  101. boot procedure includes much more actions, like:
  102. * obtaining reboot reason from BCB (see [4]_)
  103. * implementing recovery boot
  104. * implementing fastboot boot
  105. * implementing A/B slotting (see [5]_)
  106. * implementing AVB2.0 (see [6]_)
  107. But Android Boot Image booting is the most crucial part in Android boot scheme.
  108. All Android bootloader requirements documentation is available at [7]_. Some
  109. overview on the whole Android 10 boot process can be found at [8]_.
  110. C API for working with Android Boot Image format
  111. ------------------------------------------------
  112. .. kernel-doc:: boot/image-android.c
  113. :internal:
  114. References
  115. ----------
  116. .. [1] https://source.android.com/devices/bootloader/boot-image-header
  117. .. [2] https://source.android.com/devices/bootloader/recovery-image
  118. .. [3] https://source.android.com/devices/architecture/dto/partitions
  119. .. [4] :doc:`bcb`
  120. .. [5] :doc:`ab`
  121. .. [6] :doc:`avb2`
  122. .. [7] https://source.android.com/devices/bootloader
  123. .. [8] https://connect.linaro.org/resources/san19/san19-217/
  124. .. [9] https://source.android.com/docs/core/architecture/bootloader/implementing-bootconfig