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> |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 6 | #include <commonlib/bsd/cbfs_serialized.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 | enum prog_type type; |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 45 | enum cbfs_type cbfs_type; |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 46 | const char *name; |
Julius Werner | 2e97394 | 2020-09-03 21:17:20 -0700 | [diff] [blame] | 47 | void *start; /* Program start in memory. */ |
| 48 | size_t size; /* Program size in memory (including BSS). */ |
| 49 | void (*entry)(void *); /* Function pointer to entry point. */ |
| 50 | void *arg; /* Optional argument (only valid for some archs). */ |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 53 | #define PROG_INIT(type_, name_) \ |
| 54 | { \ |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 55 | .type = (type_), \ |
| 56 | .name = (name_), \ |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline const char *prog_name(const struct prog *prog) |
| 60 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 61 | return prog->name; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 64 | static inline enum prog_type prog_type(const struct prog *prog) |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 65 | { |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 66 | return prog->type; |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 69 | static inline enum cbfs_type prog_cbfs_type(const struct prog *prog) |
Patrick Rudolph | 71327fb | 2018-05-03 10:35:26 +0200 | [diff] [blame] | 70 | { |
| 71 | return prog->cbfs_type; |
| 72 | } |
| 73 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 74 | static inline size_t prog_size(const struct prog *prog) |
| 75 | { |
Julius Werner | 2e97394 | 2020-09-03 21:17:20 -0700 | [diff] [blame] | 76 | return prog->size; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline void *prog_start(const struct prog *prog) |
| 80 | { |
Julius Werner | 2e97394 | 2020-09-03 21:17:20 -0700 | [diff] [blame] | 81 | return prog->start; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static inline void *prog_entry(const struct prog *prog) |
| 85 | { |
| 86 | return prog->entry; |
| 87 | } |
| 88 | |
| 89 | static inline void *prog_entry_arg(const struct prog *prog) |
| 90 | { |
| 91 | return prog->arg; |
| 92 | } |
| 93 | |
Julius Werner | 2e97394 | 2020-09-03 21:17:20 -0700 | [diff] [blame] | 94 | /* Can be used to get an rdev representation of program area in memory. */ |
| 95 | static inline void prog_chain_rdev(const struct prog *prog, |
| 96 | struct region_device *rdev_out) |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 97 | { |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 98 | rdev_chain_mem(rdev_out, prog->start, prog->size); |
Aaron Durbin | 6a452ef | 2015-05-19 16:25:20 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 101 | static inline void prog_set_area(struct prog *prog, void *start, size_t size) |
| 102 | { |
Julius Werner | 2e97394 | 2020-09-03 21:17:20 -0700 | [diff] [blame] | 103 | prog->start = start; |
| 104 | prog->size = size; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline void prog_set_entry(struct prog *prog, void *e, void *arg) |
| 108 | { |
| 109 | prog->entry = e; |
| 110 | prog->arg = arg; |
| 111 | } |
| 112 | |
Arthur Heymans | 5331a7c | 2019-10-23 17:07:15 +0200 | [diff] [blame] | 113 | static inline void prog_set_arg(struct prog *prog, void *arg) |
| 114 | { |
| 115 | prog->arg = arg; |
| 116 | } |
| 117 | |
Frans Hendriks | fc58034 | 2019-06-14 14:36:37 +0200 | [diff] [blame] | 118 | /* The prog_locate_hook() is called prior to CBFS traversal. The hook can be |
Julius Werner | 5358467 | 2021-01-11 16:44:06 -0800 | [diff] [blame] | 119 | * used to implement policy that allows or prohibits further program loading. |
| 120 | * The type and name field within struct prog are the only valid fields. A 0 |
| 121 | * return value allows loading while a non-zero return value prohibits it. */ |
Frans Hendriks | fc58034 | 2019-06-14 14:36:37 +0200 | [diff] [blame] | 122 | int prog_locate_hook(struct prog *prog); |
Aaron Durbin | ac12c66c | 2015-05-20 12:08:55 -0500 | [diff] [blame] | 123 | |
Aaron Durbin | b3847e6 | 2015-03-20 15:55:08 -0500 | [diff] [blame] | 124 | /* Run the program described by prog. */ |
| 125 | void prog_run(struct prog *prog); |
| 126 | /* Per architecture implementation running a program. */ |
| 127 | void arch_prog_run(struct prog *prog); |
| 128 | /* Platform (SoC/chipset) specific overrides for running a program. This is |
| 129 | * called prior to calling the arch_prog_run. Thus, if there is anything |
| 130 | * special that needs to be done by the platform similar to the architecture |
| 131 | * code it needs to that as well. */ |
| 132 | void platform_prog_run(struct prog *prog); |
| 133 | |
Aaron Durbin | d1b0e87 | 2015-03-17 13:17:06 -0500 | [diff] [blame] | 134 | /************************ |
| 135 | * ROMSTAGE LOADING * |
| 136 | ************************/ |
| 137 | |
| 138 | /* Run romstage from bootblock. */ |
| 139 | void run_romstage(void); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 140 | |
Kyösti Mälkki | b8d575c | 2019-12-16 16:00:49 +0200 | [diff] [blame] | 141 | /* Runtime selector for CBFS_PREFIX of romstage. */ |
Julius Werner | 1de8708 | 2020-12-23 17:38:11 -0800 | [diff] [blame] | 142 | int legacy_romstage_select_and_load(struct prog *romstage); |
Kyösti Mälkki | b8d575c | 2019-12-16 16:00:49 +0200 | [diff] [blame] | 143 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 144 | /************************ |
| 145 | * RAMSTAGE LOADING * |
| 146 | ************************/ |
| 147 | |
Aaron Durbin | ce9efe0 | 2015-03-20 16:37:12 -0500 | [diff] [blame] | 148 | /* Run ramstage from romstage. */ |
| 149 | void run_ramstage(void); |
| 150 | |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 151 | /*********************** |
| 152 | * PAYLOAD LOADING * |
| 153 | ***********************/ |
| 154 | |
Kyösti Mälkki | a48433d | 2018-06-07 06:31:43 +0300 | [diff] [blame] | 155 | int payload_arch_usable_ram_quirk(uint64_t start, uint64_t size); |
| 156 | |
Raul E Rangel | 67798cf | 2021-07-02 17:07:05 -0600 | [diff] [blame] | 157 | /* |
| 158 | * Asynchronously preloads the payload. |
| 159 | * |
| 160 | * This should be called early on to allow the payload to load before |
| 161 | * `payload_load` is called. |
| 162 | */ |
| 163 | void payload_preload(void); |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 164 | /* Load payload into memory in preparation to run. */ |
| 165 | void payload_load(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 166 | |
| 167 | /* Run the loaded payload. */ |
Aaron Durbin | ebf2ed4 | 2015-03-20 10:20:15 -0500 | [diff] [blame] | 168 | void payload_run(void); |
Aaron Durbin | bdf913a | 2014-02-24 14:56:34 -0600 | [diff] [blame] | 169 | |
Simon Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 170 | /* |
Ronald G. Minnich | c308554 | 2018-10-24 15:46:51 -0700 | [diff] [blame] | 171 | * selfload() and selfload_check() load payloads into memory. |
| 172 | * selfload() does not check the payload to see if it targets memory. |
| 173 | * Call selfload_check() to check that the payload targets usable memory. |
| 174 | * If it does not, the load will fail and this function |
| 175 | * will return false. On successful payload loading these functions return true. |
Simon Glass | 7ae73fc | 2016-08-27 12:18:38 -0600 | [diff] [blame] | 176 | * |
| 177 | * Defined in src/lib/selfboot.c |
| 178 | */ |
Ting Shen | 0553226 | 2019-01-28 17:22:22 +0800 | [diff] [blame] | 179 | bool selfload_check(struct prog *payload, enum bootmem_type dest_type); |
Ronald G. Minnich | c308554 | 2018-10-24 15:46:51 -0700 | [diff] [blame] | 180 | bool selfload(struct prog *payload); |
Julius Werner | 965846f | 2021-01-11 16:07:02 -0800 | [diff] [blame] | 181 | /* Like selfload_check() but with the payload data already mapped to memory. */ |
| 182 | bool selfload_mapped(struct prog *payload, void *mapping, |
| 183 | enum bootmem_type dest_type); |
| 184 | |
| 185 | /* Load a FIT payload. The payload data must already be mapped to memory. */ |
| 186 | void fit_payload(struct prog *payload, void *data); |
Aaron Durbin | 04654a2 | 2015-03-17 11:43:44 -0500 | [diff] [blame] | 187 | |
| 188 | #endif /* PROGRAM_LOADING_H */ |