732984295295fb6d11cdbbd53bc85a4130ee5c97
[platform/kernel/linux-rpi.git] / drivers / firmware / efi / libstub / efi-stub-helper.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Helper functions used by the EFI stub on multiple
4  * architectures. This should be #included by the EFI stub
5  * implementation files.
6  *
7  * Copyright 2011 Intel Corporation; author Matt Fleming
8  */
9
10 #include <linux/stdarg.h>
11
12 #include <linux/efi.h>
13 #include <linux/kernel.h>
14 #include <asm/efi.h>
15 #include <asm/setup.h>
16
17 #include "efistub.h"
18
19 bool efi_nochunk;
20 bool efi_nokaslr = !IS_ENABLED(CONFIG_RANDOMIZE_BASE);
21 bool efi_novamap;
22
23 static bool efi_noinitrd;
24 static bool efi_nosoftreserve;
25 static bool efi_disable_pci_dma = IS_ENABLED(CONFIG_EFI_DISABLE_PCI_DMA);
26
27 bool __pure __efi_soft_reserve_enabled(void)
28 {
29         return !efi_nosoftreserve;
30 }
31
32 /**
33  * efi_parse_options() - Parse EFI command line options
34  * @cmdline:    kernel command line
35  *
36  * Parse the ASCII string @cmdline for EFI options, denoted by the efi=
37  * option, e.g. efi=nochunk.
38  *
39  * It should be noted that efi= is parsed in two very different
40  * environments, first in the early boot environment of the EFI boot
41  * stub, and subsequently during the kernel boot.
42  *
43  * Return:      status code
44  */
45 efi_status_t efi_parse_options(char const *cmdline)
46 {
47         size_t len;
48         efi_status_t status;
49         char *str, *buf;
50
51         if (!cmdline)
52                 return EFI_SUCCESS;
53
54         len = strnlen(cmdline, COMMAND_LINE_SIZE - 1) + 1;
55         status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, len, (void **)&buf);
56         if (status != EFI_SUCCESS)
57                 return status;
58
59         memcpy(buf, cmdline, len - 1);
60         buf[len - 1] = '\0';
61         str = skip_spaces(buf);
62
63         while (*str) {
64                 char *param, *val;
65
66                 str = next_arg(str, &param, &val);
67                 if (!val && !strcmp(param, "--"))
68                         break;
69
70                 if (!strcmp(param, "nokaslr")) {
71                         efi_nokaslr = true;
72                 } else if (!strcmp(param, "quiet")) {
73                         efi_loglevel = CONSOLE_LOGLEVEL_QUIET;
74                 } else if (!strcmp(param, "noinitrd")) {
75                         efi_noinitrd = true;
76                 } else if (!strcmp(param, "efi") && val) {
77                         efi_nochunk = parse_option_str(val, "nochunk");
78                         efi_novamap |= parse_option_str(val, "novamap");
79
80                         efi_nosoftreserve = IS_ENABLED(CONFIG_EFI_SOFT_RESERVE) &&
81                                             parse_option_str(val, "nosoftreserve");
82
83                         if (parse_option_str(val, "disable_early_pci_dma"))
84                                 efi_disable_pci_dma = true;
85                         if (parse_option_str(val, "no_disable_early_pci_dma"))
86                                 efi_disable_pci_dma = false;
87                         if (parse_option_str(val, "debug"))
88                                 efi_loglevel = CONSOLE_LOGLEVEL_DEBUG;
89                 } else if (!strcmp(param, "video") &&
90                            val && strstarts(val, "efifb:")) {
91                         efi_parse_option_graphics(val + strlen("efifb:"));
92                 }
93         }
94         efi_bs_call(free_pool, buf);
95         return EFI_SUCCESS;
96 }
97
98 /*
99  * The EFI_LOAD_OPTION descriptor has the following layout:
100  *      u32 Attributes;
101  *      u16 FilePathListLength;
102  *      u16 Description[];
103  *      efi_device_path_protocol_t FilePathList[];
104  *      u8 OptionalData[];
105  *
106  * This function validates and unpacks the variable-size data fields.
107  */
108 static
109 bool efi_load_option_unpack(efi_load_option_unpacked_t *dest,
110                             const efi_load_option_t *src, size_t size)
111 {
112         const void *pos;
113         u16 c;
114         efi_device_path_protocol_t header;
115         const efi_char16_t *description;
116         const efi_device_path_protocol_t *file_path_list;
117
118         if (size < offsetof(efi_load_option_t, variable_data))
119                 return false;
120         pos = src->variable_data;
121         size -= offsetof(efi_load_option_t, variable_data);
122
123         if ((src->attributes & ~EFI_LOAD_OPTION_MASK) != 0)
124                 return false;
125
126         /* Scan description. */
127         description = pos;
128         do {
129                 if (size < sizeof(c))
130                         return false;
131                 c = *(const u16 *)pos;
132                 pos += sizeof(c);
133                 size -= sizeof(c);
134         } while (c != L'\0');
135
136         /* Scan file_path_list. */
137         file_path_list = pos;
138         do {
139                 if (size < sizeof(header))
140                         return false;
141                 header = *(const efi_device_path_protocol_t *)pos;
142                 if (header.length < sizeof(header))
143                         return false;
144                 if (size < header.length)
145                         return false;
146                 pos += header.length;
147                 size -= header.length;
148         } while ((header.type != EFI_DEV_END_PATH && header.type != EFI_DEV_END_PATH2) ||
149                  (header.sub_type != EFI_DEV_END_ENTIRE));
150         if (pos != (const void *)file_path_list + src->file_path_list_length)
151                 return false;
152
153         dest->attributes = src->attributes;
154         dest->file_path_list_length = src->file_path_list_length;
155         dest->description = description;
156         dest->file_path_list = file_path_list;
157         dest->optional_data_size = size;
158         dest->optional_data = size ? pos : NULL;
159
160         return true;
161 }
162
163 /*
164  * At least some versions of Dell firmware pass the entire contents of the
165  * Boot#### variable, i.e. the EFI_LOAD_OPTION descriptor, rather than just the
166  * OptionalData field.
167  *
168  * Detect this case and extract OptionalData.
169  */
170 void efi_apply_loadoptions_quirk(const void **load_options, u32 *load_options_size)
171 {
172         const efi_load_option_t *load_option = *load_options;
173         efi_load_option_unpacked_t load_option_unpacked;
174
175         if (!IS_ENABLED(CONFIG_X86))
176                 return;
177         if (!load_option)
178                 return;
179         if (*load_options_size < sizeof(*load_option))
180                 return;
181         if ((load_option->attributes & ~EFI_LOAD_OPTION_BOOT_MASK) != 0)
182                 return;
183
184         if (!efi_load_option_unpack(&load_option_unpacked, load_option, *load_options_size))
185                 return;
186
187         efi_warn_once(FW_BUG "LoadOptions is an EFI_LOAD_OPTION descriptor\n");
188         efi_warn_once(FW_BUG "Using OptionalData as a workaround\n");
189
190         *load_options = load_option_unpacked.optional_data;
191         *load_options_size = load_option_unpacked.optional_data_size;
192 }
193
194 enum efistub_event {
195         EFISTUB_EVT_INITRD,
196         EFISTUB_EVT_LOAD_OPTIONS,
197         EFISTUB_EVT_COUNT,
198 };
199
200 #define STR_WITH_SIZE(s)        sizeof(s), s
201
202 static const struct {
203         u32             pcr_index;
204         u32             event_id;
205         u32             event_data_len;
206         u8              event_data[52];
207 } events[] = {
208         [EFISTUB_EVT_INITRD] = {
209                 9,
210                 INITRD_EVENT_TAG_ID,
211                 STR_WITH_SIZE("Linux initrd")
212         },
213         [EFISTUB_EVT_LOAD_OPTIONS] = {
214                 9,
215                 LOAD_OPTIONS_EVENT_TAG_ID,
216                 STR_WITH_SIZE("LOADED_IMAGE::LoadOptions")
217         },
218 };
219
220 static efi_status_t efi_measure_tagged_event(unsigned long load_addr,
221                                              unsigned long load_size,
222                                              enum efistub_event event)
223 {
224         efi_guid_t tcg2_guid = EFI_TCG2_PROTOCOL_GUID;
225         efi_tcg2_protocol_t *tcg2 = NULL;
226         efi_status_t status;
227
228         efi_bs_call(locate_protocol, &tcg2_guid, NULL, (void **)&tcg2);
229         if (tcg2) {
230                 struct efi_measured_event {
231                         efi_tcg2_event_t        event_data;
232                         efi_tcg2_tagged_event_t tagged_event;
233                         u8                      tagged_event_data[];
234                 } *evt;
235                 int size = sizeof(*evt) + events[event].event_data_len;
236
237                 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
238                                      (void **)&evt);
239                 if (status != EFI_SUCCESS)
240                         goto fail;
241
242                 evt->event_data = (struct efi_tcg2_event){
243                         .event_size                     = size,
244                         .event_header.header_size       = sizeof(evt->event_data.event_header),
245                         .event_header.header_version    = EFI_TCG2_EVENT_HEADER_VERSION,
246                         .event_header.pcr_index         = events[event].pcr_index,
247                         .event_header.event_type        = EV_EVENT_TAG,
248                 };
249
250                 evt->tagged_event = (struct efi_tcg2_tagged_event){
251                         .tagged_event_id                = events[event].event_id,
252                         .tagged_event_data_size         = events[event].event_data_len,
253                 };
254
255                 memcpy(evt->tagged_event_data, events[event].event_data,
256                        events[event].event_data_len);
257
258                 status = efi_call_proto(tcg2, hash_log_extend_event, 0,
259                                         load_addr, load_size, &evt->event_data);
260                 efi_bs_call(free_pool, evt);
261
262                 if (status != EFI_SUCCESS)
263                         goto fail;
264                 return EFI_SUCCESS;
265         }
266
267         return EFI_UNSUPPORTED;
268 fail:
269         efi_warn("Failed to measure data for event %d: 0x%lx\n", event, status);
270         return status;
271 }
272
273 /*
274  * Convert the unicode UEFI command line to ASCII to pass to kernel.
275  * Size of memory allocated return in *cmd_line_len.
276  * Returns NULL on error.
277  */
278 char *efi_convert_cmdline(efi_loaded_image_t *image, int *cmd_line_len)
279 {
280         const efi_char16_t *options = efi_table_attr(image, load_options);
281         u32 options_size = efi_table_attr(image, load_options_size);
282         int options_bytes = 0, safe_options_bytes = 0;  /* UTF-8 bytes */
283         unsigned long cmdline_addr = 0;
284         const efi_char16_t *s2;
285         bool in_quote = false;
286         efi_status_t status;
287         u32 options_chars;
288
289         if (options_size > 0)
290                 efi_measure_tagged_event((unsigned long)options, options_size,
291                                          EFISTUB_EVT_LOAD_OPTIONS);
292
293         efi_apply_loadoptions_quirk((const void **)&options, &options_size);
294         options_chars = options_size / sizeof(efi_char16_t);
295
296         if (options) {
297                 s2 = options;
298                 while (options_bytes < COMMAND_LINE_SIZE && options_chars--) {
299                         efi_char16_t c = *s2++;
300
301                         if (c < 0x80) {
302                                 if (c == L'\0' || c == L'\n')
303                                         break;
304                                 if (c == L'"')
305                                         in_quote = !in_quote;
306                                 else if (!in_quote && isspace((char)c))
307                                         safe_options_bytes = options_bytes;
308
309                                 options_bytes++;
310                                 continue;
311                         }
312
313                         /*
314                          * Get the number of UTF-8 bytes corresponding to a
315                          * UTF-16 character.
316                          * The first part handles everything in the BMP.
317                          */
318                         options_bytes += 2 + (c >= 0x800);
319                         /*
320                          * Add one more byte for valid surrogate pairs. Invalid
321                          * surrogates will be replaced with 0xfffd and take up
322                          * only 3 bytes.
323                          */
324                         if ((c & 0xfc00) == 0xd800) {
325                                 /*
326                                  * If the very last word is a high surrogate,
327                                  * we must ignore it since we can't access the
328                                  * low surrogate.
329                                  */
330                                 if (!options_chars) {
331                                         options_bytes -= 3;
332                                 } else if ((*s2 & 0xfc00) == 0xdc00) {
333                                         options_bytes++;
334                                         options_chars--;
335                                         s2++;
336                                 }
337                         }
338                 }
339                 if (options_bytes >= COMMAND_LINE_SIZE) {
340                         options_bytes = safe_options_bytes;
341                         efi_err("Command line is too long: truncated to %d bytes\n",
342                                 options_bytes);
343                 }
344         }
345
346         options_bytes++;        /* NUL termination */
347
348         status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, options_bytes,
349                              (void **)&cmdline_addr);
350         if (status != EFI_SUCCESS)
351                 return NULL;
352
353         snprintf((char *)cmdline_addr, options_bytes, "%.*ls",
354                  options_bytes - 1, options);
355
356         *cmd_line_len = options_bytes;
357         return (char *)cmdline_addr;
358 }
359
360 /**
361  * efi_exit_boot_services() - Exit boot services
362  * @handle:     handle of the exiting image
363  * @priv:       argument to be passed to @priv_func
364  * @priv_func:  function to process the memory map before exiting boot services
365  *
366  * Handle calling ExitBootServices according to the requirements set out by the
367  * spec.  Obtains the current memory map, and returns that info after calling
368  * ExitBootServices.  The client must specify a function to perform any
369  * processing of the memory map data prior to ExitBootServices.  A client
370  * specific structure may be passed to the function via priv.  The client
371  * function may be called multiple times.
372  *
373  * Return:      status code
374  */
375 efi_status_t efi_exit_boot_services(void *handle, void *priv,
376                                     efi_exit_boot_map_processing priv_func)
377 {
378         struct efi_boot_memmap *map;
379         efi_status_t status;
380
381         if (efi_disable_pci_dma)
382                 efi_pci_disable_bridge_busmaster();
383
384         status = efi_get_memory_map(&map, true);
385         if (status != EFI_SUCCESS)
386                 return status;
387
388         status = priv_func(map, priv);
389         if (status != EFI_SUCCESS) {
390                 efi_bs_call(free_pool, map);
391                 return status;
392         }
393
394         status = efi_bs_call(exit_boot_services, handle, map->map_key);
395
396         if (status == EFI_INVALID_PARAMETER) {
397                 /*
398                  * The memory map changed between efi_get_memory_map() and
399                  * exit_boot_services().  Per the UEFI Spec v2.6, Section 6.4:
400                  * EFI_BOOT_SERVICES.ExitBootServices we need to get the
401                  * updated map, and try again.  The spec implies one retry
402                  * should be sufficent, which is confirmed against the EDK2
403                  * implementation.  Per the spec, we can only invoke
404                  * get_memory_map() and exit_boot_services() - we cannot alloc
405                  * so efi_get_memory_map() cannot be used, and we must reuse
406                  * the buffer.  For all practical purposes, the headroom in the
407                  * buffer should account for any changes in the map so the call
408                  * to get_memory_map() is expected to succeed here.
409                  */
410                 map->map_size = map->buff_size;
411                 status = efi_bs_call(get_memory_map,
412                                      &map->map_size,
413                                      &map->map,
414                                      &map->map_key,
415                                      &map->desc_size,
416                                      &map->desc_ver);
417
418                 /* exit_boot_services() was called, thus cannot free */
419                 if (status != EFI_SUCCESS)
420                         return status;
421
422                 status = priv_func(map, priv);
423                 /* exit_boot_services() was called, thus cannot free */
424                 if (status != EFI_SUCCESS)
425                         return status;
426
427                 status = efi_bs_call(exit_boot_services, handle, map->map_key);
428         }
429
430         return status;
431 }
432
433 /**
434  * get_efi_config_table() - retrieve UEFI configuration table
435  * @guid:       GUID of the configuration table to be retrieved
436  * Return:      pointer to the configuration table or NULL
437  */
438 void *get_efi_config_table(efi_guid_t guid)
439 {
440         unsigned long tables = efi_table_attr(efi_system_table, tables);
441         int nr_tables = efi_table_attr(efi_system_table, nr_tables);
442         int i;
443
444         for (i = 0; i < nr_tables; i++) {
445                 efi_config_table_t *t = (void *)tables;
446
447                 if (efi_guidcmp(t->guid, guid) == 0)
448                         return efi_table_attr(t, table);
449
450                 tables += efi_is_native() ? sizeof(efi_config_table_t)
451                                           : sizeof(efi_config_table_32_t);
452         }
453         return NULL;
454 }
455
456 /*
457  * The LINUX_EFI_INITRD_MEDIA_GUID vendor media device path below provides a way
458  * for the firmware or bootloader to expose the initrd data directly to the stub
459  * via the trivial LoadFile2 protocol, which is defined in the UEFI spec, and is
460  * very easy to implement. It is a simple Linux initrd specific conduit between
461  * kernel and firmware, allowing us to put the EFI stub (being part of the
462  * kernel) in charge of where and when to load the initrd, while leaving it up
463  * to the firmware to decide whether it needs to expose its filesystem hierarchy
464  * via EFI protocols.
465  */
466 static const struct {
467         struct efi_vendor_dev_path      vendor;
468         struct efi_generic_dev_path     end;
469 } __packed initrd_dev_path = {
470         {
471                 {
472                         EFI_DEV_MEDIA,
473                         EFI_DEV_MEDIA_VENDOR,
474                         sizeof(struct efi_vendor_dev_path),
475                 },
476                 LINUX_EFI_INITRD_MEDIA_GUID
477         }, {
478                 EFI_DEV_END_PATH,
479                 EFI_DEV_END_ENTIRE,
480                 sizeof(struct efi_generic_dev_path)
481         }
482 };
483
484 /**
485  * efi_load_initrd_dev_path() - load the initrd from the Linux initrd device path
486  * @initrd:     pointer of struct to store the address where the initrd was loaded
487  *              and the size of the loaded initrd
488  * @max:        upper limit for the initrd memory allocation
489  *
490  * Return:
491  * * %EFI_SUCCESS if the initrd was loaded successfully, in which
492  *   case @load_addr and @load_size are assigned accordingly
493  * * %EFI_NOT_FOUND if no LoadFile2 protocol exists on the initrd device path
494  * * %EFI_OUT_OF_RESOURCES if memory allocation failed
495  * * %EFI_LOAD_ERROR in all other cases
496  */
497 static
498 efi_status_t efi_load_initrd_dev_path(struct linux_efi_initrd *initrd,
499                                       unsigned long max)
500 {
501         efi_guid_t lf2_proto_guid = EFI_LOAD_FILE2_PROTOCOL_GUID;
502         efi_device_path_protocol_t *dp;
503         efi_load_file2_protocol_t *lf2;
504         efi_handle_t handle;
505         efi_status_t status;
506
507         dp = (efi_device_path_protocol_t *)&initrd_dev_path;
508         status = efi_bs_call(locate_device_path, &lf2_proto_guid, &dp, &handle);
509         if (status != EFI_SUCCESS)
510                 return status;
511
512         status = efi_bs_call(handle_protocol, handle, &lf2_proto_guid,
513                              (void **)&lf2);
514         if (status != EFI_SUCCESS)
515                 return status;
516
517         initrd->size = 0;
518         status = efi_call_proto(lf2, load_file, dp, false, &initrd->size, NULL);
519         if (status != EFI_BUFFER_TOO_SMALL)
520                 return EFI_LOAD_ERROR;
521
522         status = efi_allocate_pages(initrd->size, &initrd->base, max);
523         if (status != EFI_SUCCESS)
524                 return status;
525
526         status = efi_call_proto(lf2, load_file, dp, false, &initrd->size,
527                                 (void *)initrd->base);
528         if (status != EFI_SUCCESS) {
529                 efi_free(initrd->size, initrd->base);
530                 return EFI_LOAD_ERROR;
531         }
532         return EFI_SUCCESS;
533 }
534
535 static
536 efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
537                                      struct linux_efi_initrd *initrd,
538                                      unsigned long soft_limit,
539                                      unsigned long hard_limit)
540 {
541         if (image == NULL)
542                 return EFI_UNSUPPORTED;
543
544         return handle_cmdline_files(image, L"initrd=", sizeof(L"initrd=") - 2,
545                                     soft_limit, hard_limit,
546                                     &initrd->base, &initrd->size);
547 }
548
549 /**
550  * efi_load_initrd() - Load initial RAM disk
551  * @image:      EFI loaded image protocol
552  * @soft_limit: preferred address for loading the initrd
553  * @hard_limit: upper limit address for loading the initrd
554  *
555  * Return:      status code
556  */
557 efi_status_t efi_load_initrd(efi_loaded_image_t *image,
558                              unsigned long soft_limit,
559                              unsigned long hard_limit,
560                              const struct linux_efi_initrd **out)
561 {
562         efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
563         efi_status_t status = EFI_SUCCESS;
564         struct linux_efi_initrd initrd, *tbl;
565
566         if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD) || efi_noinitrd)
567                 return EFI_SUCCESS;
568
569         status = efi_load_initrd_dev_path(&initrd, hard_limit);
570         if (status == EFI_SUCCESS) {
571                 efi_info("Loaded initrd from LINUX_EFI_INITRD_MEDIA_GUID device path\n");
572                 if (initrd.size > 0 &&
573                     efi_measure_tagged_event(initrd.base, initrd.size,
574                                              EFISTUB_EVT_INITRD) == EFI_SUCCESS)
575                         efi_info("Measured initrd data into PCR 9\n");
576         } else if (status == EFI_NOT_FOUND) {
577                 status = efi_load_initrd_cmdline(image, &initrd, soft_limit,
578                                                  hard_limit);
579                 /* command line loader disabled or no initrd= passed? */
580                 if (status == EFI_UNSUPPORTED || status == EFI_NOT_READY)
581                         return EFI_SUCCESS;
582                 if (status == EFI_SUCCESS)
583                         efi_info("Loaded initrd from command line option\n");
584         }
585         if (status != EFI_SUCCESS)
586                 goto failed;
587
588         status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, sizeof(initrd),
589                              (void **)&tbl);
590         if (status != EFI_SUCCESS)
591                 goto free_initrd;
592
593         *tbl = initrd;
594         status = efi_bs_call(install_configuration_table, &tbl_guid, tbl);
595         if (status != EFI_SUCCESS)
596                 goto free_tbl;
597
598         if (out)
599                 *out = tbl;
600         return EFI_SUCCESS;
601
602 free_tbl:
603         efi_bs_call(free_pool, tbl);
604 free_initrd:
605         efi_free(initrd.size, initrd.base);
606 failed:
607         efi_err("Failed to load initrd: 0x%lx\n", status);
608         return status;
609 }
610
611 /**
612  * efi_wait_for_key() - Wait for key stroke
613  * @usec:       number of microseconds to wait for key stroke
614  * @key:        key entered
615  *
616  * Wait for up to @usec microseconds for a key stroke.
617  *
618  * Return:      status code, EFI_SUCCESS if key received
619  */
620 efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key)
621 {
622         efi_event_t events[2], timer;
623         unsigned long index;
624         efi_simple_text_input_protocol_t *con_in;
625         efi_status_t status;
626
627         con_in = efi_table_attr(efi_system_table, con_in);
628         if (!con_in)
629                 return EFI_UNSUPPORTED;
630         efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key));
631
632         status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer);
633         if (status != EFI_SUCCESS)
634                 return status;
635
636         status = efi_bs_call(set_timer, timer, EfiTimerRelative,
637                              EFI_100NSEC_PER_USEC * usec);
638         if (status != EFI_SUCCESS)
639                 return status;
640         efi_set_event_at(events, 1, timer);
641
642         status = efi_bs_call(wait_for_event, 2, events, &index);
643         if (status == EFI_SUCCESS) {
644                 if (index == 0)
645                         status = efi_call_proto(con_in, read_keystroke, key);
646                 else
647                         status = EFI_TIMEOUT;
648         }
649
650         efi_bs_call(close_event, timer);
651
652         return status;
653 }
654
655 /**
656  * efi_remap_image - Remap a loaded image with the appropriate permissions
657  *                   for code and data
658  *
659  * @image_base: the base of the image in memory
660  * @alloc_size: the size of the area in memory occupied by the image
661  * @code_size:  the size of the leading part of the image containing code
662  *              and read-only data
663  *
664  * efi_remap_image() uses the EFI memory attribute protocol to remap the code
665  * region of the loaded image read-only/executable, and the remainder
666  * read-write/non-executable. The code region is assumed to start at the base
667  * of the image, and will therefore cover the PE/COFF header as well.
668  */
669 void efi_remap_image(unsigned long image_base, unsigned alloc_size,
670                      unsigned long code_size)
671 {
672         efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
673         efi_memory_attribute_protocol_t *memattr;
674         efi_status_t status;
675         u64 attr;
676
677         /*
678          * If the firmware implements the EFI_MEMORY_ATTRIBUTE_PROTOCOL, let's
679          * invoke it to remap the text/rodata region of the decompressed image
680          * as read-only and the data/bss region as non-executable.
681          */
682         status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
683         if (status != EFI_SUCCESS)
684                 return;
685
686         // Get the current attributes for the entire region
687         status = memattr->get_memory_attributes(memattr, image_base,
688                                                 alloc_size, &attr);
689         if (status != EFI_SUCCESS) {
690                 efi_warn("Failed to retrieve memory attributes for image region: 0x%lx\n",
691                          status);
692                 return;
693         }
694
695         // Mark the code region as read-only
696         status = memattr->set_memory_attributes(memattr, image_base, code_size,
697                                                 EFI_MEMORY_RO);
698         if (status != EFI_SUCCESS) {
699                 efi_warn("Failed to remap code region read-only\n");
700                 return;
701         }
702
703         // If the entire region was already mapped as non-exec, clear the
704         // attribute from the code region. Otherwise, set it on the data
705         // region.
706         if (attr & EFI_MEMORY_XP) {
707                 status = memattr->clear_memory_attributes(memattr, image_base,
708                                                           code_size,
709                                                           EFI_MEMORY_XP);
710                 if (status != EFI_SUCCESS)
711                         efi_warn("Failed to remap code region executable\n");
712         } else {
713                 status = memattr->set_memory_attributes(memattr,
714                                                         image_base + code_size,
715                                                         alloc_size - code_size,
716                                                         EFI_MEMORY_XP);
717                 if (status != EFI_SUCCESS)
718                         efi_warn("Failed to remap data region non-executable\n");
719         }
720 }