xref: /aosp_15_r20/external/coreboot/src/soc/amd/common/pi/image.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <agesa_headers.h>
4 #include <amdblocks/image.h>
5 #include <types.h>
6 
7 /* Check if the image has the desired module. */
validate_image(void * module_chain,const char module_signature[8])8 static bool validate_image(void *module_chain, const char module_signature[8])
9 {
10 	AMD_MODULE_HEADER *mod_ptr = (AMD_MODULE_HEADER *)module_chain;
11 	uint64_t signature = *(uint64_t *)module_signature;
12 	char *checking_str;
13 
14 	while ((mod_ptr != NULL) &&
15 	  (MODULE_SIGNATURE == *(uint32_t *)&mod_ptr->ModuleHeaderSignature)) {
16 		checking_str = (char *)&mod_ptr->ModuleIdentifier;
17 		if (signature == *(uint64_t *)checking_str)
18 			return true;
19 		mod_ptr = (AMD_MODULE_HEADER *)mod_ptr->NextBlock;
20 	}
21 	return false;
22 }
23 
24 /*
25  * Find an image that has the desired module. The image is aligned within
26  * a given range.
27  */
amd_find_image(const void * start_address,const void * end_address,uint32_t alignment,const char name[8])28 void *amd_find_image(const void *start_address, const void *end_address,
29 			uint32_t alignment, const char name[8])
30 {
31 	uint8_t *current_ptr = (uint8_t *)start_address;
32 	uint8_t *start = (uint8_t *)start_address;
33 	uint8_t *end = (uint8_t *)end_address;
34 	AMD_IMAGE_HEADER *image_ptr;
35 
36 	while ((current_ptr >= start) && (current_ptr < end)) {
37 		if (IMAGE_SIGNATURE == *((uint32_t *)current_ptr)) {
38 			image_ptr = (AMD_IMAGE_HEADER *)current_ptr;
39 
40 			/* Check if the image has the desired module */
41 			if (validate_image((void *)image_ptr->ModuleInfoOffset,
42 					   name))
43 				return current_ptr;
44 		}
45 		current_ptr += alignment;
46 	}
47 	return NULL;
48 }
49