android_bootloader_message.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * This is from the Android Project,
  3. * Repository: https://android.googlesource.com/platform/bootable/recovery
  4. * File: bootloader_message/include/bootloader_message/bootloader_message.h
  5. * Commit: See U-Boot commit description
  6. *
  7. * Copyright (C) 2008 The Android Open Source Project
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. #ifndef __ANDROID_BOOTLOADER_MESSAGE_H
  12. #define __ANDROID_BOOTLOADER_MESSAGE_H
  13. #ifndef __UBOOT__
  14. #include <assert.h>
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #else
  18. /* compiler.h defines the types that otherwise are included from stdint.h and
  19. * stddef.h
  20. */
  21. #include <compiler.h>
  22. #endif
  23. // Spaces used by misc partition are as below:
  24. // 0 - 2K For bootloader_message
  25. // 2K - 16K Used by Vendor's bootloader (the 2K - 4K range may be optionally used
  26. // as bootloader_message_ab struct)
  27. // 16K - 64K Used by uncrypt and recovery to store wipe_package for A/B devices
  28. // Note that these offsets are admitted by bootloader,recovery and uncrypt, so they
  29. // are not configurable without changing all of them.
  30. static const size_t BOOTLOADER_MESSAGE_OFFSET_IN_MISC = 0;
  31. static const size_t WIPE_PACKAGE_OFFSET_IN_MISC = 16 * 1024;
  32. /* Bootloader Message (2-KiB)
  33. *
  34. * This structure describes the content of a block in flash
  35. * that is used for recovery and the bootloader to talk to
  36. * each other.
  37. *
  38. * The command field is updated by linux when it wants to
  39. * reboot into recovery or to update radio or bootloader firmware.
  40. * It is also updated by the bootloader when firmware update
  41. * is complete (to boot into recovery for any final cleanup)
  42. *
  43. * The status field was used by the bootloader after the completion
  44. * of an "update-radio" or "update-hboot" command, which has been
  45. * deprecated since Froyo.
  46. *
  47. * The recovery field is only written by linux and used
  48. * for the system to send a message to recovery or the
  49. * other way around.
  50. *
  51. * The stage field is written by packages which restart themselves
  52. * multiple times, so that the UI can reflect which invocation of the
  53. * package it is. If the value is of the format "#/#" (eg, "1/3"),
  54. * the UI will add a simple indicator of that status.
  55. *
  56. * We used to have slot_suffix field for A/B boot control metadata in
  57. * this struct, which gets unintentionally cleared by recovery or
  58. * uncrypt. Move it into struct bootloader_message_ab to avoid the
  59. * issue.
  60. */
  61. struct bootloader_message {
  62. char command[32];
  63. char status[32];
  64. char recovery[768];
  65. // The 'recovery' field used to be 1024 bytes. It has only ever
  66. // been used to store the recovery command line, so 768 bytes
  67. // should be plenty. We carve off the last 256 bytes to store the
  68. // stage string (for multistage packages) and possible future
  69. // expansion.
  70. char stage[32];
  71. // The 'reserved' field used to be 224 bytes when it was initially
  72. // carved off from the 1024-byte recovery field. Bump it up to
  73. // 1184-byte so that the entire bootloader_message struct rounds up
  74. // to 2048-byte.
  75. char reserved[1184];
  76. };
  77. /**
  78. * We must be cautious when changing the bootloader_message struct size,
  79. * because A/B-specific fields may end up with different offsets.
  80. */
  81. #ifndef __UBOOT__
  82. #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
  83. static_assert(sizeof(struct bootloader_message) == 2048,
  84. "struct bootloader_message size changes, which may break A/B devices");
  85. #endif
  86. #endif /* __UBOOT__ */
  87. /**
  88. * The A/B-specific bootloader message structure (4-KiB).
  89. *
  90. * We separate A/B boot control metadata from the regular bootloader
  91. * message struct and keep it here. Everything that's A/B-specific
  92. * stays after struct bootloader_message, which should be managed by
  93. * the A/B-bootloader or boot control HAL.
  94. *
  95. * The slot_suffix field is used for A/B implementations where the
  96. * bootloader does not set the androidboot.ro.boot.slot_suffix kernel
  97. * commandline parameter. This is used by fs_mgr to mount /system and
  98. * other partitions with the slotselect flag set in fstab. A/B
  99. * implementations are free to use all 32 bytes and may store private
  100. * data past the first NUL-byte in this field. It is encouraged, but
  101. * not mandatory, to use 'struct bootloader_control' described below.
  102. *
  103. * The update_channel field is used to store the Omaha update channel
  104. * if update_engine is compiled with Omaha support.
  105. */
  106. struct bootloader_message_ab {
  107. struct bootloader_message message;
  108. char slot_suffix[32];
  109. char update_channel[128];
  110. // Round up the entire struct to 4096-byte.
  111. char reserved[1888];
  112. };
  113. /**
  114. * Be cautious about the struct size change, in case we put anything post
  115. * bootloader_message_ab struct (b/29159185).
  116. */
  117. #ifndef __UBOOT__
  118. #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
  119. static_assert(sizeof(struct bootloader_message_ab) == 4096,
  120. "struct bootloader_message_ab size changes");
  121. #endif
  122. #endif /* __UBOOT__ */
  123. #define BOOT_CTRL_MAGIC 0x42414342 /* Bootloader Control AB */
  124. #define BOOT_CTRL_VERSION 1
  125. struct slot_metadata {
  126. // Slot priority with 15 meaning highest priority, 1 lowest
  127. // priority and 0 the slot is unbootable.
  128. uint8_t priority : 4;
  129. // Number of times left attempting to boot this slot.
  130. uint8_t tries_remaining : 3;
  131. // 1 if this slot has booted successfully, 0 otherwise.
  132. uint8_t successful_boot : 1;
  133. // 1 if this slot is corrupted from a dm-verity corruption, 0
  134. // otherwise.
  135. uint8_t verity_corrupted : 1;
  136. // Reserved for further use.
  137. uint8_t reserved : 7;
  138. } __attribute__((packed));
  139. /* Bootloader Control AB
  140. *
  141. * This struct can be used to manage A/B metadata. It is designed to
  142. * be put in the 'slot_suffix' field of the 'bootloader_message'
  143. * structure described above. It is encouraged to use the
  144. * 'bootloader_control' structure to store the A/B metadata, but not
  145. * mandatory.
  146. */
  147. struct bootloader_control {
  148. // NUL terminated active slot suffix.
  149. char slot_suffix[4];
  150. // Bootloader Control AB magic number (see BOOT_CTRL_MAGIC).
  151. uint32_t magic;
  152. // Version of struct being used (see BOOT_CTRL_VERSION).
  153. uint8_t version;
  154. // Number of slots being managed.
  155. uint8_t nb_slot : 3;
  156. // Number of times left attempting to boot recovery.
  157. uint8_t recovery_tries_remaining : 3;
  158. // Ensure 4-bytes alignment for slot_info field.
  159. uint8_t reserved0[2];
  160. // Per-slot information. Up to 4 slots.
  161. struct slot_metadata slot_info[4];
  162. // Reserved for further use.
  163. uint8_t reserved1[8];
  164. // CRC32 of all 28 bytes preceding this field (little endian
  165. // format).
  166. uint32_t crc32_le;
  167. } __attribute__((packed));
  168. #ifndef __UBOOT__
  169. #if (__STDC_VERSION__ >= 201112L) || defined(__cplusplus)
  170. static_assert(sizeof(struct bootloader_control) ==
  171. sizeof(((struct bootloader_message_ab *)0)->slot_suffix),
  172. "struct bootloader_control has wrong size");
  173. #endif
  174. #endif /* __UBOOT__ */
  175. #ifndef __UBOOT__
  176. #ifdef __cplusplus
  177. #include <string>
  178. #include <vector>
  179. // Return the block device name for the bootloader message partition and waits
  180. // for the device for up to 10 seconds. In case of error returns the empty
  181. // string.
  182. std::string get_bootloader_message_blk_device(std::string* err);
  183. // Read bootloader message into boot. Error message will be set in err.
  184. bool read_bootloader_message(bootloader_message* boot, std::string* err);
  185. // Read bootloader message from the specified misc device into boot.
  186. bool read_bootloader_message_from(bootloader_message* boot, const std::string& misc_blk_device,
  187. std::string* err);
  188. // Write bootloader message to BCB.
  189. bool write_bootloader_message(const bootloader_message& boot, std::string* err);
  190. // Write bootloader message to the specified BCB device.
  191. bool write_bootloader_message_to(const bootloader_message& boot,
  192. const std::string& misc_blk_device, std::string* err);
  193. // Write bootloader message (boots into recovery with the options) to BCB. Will
  194. // set the command and recovery fields, and reset the rest.
  195. bool write_bootloader_message(const std::vector<std::string>& options, std::string* err);
  196. // Write bootloader message (boots into recovery with the options) to the specific BCB device. Will
  197. // set the command and recovery fields, and reset the rest.
  198. bool write_bootloader_message_to(const std::vector<std::string>& options,
  199. const std::string& misc_blk_device, std::string* err);
  200. // Update bootloader message (boots into recovery with the options) to BCB. Will
  201. // only update the command and recovery fields.
  202. bool update_bootloader_message(const std::vector<std::string>& options, std::string* err);
  203. // Update bootloader message (boots into recovery with the |options|) in |boot|. Will only update
  204. // the command and recovery fields.
  205. bool update_bootloader_message_in_struct(bootloader_message* boot,
  206. const std::vector<std::string>& options);
  207. // Clear BCB.
  208. bool clear_bootloader_message(std::string* err);
  209. // Writes the reboot-bootloader reboot reason to the bootloader_message.
  210. bool write_reboot_bootloader(std::string* err);
  211. // Read the wipe package from BCB (from offset WIPE_PACKAGE_OFFSET_IN_MISC).
  212. bool read_wipe_package(std::string* package_data, size_t size, std::string* err);
  213. // Write the wipe package into BCB (to offset WIPE_PACKAGE_OFFSET_IN_MISC).
  214. bool write_wipe_package(const std::string& package_data, std::string* err);
  215. #else
  216. #include <stdbool.h>
  217. // C Interface.
  218. bool write_bootloader_message(const char* options);
  219. bool write_reboot_bootloader(void);
  220. #endif // ifdef __cplusplus
  221. #endif /* __UBOOT__ */
  222. #endif /* __ANDROID_BOOTLOADER_MESSAGE_H */