Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Fleming | 26d7f65 | 2015-10-25 10:26:35 +0000 | [diff] [blame] | 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Jan Beulich | 13f0e4d | 2012-11-23 16:30:07 +0000 | [diff] [blame] | 17 | #include <linux/init.h> |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 18 | #include <linux/acpi.h> |
| 19 | #include <linux/efi.h> |
| 20 | #include <linux/efi-bgrt.h> |
| 21 | |
| 22 | struct acpi_table_bgrt *bgrt_tab; |
Jan Beulich | 13f0e4d | 2012-11-23 16:30:07 +0000 | [diff] [blame] | 23 | void *__initdata bgrt_image; |
| 24 | size_t __initdata bgrt_image_size; |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 25 | |
| 26 | struct bmp_header { |
| 27 | u16 id; |
| 28 | u32 size; |
| 29 | } __packed; |
| 30 | |
Jan Beulich | 13f0e4d | 2012-11-23 16:30:07 +0000 | [diff] [blame] | 31 | void __init efi_bgrt_init(void) |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 32 | { |
| 33 | acpi_status status; |
Sai Praneeth | 50a0cb5 | 2015-12-09 15:41:08 -0800 | [diff] [blame] | 34 | void *image; |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 35 | 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 Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 45 | if (bgrt_tab->header.length < sizeof(*bgrt_tab)) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 46 | pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n", |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 47 | bgrt_tab->header.length, sizeof(*bgrt_tab)); |
Jan Beulich | 5d6d578 | 2012-11-07 16:46:08 +0000 | [diff] [blame] | 48 | return; |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 49 | } |
| 50 | if (bgrt_tab->version != 1) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 51 | pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n", |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 52 | bgrt_tab->version); |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 53 | return; |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 54 | } |
Matt Fleming | 248fbcd | 2015-08-07 09:36:55 +0100 | [diff] [blame] | 55 | if (bgrt_tab->status & 0xfe) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 56 | pr_notice("Ignoring BGRT: reserved status bits are non-zero %u\n", |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 57 | bgrt_tab->status); |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 58 | return; |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 59 | } |
| 60 | if (bgrt_tab->image_type != 0) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 61 | pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n", |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 62 | bgrt_tab->image_type); |
| 63 | return; |
| 64 | } |
| 65 | if (!bgrt_tab->image_address) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 66 | pr_notice("Ignoring BGRT: null image address\n"); |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 67 | return; |
| 68 | } |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 69 | |
Matt Fleming | e2c90dd | 2015-12-21 14:12:52 +0000 | [diff] [blame] | 70 | image = memremap(bgrt_tab->image_address, sizeof(bmp_header), MEMREMAP_WB); |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 71 | if (!image) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 72 | pr_notice("Ignoring BGRT: failed to map image header memory\n"); |
Sai Praneeth | 50a0cb5 | 2015-12-09 15:41:08 -0800 | [diff] [blame] | 73 | return; |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Sai Praneeth | 50a0cb5 | 2015-12-09 15:41:08 -0800 | [diff] [blame] | 76 | memcpy(&bmp_header, image, sizeof(bmp_header)); |
Matt Fleming | e2c90dd | 2015-12-21 14:12:52 +0000 | [diff] [blame] | 77 | memunmap(image); |
Môshe van der Sterre | 66dbe99c | 2016-02-01 22:07:03 +0000 | [diff] [blame] | 78 | if (bmp_header.id != 0x4d42) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 79 | pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n", |
Môshe van der Sterre | 66dbe99c | 2016-02-01 22:07:03 +0000 | [diff] [blame] | 80 | bmp_header.id); |
| 81 | return; |
| 82 | } |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 83 | bgrt_image_size = bmp_header.size; |
| 84 | |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 85 | bgrt_image = kmalloc(bgrt_image_size, GFP_KERNEL | __GFP_NOWARN); |
| 86 | if (!bgrt_image) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 87 | pr_notice("Ignoring BGRT: failed to allocate memory for image (wanted %zu bytes)\n", |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 88 | bgrt_image_size); |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 89 | return; |
Josh Triplett | 1282278 | 2014-07-30 12:23:32 -0700 | [diff] [blame] | 90 | } |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 91 | |
Matt Fleming | e2c90dd | 2015-12-21 14:12:52 +0000 | [diff] [blame] | 92 | image = memremap(bgrt_tab->image_address, bmp_header.size, MEMREMAP_WB); |
Sai Praneeth | 50a0cb5 | 2015-12-09 15:41:08 -0800 | [diff] [blame] | 93 | if (!image) { |
Josh Boyer | 7f9b474 | 2016-05-03 20:29:41 +0100 | [diff] [blame] | 94 | pr_notice("Ignoring BGRT: failed to map image memory\n"); |
Sai Praneeth | 50a0cb5 | 2015-12-09 15:41:08 -0800 | [diff] [blame] | 95 | kfree(bgrt_image); |
| 96 | bgrt_image = NULL; |
| 97 | return; |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Sai Praneeth | 50a0cb5 | 2015-12-09 15:41:08 -0800 | [diff] [blame] | 100 | memcpy(bgrt_image, image, bgrt_image_size); |
Matt Fleming | e2c90dd | 2015-12-21 14:12:52 +0000 | [diff] [blame] | 101 | memunmap(image); |
Josh Triplett | 2223af3 | 2012-09-28 17:57:05 -0700 | [diff] [blame] | 102 | } |