blob: 19b1f2c377b9a51ff1683259085e1d636c939413 [file] [log] [blame]
Mattijs Korpershoekb30baa92024-07-10 10:40:05 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Bootmeth for Android
4 *
5 * Copyright (C) 2024 BayLibre, SAS
6 * Written by Mattijs Korpershoek <mkorpershoek@baylibre.com>
7 */
8#define LOG_CATEGORY UCLASS_BOOTSTD
9
10#include <android_ab.h>
11#include <android_image.h>
12#if CONFIG_IS_ENABLED(AVB_VERIFY)
13#include <avb_verify.h>
14#endif
15#include <bcb.h>
16#include <blk.h>
17#include <bootflow.h>
18#include <bootm.h>
19#include <bootmeth.h>
20#include <dm.h>
21#include <image.h>
22#include <malloc.h>
23#include <mapmem.h>
24#include <part.h>
Mattijs Korpershoekc818a712024-09-12 16:00:01 +020025#include <version.h>
Mattijs Korpershoekb30baa92024-07-10 10:40:05 +020026#include "bootmeth_android.h"
27
28#define BCB_FIELD_COMMAND_SZ 32
29#define BCB_PART_NAME "misc"
30#define BOOT_PART_NAME "boot"
31#define VENDOR_BOOT_PART_NAME "vendor_boot"
32
33/**
34 * struct android_priv - Private data
35 *
36 * This is read from the disk and recorded for use when the full Android
37 * kernel must be loaded and booted
38 *
39 * @boot_mode: Requested boot mode (normal, recovery, bootloader)
40 * @slot: Nul-terminated partition slot suffix read from BCB ("a\0" or "b\0")
41 * @header_version: Android boot image header version
42 */
43struct android_priv {
44 enum android_boot_mode boot_mode;
45 char slot[2];
46 u32 header_version;
47};
48
49static int android_check(struct udevice *dev, struct bootflow_iter *iter)
50{
51 /* This only works on mmc devices */
52 if (bootflow_iter_check_mmc(iter))
53 return log_msg_ret("mmc", -ENOTSUPP);
54
55 /*
56 * This only works on whole devices, as multiple
57 * partitions are needed to boot Android
58 */
59 if (iter->part != 0)
60 return log_msg_ret("mmc part", -ENOTSUPP);
61
62 return 0;
63}
64
65static int scan_boot_part(struct udevice *blk, struct android_priv *priv)
66{
67 struct blk_desc *desc = dev_get_uclass_plat(blk);
68 struct disk_partition partition;
69 char partname[PART_NAME_LEN];
70 ulong num_blks, bufsz;
71 char *buf;
72 int ret;
73
74 sprintf(partname, BOOT_PART_NAME "_%s", priv->slot);
75 ret = part_get_info_by_name(desc, partname, &partition);
76 if (ret < 0)
77 return log_msg_ret("part info", ret);
78
79 num_blks = DIV_ROUND_UP(sizeof(struct andr_boot_img_hdr_v0), desc->blksz);
80 bufsz = num_blks * desc->blksz;
81 buf = malloc(bufsz);
82 if (!buf)
83 return log_msg_ret("buf", -ENOMEM);
84
85 ret = blk_read(blk, partition.start, num_blks, buf);
86 if (ret != num_blks) {
87 free(buf);
88 return log_msg_ret("part read", -EIO);
89 }
90
91 if (!is_android_boot_image_header(buf)) {
92 free(buf);
93 return log_msg_ret("header", -ENOENT);
94 }
95
96 priv->header_version = ((struct andr_boot_img_hdr_v0 *)buf)->header_version;
97 free(buf);
98
99 return 0;
100}
101
102static int scan_vendor_boot_part(struct udevice *blk, struct android_priv *priv)
103{
104 struct blk_desc *desc = dev_get_uclass_plat(blk);
105 struct disk_partition partition;
106 char partname[PART_NAME_LEN];
107 ulong num_blks, bufsz;
108 char *buf;
109 int ret;
110
111 sprintf(partname, VENDOR_BOOT_PART_NAME "_%s", priv->slot);
112 ret = part_get_info_by_name(desc, partname, &partition);
113 if (ret < 0)
114 return log_msg_ret("part info", ret);
115
116 num_blks = DIV_ROUND_UP(sizeof(struct andr_vnd_boot_img_hdr), desc->blksz);
117 bufsz = num_blks * desc->blksz;
118 buf = malloc(bufsz);
119 if (!buf)
120 return log_msg_ret("buf", -ENOMEM);
121
122 ret = blk_read(blk, partition.start, num_blks, buf);
123 if (ret != num_blks) {
124 free(buf);
125 return log_msg_ret("part read", -EIO);
126 }
127
128 if (!is_android_vendor_boot_image_header(buf)) {
129 free(buf);
130 return log_msg_ret("header", -ENOENT);
131 }
132 free(buf);
133
134 return 0;
135}
136
137static int android_read_slot_from_bcb(struct bootflow *bflow, bool decrement)
138{
139 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
140 struct android_priv *priv = bflow->bootmeth_priv;
141 struct disk_partition misc;
142 char slot_suffix[3];
143 int ret;
144
145 ret = part_get_info_by_name(desc, BCB_PART_NAME, &misc);
146 if (ret < 0)
147 return log_msg_ret("part", ret);
148
149 ret = ab_select_slot(desc, &misc, decrement);
150 if (ret < 0)
151 return log_msg_ret("slot", ret);
152
153 priv->slot[0] = BOOT_SLOT_NAME(ret);
154 priv->slot[1] = '\0';
155
156 sprintf(slot_suffix, "_%s", priv->slot);
157 ret = bootflow_cmdline_set_arg(bflow, "androidboot.slot_suffix",
158 slot_suffix, false);
159 if (ret < 0)
160 return log_msg_ret("cmdl", ret);
161
162 return 0;
163}
164
165static int configure_serialno(struct bootflow *bflow)
166{
167 char *serialno = env_get("serial#");
168
169 if (!serialno)
170 return log_msg_ret("serial", -ENOENT);
171
172 return bootflow_cmdline_set_arg(bflow, "androidboot.serialno", serialno, false);
173}
174
Mattijs Korpershoekc818a712024-09-12 16:00:01 +0200175static int configure_bootloader_version(struct bootflow *bflow)
176{
177 return bootflow_cmdline_set_arg(bflow, "androidboot.bootloader",
178 PLAIN_VERSION, false);
179}
180
Mattijs Korpershoekb30baa92024-07-10 10:40:05 +0200181static int android_read_bootflow(struct udevice *dev, struct bootflow *bflow)
182{
183 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
184 struct disk_partition misc;
185 struct android_priv *priv;
186 char command[BCB_FIELD_COMMAND_SZ];
187 int ret;
188
189 bflow->state = BOOTFLOWST_MEDIA;
190
191 /*
192 * bcb_find_partition_and_load() will print errors to stdout
193 * if BCB_PART_NAME is not found. To avoid that, check if the
194 * partition exists first.
195 */
196 ret = part_get_info_by_name(desc, BCB_PART_NAME, &misc);
197 if (ret < 0)
198 return log_msg_ret("part", ret);
199
200 ret = bcb_find_partition_and_load("mmc", desc->devnum, BCB_PART_NAME);
201 if (ret < 0)
202 return log_msg_ret("bcb load", ret);
203
204 ret = bcb_get(BCB_FIELD_COMMAND, command, sizeof(command));
205 if (ret < 0)
206 return log_msg_ret("bcb read", ret);
207
208 priv = malloc(sizeof(struct android_priv));
209 if (!priv)
210 return log_msg_ret("buf", -ENOMEM);
211
212 if (!strcmp("bootonce-bootloader", command)) {
213 priv->boot_mode = ANDROID_BOOT_MODE_BOOTLOADER;
214 bflow->os_name = strdup("Android (bootloader)");
215 } else if (!strcmp("boot-fastboot", command)) {
216 priv->boot_mode = ANDROID_BOOT_MODE_RECOVERY;
217 bflow->os_name = strdup("Android (fastbootd)");
218 } else if (!strcmp("boot-recovery", command)) {
219 priv->boot_mode = ANDROID_BOOT_MODE_RECOVERY;
220 bflow->os_name = strdup("Android (recovery)");
221 } else {
222 priv->boot_mode = ANDROID_BOOT_MODE_NORMAL;
223 bflow->os_name = strdup("Android");
224 }
225 if (!bflow->os_name)
226 return log_msg_ret("os", -ENOMEM);
227
228 if (priv->boot_mode == ANDROID_BOOT_MODE_BOOTLOADER) {
229 /* Clear BCB */
230 memset(command, 0, sizeof(command));
231 ret = bcb_set(BCB_FIELD_COMMAND, command);
232 if (ret < 0) {
233 free(priv);
234 return log_msg_ret("bcb set", ret);
235 }
236 ret = bcb_store();
237 if (ret < 0) {
238 free(priv);
239 return log_msg_ret("bcb store", ret);
240 }
241
242 bflow->bootmeth_priv = priv;
243 bflow->state = BOOTFLOWST_READY;
244 return 0;
245 }
246
247 bflow->bootmeth_priv = priv;
248
249 /* For recovery and normal boot, we need to scan the partitions */
250 ret = android_read_slot_from_bcb(bflow, false);
251 if (ret < 0) {
252 log_err("read slot: %d", ret);
253 goto free_priv;
254 }
255
256 ret = scan_boot_part(bflow->blk, priv);
257 if (ret < 0) {
258 log_debug("scan boot failed: err=%d\n", ret);
259 goto free_priv;
260 }
261
262 if (priv->header_version != 4) {
263 log_debug("only boot.img v4 is supported %u\n", priv->header_version);
264 ret = -EINVAL;
265 goto free_priv;
266 }
267
268 ret = scan_vendor_boot_part(bflow->blk, priv);
269 if (ret < 0) {
270 log_debug("scan vendor_boot failed: err=%d\n", ret);
271 goto free_priv;
272 }
273
Mattijs Korpershoekc818a712024-09-12 16:00:01 +0200274 /*
275 * Ignoring return code for the following configurations:
276 * these are not mandatory for booting.
277 */
Mattijs Korpershoekb30baa92024-07-10 10:40:05 +0200278 configure_serialno(bflow);
Mattijs Korpershoekc818a712024-09-12 16:00:01 +0200279 configure_bootloader_version(bflow);
Mattijs Korpershoekb30baa92024-07-10 10:40:05 +0200280
281 if (priv->boot_mode == ANDROID_BOOT_MODE_NORMAL) {
282 ret = bootflow_cmdline_set_arg(bflow, "androidboot.force_normal_boot",
283 "1", false);
284 if (ret < 0) {
285 log_debug("normal_boot %d", ret);
286 goto free_priv;
287 }
288 }
289
290 bflow->state = BOOTFLOWST_READY;
291
292 return 0;
293
294 free_priv:
295 free(priv);
296 bflow->bootmeth_priv = NULL;
297 return ret;
298}
299
300static int android_read_file(struct udevice *dev, struct bootflow *bflow,
301 const char *file_path, ulong addr, ulong *sizep)
302{
303 /*
304 * Reading individual files is not supported since we only
305 * operate on whole mmc devices (because we require multiple partitions)
306 */
307 return log_msg_ret("Unsupported", -ENOSYS);
308}
309
310/**
311 * read_slotted_partition() - Read a partition by appending a slot suffix
312 *
313 * Most modern Android devices use Seamless Updates, where each partition
314 * is duplicated. For example, the boot partition has boot_a and boot_b.
315 * For more information, see:
316 * https://source.android.com/docs/core/ota/ab
317 * https://source.android.com/docs/core/ota/ab/ab_implement
318 *
319 * @blk: Block device to read
320 * @name: Partition name to read
321 * @slot: Nul-terminated slot suffixed to partition name ("a\0" or "b\0")
322 * @addr: Address where the partition content is loaded into
323 * Return: 0 if OK, negative errno on failure.
324 */
325static int read_slotted_partition(struct blk_desc *desc, const char *const name,
326 const char slot[2], ulong addr)
327{
328 struct disk_partition partition;
329 char partname[PART_NAME_LEN];
330 int ret;
331 u32 n;
332
333 /* Ensure name fits in partname it should be: <name>_<slot>\0 */
334 if (strlen(name) > (PART_NAME_LEN - 2 - 1))
335 return log_msg_ret("name too long", -EINVAL);
336
337 sprintf(partname, "%s_%s", name, slot);
338 ret = part_get_info_by_name(desc, partname, &partition);
339 if (ret < 0)
340 return log_msg_ret("part", ret);
341
342 n = blk_dread(desc, partition.start, partition.size, map_sysmem(addr, 0));
343 if (n < partition.size)
344 return log_msg_ret("part read", -EIO);
345
346 return 0;
347}
348
349#if CONFIG_IS_ENABLED(AVB_VERIFY)
350static int avb_append_commandline_arg(struct bootflow *bflow, char *arg)
351{
352 char *key = strsep(&arg, "=");
353 char *value = arg;
354 int ret;
355
356 ret = bootflow_cmdline_set_arg(bflow, key, value, false);
357 if (ret < 0)
358 return log_msg_ret("avb cmdline", ret);
359
360 return 0;
361}
362
363static int avb_append_commandline(struct bootflow *bflow, char *cmdline)
364{
365 char *arg = strsep(&cmdline, " ");
366 int ret;
367
368 while (arg) {
369 ret = avb_append_commandline_arg(bflow, arg);
370 if (ret < 0)
371 return ret;
372
373 arg = strsep(&cmdline, " ");
374 }
375
376 return 0;
377}
378
379static int run_avb_verification(struct bootflow *bflow)
380{
381 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
382 struct android_priv *priv = bflow->bootmeth_priv;
383 const char * const requested_partitions[] = {"boot", "vendor_boot"};
384 struct AvbOps *avb_ops;
385 AvbSlotVerifyResult result;
386 AvbSlotVerifyData *out_data;
387 enum avb_boot_state boot_state;
388 char *extra_args;
389 char slot_suffix[3];
390 bool unlocked = false;
391 int ret;
392
393 avb_ops = avb_ops_alloc(desc->devnum);
394 if (!avb_ops)
395 return log_msg_ret("avb ops", -ENOMEM);
396
397 sprintf(slot_suffix, "_%s", priv->slot);
398
399 ret = avb_ops->read_is_device_unlocked(avb_ops, &unlocked);
400 if (ret != AVB_IO_RESULT_OK)
401 return log_msg_ret("avb lock", -EIO);
402
403 result = avb_slot_verify(avb_ops,
404 requested_partitions,
405 slot_suffix,
406 unlocked,
407 AVB_HASHTREE_ERROR_MODE_RESTART_AND_INVALIDATE,
408 &out_data);
409
410 if (result != AVB_SLOT_VERIFY_RESULT_OK) {
411 printf("Verification failed, reason: %s\n",
412 str_avb_slot_error(result));
413 avb_slot_verify_data_free(out_data);
414 return log_msg_ret("avb verify", -EIO);
415 }
416
417 if (unlocked)
418 boot_state = AVB_ORANGE;
419 else
420 boot_state = AVB_GREEN;
421
422 extra_args = avb_set_state(avb_ops, boot_state);
423 if (extra_args) {
424 /* extra_args will be modified after this. This is fine */
425 ret = avb_append_commandline_arg(bflow, extra_args);
426 if (ret < 0)
427 goto free_out_data;
428 }
429
430 ret = avb_append_commandline(bflow, out_data->cmdline);
431 if (ret < 0)
432 goto free_out_data;
433
434 return 0;
435
436 free_out_data:
437 if (out_data)
438 avb_slot_verify_data_free(out_data);
439
440 return log_msg_ret("avb cmdline", ret);
441}
442#else
443static int run_avb_verification(struct bootflow *bflow)
444{
445 int ret;
446
447 /* When AVB is unsupported, pass ORANGE state */
448 ret = bootflow_cmdline_set_arg(bflow,
449 "androidboot.verifiedbootstate",
450 "orange", false);
451 if (ret < 0)
452 return log_msg_ret("avb cmdline", ret);
453
454 return 0;
455}
456#endif /* AVB_VERIFY */
457
458static int boot_android_normal(struct bootflow *bflow)
459{
460 struct blk_desc *desc = dev_get_uclass_plat(bflow->blk);
461 struct android_priv *priv = bflow->bootmeth_priv;
462 int ret;
463 ulong loadaddr = env_get_hex("loadaddr", 0);
464 ulong vloadaddr = env_get_hex("vendor_boot_comp_addr_r", 0);
465
466 ret = run_avb_verification(bflow);
467 if (ret < 0)
468 return log_msg_ret("avb", ret);
469
470 /* Read slot once more to decrement counter from BCB */
471 ret = android_read_slot_from_bcb(bflow, true);
472 if (ret < 0)
473 return log_msg_ret("read slot", ret);
474
475 ret = read_slotted_partition(desc, "boot", priv->slot, loadaddr);
476 if (ret < 0)
477 return log_msg_ret("read boot", ret);
478
479 ret = read_slotted_partition(desc, "vendor_boot", priv->slot, vloadaddr);
480 if (ret < 0)
481 return log_msg_ret("read vendor_boot", ret);
482
483 set_abootimg_addr(loadaddr);
484 set_avendor_bootimg_addr(vloadaddr);
485
486 ret = bootm_boot_start(loadaddr, bflow->cmdline);
487
488 return log_msg_ret("boot", ret);
489}
490
491static int boot_android_recovery(struct bootflow *bflow)
492{
493 int ret;
494
495 ret = boot_android_normal(bflow);
496
497 return log_msg_ret("boot", ret);
498}
499
500static int boot_android_bootloader(struct bootflow *bflow)
501{
502 int ret;
503
504 ret = run_command("fastboot usb 0", 0);
505 do_reset(NULL, 0, 0, NULL);
506
507 return log_msg_ret("boot", ret);
508}
509
510static int android_boot(struct udevice *dev, struct bootflow *bflow)
511{
512 struct android_priv *priv = bflow->bootmeth_priv;
513 int ret;
514
515 switch (priv->boot_mode) {
516 case ANDROID_BOOT_MODE_NORMAL:
517 ret = boot_android_normal(bflow);
518 break;
519 case ANDROID_BOOT_MODE_RECOVERY:
520 ret = boot_android_recovery(bflow);
521 break;
522 case ANDROID_BOOT_MODE_BOOTLOADER:
523 ret = boot_android_bootloader(bflow);
524 break;
525 default:
526 printf("ANDROID: Unknown boot mode %d. Running fastboot...\n",
527 priv->boot_mode);
528 boot_android_bootloader(bflow);
529 /* Tell we failed to boot since boot mode is unknown */
530 ret = -EFAULT;
531 }
532
533 return ret;
534}
535
536static int android_bootmeth_bind(struct udevice *dev)
537{
538 struct bootmeth_uc_plat *plat = dev_get_uclass_plat(dev);
539
540 plat->desc = "Android boot";
541 plat->flags = BOOTMETHF_ANY_PART;
542
543 return 0;
544}
545
546static struct bootmeth_ops android_bootmeth_ops = {
547 .check = android_check,
548 .read_bootflow = android_read_bootflow,
549 .read_file = android_read_file,
550 .boot = android_boot,
551};
552
553static const struct udevice_id android_bootmeth_ids[] = {
554 { .compatible = "u-boot,android" },
555 { }
556};
557
558U_BOOT_DRIVER(bootmeth_android) = {
559 .name = "bootmeth_android",
560 .id = UCLASS_BOOTMETH,
561 .of_match = android_bootmeth_ids,
562 .ops = &android_bootmeth_ops,
563 .bind = android_bootmeth_bind,
564};