hc
2023-11-06 9df731a176aab8e03b984b681b1bea01ccff6644
u-boot/lib/avb/libavb_user/avb_ops_user.c
....@@ -81,10 +81,9 @@
8181 return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
8282 }
8383
84
- if (part_get_info_by_name(dev_desc, partition, &part_info) < 0) {
85
- printf("Could not find \"%s\" partition\n", partition);
84
+ if (part_get_info_by_name(dev_desc, partition, &part_info) < 0)
8685 return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
87
- }
86
+
8887 *out_size_in_bytes = (part_info.size) * 512;
8988 return AVB_IO_RESULT_OK;
9089 }
....@@ -281,6 +280,7 @@
281280 {
282281 if (out_is_unlocked) {
283282 #ifdef CONFIG_OPTEE_CLIENT
283
+ uint8_t vboot_flag = 0;
284284 int ret;
285285
286286 ret = trusty_read_lock_state((uint8_t *)out_is_unlocked);
....@@ -291,7 +291,16 @@
291291 case TEE_ERROR_GENERIC:
292292 case TEE_ERROR_NO_DATA:
293293 case TEE_ERROR_ITEM_NOT_FOUND:
294
- *out_is_unlocked = 1;
294
+ if (trusty_read_vbootkey_enable_flag(&vboot_flag)) {
295
+ printf("Can't read vboot flag\n");
296
+ return AVB_IO_RESULT_ERROR_IO;
297
+ }
298
+
299
+ if (vboot_flag)
300
+ *out_is_unlocked = 0;
301
+ else
302
+ *out_is_unlocked = 1;
303
+
295304 if (trusty_write_lock_state(*out_is_unlocked)) {
296305 printf("%s: init lock state error\n", __FILE__);
297306 ret = AVB_IO_RESULT_ERROR_IO;
....@@ -429,30 +438,111 @@
429438 size_t* out_num_bytes_preloaded,
430439 int allow_verification_error)
431440 {
441
+ struct preloaded_partition *preload_info = NULL;
442
+ struct AvbOpsData *data = ops->user_data;
432443 struct blk_desc *dev_desc;
444
+ disk_partition_t part_info;
433445 ulong load_addr;
434
- int ret;
446
+ AvbIOResult ret;
447
+ int full_preload = 0;
435448
436
- /* no need go further */
437
- if (!allow_verification_error)
438
- return AVB_IO_RESULT_OK;
439
-
440
- printf("get image from preloaded partition...\n");
441449 dev_desc = rockchip_get_bootdev();
442450 if (!dev_desc)
443
- return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
451
+ return AVB_IO_RESULT_ERROR_IO;
444452
445
- load_addr = env_get_ulong("kernel_addr_r", 16, 0);
446
- if (!load_addr)
447
- return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
453
+ if (part_get_info_by_name(dev_desc, partition, &part_info) < 0) {
454
+ printf("Could not find \"%s\" partition\n", partition);
455
+ return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
456
+ }
448457
449
- ret = android_image_load_by_partname(dev_desc, partition, &load_addr);
450
- if (!ret) {
451
- *out_pointer = (u8 *)load_addr;
452
- *out_num_bytes_preloaded = num_bytes; /* return what it expects */
458
+ if (!allow_verification_error) {
459
+ if (!strncmp(partition, ANDROID_PARTITION_BOOT, 4) ||
460
+ !strncmp(partition, ANDROID_PARTITION_RECOVERY, 8))
461
+ preload_info = &data->boot;
462
+ else if (!strncmp(partition, ANDROID_PARTITION_VENDOR_BOOT, 11))
463
+ preload_info = &data->vendor_boot;
464
+ else if (!strncmp(partition, ANDROID_PARTITION_INIT_BOOT, 9))
465
+ preload_info = &data->init_boot;
466
+ else if (!strncmp(partition, ANDROID_PARTITION_RESOURCE, 8))
467
+ preload_info = &data->resource;
468
+
469
+ if (!preload_info) {
470
+ printf("Error: unknown full load partition '%s'\n", partition);
471
+ return AVB_IO_RESULT_ERROR_NO_SUCH_PARTITION;
472
+ }
473
+
474
+ printf("preloaded(s): %sfull image from '%s' at 0x%08lx - 0x%08lx\n",
475
+ preload_info->size ? "pre-" : "", partition,
476
+ (ulong)preload_info->addr,
477
+ (ulong)preload_info->addr + num_bytes);
478
+
479
+ /* If the partition hasn't yet been preloaded, do it now.*/
480
+ if (preload_info->size == 0) {
481
+ ret = ops->read_from_partition(ops, partition,
482
+ 0, num_bytes,
483
+ preload_info->addr,
484
+ &preload_info->size);
485
+ if (ret != AVB_IO_RESULT_OK)
486
+ return ret;
487
+ }
488
+ *out_pointer = preload_info->addr;
489
+ *out_num_bytes_preloaded = preload_info->size;
453490 ret = AVB_IO_RESULT_OK;
454491 } else {
455
- ret = AVB_IO_RESULT_ERROR_IO;
492
+ if (!strncmp(partition, ANDROID_PARTITION_INIT_BOOT, 9) ||
493
+ !strncmp(partition, ANDROID_PARTITION_VENDOR_BOOT, 11) ||
494
+ !strncmp(partition, ANDROID_PARTITION_BOOT, 4) ||
495
+ !strncmp(partition, ANDROID_PARTITION_RECOVERY, 8) ||
496
+ !strncmp(partition, ANDROID_PARTITION_RESOURCE, 8)) {
497
+ /* If already full preloaded, just use it */
498
+ if (!strncmp(partition, ANDROID_PARTITION_BOOT, 4) ||
499
+ !strncmp(partition, ANDROID_PARTITION_RECOVERY, 8)) {
500
+ preload_info = &data->boot;
501
+ if (preload_info->size) {
502
+ *out_pointer = preload_info->addr;
503
+ *out_num_bytes_preloaded = num_bytes;
504
+ full_preload = 1;
505
+ }
506
+ }
507
+ printf("preloaded: %s image from '%s\n",
508
+ full_preload ? "pre-full" : "distribute", partition);
509
+ } else {
510
+ printf("Error: unknown preloaded partition '%s'\n", partition);
511
+ return AVB_IO_RESULT_ERROR_OOM;
512
+ }
513
+
514
+ /*
515
+ * Already preloaded during boot/recovery loading,
516
+ * here we just return a dummy buffer.
517
+ */
518
+ if (!strncmp(partition, ANDROID_PARTITION_INIT_BOOT, 9) ||
519
+ !strncmp(partition, ANDROID_PARTITION_VENDOR_BOOT, 11) ||
520
+ !strncmp(partition, ANDROID_PARTITION_RESOURCE, 8)) {
521
+ *out_pointer = (u8 *)avb_malloc(ARCH_DMA_MINALIGN);
522
+ *out_num_bytes_preloaded = num_bytes; /* return what it expects */
523
+ return AVB_IO_RESULT_OK;
524
+ }
525
+
526
+ /* If already full preloaded, there is nothing to do and just return */
527
+ if (full_preload)
528
+ return AVB_IO_RESULT_OK;
529
+
530
+ /*
531
+ * only boot/recovery partition can reach here
532
+ * and init/vendor_boot are loaded at this round.
533
+ */
534
+ load_addr = env_get_ulong("kernel_addr_r", 16, 0);
535
+ if (!load_addr)
536
+ return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE;
537
+
538
+ ret = android_image_load_by_partname(dev_desc, partition, &load_addr);
539
+ if (!ret) {
540
+ *out_pointer = (u8 *)load_addr;
541
+ *out_num_bytes_preloaded = num_bytes; /* return what it expects */
542
+ ret = AVB_IO_RESULT_OK;
543
+ } else {
544
+ ret = AVB_IO_RESULT_ERROR_IO;
545
+ }
456546 }
457547
458548 return ret;
....@@ -489,7 +579,8 @@
489579
490580 AvbOps *avb_ops_user_new(void)
491581 {
492
- AvbOps *ops;
582
+ AvbOps *ops = NULL;
583
+ struct AvbOpsData *ops_data = NULL;
493584
494585 ops = calloc(1, sizeof(AvbOps));
495586 if (!ops) {
....@@ -510,8 +601,20 @@
510601 free(ops);
511602 goto out;
512603 }
604
+
605
+ ops_data = calloc(1, sizeof(struct AvbOpsData));
606
+ if (!ops_data) {
607
+ printf("Error allocating memory for AvbOpsData.\n");
608
+ free(ops->atx_ops);
609
+ free(ops->ab_ops);
610
+ free(ops);
611
+ goto out;
612
+ }
613
+
513614 ops->ab_ops->ops = ops;
514615 ops->atx_ops->ops = ops;
616
+ ops_data->ops = ops;
617
+ ops->user_data = ops_data;
515618
516619 ops->read_from_partition = read_from_partition;
517620 ops->write_to_partition = write_to_partition;
....@@ -533,12 +636,14 @@
533636 ops->atx_ops->set_key_version = avb_set_key_version;
534637 ops->atx_ops->get_random = rk_get_random;
535638
536
-out:
537639 return ops;
640
+out:
641
+ return NULL;
538642 }
539643
540644 void avb_ops_user_free(AvbOps *ops)
541645 {
646
+ free(ops->user_data);
542647 free(ops->ab_ops);
543648 free(ops->atx_ops);
544649 free(ops);