^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) * Copyright 2012 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author: Josh Triplett <josh@joshtriplett.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Based on the bgrt driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright 2012 Red Hat, Inc <mjg@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Author: Matthew Garrett
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/efi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/efi-bgrt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct acpi_table_bgrt bgrt_tab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) size_t bgrt_image_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct bmp_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) u16 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void __init efi_bgrt_init(struct acpi_table_header *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void *image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct bmp_header bmp_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct acpi_table_bgrt *bgrt = &bgrt_tab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (acpi_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!efi_enabled(EFI_MEMMAP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (table->length < sizeof(bgrt_tab)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) table->length, sizeof(bgrt_tab));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *bgrt = *(struct acpi_table_bgrt *)table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Only version 1 is defined but some older laptops (seen on Lenovo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * Ivy Bridge models) have a correct version 1 BGRT table with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * version set to 0, so we accept version 0 and 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (bgrt->version > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) bgrt->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (bgrt->image_type != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) bgrt->image_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!bgrt->image_address) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pr_notice("Ignoring BGRT: null image address\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) goto out;
^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) if (efi_mem_type(bgrt->image_address) != EFI_BOOT_SERVICES_DATA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pr_notice("Ignoring BGRT: invalid image address\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) image = early_memremap(bgrt->image_address, sizeof(bmp_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!image) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pr_notice("Ignoring BGRT: failed to map image header memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) memcpy(&bmp_header, image, sizeof(bmp_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) early_memunmap(image, sizeof(bmp_header));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (bmp_header.id != 0x4d42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bmp_header.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bgrt_image_size = bmp_header.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) efi_mem_reserve(bgrt->image_address, bgrt_image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) memset(bgrt, 0, sizeof(bgrt_tab));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }