android_ab.c 8.6 KB

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