efi-bgrt.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/acpi.h>
  17. #include <linux/efi.h>
  18. #include <linux/efi-bgrt.h>
  19. struct acpi_table_bgrt bgrt_tab;
  20. size_t __initdata bgrt_image_size;
  21. struct bmp_header {
  22. u16 id;
  23. u32 size;
  24. } __packed;
  25. static bool efi_bgrt_addr_valid(u64 addr)
  26. {
  27. efi_memory_desc_t *md;
  28. for_each_efi_memory_desc(md) {
  29. u64 size;
  30. u64 end;
  31. if (md->type != EFI_BOOT_SERVICES_DATA)
  32. continue;
  33. size = md->num_pages << EFI_PAGE_SHIFT;
  34. end = md->phys_addr + size;
  35. if (addr >= md->phys_addr && addr < end)
  36. return true;
  37. }
  38. return false;
  39. }
  40. void __init efi_bgrt_init(struct acpi_table_header *table)
  41. {
  42. void *image;
  43. struct bmp_header bmp_header;
  44. struct acpi_table_bgrt *bgrt = &bgrt_tab;
  45. if (acpi_disabled)
  46. return;
  47. if (!efi_enabled(EFI_MEMMAP))
  48. return;
  49. if (table->length < sizeof(bgrt_tab)) {
  50. pr_notice("Ignoring BGRT: invalid length %u (expected %zu)\n",
  51. table->length, sizeof(bgrt_tab));
  52. return;
  53. }
  54. *bgrt = *(struct acpi_table_bgrt *)table;
  55. if (bgrt->version != 1) {
  56. pr_notice("Ignoring BGRT: invalid version %u (expected 1)\n",
  57. bgrt->version);
  58. goto out;
  59. }
  60. if (bgrt->status & 0xfe) {
  61. pr_notice("Ignoring BGRT: reserved status bits are non-zero %u\n",
  62. bgrt->status);
  63. goto out;
  64. }
  65. if (bgrt->image_type != 0) {
  66. pr_notice("Ignoring BGRT: invalid image type %u (expected 0)\n",
  67. bgrt->image_type);
  68. goto out;
  69. }
  70. if (!bgrt->image_address) {
  71. pr_notice("Ignoring BGRT: null image address\n");
  72. goto out;
  73. }
  74. if (!efi_bgrt_addr_valid(bgrt->image_address)) {
  75. pr_notice("Ignoring BGRT: invalid image address\n");
  76. goto out;
  77. }
  78. image = early_memremap(bgrt->image_address, sizeof(bmp_header));
  79. if (!image) {
  80. pr_notice("Ignoring BGRT: failed to map image header memory\n");
  81. goto out;
  82. }
  83. memcpy(&bmp_header, image, sizeof(bmp_header));
  84. early_memunmap(image, sizeof(bmp_header));
  85. if (bmp_header.id != 0x4d42) {
  86. pr_notice("Ignoring BGRT: Incorrect BMP magic number 0x%x (expected 0x4d42)\n",
  87. bmp_header.id);
  88. goto out;
  89. }
  90. bgrt_image_size = bmp_header.size;
  91. efi_mem_reserve(bgrt->image_address, bgrt_image_size);
  92. return;
  93. out:
  94. memset(bgrt, 0, sizeof(bgrt_tab));
  95. }