|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Support for extracting embedded firmware for peripherals from EFI code, |
| 4 | + * |
| 5 | + * Copyright (c) 2018 Hans de Goede <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/dmi.h> |
| 9 | +#include <linux/efi.h> |
| 10 | +#include <linux/efi_embedded_fw.h> |
| 11 | +#include <linux/io.h> |
| 12 | +#include <linux/slab.h> |
| 13 | +#include <linux/types.h> |
| 14 | +#include <linux/vmalloc.h> |
| 15 | +#include <crypto/sha.h> |
| 16 | + |
| 17 | +/* Exported for use by lib/test_firmware.c only */ |
| 18 | +LIST_HEAD(efi_embedded_fw_list); |
| 19 | +EXPORT_SYMBOL_GPL(efi_embedded_fw_list); |
| 20 | + |
| 21 | +static bool checked_for_fw; |
| 22 | + |
| 23 | +static const struct dmi_system_id * const embedded_fw_table[] = { |
| 24 | + NULL |
| 25 | +}; |
| 26 | + |
| 27 | +/* |
| 28 | + * Note the efi_check_for_embedded_firmwares() code currently makes the |
| 29 | + * following 2 assumptions. This may needs to be revisited if embedded firmware |
| 30 | + * is found where this is not true: |
| 31 | + * 1) The firmware is only found in EFI_BOOT_SERVICES_CODE memory segments |
| 32 | + * 2) The firmware always starts at an offset which is a multiple of 8 bytes |
| 33 | + */ |
| 34 | +static int __init efi_check_md_for_embedded_firmware( |
| 35 | + efi_memory_desc_t *md, const struct efi_embedded_fw_desc *desc) |
| 36 | +{ |
| 37 | + struct sha256_state sctx; |
| 38 | + struct efi_embedded_fw *fw; |
| 39 | + u8 sha256[32]; |
| 40 | + u64 i, size; |
| 41 | + u8 *map; |
| 42 | + |
| 43 | + size = md->num_pages << EFI_PAGE_SHIFT; |
| 44 | + map = memremap(md->phys_addr, size, MEMREMAP_WB); |
| 45 | + if (!map) { |
| 46 | + pr_err("Error mapping EFI mem at %#llx\n", md->phys_addr); |
| 47 | + return -ENOMEM; |
| 48 | + } |
| 49 | + |
| 50 | + for (i = 0; (i + desc->length) <= size; i += 8) { |
| 51 | + if (memcmp(map + i, desc->prefix, EFI_EMBEDDED_FW_PREFIX_LEN)) |
| 52 | + continue; |
| 53 | + |
| 54 | + sha256_init(&sctx); |
| 55 | + sha256_update(&sctx, map + i, desc->length); |
| 56 | + sha256_final(&sctx, sha256); |
| 57 | + if (memcmp(sha256, desc->sha256, 32) == 0) |
| 58 | + break; |
| 59 | + } |
| 60 | + if ((i + desc->length) > size) { |
| 61 | + memunmap(map); |
| 62 | + return -ENOENT; |
| 63 | + } |
| 64 | + |
| 65 | + pr_info("Found EFI embedded fw '%s'\n", desc->name); |
| 66 | + |
| 67 | + fw = kmalloc(sizeof(*fw), GFP_KERNEL); |
| 68 | + if (!fw) { |
| 69 | + memunmap(map); |
| 70 | + return -ENOMEM; |
| 71 | + } |
| 72 | + |
| 73 | + fw->data = kmemdup(map + i, desc->length, GFP_KERNEL); |
| 74 | + memunmap(map); |
| 75 | + if (!fw->data) { |
| 76 | + kfree(fw); |
| 77 | + return -ENOMEM; |
| 78 | + } |
| 79 | + |
| 80 | + fw->name = desc->name; |
| 81 | + fw->length = desc->length; |
| 82 | + list_add(&fw->list, &efi_embedded_fw_list); |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +void __init efi_check_for_embedded_firmwares(void) |
| 88 | +{ |
| 89 | + const struct efi_embedded_fw_desc *fw_desc; |
| 90 | + const struct dmi_system_id *dmi_id; |
| 91 | + efi_memory_desc_t *md; |
| 92 | + int i, r; |
| 93 | + |
| 94 | + for (i = 0; embedded_fw_table[i]; i++) { |
| 95 | + dmi_id = dmi_first_match(embedded_fw_table[i]); |
| 96 | + if (!dmi_id) |
| 97 | + continue; |
| 98 | + |
| 99 | + fw_desc = dmi_id->driver_data; |
| 100 | + |
| 101 | + /* |
| 102 | + * In some drivers the struct driver_data contains may contain |
| 103 | + * other driver specific data after the fw_desc struct; and |
| 104 | + * the fw_desc struct itself may be empty, skip these. |
| 105 | + */ |
| 106 | + if (!fw_desc->name) |
| 107 | + continue; |
| 108 | + |
| 109 | + for_each_efi_memory_desc(md) { |
| 110 | + if (md->type != EFI_BOOT_SERVICES_CODE) |
| 111 | + continue; |
| 112 | + |
| 113 | + r = efi_check_md_for_embedded_firmware(md, fw_desc); |
| 114 | + if (r == 0) |
| 115 | + break; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + checked_for_fw = true; |
| 120 | +} |
| 121 | + |
| 122 | +int efi_get_embedded_fw(const char *name, const u8 **data, size_t *size) |
| 123 | +{ |
| 124 | + struct efi_embedded_fw *iter, *fw = NULL; |
| 125 | + |
| 126 | + if (!checked_for_fw) { |
| 127 | + pr_warn("Warning %s called while we did not check for embedded fw\n", |
| 128 | + __func__); |
| 129 | + return -ENOENT; |
| 130 | + } |
| 131 | + |
| 132 | + list_for_each_entry(iter, &efi_embedded_fw_list, list) { |
| 133 | + if (strcmp(name, iter->name) == 0) { |
| 134 | + fw = iter; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if (!fw) |
| 140 | + return -ENOENT; |
| 141 | + |
| 142 | + *data = fw->data; |
| 143 | + *size = fw->length; |
| 144 | + |
| 145 | + return 0; |
| 146 | +} |
| 147 | +EXPORT_SYMBOL_GPL(efi_get_embedded_fw); |
0 commit comments