From 1d25b7c4fb917c98fd17a1988d2f409adaba3117 Mon Sep 17 00:00:00 2001
From: wlq <wlq@rock-chips.com>
Date: Mon, 7 Jun 2021 08:59:28 +0000
Subject: [PATCH] avb: add embedded key

Change-Id: Ie7af7b979f3b52764e529ada11da119102b6fd2e
Signed-off-by: wlq <wlq@rock-chips.com>
---
 common/android_bootloader.c        |   1 +
 lib/avb/libavb_user/avb_ops_user.c | 134 +++++++++++++++++++++++------
 2 files changed, 110 insertions(+), 25 deletions(-)

diff --git a/common/android_bootloader.c b/common/android_bootloader.c
index 02a70dd997..4f752a716d 100644
--- a/common/android_bootloader.c
+++ b/common/android_bootloader.c
@@ -487,6 +487,7 @@ static AvbSlotVerifyResult android_slot_verify(char *boot_partname,
 	if (ops->read_is_device_unlocked(ops, (bool *)&unlocked) != AVB_IO_RESULT_OK)
 		printf("Error determining whether device is unlocked.\n");
 
+	unlocked = 0;
 	printf("read_is_device_unlocked() ops returned that device is %s\n",
 	       (unlocked & LOCK_MASK)? "UNLOCKED" : "LOCKED");
 
diff --git a/lib/avb/libavb_user/avb_ops_user.c b/lib/avb/libavb_user/avb_ops_user.c
index d2151c310d..18e6729ed7 100644
--- a/lib/avb/libavb_user/avb_ops_user.c
+++ b/lib/avb/libavb_user/avb_ops_user.c
@@ -185,6 +185,95 @@ static AvbIOResult write_to_partition(AvbOps *ops,
 	return AVB_IO_RESULT_OK;
 }
 
+unsigned char avb_root_pub[] = {
+  0x00, 0x00, 0x10, 0x00, 0x1f, 0x8e, 0x54, 0x9b, 0xb2, 0xda, 0x61, 0xf0,
+  0x8c, 0x2d, 0xf2, 0x94, 0x21, 0x45, 0x77, 0x33, 0x64, 0x64, 0xf2, 0x8e,
+  0x89, 0x9c, 0xeb, 0x52, 0x23, 0xda, 0xc1, 0xef, 0x0e, 0x15, 0xc4, 0xc9,
+  0x3b, 0x24, 0x8c, 0xbf, 0x4f, 0xcf, 0xe1, 0xf2, 0x0b, 0x0e, 0x26, 0x6e,
+  0x84, 0x2d, 0x55, 0x2f, 0x03, 0x07, 0xaa, 0xe4, 0x7a, 0x91, 0x08, 0x6f,
+  0xc8, 0xff, 0x5f, 0x7d, 0xe1, 0xe5, 0x5b, 0x72, 0x47, 0x96, 0x28, 0x88,
+  0x14, 0xe7, 0x12, 0xef, 0x56, 0xf1, 0x94, 0xed, 0x5a, 0xa9, 0x56, 0x05,
+  0xb7, 0x7a, 0x85, 0xf0, 0x0f, 0xea, 0x15, 0x5a, 0xee, 0x2e, 0x3a, 0x70,
+  0x87, 0x8e, 0xfe, 0x58, 0x43, 0xdf, 0x5a, 0x70, 0x84, 0x79, 0x7d, 0xa2,
+  0xf1, 0x11, 0xa9, 0x50, 0x58, 0x7f, 0xa1, 0xcd, 0x84, 0xcb, 0x50, 0xef,
+  0xf8, 0x1b, 0xa8, 0x1e, 0x93, 0x54, 0x16, 0x6d, 0x87, 0xb0, 0x98, 0x13,
+  0x7b, 0xf4, 0x75, 0x11, 0x76, 0x9d, 0x98, 0x67, 0xd3, 0xb6, 0x1b, 0xda,
+  0x55, 0xf5, 0x97, 0x6f, 0xd9, 0xd6, 0xff, 0xae, 0xd8, 0x90, 0xc1, 0x16,
+  0x6c, 0x7e, 0x4c, 0x67, 0xa0, 0x58, 0x6c, 0xf5, 0xd5, 0xa4, 0x0f, 0x80,
+  0x37, 0x42, 0x28, 0x42, 0x6f, 0x9e, 0x99, 0x28, 0xf6, 0x22, 0x9b, 0xde,
+  0xd1, 0x34, 0xc6, 0xc6, 0x05, 0xf7, 0x55, 0x00, 0xf3, 0x03, 0xc6, 0x93,
+  0x62, 0x29, 0x80, 0x54, 0x1f, 0x87, 0xb8, 0xea, 0xba, 0xa7, 0xf5, 0x31,
+  0x88, 0x9b, 0xb9, 0x18, 0x2c, 0x88, 0x02, 0x1a, 0x8b, 0xb1, 0x55, 0x47,
+  0x77, 0x5a, 0xa7, 0xc4, 0x29, 0xfa, 0x78, 0x62, 0x9c, 0x69, 0x10, 0x02,
+  0x7f, 0x2e, 0x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, 0xdd, 0x7b, 0x38, 0x33, 0xb5,
+  0x47, 0x35, 0xc3, 0x57, 0x3c, 0x3d, 0xb2, 0xe3, 0x9f, 0x78, 0x1a, 0xc1,
+  0xff, 0xa5, 0x79, 0x17, 0xfb, 0xb7, 0xf0, 0xc5, 0xb5, 0x17, 0x0e, 0xc4,
+  0x36, 0x53, 0x7b, 0xb7, 0x2c, 0x16, 0xc0, 0xf9, 0x67, 0xa7, 0xa8, 0x40,
+  0xc6, 0x60, 0xaa, 0x2e, 0xab, 0xcb, 0x4e, 0x6c, 0x04, 0x80, 0x9b, 0x89,
+  0x9e, 0xb4, 0x85, 0xdd, 0x82, 0xce, 0x6c, 0xb1, 0x11, 0xdc, 0xd5, 0x16,
+  0xf0, 0x3a, 0xf4, 0xf2, 0x4e, 0xcc, 0x50, 0x2d, 0x35, 0x83, 0x0d, 0x98,
+  0x4a, 0x1b, 0x0c, 0xe6, 0xf4, 0x1d, 0x7a, 0x0d, 0x75, 0x8c, 0x09, 0x9e
+};
+
 static AvbIOResult
 validate_vbmeta_public_key(AvbOps *ops,
 			   const uint8_t *public_key_data,
@@ -193,21 +282,18 @@ validate_vbmeta_public_key(AvbOps *ops,
 			   size_t public_key_metadata_length,
 			   bool *out_is_trusted)
 {
-/* remain AVB_VBMETA_PUBLIC_KEY_VALIDATE to compatible legacy code */
-#if defined(CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE) || \
-    defined(AVB_VBMETA_PUBLIC_KEY_VALIDATE)
-	if (out_is_trusted) {
-		avb_atx_validate_vbmeta_public_key(ops,
-						   public_key_data,
-						   public_key_length,
-						   public_key_metadata,
-						   public_key_metadata_length,
-						   out_is_trusted);
+
+	if (!public_key_length || !out_is_trusted || !public_key_data) {
+		printf("%s: Invalid parameters\n", __func__);
+		return AVB_IO_RESULT_ERROR_IO;
 	}
-#else
-	if (out_is_trusted)
+
+	*out_is_trusted = false;
+	if (public_key_length != sizeof(avb_root_pub))
+		return AVB_IO_RESULT_ERROR_IO;
+	if (memcmp(public_key_data, avb_root_pub, public_key_length) == 0)
 		*out_is_trusted = true;
-#endif
+
 	return AVB_IO_RESULT_OK;
 }
 
@@ -469,20 +555,18 @@ AvbIOResult validate_public_key_for_partition(AvbOps *ops,
 					      uint32_t *out_rollback_index_location)
 {
 /* remain AVB_VBMETA_PUBLIC_KEY_VALIDATE to compatible legacy code */
-#if defined(CONFIG_AVB_VBMETA_PUBLIC_KEY_VALIDATE) || \
-    defined(AVB_VBMETA_PUBLIC_KEY_VALIDATE)
-	if (out_is_trusted) {
-		avb_atx_validate_vbmeta_public_key(ops,
-						   public_key_data,
-						   public_key_length,
-						   public_key_metadata,
-						   public_key_metadata_length,
-						   out_is_trusted);
+	
+	if (!public_key_length || !out_is_trusted || !public_key_data) {
+		printf("%s: Invalid parameters\n", __func__);
+		return AVB_IO_RESULT_ERROR_IO;
 	}
-#else
-	if (out_is_trusted)
+
+	*out_is_trusted = false;
+	if (public_key_length != sizeof(avb_root_pub))
+		return AVB_IO_RESULT_ERROR_IO;
+	if (memcmp(public_key_data, avb_root_pub, public_key_length) == 0)
 		*out_is_trusted = true;
-#endif
+	
 	*out_rollback_index_location = 0;
 	return AVB_IO_RESULT_OK;
 }
-- 
2.17.1

