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