vboot/vboot_kernel: pass VbDiskInfo into LoadKernel

Pass VbDiskInfo struct into LoadKernel, rather than copying all
of its members into LoadKernelParams.  Remove the unused members
from LoadKernelParams.

This CL is part of a series to merge vboot1 and vboot2.0
kernel verification code; see b/181739551.

BUG=b:181739551
TEST=make clean && make runtests
BRANCH=none

Signed-off-by: Joel Kitching <kitching@google.com>
Change-Id: I60957426388c88b16e570b717addb5eaf65b5e4f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2846281
Reviewed-by: Joel Kitching <kitching@chromium.org>
Tested-by: Joel Kitching <kitching@chromium.org>
Commit-Queue: Joel Kitching <kitching@chromium.org>
diff --git a/firmware/lib/include/load_kernel_fw.h b/firmware/lib/include/load_kernel_fw.h
index 9e4db8e..b4661c3 100644
--- a/firmware/lib/include/load_kernel_fw.h
+++ b/firmware/lib/include/load_kernel_fw.h
@@ -15,26 +15,14 @@
 
 /* Interface provided by verified boot library to BDS */
 
-/* Boot flags for LoadKernel().boot_flags */
-/* GPT is external */
-#define BOOT_FLAG_EXTERNAL_GPT (0x04ULL)
-
 typedef struct LoadKernelParams {
 	/* Inputs to LoadKernel() */
 	/* Disk handle for current device */
 	VbExDiskHandle_t disk_handle;
-	/* Bytes per lba sector on current device */
-	uint64_t bytes_per_lba;
-	/* Number of LBA-addressable sectors on the main device */
-	uint64_t streaming_lba_count;
-	/* Random-access GPT size */
-	uint64_t gpt_lba_count;
 	/* Destination buffer for kernel (normally at 0x100000) */
 	void *kernel_buffer;
 	/* Size of kernel buffer in bytes */
 	uint64_t kernel_buffer_size;
-	/* Boot flags */
-	uint64_t boot_flags;
 
 	/*
 	 * Outputs from LoadKernel(); valid only if LoadKernel() returns
@@ -60,6 +48,7 @@
  *
  * Returns VB2_SUCCESS if successful.  If unsuccessful, returns an error code.
  */
-vb2_error_t LoadKernel(struct vb2_context *ctx, LoadKernelParams *params);
+vb2_error_t LoadKernel(struct vb2_context *ctx, LoadKernelParams *params,
+		       VbDiskInfo *disk_info);
 
 #endif  /* VBOOT_REFERENCE_LOAD_KERNEL_FW_H_ */
diff --git a/firmware/lib/vboot_api_kernel.c b/firmware/lib/vboot_api_kernel.c
index 5451033..f1e191a 100644
--- a/firmware/lib/vboot_api_kernel.c
+++ b/firmware/lib/vboot_api_kernel.c
@@ -91,20 +91,14 @@
 				  disk_info[i].flags);
 			continue;
 		}
-		lkp.disk_handle = disk_info[i].handle;
-		lkp.bytes_per_lba = disk_info[i].bytes_per_lba;
-		lkp.gpt_lba_count = disk_info[i].lba_count;
-		lkp.streaming_lba_count = disk_info[i].streaming_lba_count
-						?: lkp.gpt_lba_count;
-		lkp.boot_flags |= disk_info[i].flags & VB_DISK_FLAG_EXTERNAL_GPT
-				? BOOT_FLAG_EXTERNAL_GPT : 0;
 
-		vb2_error_t new_rv = LoadKernel(ctx, &lkp);
+		lkp.disk_handle = disk_info[i].handle;
+		vb2_error_t new_rv = LoadKernel(ctx, &lkp, &disk_info[i]);
 		VB2_DEBUG("LoadKernel() = %#x\n", new_rv);
 
 		/* Stop now if we found a kernel. */
 		if (VB2_SUCCESS == new_rv) {
-			VbExDiskFreeInfo(disk_info, lkp.disk_handle);
+			VbExDiskFreeInfo(disk_info, disk_info[i].handle);
 			return VB2_SUCCESS;
 		}
 
diff --git a/firmware/lib/vboot_kernel.c b/firmware/lib/vboot_kernel.c
index 53e8b12..75b40c4 100644
--- a/firmware/lib/vboot_kernel.c
+++ b/firmware/lib/vboot_kernel.c
@@ -456,7 +456,8 @@
 	return VB2_SUCCESS;
 }
 
