blob: a69150d39ce87b1692a3799a4954c0e74b58d289 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin04654a22015-03-17 11:43:44 -05003#ifndef PROGRAM_LOADING_H
4#define PROGRAM_LOADING_H
Aaron Durbinbdf913a2014-02-24 14:56:34 -06005
Ting Shen05532262019-01-28 17:22:22 +08006#include <bootmem.h>
Aaron Durbin7e7a4df2015-12-08 14:34:35 -06007#include <commonlib/region.h>
Aaron Durbinbdf913a2014-02-24 14:56:34 -06008#include <stdint.h>
9#include <stddef.h>
10
Aaron Durbin6e76fff2015-03-20 09:42:05 -050011enum {
12 /* Last segment of program. Can be used to take different actions for
13 * cache maintenance of a program load. */
14 SEG_FINAL = 1 << 0,
15};
Ionela Voinescu00903e52015-01-09 13:14:20 +000016
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060017enum prog_type {
Julius Werner55b30812018-05-21 18:30:17 +000018 PROG_UNKNOWN,
Julius Werner99f46832018-05-16 14:14:04 -070019 PROG_BOOTBLOCK,
Julius Werner55b30812018-05-21 18:30:17 +000020 PROG_VERSTAGE,
21 PROG_ROMSTAGE,
22 PROG_RAMSTAGE,
23 PROG_REFCODE,
24 PROG_PAYLOAD,
25 PROG_BL31,
26 PROG_BL32,
Philipp Deppenwiese01797b12018-11-08 10:39:39 +010027 PROG_POSTCAR,
Patrick Rudolph4c3da702019-07-07 13:10:56 +020028 PROG_OPENSBI,
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060029};
30
Aaron Durbin096f4572016-03-31 13:49:00 -050031/*
32 * prog_segment_loaded() is called for each segment of a program loaded. The
33 * SEG_FINAL flag will be set on the last segment loaded. The following two
34 * functions, platform_segment_loaded() and arch_segment_loaded(), are called
35 * in that order within prog_segment_loaded(). In short, rely on
36 * prog_segment_loaded() to perform the proper dispatch sequence.
37 */
38void prog_segment_loaded(uintptr_t start, size_t size, int flags);
39void platform_segment_loaded(uintptr_t start, size_t size, int flags);
Aaron Durbin6e76fff2015-03-20 09:42:05 -050040void arch_segment_loaded(uintptr_t start, size_t size, int flags);
Ionela Voinescu00903e52015-01-09 13:14:20 +000041
Aaron Durbin3948e532015-03-20 13:00:20 -050042/* Representation of a program. */
43struct prog {
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060044 /* The region_device is the source of program content to load. After
45 * loading program it represents the memory region of the stages and
46 * payload. For architectures that use a bounce buffer
Aaron Durbinac12c66c2015-05-20 12:08:55 -050047 * then it would represent the bounce buffer. */
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060048 enum prog_type type;
Patrick Rudolph71327fb2018-05-03 10:35:26 +020049 uint32_t cbfs_type;
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060050 const char *name;
51 struct region_device rdev;
Aaron Durbin3948e532015-03-20 13:00:20 -050052 /* Entry to program with optional argument. It's up to the architecture
53 * to decide if argument is passed. */
54 void (*entry)(void *);
55 void *arg;
56};
57
Aaron Durbinac12c66c2015-05-20 12:08:55 -050058#define PROG_INIT(type_, name_) \
59 { \
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060060 .type = (type_), \
61 .name = (name_), \
Aaron Durbinac12c66c2015-05-20 12:08:55 -050062 }
63
64static inline const char *prog_name(const struct prog *prog)
65{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060066 return prog->name;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050067}
68
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060069static inline enum prog_type prog_type(const struct prog *prog)
Aaron Durbinac12c66c2015-05-20 12:08:55 -050070{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060071 return prog->type;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050072}
73
Patrick Rudolph71327fb2018-05-03 10:35:26 +020074static inline uint32_t prog_cbfs_type(const struct prog *prog)
75{
76 return prog->cbfs_type;
77}
78
Aaron Durbinac12c66c2015-05-20 12:08:55 -050079static inline struct region_device *prog_rdev(struct prog *prog)
80{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060081 return &prog->rdev;
Aaron Durbinac12c66c2015-05-20 12:08:55 -050082}
83
Aaron Durbin6a452ef2015-05-19 16:25:20 -050084/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050085static inline size_t prog_size(const struct prog *prog)
86{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060087 return region_device_sz(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050088}
89
Aaron Durbin6a452ef2015-05-19 16:25:20 -050090/* Only valid for loaded programs. */
Aaron Durbin3948e532015-03-20 13:00:20 -050091static inline void *prog_start(const struct prog *prog)
92{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -060093 return rdev_mmap_full(&prog->rdev);
Aaron Durbin3948e532015-03-20 13:00:20 -050094}
95
96static inline void *prog_entry(const struct prog *prog)
97{
98 return prog->entry;
99}
100
101static inline void *prog_entry_arg(const struct prog *prog)
102{
103 return prog->arg;
104}
105
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500106/* region_device representing the 32-bit flat address space. */
107extern const struct mem_region_device addrspace_32bit;
108
109static inline void prog_memory_init(struct prog *prog, uintptr_t ptr,
110 size_t size)
111{
Aaron Durbin7e7a4df2015-12-08 14:34:35 -0600112 rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size);
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500113}
114
Aaron Durbin3948e532015-03-20 13:00:20 -0500115static inline void prog_set_area(struct prog *prog, void *start, size_t size)
116{
Aaron Durbin6a452ef2015-05-19 16:25:20 -0500117 prog_memory_init(prog, (uintptr_t)start, size);
Aaron Durbin3948e532015-03-20 13:00:20 -0500118}
119
120static inline void prog_set_entry(struct prog *prog, void *e, void *arg)
121{
122 prog->entry = e;
123 prog->arg = arg;
124}
125
Arthur Heymans5331a7c2019-10-23 17:07:15 +0200126static inline void prog_set_arg(struct prog *prog, void *arg)
127{
128 prog->arg = arg;
129}
130
Aaron Durbin899d13d2015-05-15 23:39:23 -0500131/* Locate the identified program to run. Return 0 on success. < 0 on error. */
Aaron Durbin6d720f32015-12-08 17:00:23 -0600132int prog_locate(struct prog *prog);
Frans Hendriksfc580342019-06-14 14:36:37 +0200133/* The prog_locate_hook() is called prior to CBFS traversal. The hook can be
134 * used to implement policy that allows or prohibits further progress through
135 * prog_locate(). The type and name field within struct prog are the only valid
136 * fields. A 0 return value allows further progress while a non-zero return
137 * value prohibits further progress */
138int prog_locate_hook(struct prog *prog);
Aaron Durbinac12c66c2015-05-20 12:08:55 -0500139
Aaron Durbinb3847e62015-03-20 15:55:08 -0500140/* Run the program described by prog. */
141void prog_run(struct prog *prog);
142/* Per architecture implementation running a program. */
143void arch_prog_run(struct prog *prog);
144/* Platform (SoC/chipset) specific overrides for running a program. This is
145 * called prior to calling the arch_prog_run. Thus, if there is anything
146 * special that needs to be done by the platform similar to the architecture
147 * code it needs to that as well. */
148void platform_prog_run(struct prog *prog);
149
Aaron Durbince9efe02015-03-20 16:37:12 -0500150struct prog_loader_ops {
151 const char *name;
Aaron Durbin5e8286b2015-04-28 15:59:12 -0500152 /* Determine if the loader is the active one. If so returns 1 else 0
153 * or < 0 on error. */
154 int (*is_loader_active)(struct prog *prog);
Aaron Durbin899d13d2015-05-15 23:39:23 -0500155 /* Returns < 0 on error or 0 on success. This function locates
156 * the rdev representing the file data associated with the passed in
157 * prog. */
158 int (*locate)(struct prog *prog);
Aaron Durbince9efe02015-03-20 16:37:12 -0500159};
160
Aaron Durbind1b0e872015-03-17 13:17:06 -0500161/************************
162 * ROMSTAGE LOADING *
163 ************************/
164
165/* Run romstage from bootblock. */
166void run_romstage(void);
Aaron Durbin04654a22015-03-17 11:43:44 -0500167
Kyösti Mälkkib8d575c2019-12-16 16:00:49 +0200168/* Runtime selector for CBFS_PREFIX of romstage. */
169int legacy_romstage_selector(struct prog *romstage);
170
Aaron Durbin04654a22015-03-17 11:43:44 -0500171/************************
172 * RAMSTAGE LOADING *
173 ************************/
174
Aaron Durbince9efe02015-03-20 16:37:12 -0500175/* Run ramstage from romstage. */
176void run_ramstage(void);
177
Kyösti Mälkki9d6f3652016-06-28 07:38:46 +0300178/* Backup OS memory to CBMEM_ID_RESUME on ACPI S3 resume path,
179 * if ramstage overwrites low memory. */
180void backup_ramstage_section(uintptr_t base, size_t size);
181
Aaron Durbin04654a22015-03-17 11:43:44 -0500182/***********************
183 * PAYLOAD LOADING *
184 ***********************/
185
Kyösti Mälkkia48433d2018-06-07 06:31:43 +0300186int payload_arch_usable_ram_quirk(uint64_t start, uint64_t size);
187
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500188/* Load payload into memory in preparation to run. */
189void payload_load(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600190
191/* Run the loaded payload. */
Aaron Durbinebf2ed42015-03-20 10:20:15 -0500192void payload_run(void);
Aaron Durbinbdf913a2014-02-24 14:56:34 -0600193
Simon Glass7ae73fc2016-08-27 12:18:38 -0600194/*
Ronald G. Minnichc3085542018-10-24 15:46:51 -0700195 * selfload() and selfload_check() load payloads into memory.
196 * selfload() does not check the payload to see if it targets memory.
197 * Call selfload_check() to check that the payload targets usable memory.
198 * If it does not, the load will fail and this function
199 * will return false. On successful payload loading these functions return true.
Simon Glass7ae73fc2016-08-27 12:18:38 -0600200 *
201 * Defined in src/lib/selfboot.c
202 */
Ting Shen05532262019-01-28 17:22:22 +0800203bool selfload_check(struct prog *payload, enum bootmem_type dest_type);
Ronald G. Minnichc3085542018-10-24 15:46:51 -0700204bool selfload(struct prog *payload);
Aaron Durbin04654a22015-03-17 11:43:44 -0500205
206#endif /* PROGRAM_LOADING_H */