File: efi-peimage.patch

package info (click to toggle)
grub2 2.14~git20250718.0e36779-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 60,688 kB
  • sloc: ansic: 541,811; asm: 68,074; sh: 9,803; cpp: 2,095; makefile: 1,895; python: 1,518; sed: 446; lex: 393; yacc: 268; awk: 85; lisp: 54; perl: 31
file content (1009 lines) | stat: -rw-r--r-- 31,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
From: Julian Andres Klode <julian.klode@canonical.com>
Date: Mon, 24 Jul 2023 15:26:10 +0200
Subject: efi/peimage: Provide an implementation of load_image, start_image,
 unload_image

The code consumes a PE-COFF image loaded into memory. The functions

* check validity of header
* copy the sections
* relocate the code
* set memory attributes
* invalidate the instruction cache
* execute the image
* return to caller

Caveats:

- We do not always check for over and underflows, but at the
  point we reach this loader, the file has been verified by
  shim already, so this is not much of a concern.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
Signed-off-by: Mate Kukri <mate.kukri@canonical.com>
---
 grub-core/Makefile.core.def    |  13 +
 grub-core/loader/efi/peimage.c | 922 +++++++++++++++++++++++++++++++++++++++++
 include/grub/efi/peimage.h     |  19 +
 3 files changed, 954 insertions(+)
 create mode 100644 grub-core/loader/efi/peimage.c
 create mode 100644 include/grub/efi/peimage.h

diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index b8bd4b3..8dfb883 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -1890,6 +1890,19 @@ module = {
   common = lib/cmdline.c;
 };
 
