Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-2-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2017 The Android Open Source Project |
| 4 | */ |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 5 | #include <android_ab.h> |
| 6 | #include <android_bootloader_message.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 7 | #include <blk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 10 | #include <part.h> |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 11 | #include <memalign.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 12 | #include <linux/err.h> |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 13 | #include <u-boot/crc.h> |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 14 | |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 23 | * Return: crc32 sum |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 24 | */ |
| 25 | static 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 39 | * Return: 0 on success and a negative on error |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 40 | */ |
| 41 | static 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 82 | * Return: 0 on success and a negative on error |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 83 | */ |
| 84 | static int ab_control_create_from_disk(struct blk_desc *dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 85 | const struct disk_partition *part_info, |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 86 | struct bootloader_control **abc, |
| 87 | ulong offset) |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 88 | { |
| 89 | ulong abc_offset, abc_blocks, ret; |
| 90 | |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 91 | abc_offset = offset + |
| 92 | offsetof(struct bootloader_message_ab, slot_suffix); |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 93 | 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 134 | * Return: 0 on success and a negative on error |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 135 | */ |
| 136 | static int ab_control_store(struct blk_desc *dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 137 | const struct disk_partition *part_info, |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 138 | struct bootloader_control *abc, ulong offset) |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 139 | { |
| 140 | ulong abc_offset, abc_blocks, ret; |
| 141 | |
Joshua Watt | a16890e | 2024-08-28 08:37:57 -0600 | [diff] [blame] | 142 | 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 Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 149 | 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 169 | * Return: Negative if the slot "a" is better, positive of the slot "b" is |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 170 | * better or 0 if they are equally good. |
| 171 | */ |
| 172 | static 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 Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 190 | int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info, |
| 191 | bool dec_tries) |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 192 | { |
| 193 | struct bootloader_control *abc = NULL; |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 194 | struct bootloader_control *backup_abc = NULL; |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 195 | u32 crc32_le; |
| 196 | int slot, i, ret; |
| 197 | bool store_needed = false; |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 198 | bool valid_backup = false; |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 199 | char slot_suffix[4]; |
| 200 | |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 201 | ret = ab_control_create_from_disk(dev_desc, part_info, &abc, 0); |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 202 | 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 McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 211 | 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 Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 218 | } |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 219 | |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 220 | 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 McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 224 | 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 Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 236 | |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 237 | if (!valid_backup) { |
| 238 | log_err(" re-initializing A/B metadata.\n"); |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 239 | ret = ab_control_default(abc); |
| 240 | if (ret < 0) { |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 241 | if (CONFIG_ANDROID_AB_BACKUP_OFFSET) |
| 242 | free(backup_abc); |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 243 | free(abc); |
| 244 | return -ENODATA; |
| 245 | } |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 246 | } |
| 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 McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 252 | if (CONFIG_ANDROID_AB_BACKUP_OFFSET) |
| 253 | free(backup_abc); |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 254 | 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 McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 261 | if (CONFIG_ANDROID_AB_BACKUP_OFFSET) |
| 262 | free(backup_abc); |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 263 | 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 Watt | 9d5251c | 2023-06-23 17:05:48 -0500 | [diff] [blame] | 313 | if (dec_tries) { |
| 314 | abc->slot_info[slot].tries_remaining--; |
| 315 | store_needed = true; |
| 316 | } |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 317 | } |
| 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 Romanov | 8cf3623 | 2023-12-25 13:22:45 +0300 | [diff] [blame] | 337 | ret = ab_control_store(dev_desc, part_info, abc, 0); |
| 338 | if (ret < 0) { |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 339 | if (CONFIG_ANDROID_AB_BACKUP_OFFSET) |
| 340 | free(backup_abc); |
Alexey Romanov | 8cf3623 | 2023-12-25 13:22:45 +0300 | [diff] [blame] | 341 | free(abc); |
| 342 | return ret; |
| 343 | } |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 344 | } |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 345 | |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 346 | 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 Romanov | 8cf3623 | 2023-12-25 13:22:45 +0300 | [diff] [blame] | 359 | } |
Colin McAllister | 61aa03c | 2024-03-12 07:57:29 -0500 | [diff] [blame] | 360 | free(backup_abc); |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 361 | } |
Joshua Watt | ad3ae7d | 2023-07-03 10:07:13 -0500 | [diff] [blame] | 362 | |
Ruslan Trofymenko | 3b7dc91 | 2019-07-05 15:37:32 +0300 | [diff] [blame] | 363 | free(abc); |
| 364 | |
| 365 | if (slot < 0) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | return slot; |
| 369 | } |