drivers/intel/fsp2_0/hand_off_block: add functions to iterate over HOBs

Introduce iterator function to go through the HOBs that will be used in
follow-up commits both from the rest of the common FSP HOB access code
and from SoC-specific code that needs to access specific HOBs.

Signed-off-by: Felix Held <felix-coreboot@felixheld.de>
Change-Id: If86dde2a9f41d0ca7941493a92f11b91a77e2ae0
Reviewed-on: https://review.coreboot.org/c/coreboot/+/69475
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/src/drivers/intel/fsp2_0/hand_off_block.c b/src/drivers/intel/fsp2_0/hand_off_block.c
index 21ce03e..13fbb06 100644
--- a/src/drivers/intel/fsp2_0/hand_off_block.c
+++ b/src/drivers/intel/fsp2_0/hand_off_block.c
@@ -126,6 +126,70 @@
 	return &fsp_hob_list_ptr;
 }
 
+enum cb_err fsp_hob_iterator_init(const struct hob_header **hob_iterator)
+{
+	*hob_iterator = fsp_get_hob_list();
+	return *hob_iterator ? CB_SUCCESS : CB_ERR;
+}
+
+static enum cb_err fsp_hob_iterator_get_next(const struct hob_header **hob_iterator,
+					     uint16_t hob_type,
+					     const struct hob_header **hob)
+{
+	const struct hob_header *current_hob;
+	while ((*hob_iterator)->type != HOB_TYPE_END_OF_HOB_LIST) {
+		current_hob = *hob_iterator;
+		*hob_iterator = fsp_next_hob(*hob_iterator);
+		if (current_hob->type == hob_type) {
+			*hob = current_hob;
+			return CB_SUCCESS;
+		}
+	}
+	return CB_ERR;
+}
+
+enum cb_err fsp_hob_iterator_get_next_resource(const struct hob_header **hob_iterator,
+					       const struct hob_resource **res)
+{
+	const struct hob_header *hob;
+	while (fsp_hob_iterator_get_next(hob_iterator, HOB_TYPE_RESOURCE_DESCRIPTOR, &hob) == CB_SUCCESS) {
+		*res = fsp_hob_header_to_resource(hob);
+		return CB_SUCCESS;
+	}
+	return CB_ERR;
+}
+
+enum cb_err fsp_hob_iterator_get_next_guid_resource(const struct hob_header **hob_iterator,
+						    const uint8_t guid[16],
+						    const struct hob_resource **res)
+{
+	const struct hob_resource *res_hob;
+	while (fsp_hob_iterator_get_next_resource(hob_iterator, &res_hob) == CB_SUCCESS) {
+		if (fsp_guid_compare(res_hob->owner_guid, guid)) {
+			*res = res_hob;
+			return CB_SUCCESS;
+		}
+	}
+	return CB_ERR;
+}
+
+enum cb_err fsp_hob_iterator_get_next_guid_extension(const struct hob_header **hob_iterator,
+						     const uint8_t guid[16],
+						     const void **data, size_t *size)
+{
+	const struct hob_header *hob;
+	const uint8_t *guid_hob;
+	while (fsp_hob_iterator_get_next(hob_iterator, HOB_TYPE_GUID_EXTENSION, &hob) == CB_SUCCESS) {
+		guid_hob = hob_header_to_struct(hob);
+		if (fsp_guid_compare(guid_hob, guid)) {
+			*size = hob->length - (HOB_HEADER_LEN + 16);
+			*data = hob_header_to_extension_hob(hob);
+			return CB_SUCCESS;
+		}
+	}
+	return CB_ERR;
+}
+
 static const
 struct hob_resource *find_resource_hob_by_guid(const struct hob_header *hob,
 					       const uint8_t guid[16])
diff --git a/src/drivers/intel/fsp2_0/include/fsp/util.h b/src/drivers/intel/fsp2_0/include/fsp/util.h
index 1cc422e..566fad9 100644
--- a/src/drivers/intel/fsp2_0/include/fsp/util.h
+++ b/src/drivers/intel/fsp2_0/include/fsp/util.h
@@ -106,6 +106,23 @@
 extern const uint8_t fsp_nv_storage_guid[16];
 extern const uint8_t fsp_reserved_memory_guid[16];
 
+/*
+ * Functions to iterate over the HOBs and get resource structs or extension HOB data. It's
+ * required to initialize the hob_iterator struct by a fsp_hob_iterator_init call before
+ * passing the fsp_hob_iterator_get_next_* functions. The fsp_hob_iterator_get_next_* functions
+ * will update the hob_iterator to point to the next HOB header, so the iterators can be called
+ * multiple times to get the data from multiple HOB instances.
+ */
+enum cb_err fsp_hob_iterator_init(const struct hob_header **hob_iterator);
+enum cb_err fsp_hob_iterator_get_next_resource(const struct hob_header **hob_iterator,
+					       const struct hob_resource **res);
+enum cb_err fsp_hob_iterator_get_next_guid_resource(const struct hob_header **hob_iterator,
+						    const uint8_t guid[16],
+						    const struct hob_resource **res);
+enum cb_err fsp_hob_iterator_get_next_guid_extension(const struct hob_header **hob_iterator,
+						     const uint8_t guid[16],
+						     const void **data, size_t *size);
+
 /* Function to extract the FSP timestamp from FPDT Hob and display */
 void fsp_display_timestamp(void);
 const void *fsp_get_hob_list(void);