blob: 1196a189ed5349ca5c088e3b6ef1796b11c69a84 [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 Watta16890e2024-08-28 08:37:57 -0600142 if (offset % part_info->blksz) {
143 log_err("ANDROID: offset not block aligned\n");
144 return -EINVAL;
145 }
146
147 abc_offset = (offset +
148 offsetof(struct bootloader_message_ab, slot_suffix)) /
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300149 part_info->blksz;
150 abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
151 part_info->blksz);
152 ret = blk_dwrite(dev_desc, part_info->start + abc_offset, abc_blocks,
153 abc);
154 if (IS_ERR_VALUE(ret)) {
155 log_err("ANDROID: Could not write back the misc partition\n");
156 return -EIO;
157 }
158
159 return 0;
160}
161
162/**
163 * Compare two slots.
164 *
165 * The function determines slot which is should we boot from among the two.
166 *
167 * @param[in] a The first bootable slot metadata
168 * @param[in] b The second bootable slot metadata
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100169 * Return: Negative if the slot "a" is better, positive of the slot "b" is
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300170 * better or 0 if they are equally good.
171 */
172static int ab_compare_slots(const struct slot_metadata *a,
173 const struct slot_metadata *b)
174{
175 /* Higher priority is better */
176 if (a->priority != b->priority)
177 return b->priority - a->priority;
178
179 /* Higher successful_boot value is better, in case of same priority */
180 if (a->successful_boot != b->successful_boot)
181 return b->successful_boot - a->successful_boot;
182
183 /* Higher tries_remaining is better to ensure round-robin */
184 if (a->tries_remaining != b->tries_remaining)
185 return b->tries_remaining - a->tries_remaining;
186
187 return 0;
188}
189
Joshua Watt9d5251c2023-06-23 17:05:48 -0500190int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info,
191 bool dec_tries)
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300192{
193 struct bootloader_control *abc = NULL;
Colin McAllister61aa03c2024-03-12 07:57:29 -0500194 struct bootloader_control *backup_abc = NULL;
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300195 u32 crc32_le;
196 int slot, i, ret;
197 bool store_needed = false;
Colin McAllister61aa03c2024-03-12 07:57:29 -0500198 bool valid_backup = false;
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300199 char slot_suffix[4];
200
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500201 ret = ab_control_create_from_disk(dev_desc, part_info, &abc, 0);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300202 if (ret < 0) {
203 /*
204 * This condition represents an actual problem with the code or
205 * the board setup, like an invalid partition information.
206 * Signal a repair mode and do not try to boot from either slot.
207 */
208 return ret;
209 }
210
Colin McAllister61aa03c2024-03-12 07:57:29 -0500211 if (CONFIG_ANDROID_AB_BACKUP_OFFSET) {
212 ret = ab_control_create_from_disk(dev_desc, part_info, &backup_abc,
213 CONFIG_ANDROID_AB_BACKUP_OFFSET);
214 if (ret < 0) {
215 free(abc);
216 return ret;
217 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500218 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500219
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300220 crc32_le = ab_control_compute_crc(abc);
221 if (abc->crc32_le != crc32_le) {
222 log_err("ANDROID: Invalid CRC-32 (expected %.8x, found %.8x),",
223 crc32_le, abc->crc32_le);
Colin McAllister61aa03c2024-03-12 07:57:29 -0500224 if (CONFIG_ANDROID_AB_BACKUP_OFFSET) {
225 crc32_le = ab_control_compute_crc(backup_abc);
226 if (backup_abc->crc32_le != crc32_le) {
227 log_err(" ANDROID: Invalid backup CRC-32 ");
228 log_err("(expected %.8x, found %.8x),",
229 crc32_le, backup_abc->crc32_le);
230 } else {
231 valid_backup = true;
232 log_info(" copying A/B metadata from backup.\n");
233 memcpy(abc, backup_abc, sizeof(*abc));
234 }
235 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500236
Colin McAllister61aa03c2024-03-12 07:57:29 -0500237 if (!valid_backup) {
238 log_err(" re-initializing A/B metadata.\n");
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500239 ret = ab_control_default(abc);
240 if (ret < 0) {
Colin McAllister61aa03c2024-03-12 07:57:29 -0500241 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
242 free(backup_abc);
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500243 free(abc);
244 return -ENODATA;
245 }
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300246 }
247 store_needed = true;
248 }
249
250 if (abc->magic != BOOT_CTRL_MAGIC) {
251 log_err("ANDROID: Unknown A/B metadata: %.8x\n", abc->magic);
Colin McAllister61aa03c2024-03-12 07:57:29 -0500252 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
253 free(backup_abc);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300254 free(abc);
255 return -ENODATA;
256 }
257
258 if (abc->version > BOOT_CTRL_VERSION) {
259 log_err("ANDROID: Unsupported A/B metadata version: %.8x\n",
260 abc->version);
Colin McAllister61aa03c2024-03-12 07:57:29 -0500261 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
262 free(backup_abc);
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300263 free(abc);
264 return -ENODATA;
265 }
266
267 /*
268 * At this point a valid boot control metadata is stored in abc,
269 * followed by other reserved data in the same block. We select a with
270 * the higher priority slot that
271 * - is not marked as corrupted and
272 * - either has tries_remaining > 0 or successful_boot is true.
273 * If the selected slot has a false successful_boot, we also decrement
274 * the tries_remaining until it eventually becomes unbootable because
275 * tries_remaining reaches 0. This mechanism produces a bootloader
276 * induced rollback, typically right after a failed update.
277 */
278
279 /* Safety check: limit the number of slots. */
280 if (abc->nb_slot > ARRAY_SIZE(abc->slot_info)) {
281 abc->nb_slot = ARRAY_SIZE(abc->slot_info);
282 store_needed = true;
283 }
284
285 slot = -1;
286 for (i = 0; i < abc->nb_slot; ++i) {
287 if (abc->slot_info[i].verity_corrupted ||
288 !abc->slot_info[i].tries_remaining) {
289 log_debug("ANDROID: unbootable slot %d tries: %d, ",
290 i, abc->slot_info[i].tries_remaining);
291 log_debug("corrupt: %d\n",
292 abc->slot_info[i].verity_corrupted);
293 continue;
294 }
295 log_debug("ANDROID: bootable slot %d pri: %d, tries: %d, ",
296 i, abc->slot_info[i].priority,
297 abc->slot_info[i].tries_remaining);
298 log_debug("corrupt: %d, successful: %d\n",
299 abc->slot_info[i].verity_corrupted,
300 abc->slot_info[i].successful_boot);
301
302 if (slot < 0 ||
303 ab_compare_slots(&abc->slot_info[i],
304 &abc->slot_info[slot]) < 0) {
305 slot = i;
306 }
307 }
308
309 if (slot >= 0 && !abc->slot_info[slot].successful_boot) {
310 log_err("ANDROID: Attempting slot %c, tries remaining %d\n",
311 BOOT_SLOT_NAME(slot),
312 abc->slot_info[slot].tries_remaining);
Joshua Watt9d5251c2023-06-23 17:05:48 -0500313 if (dec_tries) {
314 abc->slot_info[slot].tries_remaining--;
315 store_needed = true;
316 }
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300317 }
318
319 if (slot >= 0) {
320 /*
321 * Legacy user-space requires this field to be set in the BCB.
322 * Newer releases load this slot suffix from the command line
323 * or the device tree.
324 */
325 memset(slot_suffix, 0, sizeof(slot_suffix));
326 slot_suffix[0] = BOOT_SLOT_NAME(slot);
327 if (memcmp(abc->slot_suffix, slot_suffix,
328 sizeof(slot_suffix))) {
329 memcpy(abc->slot_suffix, slot_suffix,
330 sizeof(slot_suffix));
331 store_needed = true;
332 }
333 }
334
335 if (store_needed) {
336 abc->crc32_le = ab_control_compute_crc(abc);
Alexey Romanov8cf36232023-12-25 13:22:45 +0300337 ret = ab_control_store(dev_desc, part_info, abc, 0);
338 if (ret < 0) {
Colin McAllister61aa03c2024-03-12 07:57:29 -0500339 if (CONFIG_ANDROID_AB_BACKUP_OFFSET)
340 free(backup_abc);
Alexey Romanov8cf36232023-12-25 13:22:45 +0300341 free(abc);
342 return ret;
343 }
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300344 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500345
Colin McAllister61aa03c2024-03-12 07:57:29 -0500346 if (CONFIG_ANDROID_AB_BACKUP_OFFSET) {
347 /*
348 * If the backup doesn't match the primary, write the primary
349 * to the backup offset
350 */
351 if (memcmp(backup_abc, abc, sizeof(*abc)) != 0) {
352 ret = ab_control_store(dev_desc, part_info, abc,
353 CONFIG_ANDROID_AB_BACKUP_OFFSET);
354 if (ret < 0) {
355 free(backup_abc);
356 free(abc);
357 return ret;
358 }
Alexey Romanov8cf36232023-12-25 13:22:45 +0300359 }
Colin McAllister61aa03c2024-03-12 07:57:29 -0500360 free(backup_abc);
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500361 }
Joshua Wattad3ae7d2023-07-03 10:07:13 -0500362
Ruslan Trofymenko3b7dc912019-07-05 15:37:32 +0300363 free(abc);
364
365 if (slot < 0)
366 return -EINVAL;
367
368 return slot;
369}