1 // SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (C) 2017 The Android Open Source Project
6 #include <android_ab.h>
7 #include <android_bootloader_message.h>
11 #include <u-boot/crc.h>
12 #include <u-boot/crc.h>
15 * Compute the CRC-32 of the bootloader control struct.
17 * Only the bytes up to the crc32_le field are considered for the CRC-32
20 * @param[in] abc bootloader control block
24 static uint32_t ab_control_compute_crc(struct bootloader_control *abc)
26 return crc32(0, (void *)abc, offsetof(typeof(*abc), crc32_le));
30 * Initialize bootloader_control to the default value.
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.
36 * @param[in] abc bootloader control block
38 * @return 0 on success and a negative on error
40 static int ab_control_default(struct bootloader_control *abc)
43 const struct slot_metadata metadata = {
47 .verity_corrupted = 0,
54 memcpy(abc->slot_suffix, "a\0\0\0", 4);
55 abc->magic = BOOT_CTRL_MAGIC;
56 abc->version = BOOT_CTRL_VERSION;
57 abc->nb_slot = NUM_SLOTS;
58 memset(abc->reserved0, 0, sizeof(abc->reserved0));
59 for (i = 0; i < abc->nb_slot; ++i)
60 abc->slot_info[i] = metadata;
62 memset(abc->reserved1, 0, sizeof(abc->reserved1));
63 abc->crc32_le = ab_control_compute_crc(abc);
69 * Load the boot_control struct from disk into newly allocated memory.
71 * This function allocates and returns an integer number of disk blocks,
72 * based on the block size of the passed device to help performing a
73 * read-modify-write operation on the boot_control struct.
74 * The boot_control struct offset (2 KiB) must be a multiple of the device
75 * block size, for simplicity.
77 * @param[in] dev_desc Device where to read the boot_control struct from
78 * @param[in] part_info Partition in 'dev_desc' where to read from, normally
79 * the "misc" partition should be used
80 * @param[out] pointer to pointer to bootloader_control data
81 * @return 0 on success and a negative on error
83 static int ab_control_create_from_disk(struct blk_desc *dev_desc,
84 const disk_partition_t *part_info,
85 struct bootloader_control **abc)
87 ulong abc_offset, abc_blocks, ret;
89 abc_offset = offsetof(struct bootloader_message_ab, slot_suffix);
90 if (abc_offset % part_info->blksz) {
91 log_err("ANDROID: Boot control block not block aligned.\n");
94 abc_offset /= part_info->blksz;
96 abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
98 if (abc_offset + abc_blocks > part_info->size) {
99 log_err("ANDROID: boot control partition too small. Need at");
100 log_err(" least %lu blocks but have %lu blocks.\n",
101 abc_offset + abc_blocks, part_info->size);
104 *abc = malloc_cache_aligned(abc_blocks * part_info->blksz);
108 ret = blk_dread(dev_desc, part_info->start + abc_offset, abc_blocks,
110 if (IS_ERR_VALUE(ret)) {
111 log_err("ANDROID: Could not read from boot ctrl partition\n");
116 log_debug("ANDROID: Loaded ABC, %lu blocks\n", abc_blocks);
122 * Store the loaded boot_control block.
124 * Store back to the same location it was read from with
125 * ab_control_create_from_misc().
127 * @param[in] dev_desc Device where we should write the boot_control struct
128 * @param[in] part_info Partition on the 'dev_desc' where to write
129 * @param[in] abc Pointer to the boot control struct and the extra bytes after
130 * it up to the nearest block boundary
131 * @return 0 on success and a negative on error
133 static int ab_control_store(struct blk_desc *dev_desc,
134 const disk_partition_t *part_info,
135 struct bootloader_control *abc)
137 ulong abc_offset, abc_blocks, ret;
139 abc_offset = offsetof(struct bootloader_message_ab, slot_suffix) /
141 abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
143 ret = blk_dwrite(dev_desc, part_info->start + abc_offset, abc_blocks,
145 if (IS_ERR_VALUE(ret)) {
146 log_err("ANDROID: Could not write back the misc partition\n");
156 * The function determines slot which is should we boot from among the two.
158 * @param[in] a The first bootable slot metadata
159 * @param[in] b The second bootable slot metadata
160 * @return Negative if the slot "a" is better, positive of the slot "b" is
161 * better or 0 if they are equally good.
163 static int ab_compare_slots(const struct slot_metadata *a,
164 const struct slot_metadata *b)
166 /* Higher priority is better */
167 if (a->priority != b->priority)
168 return b->priority - a->priority;
170 /* Higher successful_boot value is better, in case of same priority */
171 if (a->successful_boot != b->successful_boot)
172 return b->successful_boot - a->successful_boot;
174 /* Higher tries_remaining is better to ensure round-robin */
175 if (a->tries_remaining != b->tries_remaining)
176 return b->tries_remaining - a->tries_remaining;
181 int ab_select_slot(struct blk_desc *dev_desc, disk_partition_t *part_info)
183 struct bootloader_control *abc = NULL;
186 bool store_needed = false;
189 ret = ab_control_create_from_disk(dev_desc, part_info, &abc);
192 * This condition represents an actual problem with the code or
193 * the board setup, like an invalid partition information.
194 * Signal a repair mode and do not try to boot from either slot.
199 crc32_le = ab_control_compute_crc(abc);
200 if (abc->crc32_le != crc32_le) {
201 log_err("ANDROID: Invalid CRC-32 (expected %.8x, found %.8x),",
202 crc32_le, abc->crc32_le);
203 log_err("re-initializing A/B metadata.\n");
205 ret = ab_control_default(abc);
213 if (abc->magic != BOOT_CTRL_MAGIC) {
214 log_err("ANDROID: Unknown A/B metadata: %.8x\n", abc->magic);
219 if (abc->version > BOOT_CTRL_VERSION) {
220 log_err("ANDROID: Unsupported A/B metadata version: %.8x\n",
227 * At this point a valid boot control metadata is stored in abc,
228 * followed by other reserved data in the same block. We select a with
229 * the higher priority slot that
230 * - is not marked as corrupted and
231 * - either has tries_remaining > 0 or successful_boot is true.
232 * If the selected slot has a false successful_boot, we also decrement
233 * the tries_remaining until it eventually becomes unbootable because
234 * tries_remaining reaches 0. This mechanism produces a bootloader
235 * induced rollback, typically right after a failed update.
238 /* Safety check: limit the number of slots. */
239 if (abc->nb_slot > ARRAY_SIZE(abc->slot_info)) {
240 abc->nb_slot = ARRAY_SIZE(abc->slot_info);
245 for (i = 0; i < abc->nb_slot; ++i) {
246 if (abc->slot_info[i].verity_corrupted ||
247 !abc->slot_info[i].tries_remaining) {
248 log_debug("ANDROID: unbootable slot %d tries: %d, ",
249 i, abc->slot_info[i].tries_remaining);
250 log_debug("corrupt: %d\n",
251 abc->slot_info[i].verity_corrupted);
254 log_debug("ANDROID: bootable slot %d pri: %d, tries: %d, ",
255 i, abc->slot_info[i].priority,
256 abc->slot_info[i].tries_remaining);
257 log_debug("corrupt: %d, successful: %d\n",
258 abc->slot_info[i].verity_corrupted,
259 abc->slot_info[i].successful_boot);
262 ab_compare_slots(&abc->slot_info[i],
263 &abc->slot_info[slot]) < 0) {
268 if (slot >= 0 && !abc->slot_info[slot].successful_boot) {
269 log_err("ANDROID: Attempting slot %c, tries remaining %d\n",
270 BOOT_SLOT_NAME(slot),
271 abc->slot_info[slot].tries_remaining);
272 abc->slot_info[slot].tries_remaining--;
278 * Legacy user-space requires this field to be set in the BCB.
279 * Newer releases load this slot suffix from the command line
280 * or the device tree.
282 memset(slot_suffix, 0, sizeof(slot_suffix));
283 slot_suffix[0] = BOOT_SLOT_NAME(slot);
284 if (memcmp(abc->slot_suffix, slot_suffix,
285 sizeof(slot_suffix))) {
286 memcpy(abc->slot_suffix, slot_suffix,
287 sizeof(slot_suffix));
293 abc->crc32_le = ab_control_compute_crc(abc);
294 ab_control_store(dev_desc, part_info, abc);