-vb2_error_t LoadKernel(struct vb2_context *ctx, LoadKernelParams *params)
+vb2_error_t LoadKernel(struct vb2_context *ctx, LoadKernelParams *params,
+		       VbDiskInfo *disk_info)
 {
 	struct vb2_shared_data *sd = vb2_get_sd(ctx);
 	int found_partitions = 0;
@@ -471,12 +472,13 @@
 
 	/* Read GPT data */
 	GptData gpt;
-	gpt.sector_bytes = (uint32_t)params->bytes_per_lba;
-	gpt.streaming_drive_sectors = params->streaming_lba_count;
-	gpt.gpt_drive_sectors = params->gpt_lba_count;
-	gpt.flags = params->boot_flags & BOOT_FLAG_EXTERNAL_GPT
+	gpt.sector_bytes = (uint32_t)disk_info->bytes_per_lba;
+	gpt.streaming_drive_sectors = disk_info->streaming_lba_count
+		?: disk_info->lba_count;
+	gpt.gpt_drive_sectors = disk_info->lba_count;
+	gpt.flags = disk_info->flags & VB_DISK_FLAG_EXTERNAL_GPT
 			? GPT_FLAG_EXTERNAL : 0;
-	if (AllocAndReadGptData(params->disk_handle, &gpt)) {
+	if (AllocAndReadGptData(disk_info->handle, &gpt)) {
 		VB2_DEBUG("Unable to read GPT data\n");
 		goto gpt_done;
 	}
@@ -501,7 +503,7 @@
 
 		/* Set up the stream */
 		VbExStream_t stream = NULL;
-		if (VbExStreamOpen(params->disk_handle,
+		if (VbExStreamOpen(disk_info->handle,
 				   part_start, part_size, &stream)) {
 			VB2_DEBUG("Partition error getting stream.\n");
 			VB2_DEBUG("Marking kernel as invalid.\n");
@@ -590,7 +592,7 @@
 
  gpt_done:
 	/* Write and free GPT data */
-	WriteAndFreeGptData(params->disk_handle, &gpt);
+	WriteAndFreeGptData(disk_info->handle, &gpt);
 
 	/* Handle finding a good partition */
 	if (params->partition_number > 0) {
diff --git a/tests/vboot_api_kernel_tests.c b/tests/vboot_api_kernel_tests.c
index bcb5d04..b5e1e7c 100644
--- a/tests/vboot_api_kernel_tests.c
+++ b/tests/vboot_api_kernel_tests.c
@@ -395,14 +395,15 @@
 	return VB2_SUCCESS;
 }
 
-vb2_error_t LoadKernel(struct vb2_context *c, LoadKernelParams *params)
+vb2_error_t LoadKernel(struct vb2_context *c, LoadKernelParams *params,
+		       VbDiskInfo *disk_info)
 {
 	got_find_disk = (const char *)params->disk_handle;
 	VB2_DEBUG("%s(%d): got_find_disk = %s\n", __FUNCTION__,
 		  load_kernel_calls,
 		  got_find_disk ? got_find_disk : "0");
 	if (t->external_expected[load_kernel_calls] !=
-			!!(params->boot_flags & BOOT_FLAG_EXTERNAL_GPT))
+			!!(disk_info->flags & VB_DISK_FLAG_EXTERNAL_GPT))
 		got_external_mismatch++;
 	return t->loadkernel_return_val[load_kernel_calls++];
 }
diff --git a/tests/vboot_kernel_tests.c b/tests/vboot_kernel_tests.c
index 7d088ae..6bfb3ae 100644
--- a/tests/vboot_kernel_tests.c
+++ b/tests/vboot_kernel_tests.c
@@ -55,6 +55,7 @@
 static struct vb2_gbb_header gbb;
 static VbExDiskHandle_t handle;
 static LoadKernelParams lkp;
+static VbDiskInfo disk_info;
 static struct vb2_keyblock kbh;
 static struct vb2_kernel_preamble kph;
 static struct vb2_secdata_fwmp *fwmp;
@@ -136,13 +137,16 @@
 	gbb.flags = 0;
 
 	memset(&lkp, 0, sizeof(lkp));
-	lkp.bytes_per_lba = 512;
-	lkp.streaming_lba_count = 1024;
-	lkp.gpt_lba_count = 1024;
 	lkp.kernel_buffer = kernel_buffer;
 	lkp.kernel_buffer_size = sizeof(kernel_buffer);
 	lkp.disk_handle = (VbExDiskHandle_t)1;
 
+	memset(&disk_info, 0, sizeof(disk_info));
+	disk_info.bytes_per_lba = 512;
+	disk_info.streaming_lba_count = 1024;
+	disk_info.lba_count = 1024;
+	disk_info.handle = lkp.disk_handle;
+
 	memset(&kbh, 0, sizeof(kbh));
 	kbh.data_key.key_version = 2;
 	kbh.keyblock_flags = -1;
@@ -592,7 +596,7 @@
 
 static void TestLoadKernel(int expect_retval, const char *test_name)
 {
-	TEST_EQ(LoadKernel(ctx, &lkp), expect_retval, test_name);
+	TEST_EQ(LoadKernel(ctx, &lkp, &disk_info), expect_retval, test_name);
 }
 
 /**
@@ -607,6 +611,7 @@
 	/* This causes the stream open call to fail */
 	ResetMocks();
 	lkp.disk_handle = NULL;
+	disk_info.handle = NULL;
 	TestLoadKernel(VB2_ERROR_LK_INVALID_KERNEL_FOUND, "Bad disk handle");
 }
 
@@ -861,7 +866,7 @@
 
 	/* Check that EXTERNAL_GPT flag makes it down */
 	ResetMocks();
-	lkp.boot_flags |= BOOT_FLAG_EXTERNAL_GPT;
+	disk_info.flags |= VB_DISK_FLAG_EXTERNAL_GPT;
 	TestLoadKernel(0, "Succeed external GPT");
 	TEST_EQ(gpt_flag_external, 1, "GPT was external");
 
diff --git a/tests/verify_kernel.c b/tests/verify_kernel.c
index fe68a1b..7c2ce91 100644
--- a/tests/verify_kernel.c
+++ b/tests/verify_kernel.c
@@ -24,15 +24,16 @@
 static uint8_t *diskbuf;
 
 static LoadKernelParams params;
+static VbDiskInfo disk_info;
 
 vb2_error_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start,
 			 uint64_t lba_count, void *buffer)
 {
 	if (handle != (VbExDiskHandle_t)1)
 		return VB2_ERROR_UNKNOWN;
-	if (lba_start >= params.streaming_lba_count)
+	if (lba_start >= disk_info.streaming_lba_count)
 		return VB2_ERROR_UNKNOWN;
-	if (lba_start + lba_count > params.streaming_lba_count)
+	if (lba_start + lba_count > disk_info.streaming_lba_count)
 		return VB2_ERROR_UNKNOWN;
 
 	memcpy(buffer, diskbuf + lba_start * 512, lba_count * 512);
@@ -44,9 +45,9 @@
 {
 	if (handle != (VbExDiskHandle_t)1)
 		return VB2_ERROR_UNKNOWN;
-	if (lba_start >= params.streaming_lba_count)
+	if (lba_start >= disk_info.streaming_lba_count)
 		return VB2_ERROR_UNKNOWN;
-	if (lba_start + lba_count > params.streaming_lba_count)
+	if (lba_start + lba_count > disk_info.streaming_lba_count)
 		return VB2_ERROR_UNKNOWN;
 
 	memcpy(diskbuf + lba_start * 512, buffer, lba_count * 512);
@@ -87,9 +88,10 @@
 
 	/* Set up params */
 	params.disk_handle = (VbExDiskHandle_t)1;
-	params.bytes_per_lba = 512;
-	params.streaming_lba_count = disk_bytes / 512;
-	params.gpt_lba_count = params.streaming_lba_count;
+	disk_info.handle = (VbExDiskHandle_t)1;
+	disk_info.bytes_per_lba = 512;
+	disk_info.streaming_lba_count = disk_bytes / 512;
+	disk_info.lba_count = disk_info.streaming_lba_count;
 
 	params.kernel_buffer_size = 16 * 1024 * 1024;
 	params.kernel_buffer = malloc(params.kernel_buffer_size);
@@ -99,7 +101,7 @@
 	}
 
 	/* TODO(chromium:441893): support dev-mode flag and external gpt flag */
-	params.boot_flags = 0;
+	disk_info.flags = 0;
 
 	if (vb2api_init(&workbuf, sizeof(workbuf), &ctx)) {
 		fprintf(stderr, "Can't initialize workbuf\n");
@@ -132,7 +134,7 @@
 	vb2_secdata_kernel_init(ctx);
 
 	/* Try loading kernel */
-	rv = LoadKernel(ctx, &params);
+	rv = LoadKernel(ctx, &params, &disk_info);
 	if (rv != VB2_SUCCESS) {
 		fprintf(stderr, "LoadKernel() failed with code %d\n", rv);
 		return 1;
diff --git a/utility/load_kernel_test.c b/utility/load_kernel_test.c
index c5dc7cb..c8f38f5 100644
--- a/utility/load_kernel_test.c
+++ b/utility/load_kernel_test.c
@@ -27,6 +27,7 @@
 
 /* Global variables for stub functions */
 static LoadKernelParams lkp;
+static VbDiskInfo disk_info;
 static FILE *image_file = NULL;
 
 
@@ -36,17 +37,19 @@
 {
 	printf("Read(%" PRIu64 ", %" PRIu64 ")\n", lba_start, lba_count);
 
-	if (lba_start >= lkp.streaming_lba_count ||
-	    lba_start + lba_count > lkp.streaming_lba_count) {
+	if (lba_start >= disk_info.streaming_lba_count ||
+	    lba_start + lba_count > disk_info.streaming_lba_count) {
 		fprintf(stderr,
 			"Read overrun: %" PRIu64 " + %" PRIu64
 			" > %" PRIu64 "\n", lba_start,
-			lba_count, lkp.streaming_lba_count);
+			lba_count, disk_info.streaming_lba_count);
 		return 1;
 	}
 
-	if (0 != fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET) ||
-	    1 != fread(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) {
+	if (0 != fseek(image_file, lba_start * disk_info.bytes_per_lba,
+		       SEEK_SET) ||
+	    1 != fread(buffer, lba_count * disk_info.bytes_per_lba, 1,
+		       image_file)) {
 		fprintf(stderr, "Read error.");
 		return 1;
 	}
@@ -59,12 +62,12 @@
 {
 	printf("Write(%" PRIu64 ", %" PRIu64 ")\n", lba_start, lba_count);
 
-	if (lba_start >= lkp.streaming_lba_count ||
-	    lba_start + lba_count > lkp.streaming_lba_count) {
+	if (lba_start >= disk_info.streaming_lba_count ||
+	    lba_start + lba_count > disk_info.streaming_lba_count) {
 		fprintf(stderr,
 			"Read overrun: %" PRIu64 " + %" PRIu64
 			" > %" PRIu64 "\n", lba_start, lba_count,
-			lkp.streaming_lba_count);
+			disk_info.streaming_lba_count);
 		return 1;
 	}
 
@@ -72,8 +75,9 @@
 	   our example file */
 	return VB2_SUCCESS;
 
-	fseek(image_file, lba_start * lkp.bytes_per_lba, SEEK_SET);
-	if (1 != fwrite(buffer, lba_count * lkp.bytes_per_lba, 1, image_file)) {
+	fseek(image_file, lba_start * disk_info.bytes_per_lba, SEEK_SET);
+	if (1 != fwrite(buffer, lba_count * disk_info.bytes_per_lba, 1,
+			image_file)) {
 		fprintf(stderr, "Read error.");
 		return 1;
 	}
@@ -97,7 +101,7 @@
 	char *e = 0;
 
 	memset(&lkp, 0, sizeof(LoadKernelParams));
-	lkp.bytes_per_lba = LBA_BYTES;
+	disk_info.bytes_per_lba = LBA_BYTES;
 	int boot_flags = BOOT_FLAG_RECOVERY;
 
 	/* Parse options */
@@ -186,7 +190,6 @@
 	}
 
 	printf("bootflags = %d\n", boot_flags);
-	lkp.boot_flags = boot_flags;
 
 	/* Get image size */
 	printf("Reading from image: %s\n", image_name);
@@ -196,10 +199,11 @@
 		return 1;
 	}
 	fseek(image_file, 0, SEEK_END);
-	lkp.streaming_lba_count = (ftell(image_file) / LBA_BYTES);
-	lkp.gpt_lba_count = lkp.streaming_lba_count;
+	disk_info.streaming_lba_count = (ftell(image_file) / LBA_BYTES);
+	disk_info.lba_count = disk_info.streaming_lba_count;
 	rewind(image_file);
-	printf("Streaming LBA count: %" PRIu64 "\n", lkp.streaming_lba_count);
+	printf("Streaming LBA count: %" PRIu64 "\n",
+	       disk_info.streaming_lba_count);
 
 	/* Allocate a buffer for the kernel */
 	lkp.kernel_buffer = malloc(KERNEL_BUFFER_SIZE);
@@ -241,7 +245,7 @@
 		ctx->flags |= VB2_CONTEXT_DEVELOPER_MODE;
 
 	/* Call LoadKernel() */
-	rv = LoadKernel(ctx, &lkp);
+	rv = LoadKernel(ctx, &lkp, &disk_info);
 	printf("LoadKernel() returned %d\n", rv);
 
 	if (VB2_SUCCESS == rv) {