Ilias Apalodimas | 3510ba7 | 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 | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_EFI |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 7 | #include <efi_loader.h> |
| 8 | #include <efi_load_initrd.h> |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 9 | #include <efi_variable.h> |
Ilias Apalodimas | daa924a | 2020-12-31 12:33:23 +0200 | [diff] [blame] | 10 | #include <fs.h> |
| 11 | #include <malloc.h> |
| 12 | #include <mapmem.h> |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 13 | |
Ilias Apalodimas | 3510ba7 | 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 | */ |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 27 | static const struct efi_initrd_dp dp_lf2_handle = { |
Ilias Apalodimas | 3510ba7 | 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 | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 32 | sizeof(dp_lf2_handle.vendor), |
Ilias Apalodimas | 3510ba7 | 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 | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 39 | sizeof(dp_lf2_handle.end), |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 40 | } |
| 41 | }; |
| 42 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 43 | static efi_handle_t efi_initrd_handle; |
| 44 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 45 | /** |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 46 | * get_initrd_fp() - Get initrd device path from a FilePathList device path |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 47 | * |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 48 | * @initrd_fp: the final initrd filepath |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 49 | * |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 50 | * Return: status code. Caller must free initrd_fp |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 51 | */ |
Ilias Apalodimas | b307e3d | 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 | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 53 | { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 54 | struct efi_device_path *dp = NULL; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 55 | |
Ilias Apalodimas | b307e3d | 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 | 6c405cb | 2021-10-15 02:33:33 +0200 | [diff] [blame] | 66 | dp = efi_get_dp_from_boot(efi_lf2_initrd_guid); |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 67 | if (!dp) |
| 68 | return EFI_INVALID_PARAMETER; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 69 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 70 | *initrd_fp = dp; |
| 71 | return EFI_SUCCESS; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 75 | * efi_load_file2_initrd() - load initial RAM disk |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 76 | * |
Heinrich Schuchardt | d1d8933 | 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 | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 80 | * See the UEFI spec for details. |
| 81 | * |
Heinrich Schuchardt | d1d8933 | 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 | 3510ba7 | 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 | b307e3d | 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 | 3510ba7 | 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 | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 103 | if (!this || this != &efi_lf2_protocol || |
| 104 | !buffer_size) { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 105 | ret = EFI_INVALID_PARAMETER; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 106 | goto out; |
| 107 | } |
| 108 | |
Ilias Apalodimas | b307e3d | 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 | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 112 | goto out; |
| 113 | } |
| 114 | |
| 115 | if (boot_policy) { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 116 | ret = EFI_UNSUPPORTED; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 117 | goto out; |
| 118 | } |
| 119 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 120 | ret = get_initrd_fp(&initrd_fp); |
| 121 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 122 | goto out; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 123 | |
| 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; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 129 | goto out; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 130 | } |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 131 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 132 | /* Get file size */ |
| 133 | ret = efi_file_size(f, &bs); |
| 134 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 135 | goto out; |
| 136 | |
Ilias Apalodimas | b307e3d | 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 | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 140 | } else { |
Ilias Apalodimas | b307e3d | 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; |
| 143 | } |
| 144 | |
| 145 | out: |
| 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; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 163 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 164 | ret = get_initrd_fp(&initrd_fp); |
| 165 | if (ret != EFI_SUCCESS) |
| 166 | goto out; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 167 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 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; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 179 | EFI_CALL(f->close(f)); |
| 180 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 181 | out: |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 182 | efi_free_pool(initrd_fp); |
| 183 | return ret; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 187 | * efi_initrd_register() - create handle for loading initial RAM disk |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 188 | * |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 189 | * This function creates a new handle and installs a Linux specific vendor |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 190 | * device path and an EFI_LOAD_FILE2_PROTOCOL. Linux uses the device path |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 191 | * to identify the handle and then calls the LoadFile service of the |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 192 | * EFI_LOAD_FILE2_PROTOCOL to read the initial RAM disk. |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 193 | * |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 194 | * Return: status code |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 195 | */ |
| 196 | efi_status_t efi_initrd_register(void) |
| 197 | { |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 198 | efi_status_t ret; |
| 199 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Allow the user to continue if Boot#### file path is not set for |
| 202 | * an initrd |
| 203 | */ |
| 204 | ret = check_initrd(); |
| 205 | if (ret == EFI_INVALID_PARAMETER) |
| 206 | return EFI_SUCCESS; |
| 207 | if (ret != EFI_SUCCESS) |
| 208 | return ret; |
| 209 | |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 210 | ret = efi_install_multiple_protocol_interfaces(&efi_initrd_handle, |
| 211 | /* initramfs */ |
| 212 | &efi_guid_device_path, &dp_lf2_handle, |
| 213 | /* LOAD_FILE2 */ |
| 214 | &efi_guid_load_file2_protocol, |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 215 | &efi_lf2_protocol, |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 216 | NULL); |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 217 | |
| 218 | return ret; |
| 219 | } |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * efi_initrd_deregister() - delete the handle for loading initial RAM disk |
| 223 | * |
| 224 | * This will delete the handle containing the Linux specific vendor device |
| 225 | * path and EFI_LOAD_FILE2_PROTOCOL for loading an initrd |
| 226 | * |
| 227 | * Return: status code |
| 228 | */ |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 229 | efi_status_t efi_initrd_deregister(void) |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 230 | { |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 231 | efi_status_t ret; |
| 232 | |
Heinrich Schuchardt | a0ace87 | 2022-09-30 01:55:02 +0200 | [diff] [blame] | 233 | if (!efi_initrd_handle) |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 234 | return EFI_SUCCESS; |
Heinrich Schuchardt | a0ace87 | 2022-09-30 01:55:02 +0200 | [diff] [blame] | 235 | |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 236 | ret = efi_uninstall_multiple_protocol_interfaces(efi_initrd_handle, |
| 237 | /* initramfs */ |
| 238 | &efi_guid_device_path, |
| 239 | &dp_lf2_handle, |
| 240 | /* LOAD_FILE2 */ |
| 241 | &efi_guid_load_file2_protocol, |
| 242 | &efi_lf2_protocol, |
| 243 | NULL); |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 244 | efi_initrd_handle = NULL; |
Ilias Apalodimas | 7e05279 | 2022-10-16 11:36:32 +0300 | [diff] [blame] | 245 | |
| 246 | return ret; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 247 | } |