android_ab.c 8.6 KB

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