android_ab.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. /*
  3. * Copyright (C) 2017 The Android Open Source Project
  4. */
  5. #include <common.h>
  6. #include <android_ab.h>
  7. #include <android_bootloader_message.h>
  8. #include <blk.h>
  9. #include <malloc.h>
  10. #include <part.h>
  11. #include <memalign.h>
  12. #include <linux/err.h>
  13. #include <u-boot/crc.h>
  14. #include <u-boot/crc.h>
  15. /**
  16. * Compute the CRC-32 of the bootloader control struct.
  17. *
  18. * Only the bytes up to the crc32_le field are considered for the CRC-32
  19. * calculation.
  20. *
  21. * @param[in] abc bootloader control block
  22. *
  23. * @return crc32 sum
  24. */
  25. static uint32_t ab_control_compute_crc(struct bootloader_control *abc)
  26. {
  27. return crc32(0, (void *)abc, offsetof(typeof(*abc), crc32_le));
  28. }
  29. /**
  30. * Initialize bootloader_control to the default value.
  31. *
  32. * It allows us to boot all slots in order from the first one. This value
  33. * should be used when the bootloader message is corrupted, but not when
  34. * a valid message indicates that all slots are unbootable.
  35. *
  36. * @param[in] abc bootloader control block
  37. *
  38. * @return 0 on success and a negative on error
  39. */
  40. static int ab_control_default(struct bootloader_control *abc)
  41. {
  42. int i;
  43. const struct slot_metadata metadata = {
  44. .priority = 15,
  45. .tries_remaining = 7,
  46. .successful_boot = 0,
  47. .verity_corrupted = 0,
  48. .reserved = 0
  49. };
  50. if (!abc)
  51. return -EFAULT;
  52. memcpy(abc->slot_suffix, "a\0\0\0", 4);
  53. abc->magic = BOOT_CTRL_MAGIC;
  54. abc->version = BOOT_CTRL_VERSION;
  55. abc->nb_slot = NUM_SLOTS;
  56. memset(abc->reserved0, 0, sizeof(abc->reserved0));
  57. for (i = 0; i < abc->nb_slot; ++i)
  58. abc->slot_info[i] = metadata;
  59. memset(abc->reserved1, 0, sizeof(abc->reserved1));
  60. abc->crc32_le = ab_control_compute_crc(abc);
  61. return 0;
  62. }
  63. /**
  64. * Load the boot_control struct from disk into newly allocated memory.
  65. *
  66. * This function allocates and returns an integer number of disk blocks,
  67. * based on the block size of the passed device to help performing a
  68. * read-modify-write operation on the boot_control struct.
  69. * The boot_control struct offset (2 KiB) must be a multiple of the device
  70. * block size, for simplicity.
  71. *
  72. * @param[in] dev_desc Device where to read the boot_control struct from
  73. * @param[in] part_info Partition in 'dev_desc' where to read from, normally
  74. * the "misc" partition should be used
  75. * @param[out] pointer to pointer to bootloader_control data
  76. * @return 0 on success and a negative on error
  77. */
  78. static int ab_control_create_from_disk(struct blk_desc *dev_desc,
  79. const struct disk_partition *part_info,
  80. struct bootloader_control **abc)
  81. {
  82. ulong abc_offset, abc_blocks, ret;
  83. abc_offset = offsetof(struct bootloader_message_ab, slot_suffix);
  84. if (abc_offset % part_info->blksz) {
  85. log_err("ANDROID: Boot control block not block aligned.\n");
  86. return -EINVAL;
  87. }
  88. abc_offset /= part_info->blksz;
  89. abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
  90. part_info->blksz);
  91. if (abc_offset + abc_blocks > part_info->size) {
  92. log_err("ANDROID: boot control partition too small. Need at");
  93. log_err(" least %lu blocks but have %lu blocks.\n",
  94. abc_offset + abc_blocks, part_info->size);
  95. return -EINVAL;
  96. }
  97. *abc = malloc_cache_aligned(abc_blocks * part_info->blksz);
  98. if (!*abc)
  99. return -ENOMEM;
  100. ret = blk_dread(dev_desc, part_info->start + abc_offset, abc_blocks,
  101. *abc);
  102. if (IS_ERR_VALUE(ret)) {
  103. log_err("ANDROID: Could not read from boot ctrl partition\n");
  104. free(*abc);
  105. return -EIO;
  106. }
  107. log_debug("ANDROID: Loaded ABC, %lu blocks\n", abc_blocks);
  108. return 0;
  109. }
  110. /**
  111. * Store the loaded boot_control block.
  112. *
  113. * Store back to the same location it was read from with
  114. * ab_control_create_from_misc().
  115. *
  116. * @param[in] dev_desc Device where we should write the boot_control struct
  117. * @param[in] part_info Partition on the 'dev_desc' where to write
  118. * @param[in] abc Pointer to the boot control struct and the extra bytes after
  119. * it up to the nearest block boundary
  120. * @return 0 on success and a negative on error
  121. */
  122. static int ab_control_store(struct blk_desc *dev_desc,
  123. const struct disk_partition *part_info,
  124. struct bootloader_control *abc)
  125. {
  126. ulong abc_offset, abc_blocks, ret;
  127. abc_offset = offsetof(struct bootloader_message_ab, slot_suffix) /
  128. part_info->blksz;
  129. abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
  130. part_info->blksz);
  131. ret = blk_dwrite(dev_desc, part_info->start + abc_offset, abc_blocks,
  132. abc);
  133. if (IS_ERR_VALUE(ret)) {
  134. log_err("ANDROID: Could not write back the misc partition\n");
  135. return -EIO;
  136. }
  137. return 0;
  138. }
  139. /**
  140. * Compare two slots.
  141. *
  142. * The function determines slot which is should we boot from among the two.
  143. *
  144. * @param[in] a The first bootable slot metadata
  145. * @param[in] b The second bootable slot metadata
  146. * @return Negative if the slot "a" is better, positive of the slot "b" is
  147. * better or 0 if they are equally good.
  148. */
  149. static int ab_compare_slots(const struct slot_metadata *a,
  150. const struct slot_metadata *b)
  151. {
  152. /* Higher priority is better */
  153. if (a->priority != b->priority)
  154. return b->priority - a->priority;
  155. /* Higher successful_boot value is better, in case of same priority */
  156. if (a->successful_boot != b->successful_boot)
  157. return b->successful_boot - a->successful_boot;
  158. /* Higher tries_remaining is better to ensure round-robin */
  159. if (a->tries_remaining != b->tries_remaining)
  160. return b->tries_remaining - a->tries_remaining;
  161. return 0;
  162. }
  163. int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info)
  164. {
  165. struct bootloader_control *abc = NULL;
  166. u32 crc32_le;
  167. int slot, i, ret;
  168. bool store_needed = false;
  169. char slot_suffix[4];
  170. ret = ab_control_create_from_disk(dev_desc, part_info, &abc);
  171. if (ret < 0) {
  172. /*
  173. * This condition represents an actual problem with the code or
  174. * the board setup, like an invalid partition information.
  175. * Signal a repair mode and do not try to boot from either slot.
  176. */
  177. return ret;
  178. }
  179. crc32_le = ab_control_compute_crc(abc);
  180. if (abc->crc32_le != crc32_le) {
  181. log_err("ANDROID: Invalid CRC-32 (expected %.8x, found %.8x),",
  182. crc32_le, abc->crc32_le);
  183. log_err("re-initializing A/B metadata.\n");
  184. ret = ab_control_default(abc);
  185. if (ret < 0) {
  186. free(abc);
  187. return -ENODATA;
  188. }
  189. store_needed = true;
  190. }
  191. if (abc->magic != BOOT_CTRL_MAGIC) {
  192. log_err("ANDROID: Unknown A/B metadata: %.8x\n", abc->magic);
  193. free(abc);
  194. return -ENODATA;
  195. }
  196. if (abc->version > BOOT_CTRL_VERSION) {
  197. log_err("ANDROID: Unsupported A/B metadata version: %.8x\n",
  198. abc->version);
  199. free(abc);
  200. return -ENODATA;
  201. }
  202. /*
  203. * At this point a valid boot control metadata is stored in abc,
  204. * followed by other reserved data in the same block. We select a with
  205. * the higher priority slot that
  206. * - is not marked as corrupted and
  207. * - either has tries_remaining > 0 or successful_boot is true.
  208. * If the selected slot has a false successful_boot, we also decrement
  209. * the tries_remaining until it eventually becomes unbootable because
  210. * tries_remaining reaches 0. This mechanism produces a bootloader
  211. * induced rollback, typically right after a failed update.
  212. */
  213. /* Safety check: limit the number of slots. */
  214. if (abc->nb_slot > ARRAY_SIZE(abc->slot_info)) {
  215. abc->nb_slot = ARRAY_SIZE(abc->slot_info);
  216. store_needed = true;
  217. }
  218. slot = -1;
  219. for (i = 0; i < abc->nb_slot; ++i) {
  220. if (abc->slot_info[i].verity_corrupted ||
  221. !abc->slot_info[i].tries_remaining) {
  222. log_debug("ANDROID: unbootable slot %d tries: %d, ",
  223. i, abc->slot_info[i].tries_remaining);
  224. log_debug("corrupt: %d\n",
  225. abc->slot_info[i].verity_corrupted);
  226. continue;
  227. }
  228. log_debug("ANDROID: bootable slot %d pri: %d, tries: %d, ",
  229. i, abc->slot_info[i].priority,
  230. abc->slot_info[i].tries_remaining);
  231. log_debug("corrupt: %d, successful: %d\n",
  232. abc->slot_info[i].verity_corrupted,
  233. abc->slot_info[i].successful_boot);
  234. if (slot < 0 ||
  235. ab_compare_slots(&abc->slot_info[i],
  236. &abc->slot_info[slot]) < 0) {
  237. slot = i;
  238. }
  239. }
  240. if (slot >= 0 && !abc->slot_info[slot].successful_boot) {
  241. log_err("ANDROID: Attempting slot %c, tries remaining %d\n",
  242. BOOT_SLOT_NAME(slot),
  243. abc->slot_info[slot].tries_remaining);
  244. abc->slot_info[slot].tries_remaining--;
  245. store_needed = true;
  246. }
  247. if (slot >= 0) {
  248. /*
  249. * Legacy user-space requires this field to be set in the BCB.
  250. * Newer releases load this slot suffix from the command line
  251. * or the device tree.
  252. */
  253. memset(slot_suffix, 0, sizeof(slot_suffix));
  254. slot_suffix[0] = BOOT_SLOT_NAME(slot);
  255. if (memcmp(abc->slot_suffix, slot_suffix,
  256. sizeof(slot_suffix))) {
  257. memcpy(abc->slot_suffix, slot_suffix,
  258. sizeof(slot_suffix));
  259. store_needed = true;
  260. }
  261. }
  262. if (store_needed) {
  263. abc->crc32_le = ab_control_compute_crc(abc);
  264. ab_control_store(dev_desc, part_info, abc);
  265. }
  266. free(abc);
  267. if (slot < 0)
  268. return -EINVAL;
  269. return slot;
  270. }