1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <stage_cache.h>
7 #include <amdblocks/agesawrapper.h>
8 #include <amdblocks/image.h>
10 static void *agesa_map_raw_file(const char *name
, size_t *size
)
12 enum cbfs_type type
= CBFS_TYPE_RAW
;
13 return cbfs_type_map(name
, size
, &type
);
16 static void *agesa_map_stage_file_early(const char *name
, size_t *size
)
18 enum cbfs_type type
= CBFS_TYPE_STAGE
;
19 return cbfs_type_map(name
, size
, &type
);
22 static void *agesa_map_stage_file_ramstage(const char *name
, size_t *size
)
24 struct prog prog
= PROG_INIT(PROG_REFCODE
, name
);
25 struct rmod_stage_load rmod_agesa
= {
26 .cbmem_id
= CBMEM_ID_REFCODE
,
30 if (resume_from_stage_cache()) {
31 stage_cache_load_stage(STAGE_REFCODE
, &prog
);
33 if (rmodule_stage_load(&rmod_agesa
) < 0)
36 stage_cache_add(STAGE_REFCODE
, &prog
);
39 *size
= prog_size(&prog
);
40 return prog_start(&prog
);
43 static void *agesa_map_stage_file(const char *name
, size_t *size
)
45 if (!ENV_RAMSTAGE
|| !CONFIG(AGESA_SPLIT_MEMORY_FILES
))
46 return agesa_map_stage_file_early(name
, size
);
47 return agesa_map_stage_file_ramstage(name
, size
);
50 static const char *get_agesa_cbfs_name(void)
52 if (!CONFIG(AGESA_SPLIT_MEMORY_FILES
))
53 return CONFIG_AGESA_CBFS_NAME
;
55 return CONFIG_AGESA_PRE_MEMORY_CBFS_NAME
;
56 return CONFIG_AGESA_POST_MEMORY_CBFS_NAME
;
59 const void *agesawrapper_locate_module(const char name
[8])
62 const AMD_IMAGE_HEADER
*image
;
66 /* Assume boot device is memory mapped so the mapping can leak. */
67 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED
));
69 fname
= get_agesa_cbfs_name();
71 if (CONFIG(AGESA_BINARY_PI_AS_STAGE
))
72 agesa
= agesa_map_stage_file(fname
, &file_size
);
74 agesa
= agesa_map_raw_file(fname
, &file_size
);
79 image
= amd_find_image(agesa
, agesa
+ file_size
, 4096, name
);
84 return (AMD_MODULE_HEADER
*)image
->ModuleInfoOffset
;
87 static MODULE_ENTRY agesa_dispatcher
;
89 MODULE_ENTRY
agesa_get_dispatcher(void)
91 const AMD_MODULE_HEADER
*module
;
92 static const char id
[8] = AGESA_ID
;
94 if (agesa_dispatcher
!= NULL
)
95 return agesa_dispatcher
;
97 module
= agesawrapper_locate_module(id
);
101 agesa_dispatcher
= module
->ModuleDispatcher
;
102 return agesa_dispatcher
;