util/ifittool: Error out if microcodes do not fit the FIT table

parse_microcode_blob() returns success when it reaches max_fit_entries
microcode. It makes the FIT table size verification in
fit_add_microcode_file() useless. This patch makes
parse_microcode_blob() error out if max_fit_entries is reached.

Note that this size verification is critical as a FIT table only
partially listing the microcode patches can lead to boot failures as
recently observed on Raptor Lake-P.

BRANCH=firmware-brya-14505.B
BUG=b:245380705
TEST=compilation errors out when trying to stitch more than
     CONFIG_CPU_INTEL_NUM_FIT_ENTRIES microcode patches.

Signed-off-by: Jeremy Compostella <jeremy.compostella@intel.com>
Change-Id: Id9c5fb6c1e264f3f5137d29201b9021c72d78fde
Reviewed-on: https://review.coreboot.org/c/coreboot/+/67454
Reviewed-by: Selma Bensaid <selma.bensaid@intel.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Reviewed-by: Zhixing Ma <zhixing.ma@intel.com>
diff --git a/util/cbfstool/fit.c b/util/cbfstool/fit.c
index 7f8218a..8ed2494 100644
--- a/util/cbfstool/fit.c
+++ b/util/cbfstool/fit.c
@@ -275,8 +275,10 @@
 	struct cbfs_file *mcode_file;
 
 	mcode_file = cbfs_get_entry(image, blob_name);
-	if (!mcode_file)
+	if (!mcode_file) {
+		ERROR("Couldn't find microcode blob.\n");
 		return 1;
+	}
 
 	fit_location_from_cbfs_header(&current_offset, &file_length,
 				      mcode_file);
@@ -301,6 +303,11 @@
 		    total_size > file_length)
 			break;
 
+		if (num_mcus == max_fit_entries) {
+			ERROR("Maximum of FIT entries reached.\n");
+			return 1;
+		}
+
 		/* FIXME: Should the checksum be validated? */
 		mcus[num_mcus].offset = current_offset;
 		mcus[num_mcus].size = total_size;
@@ -309,9 +316,6 @@
 		current_offset += mcus[num_mcus].size;
 		file_length -= mcus[num_mcus].size;
 		num_mcus++;
-		/* Reached limit of FIT entries. */
-		if (num_mcus == max_fit_entries)
-			break;
 		if (file_length < sizeof(struct microcode_header))
 			break;
 	}
@@ -492,13 +496,6 @@
 
 	if (parse_microcode_blob(image, blob_name, &mcus_found, mcus,
 				 max_fit_entries)) {
-		ERROR("Couldn't parse microcode blob.\n");
-		free(mcus);
-		return 1;
-	}
-
-	if (mcus_found > fit_free_space(fit, max_fit_entries)) {
-		ERROR("Maximum of FIT entries reached.\n");
 		free(mcus);
 		return 1;
 	}