Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2020, Linaro Limited |
| 4 | */ |
| 5 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_EFI |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 7 | #include <efi_loader.h> |
| 8 | #include <efi_load_initrd.h> |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 9 | #include <efi_variable.h> |
Ilias Apalodimas | 0c44452 | 2020-12-31 12:33:23 +0200 | [diff] [blame] | 10 | #include <fs.h> |
| 11 | #include <malloc.h> |
| 12 | #include <mapmem.h> |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 13 | |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 14 | static efi_status_t EFIAPI |
| 15 | efi_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 | |
| 19 | static 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 Schuchardt | 535321c | 2024-06-10 10:00:01 +0200 | [diff] [blame] | 27 | static const struct efi_lo_dp_prefix dp_lf2_handle = { |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 28 | .vendor = { |
| 29 | { |
| 30 | DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 31 | DEVICE_PATH_SUB_TYPE_VENDOR_PATH, |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 32 | sizeof(dp_lf2_handle.vendor), |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 33 | }, |
| 34 | EFI_INITRD_MEDIA_GUID, |
| 35 | }, |
| 36 | .end = { |
| 37 | DEVICE_PATH_TYPE_END, |
| 38 | DEVICE_PATH_SUB_TYPE_END, |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 39 | sizeof(dp_lf2_handle.end), |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 40 | } |
| 41 | }; |
| 42 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 43 | static efi_handle_t efi_initrd_handle; |
| 44 | |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 45 | /** |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 46 | * get_initrd_fp() - Get initrd device path from a FilePathList device path |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 47 | * |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 48 | * @initrd_fp: the final initrd filepath |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 49 | * |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 50 | * Return: status code. Caller must free initrd_fp |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 51 | */ |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 52 | static efi_status_t get_initrd_fp(struct efi_device_path **initrd_fp) |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 53 | { |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 54 | struct efi_device_path *dp = NULL; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 55 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 56 | /* |
| 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 Schuchardt | 8745f13 | 2024-04-26 16:13:08 +0200 | [diff] [blame] | 66 | dp = efi_get_dp_from_boot(&efi_lf2_initrd_guid); |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 67 | if (!dp) |
| 68 | return EFI_INVALID_PARAMETER; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 69 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 70 | *initrd_fp = dp; |
| 71 | return EFI_SUCCESS; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 75 | * efi_load_file2_initrd() - load initial RAM disk |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 76 | * |
Heinrich Schuchardt | 5cd28e1 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 77 | * 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 Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 80 | * See the UEFI spec for details. |
| 81 | * |
Heinrich Schuchardt | 5cd28e1 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 82 | * @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 Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 85 | * @buffer_size: size of allocated buffer |
| 86 | * @buffer: buffer to load the file |
| 87 | * |
| 88 | * Return: status code |
| 89 | */ |
| 90 | static efi_status_t EFIAPI |
| 91 | efi_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 Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 95 | 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 Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 99 | |
| 100 | EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy, |
| 101 | buffer_size, buffer); |
| 102 | |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 103 | if (!this || this != &efi_lf2_protocol || |
| 104 | !buffer_size) { |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 105 | ret = EFI_INVALID_PARAMETER; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 106 | goto out; |
| 107 | } |
| 108 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 109 | 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 Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | if (boot_policy) { |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 116 | ret = EFI_UNSUPPORTED; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 117 | goto out; |
| 118 | } |
| 119 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 120 | ret = get_initrd_fp(&initrd_fp); |
| 121 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 122 | goto out; |
| 123 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 124 | /* 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 Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 135 | goto out; |
| 136 | |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 137 | if (!buffer || *buffer_size < bs) { |
| 138 | ret = EFI_BUFFER_TOO_SMALL; |
| 139 | *buffer_size = bs; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 140 | } else { |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 141 | ret = EFI_CALL(f->read(f, &bs, (void *)(uintptr_t)buffer)); |
| 142 | *buffer_size = bs; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | out: |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 146 | 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 | */ |
| 158 | static 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 | |
| 181 | out: |
| 182 | efi_free_pool(initrd_fp); |
| 183 | return ret; |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 187 | * 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 Apalodimas | 70089c1 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 194 | efi_status_t efi_initrd_deregister(void) |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 195 | { |
Ilias Apalodimas | 70089c1 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 196 | efi_status_t ret; |
Heinrich Schuchardt | 8d80592 | 2022-09-30 01:55:02 +0200 | [diff] [blame] | 197 | |
Ilias Apalodimas | 70089c1 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 198 | 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 Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 209 | efi_initrd_handle = NULL; |
Ilias Apalodimas | 70089c1 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 210 | |
| 211 | return ret; |
Ilias Apalodimas | 53f6a5a | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 212 | } |
Masahisa Kojima | 05bf7ad | 2023-12-04 13:30:14 +0900 | [diff] [blame] | 213 | |
| 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 | */ |
| 220 | static 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 | */ |
| 240 | efi_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 | } |