1
2
3
4
5#include <common.h>
6#include <android_ab.h>
7#include <android_bootloader_message.h>
8#include <blk.h>
9#include <log.h>
10#include <malloc.h>
11#include <part.h>
12#include <memalign.h>
13#include <linux/err.h>
14#include <u-boot/crc.h>
15#include <u-boot/crc.h>
16
17
18
19
20
21
22
23
24
25
26
27static uint32_t ab_control_compute_crc(struct bootloader_control *abc)
28{
29 return crc32(0, (void *)abc, offsetof(typeof(*abc), crc32_le));
30}
31
32
33
34
35
36
37
38
39
40
41
42
43static int ab_control_default(struct bootloader_control *abc)
44{
45 int i;
46 const struct slot_metadata metadata = {
47 .priority = 15,
48 .tries_remaining = 7,
49 .successful_boot = 0,
50 .verity_corrupted = 0,
51 .reserved = 0
52 };
53
54 if (!abc)
55 return -EFAULT;
56
57 memcpy(abc->slot_suffix, "a\0\0\0", 4);
58 abc->magic = BOOT_CTRL_MAGIC;
59 abc->version = BOOT_CTRL_VERSION;
60 abc->nb_slot = NUM_SLOTS;
61 memset(abc->reserved0, 0, sizeof(abc->reserved0));
62 for (i = 0; i < abc->nb_slot; ++i)
63 abc->slot_info[i] = metadata;
64
65 memset(abc->reserved1, 0, sizeof(abc->reserved1));
66 abc->crc32_le = ab_control_compute_crc(abc);
67
68 return 0;
69}
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86static int ab_control_create_from_disk(struct blk_desc *dev_desc,
87 const struct disk_partition *part_info,
88 struct bootloader_control **abc)
89{
90 ulong abc_offset, abc_blocks, ret;
91
92 abc_offset = offsetof(struct bootloader_message_ab, slot_suffix);
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
126
127
128
129
130
131
132
133
134
135
136static int ab_control_store(struct blk_desc *dev_desc,
137 const struct disk_partition *part_info,
138 struct bootloader_control *abc)
139{
140 ulong abc_offset, abc_blocks, ret;
141
142 abc_offset = offsetof(struct bootloader_message_ab, slot_suffix) /
143 part_info->blksz;
144 abc_blocks = DIV_ROUND_UP(sizeof(struct bootloader_control),
145 part_info->blksz);
146 ret = blk_dwrite(dev_desc, part_info->start + abc_offset, abc_blocks,
147 abc);
148 if (IS_ERR_VALUE(ret)) {
149 log_err("ANDROID: Could not write back the misc partition\n");
150 return -EIO;
151 }
152
153 return 0;
154}
155
156
157
158
159
160
161
162
163
164
165
166static int ab_compare_slots(const struct slot_metadata *a,
167 const struct slot_metadata *b)
168{
169
170 if (a->priority != b->priority)
171 return b->priority - a->priority;
172
173
174 if (a->successful_boot != b->successful_boot)
175 return b->successful_boot - a->successful_boot;
176
177
178 if (a->tries_remaining != b->tries_remaining)
179 return b->tries_remaining - a->tries_remaining;
180
181 return 0;
182}
183
184int ab_select_slot(struct blk_desc *dev_desc, struct disk_partition *part_info)
185{
186 struct bootloader_control *abc = NULL;
187 u32 crc32_le;
188 int slot, i, ret;
189 bool store_needed = false;
190 char slot_suffix[4];
191
192 ret = ab_control_create_from_disk(dev_desc, part_info, &abc);
193 if (ret < 0) {
194
195
196
197
198
199 return ret;
200 }
201
202 crc32_le = ab_control_compute_crc(abc);
203 if (abc->crc32_le != crc32_le) {
204 log_err("ANDROID: Invalid CRC-32 (expected %.8x, found %.8x),",
205 crc32_le, abc->crc32_le);
206 log_err("re-initializing A/B metadata.\n");
207
208 ret = ab_control_default(abc);
209 if (ret < 0) {
210 free(abc);
211 return -ENODATA;
212 }
213 store_needed = true;
214 }
215
216 if (abc->magic != BOOT_CTRL_MAGIC) {
217 log_err("ANDROID: Unknown A/B metadata: %.8x\n", abc->magic);
218 free(abc);
219 return -ENODATA;
220 }
221
222 if (abc->version > BOOT_CTRL_VERSION) {
223 log_err("ANDROID: Unsupported A/B metadata version: %.8x\n",
224 abc->version);
225 free(abc);
226 return -ENODATA;
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 if (abc->nb_slot > ARRAY_SIZE(abc->slot_info)) {
243 abc->nb_slot = ARRAY_SIZE(abc->slot_info);
244 store_needed = true;
245 }
246
247 slot = -1;
248 for (i = 0; i < abc->nb_slot; ++i) {
249 if (abc->slot_info[i].verity_corrupted ||
250 !abc->slot_info[i].tries_remaining) {
251 log_debug("ANDROID: unbootable slot %d tries: %d, ",
252 i, abc->slot_info[i].tries_remaining);
253 log_debug("corrupt: %d\n",
254 abc->slot_info[i].verity_corrupted);
255 continue;
256 }
257 log_debug("ANDROID: bootable slot %d pri: %d, tries: %d, ",
258 i, abc->slot_info[i].priority,
259 abc->slot_info[i].tries_remaining);
260 log_debug("corrupt: %d, successful: %d\n",
261 abc->slot_info[i].verity_corrupted,
262 abc->slot_info[i].successful_boot);
263
264 if (slot < 0 ||
265 ab_compare_slots(&abc->slot_info[i],
266 &abc->slot_info[slot]) < 0) {
267 slot = i;
268 }
269 }
270
271 if (slot >= 0 && !abc->slot_info[slot].successful_boot) {
272 log_err("ANDROID: Attempting slot %c, tries remaining %d\n",
273 BOOT_SLOT_NAME(slot),
274 abc->slot_info[slot].tries_remaining);
275 abc->slot_info[slot].tries_remaining--;
276 store_needed = true;
277 }
278
279 if (slot >= 0) {
280
281
282
283
284
285 memset(slot_suffix, 0, sizeof(slot_suffix));
286 slot_suffix[0] = BOOT_SLOT_NAME(slot);
287 if (memcmp(abc->slot_suffix, slot_suffix,
288 sizeof(slot_suffix))) {
289 memcpy(abc->slot_suffix, slot_suffix,
290 sizeof(slot_suffix));
291 store_needed = true;
292 }
293 }
294
295 if (store_needed) {
296 abc->crc32_le = ab_control_compute_crc(abc);
297 ab_control_store(dev_desc, part_info, abc);
298 }
299 free(abc);
300
301 if (slot < 0)
302 return -EINVAL;
303
304 return slot;
305}
306