libpayload/cbfs: Fill size_out even if cbfs_map() fails

When cbfs_map()/cbfs_ro_map() fails, the caller may still want to know
the decompressed size of the CBFS file, for example, to print an error
message. Move the assignment earlier in the flow. Note that coreboot's
cbfs_map() is already doing the same.

BUG=none
TEST=emerge-geralt libpayload
BRANCH=none

Change-Id: I82c6b7e69c95bf597fa3c7d37dd11252893c01af
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/77193
Reviewed-by: Julius Werner <jwerner@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/payloads/libpayload/libcbfs/cbfs.c b/payloads/libpayload/libcbfs/cbfs.c
index a158ba8..3dc19d2 100644
--- a/payloads/libpayload/libcbfs/cbfs.c
+++ b/payloads/libpayload/libcbfs/cbfs.c
@@ -151,6 +151,7 @@
 		     bool skip_verification)
 {
 	bool malloced = false;
+	size_t buf_size = 0;
 	size_t out_size;
 	uint32_t compression = CBFS_COMPRESS_NONE;
 	const struct cbfs_file_attr_compression *cattr =
@@ -162,8 +163,13 @@
 		out_size = be32toh(mdata->h.len);
 	}
 
+	if (size_inout) {
+		buf_size = *size_inout;
+		*size_inout = out_size;
+	}
+
 	if (buf) {
-		if (!size_inout || *size_inout < out_size) {
+		if (!size_inout || buf_size < out_size) {
 			ERROR("'%s' buffer too small\n", mdata->h.filename);
 			return NULL;
 		}
@@ -183,8 +189,6 @@
 			free(buf);
 		return NULL;
 	}
-	if (size_inout)
-		*size_inout = out_size;
 
 	return buf;
 }