18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Support for extracting embedded firmware for peripherals from EFI code,
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2018 Hans de Goede <hdegoede@redhat.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/dmi.h>
98c2ecf20Sopenharmony_ci#include <linux/efi.h>
108c2ecf20Sopenharmony_ci#include <linux/efi_embedded_fw.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
158c2ecf20Sopenharmony_ci#include <crypto/sha.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* Exported for use by lib/test_firmware.c only */
188c2ecf20Sopenharmony_ciLIST_HEAD(efi_embedded_fw_list);
198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_NS_GPL(efi_embedded_fw_list, TEST_FIRMWARE);
208c2ecf20Sopenharmony_cibool efi_embedded_fw_checked;
218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_NS_GPL(efi_embedded_fw_checked, TEST_FIRMWARE);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const struct dmi_system_id * const embedded_fw_table[] = {
248c2ecf20Sopenharmony_ci#ifdef CONFIG_TOUCHSCREEN_DMI
258c2ecf20Sopenharmony_ci	touchscreen_dmi_table,
268c2ecf20Sopenharmony_ci#endif
278c2ecf20Sopenharmony_ci	NULL
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * Note the efi_check_for_embedded_firmwares() code currently makes the
328c2ecf20Sopenharmony_ci * following 2 assumptions. This may needs to be revisited if embedded firmware
338c2ecf20Sopenharmony_ci * is found where this is not true:
348c2ecf20Sopenharmony_ci * 1) The firmware is only found in EFI_BOOT_SERVICES_CODE memory segments
358c2ecf20Sopenharmony_ci * 2) The firmware always starts at an offset which is a multiple of 8 bytes
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_cistatic int __init efi_check_md_for_embedded_firmware(
388c2ecf20Sopenharmony_ci	efi_memory_desc_t *md, const struct efi_embedded_fw_desc *desc)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct efi_embedded_fw *fw;
418c2ecf20Sopenharmony_ci	u8 hash[32];
428c2ecf20Sopenharmony_ci	u64 i, size;
438c2ecf20Sopenharmony_ci	u8 *map;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	size = md->num_pages << EFI_PAGE_SHIFT;
468c2ecf20Sopenharmony_ci	map = memremap(md->phys_addr, size, MEMREMAP_WB);
478c2ecf20Sopenharmony_ci	if (!map) {
488c2ecf20Sopenharmony_ci		pr_err("Error mapping EFI mem at %#llx\n", md->phys_addr);
498c2ecf20Sopenharmony_ci		return -ENOMEM;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	for (i = 0; (i + desc->length) <= size; i += 8) {
538c2ecf20Sopenharmony_ci		if (memcmp(map + i, desc->prefix, EFI_EMBEDDED_FW_PREFIX_LEN))
548c2ecf20Sopenharmony_ci			continue;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci		sha256(map + i, desc->length, hash);
578c2ecf20Sopenharmony_ci		if (memcmp(hash, desc->sha256, 32) == 0)
588c2ecf20Sopenharmony_ci			break;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	if ((i + desc->length) > size) {
618c2ecf20Sopenharmony_ci		memunmap(map);
628c2ecf20Sopenharmony_ci		return -ENOENT;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	pr_info("Found EFI embedded fw '%s'\n", desc->name);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	fw = kmalloc(sizeof(*fw), GFP_KERNEL);
688c2ecf20Sopenharmony_ci	if (!fw) {
698c2ecf20Sopenharmony_ci		memunmap(map);
708c2ecf20Sopenharmony_ci		return -ENOMEM;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	fw->data = kmemdup(map + i, desc->length, GFP_KERNEL);
748c2ecf20Sopenharmony_ci	memunmap(map);
758c2ecf20Sopenharmony_ci	if (!fw->data) {
768c2ecf20Sopenharmony_ci		kfree(fw);
778c2ecf20Sopenharmony_ci		return -ENOMEM;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	fw->name = desc->name;
818c2ecf20Sopenharmony_ci	fw->length = desc->length;
828c2ecf20Sopenharmony_ci	list_add(&fw->list, &efi_embedded_fw_list);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return 0;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_civoid __init efi_check_for_embedded_firmwares(void)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	const struct efi_embedded_fw_desc *fw_desc;
908c2ecf20Sopenharmony_ci	const struct dmi_system_id *dmi_id;
918c2ecf20Sopenharmony_ci	efi_memory_desc_t *md;
928c2ecf20Sopenharmony_ci	int i, r;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	for (i = 0; embedded_fw_table[i]; i++) {
958c2ecf20Sopenharmony_ci		dmi_id = dmi_first_match(embedded_fw_table[i]);
968c2ecf20Sopenharmony_ci		if (!dmi_id)
978c2ecf20Sopenharmony_ci			continue;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci		fw_desc = dmi_id->driver_data;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		/*
1028c2ecf20Sopenharmony_ci		 * In some drivers the struct driver_data contains may contain
1038c2ecf20Sopenharmony_ci		 * other driver specific data after the fw_desc struct; and
1048c2ecf20Sopenharmony_ci		 * the fw_desc struct itself may be empty, skip these.
1058c2ecf20Sopenharmony_ci		 */
1068c2ecf20Sopenharmony_ci		if (!fw_desc->name)
1078c2ecf20Sopenharmony_ci			continue;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		for_each_efi_memory_desc(md) {
1108c2ecf20Sopenharmony_ci			if (md->type != EFI_BOOT_SERVICES_CODE)
1118c2ecf20Sopenharmony_ci				continue;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci			r = efi_check_md_for_embedded_firmware(md, fw_desc);
1148c2ecf20Sopenharmony_ci			if (r == 0)
1158c2ecf20Sopenharmony_ci				break;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	efi_embedded_fw_checked = true;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciint efi_get_embedded_fw(const char *name, const u8 **data, size_t *size)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct efi_embedded_fw *iter, *fw = NULL;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (!efi_embedded_fw_checked) {
1278c2ecf20Sopenharmony_ci		pr_warn("Warning %s called while we did not check for embedded fw\n",
1288c2ecf20Sopenharmony_ci			__func__);
1298c2ecf20Sopenharmony_ci		return -ENOENT;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	list_for_each_entry(iter, &efi_embedded_fw_list, list) {
1338c2ecf20Sopenharmony_ci		if (strcmp(name, iter->name) == 0) {
1348c2ecf20Sopenharmony_ci			fw = iter;
1358c2ecf20Sopenharmony_ci			break;
1368c2ecf20Sopenharmony_ci		}
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (!fw)
1408c2ecf20Sopenharmony_ci		return -ENOENT;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	*data = fw->data;
1438c2ecf20Sopenharmony_ci	*size = fw->length;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	return 0;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(efi_get_embedded_fw);
148