memconsole-x86-legacy.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 void found_v1_header(struct biosmemcon_ebda *hdr)
  47. {
  48. pr_info("memconsole: BIOS console v1 EBDA structure found at %p\n",
  49. hdr);
  50. pr_info("memconsole: BIOS console buffer at 0x%.8x, start = %d, end = %d, num = %d\n",
  51. hdr->v1.buffer_addr, hdr->v1.start,
  52. hdr->v1.end, hdr->v1.num_chars);
  53. memconsole_setup(phys_to_virt(hdr->v1.buffer_addr), hdr->v1.num_chars);
  54. }
  55. static void found_v2_header(struct biosmemcon_ebda *hdr)
  56. {
  57. pr_info("memconsole: BIOS console v2 EBDA structure found at %p\n",
  58. hdr);
  59. pr_info("memconsole: BIOS console buffer at 0x%.8x, start = %d, end = %d, num_bytes = %d\n",
  60. hdr->v2.buffer_addr, hdr->v2.start,
  61. hdr->v2.end, hdr->v2.num_bytes);
  62. memconsole_setup(phys_to_virt(hdr->v2.buffer_addr + hdr->v2.start),
  63. hdr->v2.end - hdr->v2.start);
  64. }
  65. /*
  66. * Search through the EBDA for the BIOS Memory Console, and
  67. * set the global variables to point to it. Return true if found.
  68. */
  69. static bool memconsole_ebda_init(void)
  70. {
  71. unsigned int address;
  72. size_t length, cur;
  73. address = get_bios_ebda();
  74. if (!address) {
  75. pr_info("memconsole: BIOS EBDA non-existent.\n");
  76. return false;
  77. }
  78. /* EBDA length is byte 0 of EBDA (in KB) */
  79. length = *(u8 *)phys_to_virt(address);
  80. length <<= 10; /* convert to bytes */
  81. /*
  82. * Search through EBDA for BIOS memory console structure
  83. * note: signature is not necessarily dword-aligned
  84. */
  85. for (cur = 0; cur < length; cur++) {
  86. struct biosmemcon_ebda *hdr = phys_to_virt(address + cur);
  87. /* memconsole v1 */
  88. if (hdr->signature == BIOS_MEMCONSOLE_V1_MAGIC) {
  89. found_v1_header(hdr);
  90. return true;
  91. }
  92. /* memconsole v2 */
  93. if (hdr->signature == BIOS_MEMCONSOLE_V2_MAGIC) {
  94. found_v2_header(hdr);
  95. return true;
  96. }
  97. }
  98. pr_info("memconsole: BIOS console EBDA structure not found!\n");
  99. return false;
  100. }
  101. static struct dmi_system_id memconsole_dmi_table[] __initdata = {
  102. {
  103. .ident = "Google Board",
  104. .matches = {
  105. DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
  106. },
  107. },
  108. {}
  109. };
  110. MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
  111. static bool __init memconsole_find(void)
  112. {
  113. if (!dmi_check_system(memconsole_dmi_table))
  114. return false;
  115. return memconsole_ebda_init();
  116. }
  117. static int __init memconsole_x86_init(void)
  118. {
  119. if (!memconsole_find())
  120. return -ENODEV;
  121. return memconsole_sysfs_init();
  122. }
  123. static void __exit memconsole_x86_exit(void)
  124. {
  125. memconsole_exit();
  126. }
  127. module_init(memconsole_x86_init);
  128. module_exit(memconsole_x86_exit);
  129. MODULE_AUTHOR("Google, Inc.");
  130. MODULE_LICENSE("GPL");