blob: 8bf27323f7a37c34591c45f8b39d2091ae096260 [file] [log] [blame]
Josh Triplett2223af32012-09-28 17:57:05 -07001/*
2 * Copyright 2012 Intel Corporation
3 * Author: Josh Triplett <josh@joshtriplett.org>
4 *
5 * Based on the bgrt driver:
6 * Copyright 2012 Red Hat, Inc <mjg@redhat.com>
7 * Author: Matthew Garrett
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
Matt Fleming26d7f652015-10-25 10:26:35 +000013
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Josh Triplett2223af32012-09-28 17:57:05 -070016#include <linux/kernel.h>
Jan Beulich13f0e4d2012-11-23 16:30:07 +000017#include <linux/init.h>
Josh Triplett2223af32012-09-28 17:57:05 -070018#include <linux/acpi.h>
19#include <linux/efi.h>
20#include <linux/efi-bgrt.h>
21
Dave Young7b0a9112017-01-31 13:21:40 +000022struct acpi_table_bgrt bgrt_tab;
Jan Beulich13f0e4d2012-11-23 16:30:07 +000023size_t __initdata bgrt_image_size;
Josh Triplett2223af32012-09-28 17:57:05 -070024
25struct bmp_header {
26 u16 id;
27 u32 size;
28} __packed;
29
Dave Young7b0a9112017-01-31 13:21:40 +000030void __init efi_bgrt_init(struct acpi_table_header *table)
Josh Triplett2223af32012-09-28 17:57:05 -070031{
Sai Praneeth50a0cb52015-12-09 15:41:08 -080032 void *image;
Josh Triplett2223af32012-09-28 17:57:05 -070033 struct bmp_header bmp_header;
Dave Young7b0a9112017-01-31 13:21:40 +000034 struct acpi_table_bgrt *bgrt = &bgrt_tab;
Josh Triplett2223af32012-09-28 17:57:05 -070035
36 if (acpi_disabled)
37 return;
38
Dave Young74258262017-05-26 12:36:51 +010039 if (!efi_enabled(EFI_BOOT))
40 return;
41
Dave Young7b0a9112017-01-31 13:21:40 +000042 if (table->length < sizeof(bgrt_tab)) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010043 pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
Dave Young7b0a9112017-01-31 13:21:40 +000044 table->length, sizeof(bgrt_tab));
Jan Beulich5d6d5782012-11-07 16:46:08 +000045 return;
Josh Triplett12822782014-07-30 12:23:32 -070046 }
Dave Young7b0a9112017-01-31 13:21:40 +000047 *bgrt = *(struct acpi_table_bgrt *)table;
48 if (bgrt->version != 1) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010049 pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
Dave Young7b0a9112017-01-31 13:21:40 +000050 bgrt->version);
51 goto out;
Josh Triplett12822782014-07-30 12:23:32 -070052 }
Dave Young7b0a9112017-01-31 13:21:40 +000053 if (bgrt->status & 0xfe) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010054 pr_notice("Ignoring BGRT: reserved status bits are non-zero %u\n",
Dave Young7b0a9112017-01-31 13:21:40 +000055 bgrt->status);
56 goto out;
Josh Triplett12822782014-07-30 12:23:32 -070057 }
Dave Young7b0a9112017-01-31 13:21:40 +000058 if (bgrt->image_type != 0) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010059 pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
Dave Young7b0a9112017-01-31 13:21:40 +000060 bgrt->image_type);
61 goto out;
Josh Triplett12822782014-07-30 12:23:32 -070062 }
Dave Young7b0a9112017-01-31 13:21:40 +000063 if (!bgrt->image_address) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010064 pr_notice("Ignoring BGRT: null image address\n");
Dave Young7b0a9112017-01-31 13:21:40 +000065 goto out;
Josh Triplett12822782014-07-30 12:23:32 -070066 }
Josh Triplett2223af32012-09-28 17:57:05 -070067
Dave Young7b0a9112017-01-31 13:21:40 +000068 image = early_memremap(bgrt->image_address, sizeof(bmp_header));
Josh Triplett2223af32012-09-28 17:57:05 -070069 if (!image) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010070 pr_notice("Ignoring BGRT: failed to map image header memory\n");
Dave Young7b0a9112017-01-31 13:21:40 +000071 goto out;
Josh Triplett2223af32012-09-28 17:57:05 -070072 }
73
Sai Praneeth50a0cb52015-12-09 15:41:08 -080074 memcpy(&bmp_header, image, sizeof(bmp_header));
Dave Young7b0a9112017-01-31 13:21:40 +000075 early_memunmap(image, sizeof(bmp_header));
Môshe van der Sterre66dbe992016-02-01 22:07:03 +000076 if (bmp_header.id != 0x4d42) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010077 pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n",
Môshe van der Sterre66dbe992016-02-01 22:07:03 +000078 bmp_header.id);
Dave Young7b0a9112017-01-31 13:21:40 +000079 goto out;
Môshe van der Sterre66dbe992016-02-01 22:07:03 +000080 }
Josh Triplett2223af32012-09-28 17:57:05 -070081 bgrt_image_size = bmp_header.size;
Dave Young7b0a9112017-01-31 13:21:40 +000082 efi_mem_reserve(bgrt->image_address, bgrt_image_size);
Josh Triplett2223af32012-09-28 17:57:05 -070083
Dave Young7b0a9112017-01-31 13:21:40 +000084 return;
85out:
86 memset(bgrt, 0, sizeof(bgrt_tab));
Josh Triplett2223af32012-09-28 17:57:05 -070087}