4 * Infrastructure for importing the BIOS memory based console
5 * into the kernel log ringbuffer.
7 * Copyright 2010 Google Inc. All rights reserved.
10 #include <linux/ctype.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/sysfs.h>
15 #include <linux/kobject.h>
16 #include <linux/module.h>
17 #include <linux/dmi.h>
19 #include <asm/bios_ebda.h>
22 #define BIOS_MEMCONSOLE_V1_MAGIC 0xDEADBABE
23 #define BIOS_MEMCONSOLE_V2_MAGIC (('M')|('C'<<8)|('O'<<16)|('N'<<24))
25 struct biosmemcon_ebda {
38 /* Misdocumented as number of pages! */
46 static char *memconsole_baseaddr;
47 static size_t memconsole_length;
48 static bool coreboot_system;
50 static int set_coreboot_system_flag(const struct dmi_system_id *unused)
52 coreboot_system = true;
56 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp,
57 struct bin_attribute *bin_attr, char *buf,
58 loff_t pos, size_t count)
60 return memory_read_from_buffer(buf, count, &pos, memconsole_baseaddr,
64 static struct bin_attribute memconsole_bin_attr = {
65 .attr = {.name = "log", .mode = 0444},
66 .read = memconsole_read,
70 static void found_v1_header(struct biosmemcon_ebda *hdr)
72 printk(KERN_INFO "BIOS console v1 EBDA structure found at %p\n", hdr);
73 printk(KERN_INFO "BIOS console buffer at 0x%.8x, "
74 "start = %d, end = %d, num = %d\n",
75 hdr->v1.buffer_addr, hdr->v1.start,
76 hdr->v1.end, hdr->v1.num_chars);
78 memconsole_length = hdr->v1.num_chars;
79 memconsole_baseaddr = phys_to_virt(hdr->v1.buffer_addr);
82 static void found_v2_header(struct biosmemcon_ebda *hdr)
84 printk(KERN_INFO "BIOS console v2 EBDA structure found at %p\n", hdr);
85 printk(KERN_INFO "BIOS console buffer at 0x%.8x, "
86 "start = %d, end = %d, num_bytes = %d\n",
87 hdr->v2.buffer_addr, hdr->v2.start,
88 hdr->v2.end, hdr->v2.num_bytes);
90 memconsole_length = hdr->v2.end - hdr->v2.start;
91 memconsole_baseaddr = phys_to_virt(hdr->v2.buffer_addr
96 * The function below tries to find the firmware console log saved in the
97 * appropriate coreboot CBMEM section, using the structures and constants from
100 * Coreboot's CBMEM is a structure allocated by coreboot above usable memory
101 * at a 128K aligned address. One of the CBMEM sections could contain the
102 * firmare console log.
105 /* Coreboot CBMEM section descriptor. */
113 /* CBMEM firmware console log descriptor. */
120 struct cbmem_cons __iomem *cbmem_console;
122 static bool check_cbmem(void)
125 phys_addr_t top_of_ram, scan_addr;
127 /* coreboot CBMEM constants */
128 #define CBMEM_ALIGNMENT (128 * 1024)
129 #define MEMORY_BOUNDARY (1024 * 1024 * 1024)
130 #define MAX_CBMEM_ENTRIES 16
131 #define CBMEM_ENTRY_MAGIC 0x434f5245
132 #define CBMEM_CONSOLE_ID 0x434f4e53
135 * Determine where to start looking for CBMEM signature: take the top
136 * of usable memory and align it up to 128K boundary.
139 top_of_ram = (phys_addr_t) sysi.totalram << PAGE_SHIFT;
140 scan_addr = ALIGN(top_of_ram, CBMEM_ALIGNMENT) + CBMEM_ALIGNMENT;
142 while (scan_addr % MEMORY_BOUNDARY) {
143 struct cbmem_entry __iomem *pcbm;
144 int i, remap_size = sizeof(struct cbmem_entry) * 16;
147 * See if we reached reserved memory. Bail out if so, as it is
148 * not mappable and is above the region where the CBMEM could
151 if (e820_any_mapped(scan_addr,
152 scan_addr + remap_size,
156 pcbm = ioremap(scan_addr, remap_size);
157 if (pcbm->magic != CBMEM_ENTRY_MAGIC) {
159 scan_addr += CBMEM_ALIGNMENT;
163 /* CBMEM found. Is the console log there? */
164 for (i = 1; i < MAX_CBMEM_ENTRIES; i++) {
165 if ((pcbm[i].magic == CBMEM_ENTRY_MAGIC) &&
166 (pcbm[i].id == CBMEM_CONSOLE_ID)) {
167 /* Yes, map the log. */
168 cbmem_console = ioremap(pcbm[i].base,
178 memconsole_baseaddr = cbmem_console->buffer_body;
179 memconsole_length = min(cbmem_console->buffer_cursor,
180 cbmem_console->buffer_size);
184 printk(KERN_INFO "CBMEM console structure not found!\n");
189 * Search through the EBDA for the BIOS Memory Console, and
190 * set the global variables to point to it. Return true if found.
192 static bool found_memconsole(void)
194 unsigned int address;
197 /* Is it communicated through CBMEM? */
198 if (coreboot_system && check_cbmem())
201 address = get_bios_ebda();
203 printk(KERN_INFO "BIOS EBDA non-existent.\n");
207 /* EBDA length is byte 0 of EBDA (in KB) */
208 length = *(u8 *)phys_to_virt(address);
209 length <<= 10; /* convert to bytes */
212 * Search through EBDA for BIOS memory console structure
213 * note: signature is not necessarily dword-aligned
215 for (cur = 0; cur < length; cur++) {
216 struct biosmemcon_ebda *hdr = phys_to_virt(address + cur);
219 if (hdr->signature == BIOS_MEMCONSOLE_V1_MAGIC) {
220 found_v1_header(hdr);
225 if (hdr->signature == BIOS_MEMCONSOLE_V2_MAGIC) {
226 found_v2_header(hdr);
231 printk(KERN_INFO "BIOS console EBDA structure not found!\n");
235 static struct dmi_system_id memconsole_dmi_table[] __initdata = {
237 .ident = "Google Board",
239 DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
243 .ident = "Google Board",
244 .callback = set_coreboot_system_flag,
246 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
251 MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
253 static int __init memconsole_init(void)
257 if (!dmi_check_system(memconsole_dmi_table))
260 if (!found_memconsole())
263 memconsole_bin_attr.size = memconsole_length;
265 ret = sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr);
270 static void __exit memconsole_exit(void)
273 iounmap(cbmem_console);
275 sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr);
278 module_init(memconsole_init);
279 module_exit(memconsole_exit);
281 MODULE_AUTHOR("Google, Inc.");
282 MODULE_LICENSE("GPL");