+module = {
+  name = peimage;
+  efi = loader/efi/peimage.c;
+  enable = arm_efi;
+  enable = arm64_efi;
+  enable = i386_efi;
+  enable = x86_64_efi;
+  enable = riscv32_efi;
+  enable = riscv64_efi;
+  enable = loongarch64_efi;
+};
+
+
 module = {
   name = fdt;
   efi = loader/efi/fdt.c;
diff --git a/grub-core/loader/efi/peimage.c b/grub-core/loader/efi/peimage.c
new file mode 100644
index 0000000..0d73081
--- /dev/null
+++ b/grub-core/loader/efi/peimage.c
@@ -0,0 +1,922 @@
+/* peimage.c - load EFI PE binaries (for Secure Boot support) */
+
+// SPDX-License-Identifier: GPL-3.0+
+
+#include <grub/cache.h>
+#include <grub/cpu/efi/memory.h>
+#include <grub/dl.h>
+#include <grub/efi/efi.h>
+#include <grub/efi/pe32.h>
+#include <grub/efi/peimage.h>
+#include <grub/efi/sb.h>
+#include <grub/env.h>
+#include <grub/misc.h>
+#include <grub/mm.h>
+#include <grub/setjmp.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static grub_dl_t my_mod;
+
+struct image_info
+{
+  void *data;
+  grub_efi_uint32_t data_size;
+  grub_efi_device_path_t *file_path;
+  grub_efi_uint8_t nx_compat;
+  grub_efi_uint16_t machine;
+  grub_efi_uint16_t num_sections;
+  struct grub_pe32_section_table *section;
+  struct grub_pe32_data_directory *reloc;
+  grub_uint64_t image_base;
+  grub_uint32_t section_alignment;
+  grub_uint32_t image_size;
+  grub_uint32_t header_size;
+  void *alloc_addr;
+  grub_uint32_t alloc_pages;
+  void *image_addr;
+  grub_efi_status_t (__grub_efi_api *entry_point) (
+    grub_efi_handle_t image_handle, grub_efi_system_table_t *system_table);
+};
+
+static struct
+{
+  grub_jmp_buf jmp;
+  grub_efi_handle_t image_handle;
+  grub_efi_status_t exit_status;
+  grub_efi_status_t (__grub_efi_api *exit) (grub_efi_handle_t image_handle,
+					    grub_efi_status_t exit_status,
+					    grub_efi_uintn_t exit_data_size,
+					    grub_efi_char16_t *exit_data);
+} started_image;
+
+
+static grub_uint16_t machines[] = {
+#if defined(__x86_64__)
+  GRUB_PE32_MACHINE_X86_64,
+#elif defined(__i386__)
+  GRUB_PE32_MACHINE_I386,
+#elif defined(__aarch64__)
+  GRUB_PE32_MACHINE_ARM64,
+#elif defined(__arm__)
+  GRUB_PE32_MACHINE_ARMTHUMB_MIXED,
+#elif defined(__riscv) && __riscv_xlen == 32
+  GRUB_PE32_MACHINE_RISCV32,
+#elif defined(__riscv) && __riscv_xlen == 64
+  GRUB_PE32_MACHINE_RISCV64,
+#elif defined(__loongarch__) && __loongarch_grlen == 64
+  GRUB_PE32_MACHINE_LOONGARCH64,
+#endif
+};
+
+/**
+ * check_machine_type() - check if the machine type matches the architecture
+ *
+ * @machine:	the value of the Machine field of the COFF file header.
+ * Return:	status code
+ */
+static grub_efi_status_t
+check_machine_type (grub_uint16_t machine)
+{
+  for (grub_size_t i = 0; i < sizeof (machines) / sizeof (*machines); ++i)
+    {
+      if (machine == machines[i])
+	return GRUB_EFI_SUCCESS;
+    }
+
+  return GRUB_EFI_LOAD_ERROR;
+}
+
+/**
+ * check_pe_header() - check the headers of a PE-COFF image
+ *
+ * @info:	information about the image
+ */
+static grub_efi_status_t
+check_pe_header (struct image_info *info)
+{
+  struct grub_msdos_image_header *dos_stub = info->data;
+  void *pe_magic;
+  struct grub_pe32_coff_header *coff_header;
+  struct grub_pe32_optional_header *pe32_header;
+  struct grub_pe64_optional_header *pe64_header;
+
+  if (info->data_size < sizeof (struct grub_msdos_image_header))
+    {
+      grub_dprintf ("peimage", "truncated image\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+  if (dos_stub->msdos_magic != GRUB_PE32_MAGIC)
+    {
+      grub_dprintf ("peimage", "not a PE-COFF file\n");
+      return GRUB_EFI_UNSUPPORTED;
+    }
+  if (info->data_size < dos_stub->pe_image_header_offset
+			    + GRUB_PE32_SIGNATURE_SIZE
+			    + sizeof (struct grub_pe32_coff_header)
+			    + sizeof (struct grub_pe64_optional_header))
+    {
+      grub_dprintf ("peimage", "truncated image\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+  pe_magic
+      = (void *)((unsigned long)info->data + dos_stub->pe_image_header_offset);
+  if (grub_memcmp (pe_magic, GRUB_PE32_SIGNATURE, GRUB_PE32_SIGNATURE_SIZE))
+    {
+      grub_dprintf ("peimage", "not a PE-COFF file\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  coff_header = (void *)((unsigned long)pe_magic + GRUB_PE32_SIGNATURE_SIZE);
+  info->machine = coff_header->machine;
+  info->num_sections = coff_header->num_sections;
+
+  if (check_machine_type (info->machine) != GRUB_EFI_SUCCESS)
+    {
+      grub_dprintf ("peimage", "wrong machine type %u\n", coff_header->machine);
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  pe32_header = (void *)((unsigned long)coff_header + sizeof (*coff_header));
+  pe64_header = (void *)((unsigned long)coff_header + sizeof (*coff_header));
+
+  switch (pe32_header->magic)
+    {
+    case GRUB_PE32_PE32_MAGIC:
+      if (pe32_header->subsystem != GRUB_PE32_SUBSYSTEM_EFI_APPLICATION)
+	{
+	  grub_dprintf ("peimage", "expected EFI application\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+      info->nx_compat =
+	(pe32_header->dll_characteristics & GRUB_PE32_NX_COMPAT) != 0;
+      info->section_alignment = pe32_header->section_alignment;
+      info->image_base = pe32_header->image_base;
+      info->image_size = pe32_header->image_size;
+      info->entry_point = (void *)(unsigned long)pe32_header->entry_addr;
+      info->header_size = pe32_header->header_size;
+      if (info->data_size < info->header_size)
+	{
+	  grub_dprintf ("peimage", "truncated image\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+
+      if (pe32_header->num_data_directories >= 6
+	  && pe32_header->base_relocation_table.size)
+	info->reloc = &pe32_header->base_relocation_table;
+
+      info->section
+	  = (void *)((unsigned long)&pe32_header->export_table
+		     + pe32_header->num_data_directories
+			   * sizeof (struct grub_pe32_data_directory));
+      break;
+    case GRUB_PE32_PE64_MAGIC:
+      if (pe64_header->subsystem != GRUB_PE32_SUBSYSTEM_EFI_APPLICATION)
+	{
+	  grub_dprintf ("peimage", "expected EFI application\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+      info->nx_compat =
+	(pe64_header->dll_characteristics & GRUB_PE32_NX_COMPAT) != 0;
+      info->section_alignment = pe64_header->section_alignment;
+      info->image_base = pe64_header->image_base;
+      info->image_size = pe64_header->image_size;
+      info->entry_point = (void *)(unsigned long)pe64_header->entry_addr;
+      info->header_size = pe64_header->header_size;
+      if (info->data_size < info->header_size)
+	{
+	  grub_dprintf ("peimage", "truncated image\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+
+      if (pe64_header->num_data_directories >= 6
+	  && pe64_header->base_relocation_table.size)
+	info->reloc = &pe64_header->base_relocation_table;
+
+      info->section
+	  = (void *)((unsigned long)&pe64_header->export_table
+		     + pe64_header->num_data_directories
+			   * sizeof (struct grub_pe32_data_directory));
+      break;
+    default:
+      grub_dprintf ("peimage", "not a PE-COFF file\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  if (info->nx_compat && info->section_alignment < GRUB_EFI_PAGE_SIZE)
+    {
+      grub_dprintf ("peimage", "NX compatible image with sub-page section alignment\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  if ((unsigned long)info->section
+	  + info->num_sections * sizeof (*info->section)
+      > (unsigned long)info->data + info->data_size)
+    {
+      grub_dprintf ("peimage", "truncated image\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  grub_dprintf ("peimage", "PE-COFF header checked\n");
+
+  return GRUB_EFI_SUCCESS;
+}
+
+/**
+ * load_sections() - load image sections into memory
+ *
+ * Allocate fresh memory and copy the image sections there.
+ *
+ * @info:	image information
+ */
+static grub_efi_status_t
+load_sections (struct image_info *info)
+{
+  struct grub_pe32_section_table *section;
+  unsigned long align_mask = 0xfff;
+  grub_addr_t section_addr;
+
+  /* Section alignment must be a power of two */
+  if (info->section_alignment & (info->section_alignment - 1))
+    {
+      grub_dprintf ("peimage", "invalid section alignment\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  if (info->section_alignment > align_mask)
+    align_mask = info->section_alignment - 1;
+
+  info->alloc_pages = GRUB_EFI_BYTES_TO_PAGES (info->image_size + (align_mask & ~0xfffUL));
+
+  info->alloc_addr = grub_efi_allocate_pages_real (
+      GRUB_EFI_MAX_USABLE_ADDRESS, info->alloc_pages,
+      GRUB_EFI_ALLOCATE_MAX_ADDRESS, GRUB_EFI_LOADER_CODE);
+  if (!info->alloc_addr)
+    return GRUB_EFI_OUT_OF_RESOURCES;
+
+  info->image_addr
+      = (void *)(((unsigned long)info->alloc_addr + align_mask) & ~align_mask);
+
+  grub_memcpy (info->image_addr, info->data, info->header_size);
+  for (section = &info->section[0];
+       section < &info->section[info->num_sections]; ++section)
+    {
+      if (section->virtual_address < info->header_size
+	  || (section->raw_data_size
+	      && section->raw_data_offset < info->header_size))
+	{
+	  grub_dprintf ("peimage", "section inside header\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+      if (section->raw_data_offset + section->raw_data_size > info->data_size)
+	{
+	  grub_dprintf ("peimage", "truncated image\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+      if (section->virtual_address + section->virtual_size > info->image_size)
+	{
+	  grub_dprintf ("peimage", "section outside image\n");
+	  return GRUB_EFI_LOAD_ERROR;
+	}
+
+      if (section->raw_data_size)
+	{
+	  if (section->raw_data_offset < info->header_size)
+	    {
+	      grub_dprintf ("peimage", "section data inside header\n");
+	      return GRUB_EFI_LOAD_ERROR;
+	    }
+	  if (section->raw_data_offset + section->raw_data_size > info->data_size)
+	    {
+	      grub_dprintf ("peimage", "section data outside file\n");
+	      return GRUB_EFI_LOAD_ERROR;
+	    }
+	  if (section->raw_data_size > ALIGN_UP(section->virtual_size, GRUB_EFI_PAGE_SIZE))
+	    {
+	      grub_dprintf ("peimage", "section data larger than virtual size\n");
+	      return GRUB_EFI_LOAD_ERROR;
+	    }
+	}
+
+      section_addr = (grub_addr_t)info->image_addr + section->virtual_address;
+
+      grub_memset ((void *) section_addr, 0, section->virtual_size);
+      grub_memcpy (
+	  (void *) section_addr,
+	  (void *)((unsigned long)info->data + section->raw_data_offset),
+	  section->raw_data_size);
+    }
+
+  info->entry_point = (void *)((unsigned long)info->entry_point
+			       + (unsigned long)info->image_addr);
+
+  grub_dprintf ("peimage", "sections loaded\n");
+
+  return GRUB_EFI_SUCCESS;
+}
+
+/**
+ * lo12i_get() - get the immediate value of a format I instruction
+ *
+ * Instruction format I::
+ *
+ *     +---------------------------------------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +-----------------------+---------+-----+---------+-------------+
+ *     |       imm[11:0]       |   rs1   |fun3 |   rd    |   opcode    |
+ *     +-----------------------+---------+-----+---------+-------------+
+ *
+ * @instr:	pointer to instruction
+ * Return:	immediate value
+ */
+static grub_uint16_t
+lo12i_get (grub_uint32_t *instr)
+{
+  return ((*instr & 0xfff00000) >> 20);
+}
+
+/**
+ * lo12i_set() - set the immediate value of a format I instruction
+ *
+ * Instruction format I::
+ *
+ *     +---------------------------------------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +-----------------------+---------+-----+---------+-------------+
+ *     |       imm[11:0]       |   rs1   |fun3 |   rd    |   opcode    |
+ *     +-----------------------+---------+-----+---------+-------------+
+ *
+ * @instr:	pointer to instruction
+ * @imm:	immediate value
+ */
+static void
+lo12i_set (grub_uint32_t *instr, grub_uint32_t imm)
+{
+  *instr = (*instr & 0x000fffff) | (imm & 0x00000fff << 20);
+}
+
+/**
+ * hi20_get() - get the immediate value of a format I instruction
+ *
+ * Instruction format U::
+ *
+ *     +---------------------------------------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +---------------------------------------+---------+-------------+
+ *     |                imm[31:12]             |   rd    |   opcode    |
+ *     +---------------------------------------+---------+-------------+
+ *
+ * @instr:	pointer to instruction
+ * Return:	immediate value
+ */
+static grub_uint16_t
+hi20_get (grub_uint32_t *instr)
+{
+  return *instr & 0xfffff000;
+}
+
+/**
+ * hi20_set() - set the immediate value of a format I instruction
+ *
+ * Instruction format U::
+ *
+ *     +---------------------------------------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +---------------------------------------+---------+-------------+
+ *     |                imm[31:12]             |   rd    |   opcode    |
+ *     +---------------------------------------+---------+-------------+
+ *
+ * @instr:	pointer to instruction
+ * @imm:	immediate value
+ */
+static void
+hi20_set (grub_uint32_t *instr, grub_uint32_t imm)
+{
+  *instr = (*instr & 0x00000fff) | (imm & 0xfffff000);
+}
+
+/**
+ * lo12s_get() - get the immediate value of a format I instruction
+ *
+ * Instruction format S::
+ *
+ *     +---------------------------------------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +-------------+---------+---------+-----+----+----+-------------+
+ *     |  imm[11:5]  |   rs2   |   rs1   |fun3 |imm[4:0] |   opcode    |
+ *     +-------------+---------+---------+-----+----+----+-------------+
+ *
+ * @instr:	pointer to instruction
+ * Return:	immediate value
+ */
+static grub_uint16_t
+lo12s_get (grub_uint32_t *instr)
+{
+  return ((*instr & 0x00000f80) >> 7) | ((*instr & 0xfe000000) >> 20);
+}
+
+/**
+ * lo12s_set() - set the immediate value of a format I instruction
+ *
+ * Instruction format S::
+ *
+ *     +---------------------------------------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +-------------+---------+---------+-----+----+----+-------------+
+ *     |  imm[11:5]  |   rs2   |   rs1   |fun3 |imm[4:0] |   opcode    |
+ *     +-------------+---------+---------+-----+----+----+-------------+
+ *
+ * @instr:	pointer to instruction
+ * @imm:	immediate value
+ */
+static void
+lo12s_set (grub_uint32_t *instr, grub_uint32_t imm)
+{
+  *instr = (*instr & 0x01fff07f) | (imm & 0x00000fe0 << 20)
+	   | (imm & 0x0000001f << 7);
+}
+
+/**
+ * movw_get_imm() - get the immediate value of MOVT and MOVW instructions
+ *
+ * MOVT::
+ *
+ *     +-------------------------------+-------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0|f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *     |1 1 1 1 0|i|1 0 1 1 0 0| imm4  |0| imm3|   Rd  |     imm8      |
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *
+ * MOVW::
+ *
+ *     +-------------------------------+-------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0|f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *     |1 1 1 1 0|i|1 0 0 1 0 0| imm4  |0| imm3|   Rd  |     imm8      |
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *
+ * @instr:	pointer to instruction
+ * Return:	immediate value
+ */
+static grub_uint16_t
+movw_get_imm (grub_uint16_t *instr)
+{
+  /* imm16 = imm4:i:imm3:imm8; */
+  return (instr[1] & 0x00ff) | ((instr[1] & 0x7000) >> 3)
+	 | ((instr[0] & 0x0400) >> 8) | ((instr[0] & 0x000f) << 12);
+}
+
+/**
+ * movw_set_imm() - set the immediate value of MOVT and MOVW instructions
+ *
+ * MOVT::
+ *
+ *     +-------------------------------+-------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0|f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *     |1 1 1 1 0|i|1 0 1 1 0 0| imm4  |0| imm3|   Rd  |     imm8      |
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *
+ * MOVW::
+ *
+ *     +-------------------------------+-------------------------------+
+ *     |f e d c b a 9 8 7 6 5 4 3 2 1 0|f e d c b a 9 8 7 6 5 4 3 2 1 0|
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *     |1 1 1 1 0|i|1 0 0 1 0 0| imm4  |0| imm3|   Rd  |     imm8      |
+ *     +---------+-+-----------+-------+-+-----+-------+---------------+
+ *
+ * @instr:	pointer to instruction
+ * @imm		immediate value
+ */
+static void
+movw_set_imm (grub_uint16_t *instr, grub_uint16_t imm)
+{
+  /* imm16 = imm4:i:imm3:imm8; */
+  instr[0] = (instr[0] & 0xfbf0) | (imm & 0xf000) >> 12 | (imm & 0x0800) << 3;
+  instr[1] = (instr[0] & 0x8f00) | (imm & 0xff) | (imm & 0x0700) >> 4;
+}
+
+/**
+ * relocate() - apply relocations to the image
+ *
+ * @info:	information about the loaded image
+ */
+static grub_efi_status_t
+relocate (struct image_info *info)
+{
+  struct grub_pe32_fixup_block *block, *reloc_end;
+  unsigned long offset;
+  grub_uint16_t reloc_type;
+  grub_uint16_t *reloc_entry;
+  grub_uint32_t *rvhi20_addr = NULL;
+
+  if (!info->reloc || !(info->reloc->size))
+    {
+      grub_dprintf ("peimage", "no relocations\n");
+      return GRUB_EFI_SUCCESS;
+    }
+
+  if (info->reloc->rva + info->reloc->size > info->image_size)
+    {
+      grub_dprintf ("peimage", "relocation block outside image\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  /*
+   * The relocations are based on the difference between
+   * actual load address and the preferred base address.
+   */
+  offset = (unsigned long)info->image_addr - info->image_base;
+
+  block = (void *)((unsigned long)info->image_addr + info->reloc->rva);
+  reloc_end = (void *)((unsigned long)block + info->reloc->size);
+
+  for (; block < reloc_end;
+       block = (void *)((unsigned long)block + block->block_size))
+    {
+      reloc_entry = block->entries;
+      grub_uint16_t *block_end
+	  = (void *)((unsigned long)block + block->block_size);
+
+      for (; reloc_entry < block_end; ++reloc_entry)
+	{
+	  void *addr = (void *)((unsigned long)info->image_addr
+				+ block->page_rva + (*reloc_entry & 0xfff));
+
+	  reloc_type = *reloc_entry >> 12;
+
+	  switch (reloc_type)
+	    {
+	    case GRUB_PE32_REL_BASED_ABSOLUTE:
+	      /* skip */
+	      break;
+	    case GRUB_PE32_REL_BASED_HIGH:
+	      *(grub_uint16_t *)addr += offset >> 16;
+	      break;
+	    case GRUB_PE32_REL_BASED_LOW:
+	      *(grub_uint16_t *)addr += offset;
+	      break;
+	    case GRUB_PE32_REL_BASED_HIGHLOW:
+	      *(grub_uint32_t *)addr += offset;
+	      break;
+	    case GRUB_PE32_REL_BASED_RISCV_HI20:
+	      switch (info->machine)
+		{
+		case GRUB_PE32_MACHINE_RISCV32:
+		case GRUB_PE32_MACHINE_RISCV64:
+		  rvhi20_addr = addr;
+		  break;
+		default:
+		  goto bad_reloc;
+		}
+	      break;
+	    case GRUB_PE32_REL_BASED_ARM_MOV32T:
+	      /* = GRUB_PE32_REL_BASED_RISCV_LOW12I */
+	      switch (info->machine)
+		{
+		case GRUB_PE32_MACHINE_ARMTHUMB_MIXED:
+		  {
+		    grub_uint16_t *instr = addr;
+		    grub_uint32_t val;
+
+		    val = movw_get_imm (&instr[0])
+			  + (movw_get_imm (&instr[2]) << 16) + offset;
+		    movw_set_imm (&instr[0], val);
+		    movw_set_imm (&instr[2], val >> 16);
+		    break;
+		  }
+		case GRUB_PE32_MACHINE_RISCV32:
+		case GRUB_PE32_MACHINE_RISCV64:
+		  if (rvhi20_addr)
+		    {
+		      grub_uint32_t val
+			  = hi20_get (rvhi20_addr) + lo12i_get (addr) + offset;
+		      hi20_set (rvhi20_addr, val);
+		      lo12i_set (addr, val);
+		      rvhi20_addr = NULL;
+		    }
+		  else
+		    {
+		      goto bad_reloc;
+		    }
+		  break;
+		default:
+		  goto bad_reloc;
+		}
+	      break;
+	    case GRUB_PE32_REL_BASED_RISCV_LOW12S:
+	      switch (info->machine)
+		{
+		case GRUB_PE32_MACHINE_RISCV32:
+		case GRUB_PE32_MACHINE_RISCV64:
+		  if (rvhi20_addr)
+		    {
+		      grub_uint32_t val
+			  = hi20_get (rvhi20_addr) + lo12s_get (addr) + offset;
+		      hi20_set (rvhi20_addr, val);
+		      lo12s_set (addr, val);
+		      rvhi20_addr = NULL;
+		    }
+		  else
+		    {
+		      goto bad_reloc;
+		    }
+		  break;
+		default:
+		  goto bad_reloc;
+		}
+	      break;
+	    case GRUB_PE32_REL_BASED_DIR64:
+	      *(grub_uint64_t *)addr += offset;
+	      break;
+	    default:
+	      goto bad_reloc;
+	    }
+	}
+    }
+
+  grub_dprintf ("peimage", "image relocated\n");
+
+  return GRUB_EFI_SUCCESS;
+
+bad_reloc:
+  grub_dprintf ("peimage", "unsupported relocation type %d, rva 0x%08lx\n",
+	      *reloc_entry >> 12,
+	      (unsigned long)reloc_entry - (unsigned long)info->image_addr);
+  return GRUB_EFI_LOAD_ERROR;
+}
+
+static grub_efi_status_t
+set_section_mem_attrs (struct image_info *info)
+{
+  struct grub_pe32_section_table *section;
+  grub_addr_t section_addr;
+  grub_err_t err;
+  grub_uint64_t section_set_mem_attr, section_clear_mem_attr;
+
+  for (section = &info->section[0];
+       section < &info->section[info->num_sections]; ++section)
+    {
+      section_addr = (grub_addr_t)info->image_addr + section->virtual_address;
+
+      if (section_addr & (GRUB_EFI_PAGE_SIZE - 1))
+        {
+          grub_dprintf ("peimage", "NX compatible image with badly aligned section\n");
+          return GRUB_EFI_LOAD_ERROR;
+        }
+
+      if ((section->characteristics & GRUB_PE32_SCN_MEM_WRITE) && (section->characteristics & GRUB_PE32_SCN_MEM_EXECUTE))
+        {
+          grub_dprintf ("peimage", "NX compatible image with W|X section\n");
+          return GRUB_EFI_LOAD_ERROR;
+        }
+
+      section_set_mem_attr = 0;
+      section_clear_mem_attr = 0;
+      if (section->characteristics & GRUB_PE32_SCN_MEM_READ)
+        section_set_mem_attr |= GRUB_MEM_ATTR_R;
+      else
+        section_clear_mem_attr |= GRUB_MEM_ATTR_R;
+      if (section->characteristics & GRUB_PE32_SCN_MEM_WRITE)
+        section_set_mem_attr |= GRUB_MEM_ATTR_W;
+      else
+        section_clear_mem_attr |= GRUB_MEM_ATTR_W;
+      if (section->characteristics & GRUB_PE32_SCN_MEM_EXECUTE)
+        section_set_mem_attr |= GRUB_MEM_ATTR_X;
+      else
+        section_clear_mem_attr |= GRUB_MEM_ATTR_X;
+
+      err = grub_update_mem_attrs (section_addr,
+                                   GRUB_EFI_BYTES_TO_PAGES(section->virtual_size) * GRUB_EFI_PAGE_SIZE,
+                                   section_set_mem_attr, section_clear_mem_attr);
+      if (err != GRUB_ERR_NONE)
+        {
+          grub_dprintf ("peimage", "Failed to set PE section memory attributes\n");
+          return GRUB_EFI_LOAD_ERROR;
+        }
+    }
+
+  grub_dprintf ("peimage", "PE section memory attributes set\n");
+  return GRUB_EFI_SUCCESS;
+}
+
+/**
+ * efi_exit() - replacement for EFI_BOOT_SERVICES.Exit()
+ *
+ * This function is inserted into system table to trap invocations of
+ * EFI_BOOT_SERVICES.Exit(). If Exit() is called with our handle
+ * return to our start routine using a long jump.
+ *
+ * @image_handle:       handle of the application as passed on entry
+ * @exit_status:        the images exit code
+ * @exit_data_size:     size of @exit_data
+ * @exit_data:          null terminated string followed by optional data
+ */
+static grub_efi_status_t __grub_efi_api
+efi_exit (grub_efi_handle_t image_handle, grub_efi_status_t exit_status,
+	  grub_efi_uintn_t exit_data_size, grub_efi_char16_t *exit_data)
+{
+  grub_efi_system_table->boot_services->exit = started_image.exit;
+
+  if (!image_handle)
+    return GRUB_EFI_INVALID_PARAMETER;
+
+  if (image_handle != started_image.image_handle)
+    {
+      grub_dprintf ("peimage", "delegating Exit()\n");
+      return started_image.exit (image_handle, exit_status, exit_data_size,
+				(grub_efi_char16_t *)exit_data);
+    }
+
+  started_image.exit_status = exit_status;
+
+  if (exit_status != GRUB_EFI_SUCCESS)
+    {
+      grub_printf ("Application failed, r = %d\n",
+		   (int)exit_status & 0x7fffffff);
+      if (exit_data_size && exit_data)
+        {
+	  grub_printf ("exit message: ");
+	  for (grub_efi_uintn_t pos = 0;
+	       exit_data[pos] && pos < exit_data_size / 2; ++pos)
+	    grub_printf ("%C", exit_data[pos]);
+	  grub_printf ("\n");
+        }
+    }
+  if (exit_data_size && exit_data)
+    {
+      /* exit data must be freed by the caller */
+      grub_efi_system_table->boot_services->free_pool (exit_data);
+    }
+  grub_longjmp (started_image.jmp, 1);
+}
+
+static grub_efi_status_t __grub_efi_api
+do_unload_image (grub_efi_handle_t image_handle);
+
+/**
+ * start_image() - our implementation of StartImage()
+ *
+ * As we do not load the image via LoadImage() we need our own implementation
+ * of StartImage() to launch the PE-COFF image.
+ */
+static grub_efi_status_t
+start_image (struct image_info *info)
+{
+  int ret;
+  grub_efi_status_t status;
+  grub_efi_loaded_image_t *loaded_image;
+
+  /*
+   * NOTE: We cannot easily comply with the UEFI specification and provide the
+   * child its own handle, otherwise things can go horribly wrong if said custom
+   * handle is passed to the firmware by child images
+   */
+  started_image.image_handle = grub_efi_image_handle;
+
+  loaded_image = grub_efi_get_loaded_image (grub_efi_image_handle);
+  if (loaded_image)
+    {
+      loaded_image->image_base = info->image_addr;
+      loaded_image->image_size = info->image_size;
+
+      // Pass just the file path portion to the loaded image
+      loaded_image->file_path = info->file_path;
+      while (loaded_image->file_path &&
+	     (loaded_image->file_path->type != GRUB_EFI_MEDIA_DEVICE_PATH_TYPE
+	      || loaded_image->file_path->subtype != GRUB_EFI_FILE_PATH_DEVICE_PATH_SUBTYPE))
+	loaded_image->file_path = GRUB_EFI_NEXT_DEVICE_PATH (loaded_image->file_path);
+    }
+  else
+    {
+      grub_dprintf ("peimage", "Loaded image protocol missing\n");
+    }
+
+  ret = grub_setjmp (started_image.jmp);
+  if (ret)
+    {
+      do_unload_image(started_image.image_handle);
+      started_image.image_handle = NULL;
+      return started_image.exit_status;
+    }
+
+  started_image.exit = grub_efi_system_table->boot_services->exit;
+  grub_efi_system_table->boot_services->exit = efi_exit;
+
+  grub_dprintf (
+      "peimage",
+      "Executing image loaded at 0x%lx\nEntry point 0x%lx\nSize 0x%08x\n",
+      (unsigned long)info->image_addr, (unsigned long)info->entry_point,
+      info->image_size);
+
+  /* Invalidate the instruction cache */
+  grub_arch_sync_caches (info->image_addr, info->image_size);
+
+  status = info->entry_point (started_image.image_handle, grub_efi_system_table);
+
+  grub_dprintf ("peimage", "Application returned\n");
+
+  return efi_exit (started_image.image_handle, status, 0, NULL);
+}
+
+static struct image_info info;
+
+/* TODO: move the creation of the load options here */
+static grub_efi_status_t __grub_efi_api
+do_load_image (grub_efi_boolean_t boot_policy __attribute__ ((unused)),
+               grub_efi_handle_t parent_image_handle __attribute__ ((unused)),
+               grub_efi_device_path_t *file_path,
+               void *source_buffer, grub_efi_uintn_t source_size,
+               grub_efi_handle_t *image_handle)
+{
+  grub_efi_status_t ret = GRUB_EFI_SUCCESS;
+  if (info.data != NULL)
+    {
+      grub_dprintf ("peimage", "cannot load multiple images\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+
+  grub_dl_ref (my_mod);
+
+  info = (struct image_info){
+    .data = source_buffer,
+    .data_size = source_size,
+    .file_path = grub_efi_duplicate_device_path(file_path),
+  };
+
+  ret = check_pe_header (&info);
+  if (ret != GRUB_EFI_SUCCESS)
+    goto err;
+
+  ret = load_sections (&info);
+  if (ret != GRUB_EFI_SUCCESS)
+    goto err;
+
+  ret = relocate (&info);
+  if (ret != GRUB_EFI_SUCCESS)
+    goto err;
+
+  if (info.nx_compat)
+    {
+      ret = set_section_mem_attrs (&info);
+      if (ret != GRUB_EFI_SUCCESS)
+        goto err;
+    }
+
+  // We are hacking this up as we go along
+  *image_handle = grub_efi_image_handle;
+  return GRUB_EFI_SUCCESS;
+
+err:
+  do_unload_image (NULL); // NOTE: arg unused
+  return ret;
+}
+
+static grub_efi_status_t __grub_efi_api
+do_start_image (grub_efi_handle_t image_handle __attribute__ ((unused)),
+		grub_efi_uintn_t *exit_data_size __attribute__ ((unused)),
+		grub_efi_char16_t **exit_data __attribute__ ((unused)))
+{
+  if (info.data == NULL)
+    {
+      grub_dprintf ("peimage", "image not loaded\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+  return start_image (&info);
+}
+
+static grub_efi_status_t __grub_efi_api
+do_unload_image (grub_efi_handle_t image_handle __attribute__ ((unused)))
+{
+  if (info.data == NULL)
+    {
+      grub_dprintf ("peimage", "image not loaded\n");
+      return GRUB_EFI_LOAD_ERROR;
+    }
+  if (info.alloc_addr)
+    grub_efi_free_pages ((unsigned long)info.alloc_addr, info.alloc_pages);
+  if (info.file_path)
+    grub_free(info.file_path);
+
+  grub_dl_unref (my_mod);
+  info = (struct image_info){};
+
+  return GRUB_EFI_SUCCESS;
+}
+
+static const grub_efi_loader_t peimage_loader = {
+  .load_image = do_load_image,
+  .start_image = do_start_image,
+  .unload_image = do_unload_image,
+};
+
+GRUB_MOD_INIT (peimage)
+{
+  grub_efi_register_loader (&peimage_loader);
+  my_mod = mod;
+}
+
+GRUB_MOD_FINI (peimage)
+{
+  grub_efi_unregister_loader (&peimage_loader);
+}
diff --git a/include/grub/efi/peimage.h b/include/grub/efi/peimage.h
new file mode 100644
index 0000000..e57d6de
--- /dev/null
+++ b/include/grub/efi/peimage.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-3.0+ */
+
+/* Distinguishing our loaded image handles from the firmware's */
+#define GRUB_PEIMAGE_MARKER_GUID \
+  { 0xda24567a, 0xf899, 0x4566, \
+    { 0xb8, 0x27, 0x9f, 0x66, 0x00, 0xc2, 0x14, 0x39 } \
+  }
+
+/* Associates an image handle with the device path it was loaded from */
+#define GRUB_EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID \
+  { 0xbc62157e, 0x3e33, 0x4fec, \
+    { 0x99, 0x20, 0x2d, 0x3b, 0x36, 0xd7, 0x50, 0xdf } \
+  }
+
+/* Revision defined for the EFI_LOADED_IMAGE_PROTOCOL */
+#define GRUB_EFI_LOADED_IMAGE_REVISION  0x1000
+
+/* Value of the signature field of a PE image header */
+#define GRUB_PE32_SIGNATURE "PE\0"