Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Support for extracting embedded firmware for peripherals from EFI code,
0004  *
0005  * Copyright (c) 2018 Hans de Goede <hdegoede@redhat.com>
0006  */
0007 
0008 #include <linux/dmi.h>
0009 #include <linux/efi.h>
0010 #include <linux/efi_embedded_fw.h>
0011 #include <linux/io.h>
0012 #include <linux/slab.h>
0013 #include <linux/types.h>
0014 #include <linux/vmalloc.h>
0015 #include <crypto/sha2.h>
0016 
0017 /* Exported for use by lib/test_firmware.c only */
0018 LIST_HEAD(efi_embedded_fw_list);
0019 EXPORT_SYMBOL_NS_GPL(efi_embedded_fw_list, TEST_FIRMWARE);
0020 bool efi_embedded_fw_checked;
0021 EXPORT_SYMBOL_NS_GPL(efi_embedded_fw_checked, TEST_FIRMWARE);
0022 
0023 static const struct dmi_system_id * const embedded_fw_table[] = {
0024 #ifdef CONFIG_TOUCHSCREEN_DMI
0025     touchscreen_dmi_table,
0026 #endif
0027     NULL
0028 };
0029 
0030 /*
0031  * Note the efi_check_for_embedded_firmwares() code currently makes the
0032  * following 2 assumptions. This may needs to be revisited if embedded firmware
0033  * is found where this is not true:
0034  * 1) The firmware is only found in EFI_BOOT_SERVICES_CODE memory segments
0035  * 2) The firmware always starts at an offset which is a multiple of 8 bytes
0036  */
0037 static int __init efi_check_md_for_embedded_firmware(
0038     efi_memory_desc_t *md, const struct efi_embedded_fw_desc *desc)
0039 {
0040     struct efi_embedded_fw *fw;
0041     u8 hash[32];
0042     u64 i, size;
0043     u8 *map;
0044 
0045     size = md->num_pages << EFI_PAGE_SHIFT;
0046     map = memremap(md->phys_addr, size, MEMREMAP_WB);
0047     if (!map) {
0048         pr_err("Error mapping EFI mem at %#llx\n", md->phys_addr);
0049         return -ENOMEM;
0050     }
0051 
0052     for (i = 0; (i + desc->length) <= size; i += 8) {
0053         if (memcmp(map + i, desc->prefix, EFI_EMBEDDED_FW_PREFIX_LEN))
0054             continue;
0055 
0056         sha256(map + i, desc->length, hash);
0057         if (memcmp(hash, desc->sha256, 32) == 0)
0058             break;
0059     }
0060     if ((i + desc->length) > size) {
0061         memunmap(map);
0062         return -ENOENT;
0063     }
0064 
0065     pr_info("Found EFI embedded fw '%s'\n", desc->name);
0066 
0067     fw = kmalloc(sizeof(*fw), GFP_KERNEL);
0068     if (!fw) {
0069         memunmap(map);
0070         return -ENOMEM;
0071     }
0072 
0073     fw->data = kmemdup(map + i, desc->length, GFP_KERNEL);
0074     memunmap(map);
0075     if (!fw->data) {
0076         kfree(fw);
0077         return -ENOMEM;
0078     }
0079 
0080     fw->name = desc->name;
0081     fw->length = desc->length;
0082     list_add(&fw->list, &efi_embedded_fw_list);
0083 
0084     return 0;
0085 }
0086 
0087 void __init efi_check_for_embedded_firmwares(void)
0088 {
0089     const struct efi_embedded_fw_desc *fw_desc;
0090     const struct dmi_system_id *dmi_id;
0091     efi_memory_desc_t *md;
0092     int i, r;
0093 
0094     for (i = 0; embedded_fw_table[i]; i++) {
0095         dmi_id = dmi_first_match(embedded_fw_table[i]);
0096         if (!dmi_id)
0097             continue;
0098 
0099         fw_desc = dmi_id->driver_data;
0100 
0101         /*
0102          * In some drivers the struct driver_data contains may contain
0103          * other driver specific data after the fw_desc struct; and
0104          * the fw_desc struct itself may be empty, skip these.
0105          */
0106         if (!fw_desc->name)
0107             continue;
0108 
0109         for_each_efi_memory_desc(md) {
0110             if (md->type != EFI_BOOT_SERVICES_CODE)
0111                 continue;
0112 
0113             r = efi_check_md_for_embedded_firmware(md, fw_desc);
0114             if (r == 0)
0115                 break;
0116         }
0117     }
0118 
0119     efi_embedded_fw_checked = true;
0120 }
0121 
0122 int efi_get_embedded_fw(const char *name, const u8 **data, size_t *size)
0123 {
0124     struct efi_embedded_fw *iter, *fw = NULL;
0125 
0126     if (!efi_embedded_fw_checked) {
0127         pr_warn("Warning %s called while we did not check for embedded fw\n",
0128             __func__);
0129         return -ENOENT;
0130     }
0131 
0132     list_for_each_entry(iter, &efi_embedded_fw_list, list) {
0133         if (strcmp(name, iter->name) == 0) {
0134             fw = iter;
0135             break;
0136         }
0137     }
0138 
0139     if (!fw)
0140         return -ENOENT;
0141 
0142     *data = fw->data;
0143     *size = fw->length;
0144 
0145     return 0;
0146 }
0147 EXPORT_SYMBOL_GPL(efi_get_embedded_fw);