cbfs: Remove prog_locate() for stages and rmodules

This patch removes the prog_locate() step for stages and rmodules.
Instead, the stage and rmodule loading functions will now perform the
locate step directly together with the actual loading. The long-term
goal of this is to eliminate prog_locate() (and the rdev member in
struct prog that it fills) completely in order to make CBFS verification
code safer and its security guarantees easier to follow. prog_locate()
is the main remaining use case where a raw rdev of CBFS file data
"leaks" out of cbfs.c into other code, and that other code needs to
manually make sure that the contents of the rdev get verified during
loading. By eliminating this step and moving all code that directly
deals with file data into cbfs.c, we can concentrate the code that needs
to worry about file data hashing (and needs access to cbfs_private.h
APIs) into one file, making it easier to keep track of and reason about.

This patch is the first step of this move, later patches will do the
same for SELFs and other program types.

Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: Ia600e55f77c2549a00e2606f09befc1f92594a3a
Reviewed-on: https://review.coreboot.org/c/coreboot/+/49335
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index 7df9dc6..a274551 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -382,19 +382,29 @@
 	return cbmem_add((uintptr_t)arg, size);
 }
 
-int cbfs_prog_stage_load(struct prog *pstage)
+cb_err_t cbfs_prog_stage_load(struct prog *pstage)
 {
+	union cbfs_mdata mdata;
+	struct region_device rdev;
 	struct cbfs_stage stage;
 	uint8_t *load;
 	void *entry;
 	size_t fsize;
 	size_t foffset;
-	const struct region_device *fh = prog_rdev(pstage);
+	cb_err_t err;
 
-	if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
-		return -1;
+	prog_locate_hook(pstage);
 
-	fsize = region_device_sz(fh);
+	if ((err = cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev)))
+		return err;
+
+	assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE);
+	pstage->cbfs_type = CBFS_TYPE_STAGE;
+
+	if (rdev_readat(&rdev, &stage, 0, sizeof(stage)) != sizeof(stage))
+		return CB_CBFS_IO;
+
+	fsize = region_device_sz(&rdev);
 	fsize -= sizeof(stage);
 	foffset = 0;
 	foffset += sizeof(stage);
@@ -416,16 +426,16 @@
 	 * that would hit this path initialize themselves. */
 	if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) &&
 	    !CONFIG(NO_XIP_EARLY_STAGES) && CONFIG(BOOT_DEVICE_MEMORY_MAPPED)) {
-		void *mapping = rdev_mmap(fh, foffset, fsize);
-		rdev_munmap(fh, mapping);
+		void *mapping = rdev_mmap(&rdev, foffset, fsize);
+		rdev_munmap(&rdev, mapping);
 		if (mapping == load)
 			goto out;
 	}
 
-	fsize = cbfs_stage_load_and_decompress(fh, foffset, fsize, load,
-					stage.memlen, stage.compression);
+	fsize = cbfs_stage_load_and_decompress(&rdev, foffset, fsize, load,
+					       stage.memlen, stage.compression);
 	if (!fsize)
-		return -1;
+		return CB_ERR;
 
 	/* Clear area not covered by file. */
 	memset(&load[fsize], 0, stage.memlen - fsize);
@@ -436,7 +446,7 @@
 	prog_set_area(pstage, load, stage.memlen);
 	prog_set_entry(pstage, entry, NULL);
 
-	return 0;
+	return CB_SUCCESS;
 }
 
 void cbfs_boot_device_find_mcache(struct cbfs_boot_device *cbd, uint32_t id)