memconsole-x86-legacy.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * memconsole-x86-legacy.c
  4. *
  5. * EBDA specific parts of the memory based BIOS console.
  6. *
  7. * Copyright 2017 Google Inc.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/dmi.h>
  12. #include <linux/mm.h>
  13. #include <asm/bios_ebda.h>
  14. #include <linux/acpi.h>
  15. #include "memconsole.h"
  16. #define BIOS_MEMCONSOLE_V1_MAGIC 0xDEADBABE
  17. #define BIOS_MEMCONSOLE_V2_MAGIC (('M')|('C'<<8)|('O'<<16)|('N'<<24))
  18. struct biosmemcon_ebda {
  19. u32 signature;
  20. union {
  21. struct {
  22. u8 enabled;
  23. u32 buffer_addr;
  24. u16 start;
  25. u16 end;
  26. u16 num_chars;
  27. u8 wrapped;
  28. } __packed v1;
  29. struct {
  30. u32 buffer_addr;
  31. /* Misdocumented as number of pages! */
  32. u16 num_bytes;
  33. u16 start;
  34. u16 end;
  35. } __packed v2;
  36. };
  37. } __packed;
  38. static char *memconsole_baseaddr;
  39. static size_t memconsole_length;
  40. static ssize_t memconsole_read(char *buf, loff_t pos, size_t count)
  41. {
  42. return memory_read_from_buffer(buf, count, &pos, memconsole_baseaddr,
  43. memconsole_length);
  44. }
  45. static void found_v1_header(struct biosmemcon_ebda *hdr)
  46. {
  47. pr_info("memconsole: BIOS console v1 EBDA structure found at %p\n",
  48. hdr);
  49. pr_info("memconsole: BIOS console buffer at 0x%.8x, start = %d, end = %d, num = %d\n",
  50. hdr->v1.buffer_addr, hdr->v1.start,
  51. hdr->v1.end, hdr->v1.num_chars);
  52. memconsole_baseaddr = phys_to_virt(hdr->v1.buffer_addr);
  53. memconsole_length = hdr->v1.num_chars;
  54. memconsole_setup(memconsole_read);
  55. }
  56. static void found_v2_header(struct biosmemcon_ebda *hdr)
  57. {
  58. pr_info("memconsole: BIOS console v2 EBDA structure found at %p\n",
  59. hdr);
  60. pr_info("memconsole: BIOS console buffer at 0x%.8x, start = %d, end = %d, num_bytes = %d\n",
  61. hdr->v2.buffer_addr, hdr->v2.start,
  62. hdr->v2.end, hdr->v2.num_bytes);
  63. memconsole_baseaddr = phys_to_virt(hdr->v2.buffer_addr + hdr->v2.start);
  64. memconsole_length = hdr->v2.end - hdr->v2.start;
  65. memconsole_setup(memconsole_read);
  66. }
  67. /*
  68. * Search through the EBDA for the BIOS Memory Console, and
  69. * set the global variables to point to it. Return true if found.
  70. */
  71. static bool memconsole_ebda_init(void)
  72. {
  73. unsigned int address;
  74. size_t length, cur;
  75. address = get_bios_ebda();
  76. if (!address) {
  77. pr_info("memconsole: BIOS EBDA non-existent.\n");
  78. return false;
  79. }
  80. /* EBDA length is byte 0 of EBDA (in KB) */
  81. length = *(u8 *)phys_to_virt(address);
  82. length <<= 10; /* convert to bytes */
  83. /*
  84. * Search through EBDA for BIOS memory console structure
  85. * note: signature is not necessarily dword-aligned
  86. */
  87. for (cur = 0; cur < length; cur++) {
  88. struct biosmemcon_ebda *hdr = phys_to_virt(address + cur);
  89. /* memconsole v1 */
  90. if (hdr->signature == BIOS_MEMCONSOLE_V1_MAGIC) {
  91. found_v1_header(hdr);
  92. return true;
  93. }
  94. /* memconsole v2 */
  95. if (hdr->signature == BIOS_MEMCONSOLE_V2_MAGIC) {
  96. found_v2_header(hdr);
  97. return true;
  98. }
  99. }
  100. pr_info("memconsole: BIOS console EBDA structure not found!\n");
  101. return false;
  102. }
  103. static const struct dmi_system_id memconsole_dmi_table[] __initconst = {
  104. {
  105. .ident = "Google Board",
  106. .matches = {
  107. DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
  108. },
  109. },
  110. {}
  111. };
  112. MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
  113. static bool __init memconsole_find(void)
  114. {
  115. if (!dmi_check_system(memconsole_dmi_table))
  116. return false;
  117. return memconsole_ebda_init();
  118. }
  119. static int __init memconsole_x86_init(void)
  120. {
  121. if (!memconsole_find())
  122. return -ENODEV;
  123. return memconsole_sysfs_init();
  124. }
  125. static void __exit memconsole_x86_exit(void)
  126. {
  127. memconsole_exit();
  128. }
  129. module_init(memconsole_x86_init);
  130. module_exit(memconsole_x86_exit);
  131. MODULE_AUTHOR("Google, Inc.");
  132. MODULE_LICENSE("GPL");