Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 3 | #ifndef PROGRAM_LOADING_H |
| 4 | #define PROGRAM_LOADING_H |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 5 | |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 6 | #include <bootmem.h> |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 7 | #include <commonlib/region.h> |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | #include <stddef.h> |
| 10 | |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 11 | enum { |
| 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 Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 16 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 17 | enum prog_type { |
Julius Werner | 55b3081 | 2018-05-21 18:30:17 +0000 | [diff] [blame] | 18 | PROG_UNKNOWN, |
Julius Werner | 99f4683 | 2018-05-16 14:14:04 -0700 | [diff] [blame] | 19 | PROG_BOOTBLOCK, |
Julius Werner | 55b3081 | 2018-05-21 18:30:17 +0000 | [diff] [blame] | 20 | PROG_VERSTAGE, |
| 21 | PROG_ROMSTAGE, |
| 22 | PROG_RAMSTAGE, |
| 23 | PROG_REFCODE, |
| 24 | PROG_PAYLOAD, |
| 25 | PROG_BL31, |
| 26 | PROG_BL32, |
Philipp Deppenwiese | 01797b1 | 2018-11-08 10:39:39 +0100 | [diff] [blame] | 27 | PROG_POSTCAR, |
Patrick Rudolph | 4c3da70 | 2019-07-07 13:10:56 +0200 | [diff] [blame] | 28 | PROG_OPENSBI, |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 29 | }; |
| 30 | |
Aaron Durbin | 096f457 | 2016-03-31 13:49:00 -0500 | [diff] [blame] | 31 | /* |
| 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 | */ |
| 38 | void prog_segment_loaded(uintptr_t start, size_t size, int flags); |
| 39 | void platform_segment_loaded(uintptr_t start, size_t size, int flags); |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 40 | void arch_segment_loaded(uintptr_t start, size_t size, int flags); |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 41 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 42 | /* Representation of a program. */ |
| 43 | struct prog { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 44 | /* 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 Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 47 | * then it would represent the bounce buffer. */ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 48 | enum prog_type type; |
Patrick Rudolph | 71327fb | 2018-05-03 10:35:26 +0200 | [diff] [blame] | 49 | uint32_t cbfs_type; |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 50 | const char *name; |
| 51 | struct region_device rdev; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 52 | /* 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 Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 58 | #define PROG_INIT(type_, name_) \ |
| 59 | { \ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 60 | .type = (type_), \ |
| 61 | .name = (name_), \ |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static inline const char *prog_name(const struct prog *prog) |
| 65 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 66 | return prog->name; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 69 | static inline enum prog_type prog_type(const struct prog *prog) |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 70 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 71 | return prog->type; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Patrick Rudolph | 71327fb | 2018-05-03 10:35:26 +0200 | [diff] [blame] | 74 | static inline uint32_t prog_cbfs_type(const struct prog *prog) |
| 75 | { |
| 76 | return prog->cbfs_type; |
| 77 | } |
| 78 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 79 | static inline struct region_device *prog_rdev(struct prog *prog) |
| 80 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 81 | return &prog->rdev; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 84 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 85 | static inline size_t prog_size(const struct prog *prog) |
| 86 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 87 | return region_device_sz(&prog->rdev); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 90 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 91 | static inline void *prog_start(const struct prog *prog) |
| 92 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 93 | return rdev_mmap_full(&prog->rdev); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static inline void *prog_entry(const struct prog *prog) |
| 97 | { |
| 98 | return prog->entry; |
| 99 | } |
| 100 | |
| 101 | static inline void *prog_entry_arg(const struct prog *prog) |
| 102 | { |
| 103 | return prog->arg; |
| 104 | } |
| 105 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 106 | /* region_device representing the 32-bit flat address space. */ |
| 107 | extern const struct mem_region_device addrspace_32bit; |
| 108 | |
| 109 | static inline void prog_memory_init(struct prog *prog, uintptr_t ptr, |
| 110 | size_t size) |
| 111 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 112 | rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 115 | static inline void prog_set_area(struct prog *prog, void *start, size_t size) |
| 116 | { |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 117 | prog_memory_init(prog, (uintptr_t)start, size); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static inline void prog_set_entry(struct prog *prog, void *e, void *arg) |
| 121 | { |
| 122 | prog->entry = e; |
| 123 | prog->arg = arg; |
| 124 | } |
| 125 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 126 | static inline void prog_set_arg(struct prog *prog, void *arg) |
| 127 | { |
| 128 | prog->arg = arg; |
| 129 | } |
| 130 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 131 | /* Locate the identified program to run. Return 0 on success. < 0 on error. */ |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 132 | int prog_locate(struct prog *prog); |
Frans Hendriks | fc58034 | 2019-06-14 14:36:37 +0200 | [diff] [blame] | 133 | /* 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 */ |
| 138 | int prog_locate_hook(struct prog *prog); |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 139 | |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 140 | /* Run the program described by prog. */ |
| 141 | void prog_run(struct prog *prog); |
| 142 | /* Per architecture implementation running a program. */ |
| 143 | void 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. */ |
| 148 | void platform_prog_run(struct prog *prog); |
| 149 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 150 | struct prog_loader_ops { |
| 151 | const char *name; |
Aaron Durbin | 5e8286b | 2015-04-28 15:59:12 -0500 | [diff] [blame] | 152 | /* 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 Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 155 | /* 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 Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 159 | }; |
| 160 | |
Aaron Durbin | d1b0e87 | 2015-03-17 13:17:06 -0500 | [diff] [blame] | 161 | /************************ |
| 162 | * ROMSTAGE LOADING * |
| 163 | ************************/ |
| 164 | |
| 165 | /* Run romstage from bootblock. */ |
| 166 | void run_romstage(void); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 167 | |
Kyösti Mälkki | b8d575c | 2019-12-16 16:00:49 +0200 | [diff] [blame] | 168 | /* Runtime selector for CBFS_PREFIX of romstage. */ |
| 169 | int legacy_romstage_selector(struct prog *romstage); |
| 170 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 171 | /************************ |
| 172 | * RAMSTAGE LOADING * |
| 173 | ************************/ |
| 174 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 175 | /* Run ramstage from romstage. */ |
| 176 | void run_ramstage(void); |
| 177 | |
Kyösti Mälkki | 9d6f365 | 2016-06-28 07:38:46 +0300 | [diff] [blame] | 178 | /* Backup OS memory to CBMEM_ID_RESUME on ACPI S3 resume path, |
| 179 | * if ramstage overwrites low memory. */ |
| 180 | void backup_ramstage_section(uintptr_t base, size_t size); |
| 181 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 182 | /*********************** |
| 183 | * PAYLOAD LOADING * |
| 184 | ***********************/ |
| 185 | |
Kyösti Mälkki | a48433d | 2018-06-07 06:31:43 +0300 | [diff] [blame] | 186 | int payload_arch_usable_ram_quirk(uint64_t start, uint64_t size); |
| 187 | |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 188 | /* Load payload into memory in preparation to run. */ |
| 189 | void payload_load(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 190 | |
| 191 | /* Run the loaded payload. */ |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 192 | void payload_run(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 193 | |
Simon Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 194 | /* |
Ronald G. Minnich | c308554 | 2018-10-24 15:46:51 -0700 | [diff] [blame] | 195 | * 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 Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 200 | * |
| 201 | * Defined in src/lib/selfboot.c |
| 202 | */ |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 203 | bool selfload_check(struct prog *payload, enum bootmem_type dest_type); |
Ronald G. Minnich | c308554 | 2018-10-24 15:46:51 -0700 | [diff] [blame] | 204 | bool selfload(struct prog *payload); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 205 | |
| 206 | #endif /* PROGRAM_LOADING_H */ |