blob: 143f373aae96f9ea5bac04c660148a0fb413ce10 [file] [log] [blame]
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +03001// SPDX-License-Identifier: BSD-2-Clause
2/*
3 * Copyright (C) 2017 The Android Open Source Project
4 */
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +03005#include <android_ab.h>
6#include <android_bootloader_message.h>
Simon Glass655306c2020-05-10 11:39:58 -06007#include <blk.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass655306c2020-05-10 11:39:58 -060010#include <part.h>
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030011#include <memalign.h>
Simon Glass655306c2020-05-10 11:39:58 -060012#include <linux/err.h>
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030013#include <u-boot/crc.h>
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030014
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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010023 * Return: crc32 sum
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030024 */
25static 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/**
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 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010039 * Return: 0 on success and a negative on error
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030040 */
41static 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
52 if (!abc)
53 return -EFAULT;
54
55 memcpy(abc->slot_suffix, "a\0\0\0", 4);
56 abc->magic = BOOT_CTRL_MAGIC;
57 abc->version = BOOT_CTRL_VERSION;
58 abc->nb_slot = NUM_SLOTS;
59 memset(abc->reserved0, 0, sizeof(abc->reserved0));
60 for (i = 0; i < abc->nb_slot; ++i)
61 abc->slot_info[i] = metadata;
62
63 memset(abc->reserved1, 0, sizeof(abc->reserved1));
64 abc->crc32_le = ab_control_compute_crc(abc);
65
66 return 0;
67}
68
69/**
70 * Load the boot_control struct from disk into newly allocated memory.
71 *
72 * This function allocates and returns an integer number of disk blocks,
73 * based on the block size of the passed device to help performing a
74 * read-modify-write operation on the boot_control struct.
75 * The boot_control struct offset (2 KiB) must be a multiple of the device
76 * block size, for simplicity.
77 *
78 * @param[in] dev_desc Device where to read the boot_control struct from
79 * @param[in] part_info Partition in 'dev_desc' where to read from, normally
80 * the "misc" partition should be used
81 * @param[out] pointer to pointer to bootloader_control data
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010082 * Return: 0 on success and a negative on error
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030083 */
84static int ab_control_create_from_disk(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -060085 const struct disk_partition *part_info,
Joshua Wattad3ae7d2023-07-03 10:07:13 -050086 struct bootloader_control **abc,
87 ulong offset)
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030088{
89 ulong abc_offset, abc_blocks, ret;
90
Joshua Wattad3ae7d2023-07-03 10:07:13 -050091 abc_offset = offset +
92 offsetof(struct bootloader_message_ab, slot_suffix);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +030093 if (abc_offset % part_info->blksz) {
94 log_err("ANDROID: Boot control block not block aligned.\n");
95 return -EINVAL;
96 }
97 abc_offset /= part_info->blksz;
98
99 abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
100 part_info->blksz);
101 if (abc_offset + abc_blocks > part_info->size) {
102 log_err("ANDROID: boot control partition too small. Need at");
103 log_err(" least %lu blocks but have %lu blocks.\n",
104 abc_offset + abc_blocks, part_info->size);
105 return -EINVAL;
106 }
107 *abc = malloc_cache_aligned(abc_blocks * part_info->blksz);
108 if (!*abc)
109 return -ENOMEM;
110
111 ret = blk_dread(dev_desc, part_info->start + abc_offset, abc_blocks,
112 *abc);
113 if (IS_ERR_VALUE(ret)) {
114 log_err("ANDROID: Could not read from boot ctrl partition\n");
115 free(*abc);
116 return -EIO;
117 }
118
119 log_debug("ANDROID: Loaded ABC, %lu blocks\n", abc_blocks);
120
121 return 0;
122}
123
124/**
125 * Store the loaded boot_control block.
126 *
127 * Store back to the same location it was read from with
128 * ab_control_create_from_misc().
129 *
130 * @param[in] dev_desc Device where we should write the boot_control struct
131 * @param[in] part_info Partition on the 'dev_desc' where to write
132 * @param[in] abc Pointer to the boot control struct and the extra bytes after
133 * it up to the nearest block boundary
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100134 * Return: 0 on success and a negative on error
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300135 */
136static int ab_control_store(struct blk_desc *dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600137 const struct disk_partition *part_info,
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500138 struct bootloader_control *abc, ulong offset)
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300139{
140 ulong abc_offset, abc_blocks, ret;
141
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500142 abc_offset = offset +
143 offsetof(struct bootloader_message_ab, slot_suffix) /
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300144 part_info->blksz;
145 abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
146 part_info->blksz);
147 ret = blk_dwrite(dev_desc, part_info->start + abc_offset, abc_blocks,
148 abc);
149 if (IS_ERR_VALUE(ret)) {
150 log_err("ANDROID: Could not write back the misc partition\n");
151 return -EIO;
152 }
153
154 return 0;
155}
156
157/**
158 * Compare two slots.
159 *
160 * The function determines slot which is should we boot from among the two.
161 *
162 * @param[in] a The first bootable slot metadata
163 * @param[in] b The second bootable slot metadata
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100164 * Return: Negative if the slot "a" is better, positive of the slot "b" is
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300165 * better or 0 if they are equally good.
166 */
167static int ab_compare_slots(const struct slot_metadata *a,
168 const struct slot_metadata *b)
169{
170 /* Higher priority is better */
171 if (a->priority != b->priority)
172 return b->priority - a->priority;
173
174 /* Higher successful_boot value is better, in case of same priority */
175 if (a->successful_boot != b->successful_boot)
176 return b->successful_boot - a->successful_boot;
177
178 /* Higher tries_remaining is better to ensure round-robin */
179 if (a->tries_remaining != b->tries_remaining)
180 return b->tries_remaining - a->tries_remaining;
181
182 return 0;
183}
184
Joshua Watt9d5251c2023-06-23 17:05:48 -0500185int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
186 bool dec_tries)
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300187{
188 struct bootloader_control *abc = NULL;
Colin McAllister61aa03c2024-03-12 07:57:29 -0500189 struct bootloader_control *backup_abc = NULL;
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300190 u32 crc32_le;
191 int slot, i, ret;
192 bool store_needed = false;
Colin McAllister61aa03c2024-03-12 07:57:29 -0500193 bool valid_backup = false;
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300194 char slot_suffix[4];
195
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500196 ret = ab_control_create_from_disk(dev_desc, part_info, &abc, 0);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300197 if (ret < 0) {
198 /*
199 * This condition represents an actual problem with the code or
200 * the board setup, like an invalid partition information.
201 * Signal a repair mode and do not try to boot from either slot.
202 */
203 return ret;
204 }
205
Colin McAllister61aa03c2024-03-12 07:57:29 -0500206 if (CONFIG_ANDROID_AB_BACKUP_OFFSET) {
207 ret = ab_control_create_from_disk(dev_desc, part_info, &backup_abc,
208 CONFIG_ANDROID_AB_BACKUP_OFFSET);
209 if (ret < 0) {
210 free(abc);
211 return ret;
212 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500213 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500214
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300215 crc32_le = ab_control_compute_crc(abc);
216 if (abc->crc32_le != crc32_le) {
217 log_err("ANDROID: Invalid CRC-32 (expected %.8x, found %.8x),",
218 crc32_le, abc->crc32_le);
Colin McAllister61aa03c2024-03-12 07:57:29 -0500219 if (CONFIG_ANDROID_AB_BACKUP_OFFSET) {
220 crc32_le = ab_control_compute_crc(backup_abc);
221 if (backup_abc->crc32_le != crc32_le) {
222 log_err(" ANDROID: Invalid backup CRC-32 ");
223 log_err("(expected %.8x, found %.8x),",
224 crc32_le, backup_abc->crc32_le);
225 } else {
226 valid_backup = true;
227 log_info(" copying A/B metadata from backup.\n");
228 memcpy(abc, backup_abc, sizeof(*abc));
229 }
230 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500231
Colin McAllister61aa03c2024-03-12 07:57:29 -0500232 if (!valid_backup) {
233 log_err(" re-initializing A/B metadata.\n");
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500234 ret = ab_control_default(abc);
235 if (ret < 0) {
Colin McAllister61aa03c2024-03-12 07:57:29 -0500236 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
237 free(backup_abc);
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500238 free(abc);
239 return -ENODATA;
240 }
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300241 }
242 store_needed = true;
243 }
244
245 if (abc->magic != BOOT_CTRL_MAGIC) {
246 log_err("ANDROID: Unknown A/B metadata: %.8x\n", abc->magic);
Colin McAllister61aa03c2024-03-12 07:57:29 -0500247 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
248 free(backup_abc);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300249 free(abc);
250 return -ENODATA;
251 }
252
253 if (abc->version > BOOT_CTRL_VERSION) {
254 log_err("ANDROID: Unsupported A/B metadata version: %.8x\n",
255 abc->version);
Colin McAllister61aa03c2024-03-12 07:57:29 -0500256 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
257 free(backup_abc);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300258 free(abc);
259 return -ENODATA;
260 }
261
262 /*
263 * At this point a valid boot control metadata is stored in abc,
264 * followed by other reserved data in the same block. We select a with
265 * the higher priority slot that
266 * - is not marked as corrupted and
267 * - either has tries_remaining > 0 or successful_boot is true.
268 * If the selected slot has a false successful_boot, we also decrement
269 * the tries_remaining until it eventually becomes unbootable because
270 * tries_remaining reaches 0. This mechanism produces a bootloader
271 * induced rollback, typically right after a failed update.
272 */
273
274 /* Safety check: limit the number of slots. */
275 if (abc->nb_slot > ARRAY_SIZE(abc->slot_info)) {
276 abc->nb_slot = ARRAY_SIZE(abc->slot_info);
277 store_needed = true;
278 }
279
280 slot = -1;
281 for (i = 0; i < abc->nb_slot; ++i) {
282 if (abc->slot_info[i].verity_corrupted ||
283 !abc->slot_info[i].tries_remaining) {
284 log_debug("ANDROID: unbootable slot %d tries: %d, ",
285 i, abc->slot_info[i].tries_remaining);
286 log_debug("corrupt: %d\n",
287 abc->slot_info[i].verity_corrupted);
288 continue;
289 }
290 log_debug("ANDROID: bootable slot %d pri: %d, tries: %d, ",
291 i, abc->slot_info[i].priority,
292 abc->slot_info[i].tries_remaining);
293 log_debug("corrupt: %d, successful: %d\n",
294 abc->slot_info[i].verity_corrupted,
295 abc->slot_info[i].successful_boot);
296
297 if (slot < 0 ||
298 ab_compare_slots(&abc->slot_info[i],
299 &abc->slot_info[slot]) < 0) {
300 slot = i;
301 }
302 }
303
304 if (slot >= 0 && !abc->slot_info[slot].successful_boot) {
305 log_err("ANDROID: Attempting slot %c, tries remaining %d\n",
306 BOOT_SLOT_NAME(slot),
307 abc->slot_info[slot].tries_remaining);
Joshua Watt9d5251c2023-06-23 17:05:48 -0500308 if (dec_tries) {
309 abc->slot_info[slot].tries_remaining--;
310 store_needed = true;
311 }
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300312 }
313
314 if (slot >= 0) {
315 /*
316 * Legacy user-space requires this field to be set in the BCB.
317 * Newer releases load this slot suffix from the command line
318 * or the device tree.
319 */
320 memset(slot_suffix, 0, sizeof(slot_suffix));
321 slot_suffix[0] = BOOT_SLOT_NAME(slot);
322 if (memcmp(abc->slot_suffix, slot_suffix,
323 sizeof(slot_suffix))) {
324 memcpy(abc->slot_suffix, slot_suffix,
325 sizeof(slot_suffix));
326 store_needed = true;
327 }
328 }
329
330 if (store_needed) {
331 abc->crc32_le = ab_control_compute_crc(abc);
Alexey Romanov8cf36232023-12-25 13:22:45 +0300332 ret = ab_control_store(dev_desc, part_info, abc, 0);
333 if (ret < 0) {
Colin McAllister61aa03c2024-03-12 07:57:29 -0500334 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
335 free(backup_abc);
Alexey Romanov8cf36232023-12-25 13:22:45 +0300336 free(abc);
337 return ret;
338 }
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300339 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500340
Colin McAllister61aa03c2024-03-12 07:57:29 -0500341 if (CONFIG_ANDROID_AB_BACKUP_OFFSET) {
342 /*
343 * If the backup doesn't match the primary, write the primary
344 * to the backup offset
345 */
346 if (memcmp(backup_abc, abc, sizeof(*abc)) != 0) {
347 ret = ab_control_store(dev_desc, part_info, abc,
348 CONFIG_ANDROID_AB_BACKUP_OFFSET);
349 if (ret < 0) {
350 free(backup_abc);
351 free(abc);
352 return ret;
353 }
Alexey Romanov8cf36232023-12-25 13:22:45 +0300354 }
Colin McAllister61aa03c2024-03-12 07:57:29 -0500355 free(backup_abc);
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500356 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500357
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300358 free(abc);
359
360 if (slot < 0)
361 return -EINVAL;
362
363 return slot;
364}