blob: 9a52b5c4438f70d3342d64319f54c5d6a1e09d5b [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;
34 void __iomem *image;
35 bool ioremapped = false;
36 struct bmp_header bmp_header;
37
38 if (acpi_disabled)
39 return;
40
41 status = acpi_get_table("BGRT", 0,
42 (struct acpi_table_header **)&bgrt_tab);
43 if (ACPI_FAILURE(status))
44 return;
45
Josh Triplett12822782014-07-30 12:23:32 -070046 if (bgrt_tab->header.length < sizeof(*bgrt_tab)) {
47 pr_err("Ignoring BGRT: invalid length %u (expected %zu)\n",
48 bgrt_tab->header.length, sizeof(*bgrt_tab));
Jan Beulich5d6d5782012-11-07 16:46:08 +000049 return;
Josh Triplett12822782014-07-30 12:23:32 -070050 }
51 if (bgrt_tab->version != 1) {
52 pr_err("Ignoring BGRT: invalid version %u (expected 1)\n",
53 bgrt_tab->version);
Josh Triplett2223af32012-09-28 17:57:05 -070054 return;
Josh Triplett12822782014-07-30 12:23:32 -070055 }
Matt Fleming248fbcd2015-08-07 09:36:55 +010056 if (bgrt_tab->status & 0xfe) {
57 pr_err("Ignoring BGRT: reserved status bits are non-zero %u\n",
Josh Triplett12822782014-07-30 12:23:32 -070058 bgrt_tab->status);
Josh Triplett2223af32012-09-28 17:57:05 -070059 return;
Josh Triplett12822782014-07-30 12:23:32 -070060 }
Matt Fleming248fbcd2015-08-07 09:36:55 +010061 if (bgrt_tab->status != 1) {
62 pr_debug("Ignoring BGRT: invalid status %u (expected 1)\n",
63 bgrt_tab->status);
64 return;
65 }
Josh Triplett12822782014-07-30 12:23:32 -070066 if (bgrt_tab->image_type != 0) {
67 pr_err("Ignoring BGRT: invalid image type %u (expected 0)\n",
68 bgrt_tab->image_type);
69 return;
70 }
71 if (!bgrt_tab->image_address) {
72 pr_err("Ignoring BGRT: null image address\n");
73 return;
74 }
Josh Triplett2223af32012-09-28 17:57:05 -070075
76 image = efi_lookup_mapped_addr(bgrt_tab->image_address);
77 if (!image) {
Juergen Gross954e12f2015-02-24 10:13:31 +010078 image = early_ioremap(bgrt_tab->image_address,
Matt Fleming081cd622014-01-14 12:40:09 +000079 sizeof(bmp_header));
Josh Triplett2223af32012-09-28 17:57:05 -070080 ioremapped = true;
Josh Triplett12822782014-07-30 12:23:32 -070081 if (!image) {
82 pr_err("Ignoring BGRT: failed to map image header memory\n");
Josh Triplett2223af32012-09-28 17:57:05 -070083 return;
Josh Triplett12822782014-07-30 12:23:32 -070084 }
Josh Triplett2223af32012-09-28 17:57:05 -070085 }
86
87 memcpy_fromio(&bmp_header, image, sizeof(bmp_header));
88 if (ioremapped)
Matt Fleming081cd622014-01-14 12:40:09 +000089 early_iounmap(image, sizeof(bmp_header));
Josh Triplett2223af32012-09-28 17:57:05 -070090 bgrt_image_size = bmp_header.size;
91
Josh Triplett12822782014-07-30 12:23:32 -070092 bgrt_image = kmalloc(bgrt_image_size, GFP_KERNEL | __GFP_NOWARN);
93 if (!bgrt_image) {
94 pr_err("Ignoring BGRT: failed to allocate memory for image (wanted %zu bytes)\n",
95 bgrt_image_size);
Josh Triplett2223af32012-09-28 17:57:05 -070096 return;
Josh Triplett12822782014-07-30 12:23:32 -070097 }
Josh Triplett2223af32012-09-28 17:57:05 -070098
99 if (ioremapped) {
Juergen Gross954e12f2015-02-24 10:13:31 +0100100 image = early_ioremap(bgrt_tab->image_address,
Matt Fleming081cd622014-01-14 12:40:09 +0000101 bmp_header.size);
Josh Triplett2223af32012-09-28 17:57:05 -0700102 if (!image) {
Josh Triplett12822782014-07-30 12:23:32 -0700103 pr_err("Ignoring BGRT: failed to map image memory\n");
Josh Triplett2223af32012-09-28 17:57:05 -0700104 kfree(bgrt_image);
105 bgrt_image = NULL;
106 return;
107 }
108 }
109
110 memcpy_fromio(bgrt_image, image, bgrt_image_size);
111 if (ioremapped)
Matt Fleming081cd622014-01-14 12:40:09 +0000112 early_iounmap(image, bmp_header.size);
Josh Triplett2223af32012-09-28 17:57:05 -0700113}