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 <common.h> |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 8 | #include <efi_loader.h> |
| 9 | #include <efi_load_initrd.h> |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 10 | #include <efi_variable.h> |
Ilias Apalodimas | daa924a | 2020-12-31 12:33:23 +0200 | [diff] [blame] | 11 | #include <fs.h> |
| 12 | #include <malloc.h> |
| 13 | #include <mapmem.h> |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 14 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 15 | static efi_status_t EFIAPI |
| 16 | efi_load_file2_initrd(struct efi_load_file_protocol *this, |
| 17 | struct efi_device_path *file_path, bool boot_policy, |
| 18 | efi_uintn_t *buffer_size, void *buffer); |
| 19 | |
| 20 | static const struct efi_load_file_protocol efi_lf2_protocol = { |
| 21 | .load_file = efi_load_file2_initrd, |
| 22 | }; |
| 23 | |
| 24 | /* |
| 25 | * Device path defined by Linux to identify the handle providing the |
| 26 | * EFI_LOAD_FILE2_PROTOCOL used for loading the initial ramdisk. |
| 27 | */ |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 28 | static const struct efi_initrd_dp dp_lf2_handle = { |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 29 | .vendor = { |
| 30 | { |
| 31 | DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 32 | DEVICE_PATH_SUB_TYPE_VENDOR_PATH, |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 33 | sizeof(dp_lf2_handle.vendor), |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 34 | }, |
| 35 | EFI_INITRD_MEDIA_GUID, |
| 36 | }, |
| 37 | .end = { |
| 38 | DEVICE_PATH_TYPE_END, |
| 39 | DEVICE_PATH_SUB_TYPE_END, |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 40 | sizeof(dp_lf2_handle.end), |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 41 | } |
| 42 | }; |
| 43 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 44 | static efi_handle_t efi_initrd_handle; |
| 45 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 46 | /** |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 47 | * get_initrd_fp() - Get initrd device path from a FilePathList device path |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 48 | * |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 49 | * @initrd_fp: the final initrd filepath |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 50 | * |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 51 | * Return: status code. Caller must free initrd_fp |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 52 | */ |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 53 | 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] | 54 | { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 55 | const efi_guid_t lf2_initrd_guid = EFI_INITRD_MEDIA_GUID; |
| 56 | struct efi_device_path *dp = NULL; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 57 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 58 | /* |
| 59 | * if bootmgr is setup with and initrd, the device path will be |
| 60 | * in the FilePathList[] of our load options in Boot####. |
| 61 | * The first device path of the multi instance device path will |
| 62 | * start with a VenMedia and the initrds will follow. |
| 63 | * |
| 64 | * If the device path is not found return EFI_INVALID_PARAMETER. |
| 65 | * We can then use this specific return value and not install the |
| 66 | * protocol, while allowing the boot to continue |
| 67 | */ |
| 68 | dp = efi_get_dp_from_boot(lf2_initrd_guid); |
| 69 | if (!dp) |
| 70 | return EFI_INVALID_PARAMETER; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 71 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 72 | *initrd_fp = dp; |
| 73 | return EFI_SUCCESS; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 77 | * efi_load_file2_initrd() - load initial RAM disk |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 78 | * |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 79 | * This function implements the LoadFile service of the EFI_LOAD_FILE2_PROTOCOL |
| 80 | * in order to load an initial RAM disk requested by the Linux kernel stub. |
| 81 | * |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 82 | * See the UEFI spec for details. |
| 83 | * |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 84 | * @this: EFI_LOAD_FILE2_PROTOCOL instance |
| 85 | * @file_path: media device path of the file, "" in this case |
| 86 | * @boot_policy: must be false |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 87 | * @buffer_size: size of allocated buffer |
| 88 | * @buffer: buffer to load the file |
| 89 | * |
| 90 | * Return: status code |
| 91 | */ |
| 92 | static efi_status_t EFIAPI |
| 93 | efi_load_file2_initrd(struct efi_load_file_protocol *this, |
| 94 | struct efi_device_path *file_path, bool boot_policy, |
| 95 | efi_uintn_t *buffer_size, void *buffer) |
| 96 | { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 97 | struct efi_device_path *initrd_fp = NULL; |
| 98 | efi_status_t ret = EFI_NOT_FOUND; |
| 99 | struct efi_file_handle *f = NULL; |
| 100 | efi_uintn_t bs; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 101 | |
| 102 | EFI_ENTRY("%p, %p, %d, %p, %p", this, file_path, boot_policy, |
| 103 | buffer_size, buffer); |
| 104 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 105 | if (!this || this != &efi_lf2_protocol || |
| 106 | !buffer_size) { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 107 | ret = EFI_INVALID_PARAMETER; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 108 | goto out; |
| 109 | } |
| 110 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 111 | if (file_path->type != dp_lf2_handle.end.type || |
| 112 | file_path->sub_type != dp_lf2_handle.end.sub_type) { |
| 113 | ret = EFI_INVALID_PARAMETER; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | if (boot_policy) { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 118 | ret = EFI_UNSUPPORTED; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 119 | goto out; |
| 120 | } |
| 121 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 122 | ret = get_initrd_fp(&initrd_fp); |
| 123 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 124 | goto out; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 125 | |
| 126 | /* Open file */ |
| 127 | f = efi_file_from_path(initrd_fp); |
| 128 | if (!f) { |
| 129 | log_err("Can't find initrd specified in Boot####\n"); |
| 130 | ret = EFI_NOT_FOUND; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 131 | goto out; |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 132 | } |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 133 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 134 | /* Get file size */ |
| 135 | ret = efi_file_size(f, &bs); |
| 136 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 137 | goto out; |
| 138 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 139 | if (!buffer || *buffer_size < bs) { |
| 140 | ret = EFI_BUFFER_TOO_SMALL; |
| 141 | *buffer_size = bs; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 142 | } else { |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 143 | ret = EFI_CALL(f->read(f, &bs, (void *)(uintptr_t)buffer)); |
| 144 | *buffer_size = bs; |
| 145 | } |
| 146 | |
| 147 | out: |
| 148 | efi_free_pool(initrd_fp); |
| 149 | if (f) |
| 150 | EFI_CALL(f->close(f)); |
| 151 | return EFI_EXIT(ret); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * check_initrd() - Determine if the file defined as an initrd in Boot#### |
| 156 | * load_options device path is present |
| 157 | * |
| 158 | * Return: status code |
| 159 | */ |
| 160 | static efi_status_t check_initrd(void) |
| 161 | { |
| 162 | struct efi_device_path *initrd_fp = NULL; |
| 163 | struct efi_file_handle *f; |
| 164 | efi_status_t ret; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 165 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 166 | ret = get_initrd_fp(&initrd_fp); |
| 167 | if (ret != EFI_SUCCESS) |
| 168 | goto out; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 169 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 170 | /* |
| 171 | * If the file is not found, but the file path is set, return an error |
| 172 | * and trigger the bootmgr fallback |
| 173 | */ |
| 174 | f = efi_file_from_path(initrd_fp); |
| 175 | if (!f) { |
| 176 | log_err("Can't find initrd specified in Boot####\n"); |
| 177 | ret = EFI_NOT_FOUND; |
| 178 | goto out; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 181 | EFI_CALL(f->close(f)); |
| 182 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 183 | out: |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 184 | efi_free_pool(initrd_fp); |
| 185 | return ret; |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 189 | * efi_initrd_register() - create handle for loading initial RAM disk |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 190 | * |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 191 | * This function creates a new handle and installs a Linux specific vendor |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 192 | * 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] | 193 | * to identify the handle and then calls the LoadFile service of the |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 194 | * EFI_LOAD_FILE2_PROTOCOL to read the initial RAM disk. |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 195 | * |
Heinrich Schuchardt | d1d8933 | 2020-10-03 12:44:31 +0200 | [diff] [blame] | 196 | * Return: status code |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 197 | */ |
| 198 | efi_status_t efi_initrd_register(void) |
| 199 | { |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 200 | efi_status_t ret; |
| 201 | |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 202 | /* |
| 203 | * Allow the user to continue if Boot#### file path is not set for |
| 204 | * an initrd |
| 205 | */ |
| 206 | ret = check_initrd(); |
| 207 | if (ret == EFI_INVALID_PARAMETER) |
| 208 | return EFI_SUCCESS; |
| 209 | if (ret != EFI_SUCCESS) |
| 210 | return ret; |
| 211 | |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 212 | ret = EFI_CALL(efi_install_multiple_protocol_interfaces |
| 213 | (&efi_initrd_handle, |
| 214 | /* initramfs */ |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 215 | &efi_guid_device_path, &dp_lf2_handle, |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 216 | /* LOAD_FILE2 */ |
| 217 | &efi_guid_load_file2_protocol, |
| 218 | (void *)&efi_lf2_protocol, |
| 219 | NULL)); |
| 220 | |
| 221 | return ret; |
| 222 | } |
Ilias Apalodimas | b307e3d | 2021-03-17 21:55:00 +0200 | [diff] [blame] | 223 | |
| 224 | /** |
| 225 | * efi_initrd_deregister() - delete the handle for loading initial RAM disk |
| 226 | * |
| 227 | * This will delete the handle containing the Linux specific vendor device |
| 228 | * path and EFI_LOAD_FILE2_PROTOCOL for loading an initrd |
| 229 | * |
| 230 | * Return: status code |
| 231 | */ |
| 232 | void efi_initrd_deregister(void) |
| 233 | { |
| 234 | efi_delete_handle(efi_initrd_handle); |
| 235 | efi_initrd_handle = NULL; |
| 236 | } |