Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 2 | #ifndef PROGRAM_LOADING_H |
| 3 | #define PROGRAM_LOADING_H |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 4 | |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 5 | #include <bootmem.h> |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 6 | #include <commonlib/region.h> |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | #include <stddef.h> |
| 9 | |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 10 | enum { |
| 11 | /* Last segment of program. Can be used to take different actions for |
| 12 | * cache maintenance of a program load. */ |
| 13 | SEG_FINAL = 1 << 0, |
| 14 | }; |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 15 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 16 | enum prog_type { |
Julius Werner | 55b3081 | 2018-05-21 18:30:17 +0000 | [diff] [blame] | 17 | PROG_UNKNOWN, |
Julius Werner | 99f4683 | 2018-05-16 14:14:04 -0700 | [diff] [blame] | 18 | PROG_BOOTBLOCK, |
Julius Werner | 55b3081 | 2018-05-21 18:30:17 +0000 | [diff] [blame] | 19 | PROG_VERSTAGE, |
| 20 | PROG_ROMSTAGE, |
| 21 | PROG_RAMSTAGE, |
| 22 | PROG_REFCODE, |
| 23 | PROG_PAYLOAD, |
| 24 | PROG_BL31, |
| 25 | PROG_BL32, |
Philipp Deppenwiese | 01797b1 | 2018-11-08 10:39:39 +0100 | [diff] [blame] | 26 | PROG_POSTCAR, |
Patrick Rudolph | 4c3da70 | 2019-07-07 13:10:56 +0200 | [diff] [blame] | 27 | PROG_OPENSBI, |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 28 | }; |
| 29 | |
Aaron Durbin | 096f457 | 2016-03-31 13:49:00 -0500 | [diff] [blame] | 30 | /* |
| 31 | * prog_segment_loaded() is called for each segment of a program loaded. The |
| 32 | * SEG_FINAL flag will be set on the last segment loaded. The following two |
| 33 | * functions, platform_segment_loaded() and arch_segment_loaded(), are called |
| 34 | * in that order within prog_segment_loaded(). In short, rely on |
| 35 | * prog_segment_loaded() to perform the proper dispatch sequence. |
| 36 | */ |
| 37 | void prog_segment_loaded(uintptr_t start, size_t size, int flags); |
| 38 | void platform_segment_loaded(uintptr_t start, size_t size, int flags); |
Aaron Durbin | 6e76fff | 2015-03-20 09:42:05 -0500 | [diff] [blame] | 39 | void arch_segment_loaded(uintptr_t start, size_t size, int flags); |
Ionela Voinescu | 00903e5 | 2015-01-09 13:14:20 +0000 | [diff] [blame] | 40 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 41 | /* Representation of a program. */ |
| 42 | struct prog { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 43 | /* The region_device is the source of program content to load. After |
| 44 | * loading program it represents the memory region of the stages and |
| 45 | * payload. For architectures that use a bounce buffer |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 46 | * then it would represent the bounce buffer. */ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 47 | enum prog_type type; |
Patrick Rudolph | 71327fb | 2018-05-03 10:35:26 +0200 | [diff] [blame] | 48 | uint32_t cbfs_type; |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 49 | const char *name; |
| 50 | struct region_device rdev; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 51 | /* Entry to program with optional argument. It's up to the architecture |
| 52 | * to decide if argument is passed. */ |
| 53 | void (*entry)(void *); |
| 54 | void *arg; |
| 55 | }; |
| 56 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 57 | #define PROG_INIT(type_, name_) \ |
| 58 | { \ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 59 | .type = (type_), \ |
| 60 | .name = (name_), \ |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static inline const char *prog_name(const struct prog *prog) |
| 64 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 65 | return prog->name; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 68 | static inline enum prog_type prog_type(const struct prog *prog) |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 69 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 70 | return prog->type; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Patrick Rudolph | 71327fb | 2018-05-03 10:35:26 +0200 | [diff] [blame] | 73 | static inline uint32_t prog_cbfs_type(const struct prog *prog) |
| 74 | { |
| 75 | return prog->cbfs_type; |
| 76 | } |
| 77 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 78 | static inline struct region_device *prog_rdev(struct prog *prog) |
| 79 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 80 | return &prog->rdev; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 83 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 84 | static inline size_t prog_size(const struct prog *prog) |
| 85 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 86 | return region_device_sz(&prog->rdev); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 89 | /* Only valid for loaded programs. */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 90 | static inline void *prog_start(const struct prog *prog) |
| 91 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 92 | return rdev_mmap_full(&prog->rdev); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline void *prog_entry(const struct prog *prog) |
| 96 | { |
| 97 | return prog->entry; |
| 98 | } |
| 99 | |
| 100 | static inline void *prog_entry_arg(const struct prog *prog) |
| 101 | { |
| 102 | return prog->arg; |
| 103 | } |
| 104 | |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 105 | /* region_device representing the 32-bit flat address space. */ |
| 106 | extern const struct mem_region_device addrspace_32bit; |
| 107 | |
| 108 | static inline void prog_memory_init(struct prog *prog, uintptr_t ptr, |
| 109 | size_t size) |
| 110 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 111 | rdev_chain(&prog->rdev, &addrspace_32bit.rdev, ptr, size); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 114 | static inline void prog_set_area(struct prog *prog, void *start, size_t size) |
| 115 | { |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 116 | prog_memory_init(prog, (uintptr_t)start, size); |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static inline void prog_set_entry(struct prog *prog, void *e, void *arg) |
| 120 | { |
| 121 | prog->entry = e; |
| 122 | prog->arg = arg; |
| 123 | } |
| 124 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 125 | static inline void prog_set_arg(struct prog *prog, void *arg) |
| 126 | { |
| 127 | prog->arg = arg; |
| 128 | } |
| 129 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 130 | /* 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] | 131 | int prog_locate(struct prog *prog); |
Frans Hendriks | fc58034 | 2019-06-14 14:36:37 +0200 | [diff] [blame] | 132 | /* The prog_locate_hook() is called prior to CBFS traversal. The hook can be |
| 133 | * used to implement policy that allows or prohibits further progress through |
| 134 | * prog_locate(). The type and name field within struct prog are the only valid |
| 135 | * fields. A 0 return value allows further progress while a non-zero return |
| 136 | * value prohibits further progress */ |
| 137 | int prog_locate_hook(struct prog *prog); |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 138 | |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 139 | /* Run the program described by prog. */ |
| 140 | void prog_run(struct prog *prog); |
| 141 | /* Per architecture implementation running a program. */ |
| 142 | void arch_prog_run(struct prog *prog); |
| 143 | /* Platform (SoC/chipset) specific overrides for running a program. This is |
| 144 | * called prior to calling the arch_prog_run. Thus, if there is anything |
| 145 | * special that needs to be done by the platform similar to the architecture |
| 146 | * code it needs to that as well. */ |
| 147 | void platform_prog_run(struct prog *prog); |
| 148 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 149 | struct prog_loader_ops { |
| 150 | const char *name; |
Aaron Durbin | 5e8286b | 2015-04-28 15:59:12 -0500 | [diff] [blame] | 151 | /* Determine if the loader is the active one. If so returns 1 else 0 |
| 152 | * or < 0 on error. */ |
| 153 | int (*is_loader_active)(struct prog *prog); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 154 | /* Returns < 0 on error or 0 on success. This function locates |
| 155 | * the rdev representing the file data associated with the passed in |
| 156 | * prog. */ |
| 157 | int (*locate)(struct prog *prog); |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 158 | }; |
| 159 | |
Aaron Durbin | d1b0e87 | 2015-03-17 13:17:06 -0500 | [diff] [blame] | 160 | /************************ |
| 161 | * ROMSTAGE LOADING * |
| 162 | ************************/ |
| 163 | |
| 164 | /* Run romstage from bootblock. */ |
| 165 | void run_romstage(void); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 166 | |
Kyösti Mälkki | b8d575c | 2019-12-16 16:00:49 +0200 | [diff] [blame] | 167 | /* Runtime selector for CBFS_PREFIX of romstage. */ |
| 168 | int legacy_romstage_selector(struct prog *romstage); |
| 169 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 170 | /************************ |
| 171 | * RAMSTAGE LOADING * |
| 172 | ************************/ |
| 173 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 174 | /* Run ramstage from romstage. */ |
| 175 | void run_ramstage(void); |
| 176 | |
Kyösti Mälkki | 9d6f365 | 2016-06-28 07:38:46 +0300 | [diff] [blame] | 177 | /* Backup OS memory to CBMEM_ID_RESUME on ACPI S3 resume path, |
| 178 | * if ramstage overwrites low memory. */ |
| 179 | void backup_ramstage_section(uintptr_t base, size_t size); |
| 180 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 181 | /*********************** |
| 182 | * PAYLOAD LOADING * |
| 183 | ***********************/ |
| 184 | |
Kyösti Mälkki | a48433d | 2018-06-07 06:31:43 +0300 | [diff] [blame] | 185 | int payload_arch_usable_ram_quirk(uint64_t start, uint64_t size); |
| 186 | |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 187 | /* Load payload into memory in preparation to run. */ |
| 188 | void payload_load(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 189 | |
| 190 | /* Run the loaded payload. */ |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 191 | void payload_run(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 192 | |
Simon Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 193 | /* |
Ronald G. Minnich | c308554 | 2018-10-24 15:46:51 -0700 | [diff] [blame] | 194 | * selfload() and selfload_check() load payloads into memory. |
| 195 | * selfload() does not check the payload to see if it targets memory. |
| 196 | * Call selfload_check() to check that the payload targets usable memory. |
| 197 | * If it does not, the load will fail and this function |
| 198 | * will return false. On successful payload loading these functions return true. |
Simon Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 199 | * |
| 200 | * Defined in src/lib/selfboot.c |
| 201 | */ |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 202 | bool selfload_check(struct prog *payload, enum bootmem_type dest_type); |
Ronald G. Minnich | c308554 | 2018-10-24 15:46:51 -0700 | [diff] [blame] | 203 | bool selfload(struct prog *payload); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 204 | |
| 205 | #endif /* PROGRAM_LOADING_H */ |