18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2012 Intel Corporation
48c2ecf20Sopenharmony_ci * Author: Josh Triplett <josh@joshtriplett.org>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Based on the bgrt driver:
78c2ecf20Sopenharmony_ci * Copyright 2012 Red Hat, Inc <mjg@redhat.com>
88c2ecf20Sopenharmony_ci * Author: Matthew Garrett
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/acpi.h>
168c2ecf20Sopenharmony_ci#include <linux/efi.h>
178c2ecf20Sopenharmony_ci#include <linux/efi-bgrt.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct acpi_table_bgrt bgrt_tab;
208c2ecf20Sopenharmony_cisize_t bgrt_image_size;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct bmp_header {
238c2ecf20Sopenharmony_ci	u16 id;
248c2ecf20Sopenharmony_ci	u32 size;
258c2ecf20Sopenharmony_ci} __packed;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_civoid __init efi_bgrt_init(struct acpi_table_header *table)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	void *image;
308c2ecf20Sopenharmony_ci	struct bmp_header bmp_header;
318c2ecf20Sopenharmony_ci	struct acpi_table_bgrt *bgrt = &bgrt_tab;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	if (acpi_disabled)
348c2ecf20Sopenharmony_ci		return;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	if (!efi_enabled(EFI_MEMMAP))
378c2ecf20Sopenharmony_ci		return;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if (table->length < sizeof(bgrt_tab)) {
408c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
418c2ecf20Sopenharmony_ci		       table->length, sizeof(bgrt_tab));
428c2ecf20Sopenharmony_ci		return;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci	*bgrt = *(struct acpi_table_bgrt *)table;
458c2ecf20Sopenharmony_ci	/*
468c2ecf20Sopenharmony_ci	 * Only version 1 is defined but some older laptops (seen on Lenovo
478c2ecf20Sopenharmony_ci	 * Ivy Bridge models) have a correct version 1 BGRT table with the
488c2ecf20Sopenharmony_ci	 * version set to 0, so we accept version 0 and 1.
498c2ecf20Sopenharmony_ci	 */
508c2ecf20Sopenharmony_ci	if (bgrt->version > 1) {
518c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
528c2ecf20Sopenharmony_ci		       bgrt->version);
538c2ecf20Sopenharmony_ci		goto out;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci	if (bgrt->image_type != 0) {
568c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
578c2ecf20Sopenharmony_ci		       bgrt->image_type);
588c2ecf20Sopenharmony_ci		goto out;
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	if (!bgrt->image_address) {
618c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: null image address\n");
628c2ecf20Sopenharmony_ci		goto out;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (efi_mem_type(bgrt->image_address) != EFI_BOOT_SERVICES_DATA) {
668c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: invalid image address\n");
678c2ecf20Sopenharmony_ci		goto out;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci	image = early_memremap(bgrt->image_address, sizeof(bmp_header));
708c2ecf20Sopenharmony_ci	if (!image) {
718c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: failed to map image header memory\n");
728c2ecf20Sopenharmony_ci		goto out;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	memcpy(&bmp_header, image, sizeof(bmp_header));
768c2ecf20Sopenharmony_ci	early_memunmap(image, sizeof(bmp_header));
778c2ecf20Sopenharmony_ci	if (bmp_header.id != 0x4d42) {
788c2ecf20Sopenharmony_ci		pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n",
798c2ecf20Sopenharmony_ci			bmp_header.id);
808c2ecf20Sopenharmony_ci		goto out;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	bgrt_image_size = bmp_header.size;
838c2ecf20Sopenharmony_ci	efi_mem_reserve(bgrt->image_address, bgrt_image_size);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return;
868c2ecf20Sopenharmony_ciout:
878c2ecf20Sopenharmony_ci	memset(bgrt, 0, sizeof(bgrt_tab));
888c2ecf20Sopenharmony_ci}
89