efi-bgrt.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/acpi.h>
  14. #include <linux/efi.h>
  15. #include <linux/efi-bgrt.h>
  16. struct acpi_table_bgrt bgrt_tab;
  17. size_t bgrt_image_size;
  18. struct bmp_header {
  19. u16 id;
  20. u32 size;
  21. } __packed;
  22. void __init efi_bgrt_init(struct acpi_table_header *table)
  23. {
  24. void *image;
  25. struct bmp_header bmp_header;
  26. struct acpi_table_bgrt *bgrt = &bgrt_tab;
  27. if (acpi_disabled)
  28. return;
  29. if (!efi_enabled(EFI_MEMMAP))
  30. return;
  31. if (table->length < sizeof(bgrt_tab)) {
  32. pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
  33. table->length, sizeof(bgrt_tab));
  34. return;
  35. }
  36. *bgrt = *(struct acpi_table_bgrt *)table;
  37. /*
  38. * Only version 1 is defined but some older laptops (seen on Lenovo
  39. * Ivy Bridge models) have a correct version 1 BGRT table with the
  40. * version set to 0, so we accept version 0 and 1.
  41. */
  42. if (bgrt->version > 1) {
  43. pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
  44. bgrt->version);
  45. goto out;
  46. }
  47. if (bgrt->image_type != 0) {
  48. pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
  49. bgrt->image_type);
  50. goto out;
  51. }
  52. if (!bgrt->image_address) {
  53. pr_notice("Ignoring BGRT: null image address\n");
  54. goto out;
  55. }
  56. if (efi_mem_type(bgrt->image_address) != EFI_BOOT_SERVICES_DATA) {
  57. pr_notice("Ignoring BGRT: invalid image address\n");
  58. goto out;
  59. }
  60. image = early_memremap(bgrt->image_address, sizeof(bmp_header));
  61. if (!image) {
  62. pr_notice("Ignoring BGRT: failed to map image header memory\n");
  63. goto out;
  64. }
  65. memcpy(&bmp_header, image, sizeof(bmp_header));
  66. early_memunmap(image, sizeof(bmp_header));
  67. if (bmp_header.id != 0x4d42) {
  68. pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n",
  69. bmp_header.id);
  70. goto out;
  71. }
  72. bgrt_image_size = bmp_header.size;
  73. efi_mem_reserve(bgrt->image_address, bgrt_image_size);
  74. return;
  75. out:
  76. memset(bgrt, 0, sizeof(bgrt_tab));
  77. }