]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/boot/efi/linux.c
boot: Use memcmp/memcpy/memset
[thirdparty/systemd.git] / src / boot / efi / linux.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 /*
4 * Generic Linux boot protocol using the EFI/PE entry point of the kernel. Passes
5 * initrd with the LINUX_INITRD_MEDIA_GUID DevicePath and cmdline with
6 * EFI LoadedImageProtocol.
7 *
8 * This method works for Linux 5.8 and newer on ARM/Aarch64, x86/x68_64 and RISC-V.
9 */
10
11 #include <efi.h>
12 #include <efilib.h>
13
14 #include "initrd.h"
15 #include "linux.h"
16 #include "pe.h"
17 #include "util.h"
18
19 static EFI_LOADED_IMAGE * loaded_image_free(EFI_LOADED_IMAGE *img) {
20 if (!img)
21 return NULL;
22 mfree(img->LoadOptions);
23 return mfree(img);
24 }
25
26 static EFI_STATUS loaded_image_register(
27 const CHAR8 *cmdline, UINTN cmdline_len,
28 const void *linux_buffer, UINTN linux_length,
29 EFI_HANDLE *ret_image) {
30
31 EFI_LOADED_IMAGE *loaded_image = NULL;
32 EFI_STATUS err;
33
34 assert(cmdline || cmdline_len > 0);
35 assert(linux_buffer && linux_length > 0);
36 assert(ret_image);
37
38 /* create and install new LoadedImage Protocol */
39 loaded_image = xnew(EFI_LOADED_IMAGE, 1);
40 *loaded_image = (EFI_LOADED_IMAGE) {
41 .ImageBase = (void *) linux_buffer,
42 .ImageSize = linux_length
43 };
44
45 /* if a cmdline is set convert it to UCS2 */
46 if (cmdline) {
47 loaded_image->LoadOptions = xstra_to_str(cmdline);
48 loaded_image->LoadOptionsSize = strsize16(loaded_image->LoadOptions);
49 }
50
51 /* install a new LoadedImage protocol. ret_handle is a new image handle */
52 err = BS->InstallMultipleProtocolInterfaces(
53 ret_image,
54 &LoadedImageProtocol, loaded_image,
55 NULL);
56 if (EFI_ERROR(err))
57 loaded_image = loaded_image_free(loaded_image);
58
59 return err;
60 }
61
62 static EFI_STATUS loaded_image_unregister(EFI_HANDLE loaded_image_handle) {
63 EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
64 EFI_STATUS err;
65
66 if (!loaded_image_handle)
67 return EFI_SUCCESS;
68
69 /* get the LoadedImage protocol that we allocated earlier */
70 err = BS->OpenProtocol(
71 loaded_image_handle, &LoadedImageProtocol, (void **) &loaded_image,
72 NULL, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
73 if (EFI_ERROR(err))
74 return err;
75
76 /* close the handle */
77 (void) BS->CloseProtocol(loaded_image_handle, &LoadedImageProtocol, NULL, NULL);
78 err = BS->UninstallMultipleProtocolInterfaces(
79 loaded_image_handle,
80 &LoadedImageProtocol, loaded_image,
81 NULL);
82 if (EFI_ERROR(err))
83 return err;
84 loaded_image_handle = NULL;
85 loaded_image = loaded_image_free(loaded_image);
86
87 return EFI_SUCCESS;
88 }
89
90 static inline void cleanup_loaded_image(EFI_HANDLE *loaded_image_handle) {
91 (void) loaded_image_unregister(*loaded_image_handle);
92 *loaded_image_handle = NULL;
93 }
94
95 /* struct to call cleanup_pages */
96 struct pages {
97 EFI_PHYSICAL_ADDRESS addr;
98 UINTN num;
99 };
100
101 static inline void cleanup_pages(struct pages *p) {
102 if (p->addr == 0)
103 return;
104 (void) BS->FreePages(p->addr, p->num);
105 }
106
107 EFI_STATUS linux_exec(
108 EFI_HANDLE image,
109 const CHAR8 *cmdline, UINTN cmdline_len,
110 const void *linux_buffer, UINTN linux_length,
111 const void *initrd_buffer, UINTN initrd_length) {
112
113 _cleanup_(cleanup_initrd) EFI_HANDLE initrd_handle = NULL;
114 _cleanup_(cleanup_loaded_image) EFI_HANDLE loaded_image_handle = NULL;
115 UINT32 kernel_alignment, kernel_size_of_image, kernel_entry_address;
116 EFI_IMAGE_ENTRY_POINT kernel_entry;
117 _cleanup_(cleanup_pages) struct pages kernel = {};
118 void *new_buffer;
119 EFI_STATUS err;
120
121 assert(image);
122 assert(cmdline || cmdline_len == 0);
123 assert(linux_buffer && linux_length > 0);
124 assert(initrd_buffer || initrd_length == 0);
125
126 /* get the necessary fields from the PE header */
127 err = pe_alignment_info(linux_buffer, &kernel_entry_address, &kernel_size_of_image, &kernel_alignment);
128 if (EFI_ERROR(err))
129 return err;
130 /* sanity check */
131 assert(kernel_size_of_image >= linux_length);
132
133 /* Linux kernel complains if it's not loaded at a properly aligned memory address. The correct alignment
134 is provided by Linux as the SegmentAlignment in the PeOptionalHeader. Additionally the kernel needs to
135 be in a memory segment that's SizeOfImage (again from PeOptionalHeader) large, so that the Kernel has
136 space for its BSS section. SizeOfImage is always larger than linux_length, which is only the size of
137 Code, (static) Data and Headers.
138
139 Interrestingly only ARM/Aarch64 and RISC-V kernel stubs check these assertions and can even boot (with warnings)
140 if they are not met. x86 and x86_64 kernel stubs don't do checks and fail if the BSS section is too small.
141 */
142 /* allocate SizeOfImage + SectionAlignment because the new_buffer can move up to Alignment-1 bytes */
143 kernel.num = EFI_SIZE_TO_PAGES(ALIGN_TO(kernel_size_of_image, kernel_alignment) + kernel_alignment);
144 err = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, kernel.num, &kernel.addr);
145 if (EFI_ERROR(err))
146 return EFI_OUT_OF_RESOURCES;
147 new_buffer = PHYSICAL_ADDRESS_TO_POINTER(ALIGN_TO(kernel.addr, kernel_alignment));
148 memcpy(new_buffer, linux_buffer, linux_length);
149 /* zero out rest of memory (probably not needed, but BSS section should be 0) */
150 memset((UINT8 *)new_buffer + linux_length, 0, kernel_size_of_image - linux_length);
151
152 /* get the entry point inside the relocated kernel */
153 kernel_entry = (EFI_IMAGE_ENTRY_POINT) ((const UINT8 *)new_buffer + kernel_entry_address);
154
155 /* register a LoadedImage Protocol in order to pass on the commandline */
156 err = loaded_image_register(cmdline, cmdline_len, new_buffer, linux_length, &loaded_image_handle);
157 if (EFI_ERROR(err))
158 return err;
159
160 /* register a LINUX_INITRD_MEDIA DevicePath to serve the initrd */
161 err = initrd_register(initrd_buffer, initrd_length, &initrd_handle);
162 if (EFI_ERROR(err))
163 return err;
164
165 /* call the kernel */
166 return kernel_entry(loaded_image_handle, ST);
167 }