android_ab.c 8.7 KB

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