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