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