|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <common.h> |
| 3 | +#include <driver.h> |
| 4 | +#include <init.h> |
| 5 | +#include <linux/hw_random.h> |
| 6 | +#include <efi.h> |
| 7 | +#include <efi/efi-device.h> |
| 8 | +#include <efi/device-path.h> |
| 9 | +#include <efi/efi-payload.h> |
| 10 | + |
| 11 | +static efi_status_t EFIAPI efi_initrd_load_file2( |
| 12 | + struct efi_load_file_protocol *this, struct efi_device_path *file_path, |
| 13 | + bool boot_policy, unsigned long *buffer_size, void *buffer); |
| 14 | + |
| 15 | +static const struct { |
| 16 | + struct efi_device_path_vendor vendor; |
| 17 | + struct efi_device_path end; |
| 18 | +} __packed initrd_dev_path = { { { |
| 19 | + DEVICE_PATH_TYPE_MEDIA_DEVICE, |
| 20 | + DEVICE_PATH_SUB_TYPE_VENDOR_PATH, |
| 21 | + sizeof(initrd_dev_path.vendor), |
| 22 | + }, |
| 23 | + EFI_LINUX_INITRD_MEDIA_GUID }, |
| 24 | + { DEVICE_PATH_TYPE_END, DEVICE_PATH_SUB_TYPE_END, |
| 25 | + DEVICE_PATH_END_LENGTH } }; |
| 26 | + |
| 27 | +static struct efi_device_path_memory *initrd_dev; |
| 28 | +static efi_handle_t lf2_handle = NULL; |
| 29 | +static struct efi_load_file_protocol efi_lf2_p = { |
| 30 | + .load_file = efi_initrd_load_file2 |
| 31 | +}; |
| 32 | + |
| 33 | +static efi_status_t EFIAPI efi_initrd_load_file2( |
| 34 | + struct efi_load_file_protocol *this, struct efi_device_path *file_path, |
| 35 | + bool boot_policy, unsigned long *buffer_size, void *buffer) |
| 36 | +{ |
| 37 | + size_t initrd_size; |
| 38 | + |
| 39 | + pr_info("Anis here we call initrd load file2\n"); |
| 40 | + |
| 41 | + if (!this || this != &efi_lf2_p || !buffer_size) |
| 42 | + return EFI_INVALID_PARAMETER; |
| 43 | + |
| 44 | + if (file_path->type != initrd_dev_path.end.type || |
| 45 | + file_path->sub_type != initrd_dev_path.end.sub_type) |
| 46 | + return EFI_INVALID_PARAMETER; |
| 47 | + |
| 48 | + if (boot_policy) |
| 49 | + return EFI_UNSUPPORTED; |
| 50 | + |
| 51 | + initrd_size = initrd_dev->ending_address - initrd_dev->starting_address; |
| 52 | + if (!buffer || *buffer_size < initrd_size) { |
| 53 | + *buffer_size = initrd_size; |
| 54 | + return EFI_BUFFER_TOO_SMALL; |
| 55 | + } else { |
| 56 | + memcpy(buffer, (void *)(uintptr_t)initrd_dev->starting_address, |
| 57 | + initrd_size); |
| 58 | + *buffer_size = initrd_size; |
| 59 | + } |
| 60 | + |
| 61 | + return EFI_SUCCESS; |
| 62 | +} |
| 63 | + |
| 64 | +int efi_initrd_register(void *initrd, size_t initrd_sz) |
| 65 | +{ |
| 66 | + efi_physical_addr_t mem; |
| 67 | + efi_status_t efiret; |
| 68 | + size_t sz; |
| 69 | + int ret; |
| 70 | + |
| 71 | + sz = sizeof(struct efi_device_path_memory) + |
| 72 | + sizeof(struct efi_device_path); |
| 73 | + efiret = BS->allocate_pool(EFI_BOOT_SERVICES_DATA, sz, (void **)&mem); |
| 74 | + if (EFI_ERROR(efiret)) { |
| 75 | + pr_err("Failed to allocate memory for INITRD %s\n", |
| 76 | + efi_strerror(efiret)); |
| 77 | + ret = -efi_errno(efiret); |
| 78 | + return ret; |
| 79 | + } |
| 80 | + |
| 81 | + initrd_dev = efi_phys_to_virt(mem); |
| 82 | + initrd_dev->header.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 83 | + initrd_dev->header.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY; |
| 84 | + initrd_dev->header.length = sizeof(struct efi_device_path_memory); |
| 85 | + initrd_dev->memory_type = EFI_LOADER_DATA; |
| 86 | + initrd_dev->starting_address = efi_virt_to_phys(initrd); |
| 87 | + initrd_dev->ending_address = efi_virt_to_phys(initrd) + initrd_sz; |
| 88 | + |
| 89 | + memcpy(&initrd_dev[1], &initrd_dev_path.end, DEVICE_PATH_END_LENGTH); |
| 90 | + |
| 91 | + efiret = BS->install_multiple_protocol_interfaces( |
| 92 | + &lf2_handle, &efi_load_file2_protocol_guid, &efi_lf2_p, |
| 93 | + &efi_device_path_protocol_guid, &initrd_dev_path, NULL); |
| 94 | + if (EFI_ERROR(efiret)) { |
| 95 | + pr_err("Failed to install protocols for INITRD %s\n", |
| 96 | + efi_strerror(efiret)); |
| 97 | + ret = -efi_errno(efiret); |
| 98 | + goto out; |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | +out: |
| 103 | + BS->free_pool(efi_phys_to_virt(mem)); |
| 104 | + return ret; |
| 105 | +} |
| 106 | + |
| 107 | +void efi_initrd_unregister() |
| 108 | +{ |
| 109 | + BS->uninstall_multiple_protocol_interfaces( |
| 110 | + lf2_handle, &efi_device_path_protocol_guid, &initrd_dev_path, |
| 111 | + &efi_load_file2_protocol_guid, &efi_lf2_p, NULL); |
| 112 | + |
| 113 | + BS->free_pool(initrd_dev); |
| 114 | + initrd_dev = NULL; |
| 115 | +} |
0 commit comments