blob: 23508431c833e7b7d7b7ae1c61d94d5a8c53f9f6 [file] [log] [blame]
Ilias Apalodimasec80b472020-02-21 09:55:45 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2020, Linaro Limited
4 */
5
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +02006#define LOG_CATEGORY LOGC_EFI
Ilias Apalodimasec80b472020-02-21 09:55:45 +02007#include <efi_loader.h>
8#include <efi_load_initrd.h>
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +02009#include <efi_variable.h>
Ilias Apalodimas0c444522020-12-31 12:33:23 +020010#include <fs.h>
11#include <malloc.h>
12#include <mapmem.h>
Ilias Apalodimasec80b472020-02-21 09:55:45 +020013
Ilias Apalodimasec80b472020-02-21 09:55:45 +020014static efi_status_t EFIAPI
15efi_load_file2_initrd(struct efi_load_file_protocol *this,
16 struct efi_device_path *file_path, bool boot_policy,
17 efi_uintn_t *buffer_size, void *buffer);
18
19static const struct efi_load_file_protocol efi_lf2_protocol = {
20 .load_file = efi_load_file2_initrd,
21};
22
23/*
24 * Device path defined by Linux to identify the handle providing the
25 * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk.
26 */
Heinrich Schuchardt535321c2024-06-10 10:00:01 +020027static const struct efi_lo_dp_prefix dp_lf2_handle = {
Ilias Apalodimasec80b472020-02-21 09:55:45 +020028 .vendor = {
29 {
30 DEVICE_PATH_TYPE_MEDIA_DEVICE,
31 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020032 sizeof(dp_lf2_handle.vendor),
Ilias Apalodimasec80b472020-02-21 09:55:45 +020033 },
34 EFI_INITRD_MEDIA_GUID,
35 },
36 .end = {
37 DEVICE_PATH_TYPE_END,
38 DEVICE_PATH_SUB_TYPE_END,
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020039 sizeof(dp_lf2_handle.end),
Ilias Apalodimasec80b472020-02-21 09:55:45 +020040 }
41};
42
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020043static efi_handle_t efi_initrd_handle;
44
Ilias Apalodimasec80b472020-02-21 09:55:45 +020045/**
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020046 * get_initrd_fp() - Get initrd device path from a FilePathList device path
Ilias Apalodimasec80b472020-02-21 09:55:45 +020047 *
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020048 * @initrd_fp: the final initrd filepath
Ilias Apalodimasec80b472020-02-21 09:55:45 +020049 *
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020050 * Return: status code. Caller must free initrd_fp
Ilias Apalodimasec80b472020-02-21 09:55:45 +020051 */
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020052static efi_status_t get_initrd_fp(struct efi_device_path **initrd_fp)
Ilias Apalodimasec80b472020-02-21 09:55:45 +020053{
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020054 struct efi_device_path *dp = NULL;
Ilias Apalodimasec80b472020-02-21 09:55:45 +020055
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020056 /*
57 * if bootmgr is setup with and initrd, the device path will be
58 * in the FilePathList[] of our load options in Boot####.
59 * The first device path of the multi instance device path will
60 * start with a VenMedia and the initrds will follow.
61 *
62 * If the device path is not found return EFI_INVALID_PARAMETER.
63 * We can then use this specific return value and not install the
64 * protocol, while allowing the boot to continue
65 */
Heinrich Schuchardt8745f132024-04-26 16:13:08 +020066 dp = efi_get_dp_from_boot(&efi_lf2_initrd_guid);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020067 if (!dp)
68 return EFI_INVALID_PARAMETER;
Ilias Apalodimasec80b472020-02-21 09:55:45 +020069
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020070 *initrd_fp = dp;
71 return EFI_SUCCESS;
Ilias Apalodimasec80b472020-02-21 09:55:45 +020072}
73
74/**
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020075 * efi_load_file2_initrd() - load initial RAM disk
Ilias Apalodimasec80b472020-02-21 09:55:45 +020076 *
Heinrich Schuchardt5cd28e12020-10-03 12:44:31 +020077 * This function implements the LoadFile service of the EFI_LOAD_FILE2_PROTOCOL
78 * in order to load an initial RAM disk requested by the Linux kernel stub.
79 *
Ilias Apalodimasec80b472020-02-21 09:55:45 +020080 * See the UEFI spec for details.
81 *
Heinrich Schuchardt5cd28e12020-10-03 12:44:31 +020082 * @this: EFI_LOAD_FILE2_PROTOCOL instance
83 * @file_path: media device path of the file, "" in this case
84 * @boot_policy: must be false
Ilias Apalodimasec80b472020-02-21 09:55:45 +020085 * @buffer_size: size of allocated buffer
86 * @buffer: buffer to load the file
87 *
88 * Return: status code
89 */
90static efi_status_t EFIAPI
91efi_load_file2_initrd(struct efi_load_file_protocol *this,
92 struct efi_device_path *file_path, bool boot_policy,
93 efi_uintn_t *buffer_size, void *buffer)
94{
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +020095 struct efi_device_path *initrd_fp = NULL;
96 efi_status_t ret = EFI_NOT_FOUND;
97 struct efi_file_handle *f = NULL;
98 efi_uintn_t bs;
Ilias Apalodimasec80b472020-02-21 09:55:45 +020099
100 EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy,
101 buffer_size, buffer);
102
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200103 if (!this || this != &efi_lf2_protocol ||
104 !buffer_size) {
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200105 ret = EFI_INVALID_PARAMETER;
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200106 goto out;
107 }
108
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200109 if (file_path->type != dp_lf2_handle.end.type ||
110 file_path->sub_type != dp_lf2_handle.end.sub_type) {
111 ret = EFI_INVALID_PARAMETER;
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200112 goto out;
113 }
114
115 if (boot_policy) {
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200116 ret = EFI_UNSUPPORTED;
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200117 goto out;
118 }
119
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200120 ret = get_initrd_fp(&initrd_fp);
121 if (ret != EFI_SUCCESS)
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200122 goto out;
123
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200124 /* Open file */
125 f = efi_file_from_path(initrd_fp);
126 if (!f) {
127 log_err("Can't find initrd specified in Boot####\n");
128 ret = EFI_NOT_FOUND;
129 goto out;
130 }
131
132 /* Get file size */
133 ret = efi_file_size(f, &bs);
134 if (ret != EFI_SUCCESS)
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200135 goto out;
136
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200137 if (!buffer || *buffer_size < bs) {
138 ret = EFI_BUFFER_TOO_SMALL;
139 *buffer_size = bs;
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200140 } else {
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200141 ret = EFI_CALL(f->read(f, &bs, (void *)(uintptr_t)buffer));
142 *buffer_size = bs;
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200143 }
144
145out:
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200146 efi_free_pool(initrd_fp);
147 if (f)
148 EFI_CALL(f->close(f));
149 return EFI_EXIT(ret);
150}
151
152/**
153 * check_initrd() - Determine if the file defined as an initrd in Boot####
154 * load_options device path is present
155 *
156 * Return: status code
157 */
158static efi_status_t check_initrd(void)
159{
160 struct efi_device_path *initrd_fp = NULL;
161 struct efi_file_handle *f;
162 efi_status_t ret;
163
164 ret = get_initrd_fp(&initrd_fp);
165 if (ret != EFI_SUCCESS)
166 goto out;
167
168 /*
169 * If the file is not found, but the file path is set, return an error
170 * and trigger the bootmgr fallback
171 */
172 f = efi_file_from_path(initrd_fp);
173 if (!f) {
174 log_err("Can't find initrd specified in Boot####\n");
175 ret = EFI_NOT_FOUND;
176 goto out;
177 }
178
179 EFI_CALL(f->close(f));
180
181out:
182 efi_free_pool(initrd_fp);
183 return ret;
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200184}
185
186/**
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200187 * efi_initrd_deregister() - delete the handle for loading initial RAM disk
188 *
189 * This will delete the handle containing the Linux specific vendor device
190 * path and EFI_LOAD_FILE2_PROTOCOL for loading an initrd
191 *
192 * Return: status code
193 */
Ilias Apalodimas70089c12022-10-16 11:36:32 +0300194efi_status_t efi_initrd_deregister(void)
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200195{
Ilias Apalodimas70089c12022-10-16 11:36:32 +0300196 efi_status_t ret;
Heinrich Schuchardt8d805922022-09-30 01:55:02 +0200197
Ilias Apalodimas70089c12022-10-16 11:36:32 +0300198 if (!efi_initrd_handle)
199 return EFI_SUCCESS;
200
201 ret = efi_uninstall_multiple_protocol_interfaces(efi_initrd_handle,
202 /* initramfs */
203 &efi_guid_device_path,
204 &dp_lf2_handle,
205 /* LOAD_FILE2 */
206 &efi_guid_load_file2_protocol,
207 &efi_lf2_protocol,
208 NULL);
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200209 efi_initrd_handle = NULL;
Ilias Apalodimas70089c12022-10-16 11:36:32 +0300210
211 return ret;
Ilias Apalodimas53f6a5a2021-03-17 21:55:00 +0200212}
Masahisa Kojima05bf7ad2023-12-04 13:30:14 +0900213
214/**
215 * efi_initrd_return_notify() - return to efibootmgr callback
216 *
217 * @event: the event for which this notification function is registered
218 * @context: event context
219 */
220static void EFIAPI efi_initrd_return_notify(struct efi_event *event,
221 void *context)
222{
223 efi_status_t ret;
224
225 EFI_ENTRY("%p, %p", event, context);
226 ret = efi_initrd_deregister();
227 EFI_EXIT(ret);
228}
229
230/**
231 * efi_initrd_register() - create handle for loading initial RAM disk
232 *
233 * This function creates a new handle and installs a Linux specific vendor
234 * device path and an EFI_LOAD_FILE2_PROTOCOL. Linux uses the device path
235 * to identify the handle and then calls the LoadFile service of the
236 * EFI_LOAD_FILE2_PROTOCOL to read the initial RAM disk.
237 *
238 * Return: status code
239 */
240efi_status_t efi_initrd_register(void)
241{
242 efi_status_t ret;
243 struct efi_event *event;
244
245 /*
246 * Allow the user to continue if Boot#### file path is not set for
247 * an initrd
248 */
249 ret = check_initrd();
250 if (ret == EFI_INVALID_PARAMETER)
251 return EFI_SUCCESS;
252 if (ret != EFI_SUCCESS)
253 return ret;
254
255 ret = efi_install_multiple_protocol_interfaces(&efi_initrd_handle,
256 /* initramfs */
257 &efi_guid_device_path, &dp_lf2_handle,
258 /* LOAD_FILE2 */
259 &efi_guid_load_file2_protocol,
260 &efi_lf2_protocol,
261 NULL);
262 if (ret != EFI_SUCCESS) {
263 log_err("installing EFI_LOAD_FILE2_PROTOCOL failed\n");
264 return ret;
265 }
266
267 ret = efi_create_event(EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
268 efi_initrd_return_notify, NULL,
269 &efi_guid_event_group_return_to_efibootmgr,
270 &event);
271 if (ret != EFI_SUCCESS)
272 log_err("Creating event failed\n");
273
274 return ret;
275}