Debian Codegraph @

src/boot/initrd.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "efi-log.h"
4 #include "initrd.h"
5 #include "iovec-util.h"
6 #include "proto/device-path.h"
7 #include "proto/load-file.h"
8 #include "util.h"
9
10 #define LINUX_INITRD_MEDIA_GUID \
11 GUID_DEF(0x5568e427, 0x68fc, 0x4f3d, 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68)
12
13 /* extend LoadFileProtocol */
14 struct initrd_loader {
15 EFI_LOAD_FILE_PROTOCOL load_file;
16 struct iovec data;
17 };
18
19 /* static structure for LINUX_INITRD_MEDIA device path
20 see https://github.com/torvalds/linux/blob/v5.13/drivers/firmware/efi/libstub/efi-stub-helper.c
21 */
22 static const struct {
23 VENDOR_DEVICE_PATH vendor;
24 EFI_DEVICE_PATH end;
25 } _packed_ efi_initrd_device_path = {
26 .vendor = {
27 .Header = {
28 .Type = MEDIA_DEVICE_PATH,
29 .SubType = MEDIA_VENDOR_DP,
30 .Length = sizeof(efi_initrd_device_path.vendor),
31 },
32 .Guid = LINUX_INITRD_MEDIA_GUID
33 },
34 .end = {
35 .Type = END_DEVICE_PATH_TYPE,
36 .SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE,
37 .Length = sizeof(efi_initrd_device_path.end),
38 }
39 };
40
41 static EFIAPI EFI_STATUS initrd_load_file(
42 EFI_LOAD_FILE_PROTOCOL *this,
43 EFI_DEVICE_PATH *file_path,
44 bool boot_policy,
45 size_t *buffer_size,
46 void *buffer) {
47
48 struct initrd_loader *loader;
49
50 if (!this || !buffer_size || !file_path)
51 return EFI_INVALID_PARAMETER;
52 if (boot_policy)
53 return EFI_UNSUPPORTED;
54
55 loader = (struct initrd_loader *) this;
56 if (!iovec_is_set(&loader->data))
57 return EFI_NOT_FOUND;
58
59 if (!buffer || *buffer_size < loader->data.iov_len) {
60 *buffer_size = loader->data.iov_len;
61 return EFI_BUFFER_TOO_SMALL;
62 }
63
64 memcpy(buffer, loader->data.iov_base, loader->data.iov_len);
65 *buffer_size = loader->data.iov_len;
66 return EFI_SUCCESS;
67 }
68
69 EFI_STATUS initrd_register(
70 const struct iovec *initrd,
71 EFI_HANDLE *ret_initrd_handle) {
72
73 EFI_STATUS err;
74
75 assert(ret_initrd_handle);
76
77 /* If no initrd is specified we'll not install any. This avoids registration of the protocol for that
78 * case, leaving it open for something else. */
79
80 if (!iovec_is_set(initrd))
81 return EFI_SUCCESS;
82
83 /* We want to override the LINUX_INITRD_MEDIA device, let's hence first unregister any existing
84 * one. We don't really expect multiple of these to be registered, but who knows? Let's kill all we
85 * can find. */
86 for (unsigned attempt = 0;; attempt++) {
87
88 if (attempt >= 16)
89 return log_debug_status(EFI_DEVICE_ERROR, "Unable to free LINUX_INITRD_MEDIA device path after %u attempts, giving up.", attempt);
90
91 EFI_DEVICE_PATH *dp = (EFI_DEVICE_PATH *) &efi_initrd_device_path;
92 EFI_HANDLE handle = NULL;
93 err = BS->LocateDevicePath(MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), &dp, &handle);
94 if (err == EFI_NOT_FOUND) /* Yay! All gone */
95 break;
96 if (err != EFI_SUCCESS)
97 return log_debug_status(err, "Failed to locate LINUX_INITRD_MEDIA device: %m");
98
99 /* Get the *actually* installed pointer for the device path */
100 err = BS->HandleProtocol(handle, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void**) &dp);
101 if (err != EFI_SUCCESS)
102 return log_debug_status(err, "Failed to acquire DevicePath protocol on LINUX_INITRD_MEDIA device: %m");
103
104 /* Take away the device path protocol */
105 err = BS->UninstallMultipleProtocolInterfaces(
106 handle,
107 MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), dp,
108 /* sentinel= */ NULL);
109 if (err != EFI_SUCCESS)
110 return log_debug_status(err, "Unable to release DevicePath protocol from old handle: %m");
111
112 /* NB: we leave the handle around (and thus leave the LoadFile2 protocol installed), because
113 * the owner might be unhappy if we destroy it for them. It will no longer have the device
114 * path we want to take possession of on it though. The assumption here is that whoever
115 * registered the device path is OK with the device path being taken away, even if it might
116 * not be OK with the handle being invalidated as a whole. */
117
118 log_debug("Successfully unregistered previous LINUX_INITRD_MEDIA device.");
119 }
120
121 _cleanup_free_ struct initrd_loader *loader = xnew(struct initrd_loader, 1);
122 *loader = (struct initrd_loader) {
123 .load_file.LoadFile = initrd_load_file,
124 .data = *initrd,
125 };
126
127 /* create a new handle and register the LoadFile2 protocol with the InitrdMediaPath on it */
128 err = BS->InstallMultipleProtocolInterfaces(
129 ret_initrd_handle,
130 MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), &efi_initrd_device_path,
131 MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), loader,
132 /* sentinel= */ NULL);
133 if (err != EFI_SUCCESS)
134 return log_debug_status(err, "Failed to install new initrd device: %m");
135
136 log_debug("Installed new initrd of size %zu.", loader->data.iov_len);
137
138 TAKE_PTR(loader);
139 return EFI_SUCCESS;
140 }
141
142 EFI_STATUS initrd_unregister(EFI_HANDLE initrd_handle) {
143 struct initrd_loader *loader;
144 EFI_STATUS err;
145
146 if (!initrd_handle)
147 return EFI_SUCCESS;
148
149 /* Get the LoadFile2 protocol that we allocated earlier */
150 err = BS->HandleProtocol(initrd_handle, MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), (void **) &loader);
151 if (err != EFI_SUCCESS)
152 return log_debug_status(err, "Failed to acquire LoadFile2 protocol on our own initrd handle: %m");
153
154 /* We uninstall the DevicePath and the LoadFile2 protocol in separate steps. That's because we want
155 * to gracefully handle the former (because it's OK if something else takes over the device path),
156 * but be strict on the latter, because that's genuinely ours */
157
158 (void) BS->UninstallMultipleProtocolInterfaces(
159 initrd_handle,
160 MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), &efi_initrd_device_path,
161 /* sentinel= */ NULL);
162
163 /* This second call will also invalidate the handle, because it should be the last protocol on the handle */
164 err = BS->UninstallMultipleProtocolInterfaces(
165 initrd_handle,
166 MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), loader,
167 /* sentinel= */ NULL);
168 if (err != EFI_SUCCESS)
169 return log_debug_status(err, "Failed to uninstall LoadFile2 protocol from our own initrd handle: %m");
170
171 free(loader);
172 return EFI_SUCCESS;
173 }
174
175 EFI_STATUS initrd_read_previous(struct iovec *ret_initrd) {
176 EFI_STATUS err;
177
178 /* If there's already an initrd registered, read it out, so that we can incorporate it in ours */
179
180 assert(ret_initrd);
181
182 /* Get from the device path to the handle */
183 EFI_DEVICE_PATH *dp = (EFI_DEVICE_PATH *) &efi_initrd_device_path;
184 EFI_HANDLE handle;
185 err = BS->LocateDevicePath(MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), &dp, &handle);
186 if (err != EFI_SUCCESS)
187 return err;
188
189 /* Get from the handle to the protocol */
190 EFI_LOAD_FILE2_PROTOCOL *protocol = NULL;
191 err = BS->HandleProtocol(handle, MAKE_GUID_PTR(EFI_LOAD_FILE2_PROTOCOL), (void**) &protocol);
192 if (err != EFI_SUCCESS)
193 return err;
194
195 size_t size = 0;
196 err = protocol->LoadFile(protocol, dp, /* bootPolicy= */ false, &size, /* Buffer= */ NULL);
197 if (err == EFI_SUCCESS) /* Success? Kinda unexpected given we set Buffer to NULL, but it probably
198 * means, that the file is zero-sized, let's treat it as such. */
199 size = 0;
200 else if (err != EFI_BUFFER_TOO_SMALL)
201 return err;
202
203 if (size == 0)
204 return EFI_NOT_FOUND; /* Treat empty initrds like missing ones */
205
206 _cleanup_free_ void *data = xmalloc(size);
207 err = protocol->LoadFile(protocol, dp, /* bootPolicy= */ false, &size, data);
208 if (err != EFI_SUCCESS)
209 return err;
210
211 *ret_initrd = (struct iovec) {
212 .iov_base = TAKE_PTR(data),
213 .iov_len = size,
214 };
215
216 return EFI_SUCCESS;
217 }
218
219 EFI_STATUS combine_initrds(
220 const struct iovec initrds[], size_t n_initrds,
221 Pages *ret_initrd_pages, size_t *ret_initrd_size) {
222
223 size_t n = 0;
224
225 /* Combine initrds by concatenation in memory */
226
227 assert(initrds || n_initrds == 0);
228 assert(ret_initrd_pages);
229 assert(ret_initrd_size);
230
231 FOREACH_ARRAY(i, initrds, n_initrds) {
232 /* some initrds (the ones from UKI sections) need padding, pad all to be safe */
233 size_t initrd_size = ALIGN4(i->iov_len);
234 if (n > SIZE_MAX - initrd_size)
235 return EFI_OUT_OF_RESOURCES;
236
237 n += initrd_size;
238 }
239
240 _cleanup_pages_ Pages pages = xmalloc_initrd_pages(n);
241 uint8_t *p = PHYSICAL_ADDRESS_TO_POINTER(pages.addr);
242
243 FOREACH_ARRAY(i, initrds, n_initrds) {
244 size_t pad;
245
246 p = mempcpy(p, i->iov_base, i->iov_len);
247
248 pad = ALIGN4(i->iov_len) - i->iov_len;
249 if (pad == 0)
250 continue;
251
252 memzero(p, pad);
253 p += pad;
254 }
255
256 assert(PHYSICAL_ADDRESS_TO_POINTER(pages.addr + n) == p);
257
258 *ret_initrd_pages = TAKE_STRUCT(pages);
259 *ret_initrd_size = n;
260
261 return EFI_SUCCESS;
262 }