blob: 6a2f5691b1ab57261809617ca9a9b375adfe135e [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
22struct acpi_table_bgrt *bgrt_tab;
Jan Beulich13f0e4d2012-11-23 16:30:07 +000023void *__initdata bgrt_image;
24size_t __initdata bgrt_image_size;
Josh Triplett2223af32012-09-28 17:57:05 -070025
26struct bmp_header {
27 u16 id;
28 u32 size;
29} __packed;
30
Jan Beulich13f0e4d2012-11-23 16:30:07 +000031void __init efi_bgrt_init(void)
Josh Triplett2223af32012-09-28 17:57:05 -070032{
33 acpi_status status;
Sai Praneeth50a0cb52015-12-09 15:41:08 -080034 void *image;
Josh Triplett2223af32012-09-28 17:57:05 -070035 struct bmp_header bmp_header;
36
37 if (acpi_disabled)
38 return;
39
40 status = acpi_get_table("BGRT", 0,
41 (struct acpi_table_header **)&bgrt_tab);
42 if (ACPI_FAILURE(status))
43 return;
44
Josh Triplett12822782014-07-30 12:23:32 -070045 if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010046 pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
Josh Triplett12822782014-07-30 12:23:32 -070047 bgrt_tab->header.length, sizeof(*bgrt_tab));
Jan Beulich5d6d5782012-11-07 16:46:08 +000048 return;
Josh Triplett12822782014-07-30 12:23:32 -070049 }
50 if (bgrt_tab->version != 1) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010051 pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
Josh Triplett12822782014-07-30 12:23:32 -070052 bgrt_tab->version);
Josh Triplett2223af32012-09-28 17:57:05 -070053 return;
Josh Triplett12822782014-07-30 12:23:32 -070054 }
Matt Fleming248fbcd2015-08-07 09:36:55 +010055 if (bgrt_tab->status & 0xfe) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010056 pr_notice("Ignoring BGRT: reserved status bits are non-zero %u\n",
Josh Triplett12822782014-07-30 12:23:32 -070057 bgrt_tab->status);
Josh Triplett2223af32012-09-28 17:57:05 -070058 return;
Josh Triplett12822782014-07-30 12:23:32 -070059 }
60 if (bgrt_tab->image_type != 0) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010061 pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
Josh Triplett12822782014-07-30 12:23:32 -070062 bgrt_tab->image_type);
63 return;
64 }
65 if (!bgrt_tab->image_address) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010066 pr_notice("Ignoring BGRT: null image address\n");
Josh Triplett12822782014-07-30 12:23:32 -070067 return;
68 }
Josh Triplett2223af32012-09-28 17:57:05 -070069
Matt Fleminge2c90dd2015-12-21 14:12:52 +000070 image = memremap(bgrt_tab->image_address, sizeof(bmp_header), MEMREMAP_WB);
Josh Triplett2223af32012-09-28 17:57:05 -070071 if (!image) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010072 pr_notice("Ignoring BGRT: failed to map image header memory\n");
Sai Praneeth50a0cb52015-12-09 15:41:08 -080073 return;
Josh Triplett2223af32012-09-28 17:57:05 -070074 }
75
Sai Praneeth50a0cb52015-12-09 15:41:08 -080076 memcpy(&bmp_header, image, sizeof(bmp_header));
Matt Fleminge2c90dd2015-12-21 14:12:52 +000077 memunmap(image);
Môshe van der Sterre66dbe99c2016-02-01 22:07:03 +000078 if (bmp_header.id != 0x4d42) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010079 pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n",
Môshe van der Sterre66dbe99c2016-02-01 22:07:03 +000080 bmp_header.id);
81 return;
82 }
Josh Triplett2223af32012-09-28 17:57:05 -070083 bgrt_image_size = bmp_header.size;
84
Josh Triplett12822782014-07-30 12:23:32 -070085 bgrt_image = kmalloc(bgrt_image_size, GFP_KERNEL | __GFP_NOWARN);
86 if (!bgrt_image) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010087 pr_notice("Ignoring BGRT: failed to allocate memory for image (wanted %zu bytes)\n",
Josh Triplett12822782014-07-30 12:23:32 -070088 bgrt_image_size);
Josh Triplett2223af32012-09-28 17:57:05 -070089 return;
Josh Triplett12822782014-07-30 12:23:32 -070090 }
Josh Triplett2223af32012-09-28 17:57:05 -070091
Matt Fleminge2c90dd2015-12-21 14:12:52 +000092 image = memremap(bgrt_tab->image_address, bmp_header.size, MEMREMAP_WB);
Sai Praneeth50a0cb52015-12-09 15:41:08 -080093 if (!image) {
Josh Boyer7f9b4742016-05-03 20:29:41 +010094 pr_notice("Ignoring BGRT: failed to map image memory\n");
Sai Praneeth50a0cb52015-12-09 15:41:08 -080095 kfree(bgrt_image);
96 bgrt_image = NULL;
97 return;
Josh Triplett2223af32012-09-28 17:57:05 -070098 }
99
Sai Praneeth50a0cb52015-12-09 15:41:08 -0800100 memcpy(bgrt_image, image, bgrt_image_size);
Matt Fleminge2c90dd2015-12-21 14:12:52 +0000101 memunmap(image);
Josh Triplett2223af32012-09-28 17:57:05 -0700102}