memconsole-x86-legacy.c 3.9 KB

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