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>
21 #include <acpi/acpi_bus.h>
23 #define BIOS_MEMCONSOLE_V1_MAGIC 0xDEADBABE
24 #define BIOS_MEMCONSOLE_V2_MAGIC (('M')|('C'<<8)|('O'<<16)|('N'<<24))
26 struct biosmemcon_ebda {
39 /* Misdocumented as number of pages! */
47 static char *memconsole_baseaddr;
48 static size_t memconsole_length;
49 static bool coreboot_system;
51 static int set_coreboot_system_flag(const struct dmi_system_id *unused)
53 coreboot_system = true;
57 static ssize_t memconsole_read(struct file *filp, struct kobject *kobp,
58 struct bin_attribute *bin_attr, char *buf,
59 loff_t pos, size_t count)
61 return memory_read_from_buffer(buf, count, &pos, memconsole_baseaddr,
65 static struct bin_attribute memconsole_bin_attr = {
66 .attr = {.name = "log", .mode = 0444},
67 .read = memconsole_read,
71 static void found_v1_header(struct biosmemcon_ebda *hdr)
73 printk(KERN_INFO "BIOS console v1 EBDA structure found at %p\n", hdr);
74 printk(KERN_INFO "BIOS console buffer at 0x%.8x, "
75 "start = %d, end = %d, num = %d\n",
76 hdr->v1.buffer_addr, hdr->v1.start,
77 hdr->v1.end, hdr->v1.num_chars);
79 memconsole_length = hdr->v1.num_chars;
80 memconsole_baseaddr = phys_to_virt(hdr->v1.buffer_addr);
83 static void found_v2_header(struct biosmemcon_ebda *hdr)
85 printk(KERN_INFO "BIOS console v2 EBDA structure found at %p\n", hdr);
86 printk(KERN_INFO "BIOS console buffer at 0x%.8x, "
87 "start = %d, end = %d, num_bytes = %d\n",
88 hdr->v2.buffer_addr, hdr->v2.start,
89 hdr->v2.end, hdr->v2.num_bytes);
91 memconsole_length = hdr->v2.end - hdr->v2.start;
92 memconsole_baseaddr = phys_to_virt(hdr->v2.buffer_addr
97 * The function below tries to find the firmware console log saved in the
98 * appropriate coreboot CBMEM section, using the structures and constants from
101 * Coreboot's CBMEM is a structure allocated by coreboot above usable memory
102 * at a 128K aligned address. One of the CBMEM sections could contain the
103 * firmare console log.
106 /* Coreboot CBMEM section descriptor. */
114 /* CBMEM firmware console log descriptor. */
121 struct cbmem_cons __iomem *cbmem_console;
123 /* coreboot CBMEM constants */
124 #define CBMEM_ALIGNMENT (128 * 1024)
125 #define MEMORY_BOUNDARY (1024 * 1024 * 1024)
126 #define MAX_CBMEM_ENTRIES 16
127 #define CBMEM_ENTRY_MAGIC 0x434f5245
128 #define CBMEM_CONSOLE_ID 0x434f4e53
129 #define CBMEM_ACPI_NAME "\\CMEM"
131 static bool check_cbmem(void)
134 phys_addr_t top_of_ram, scan_addr = 0;
137 unsigned long long value;
140 * Attempt to use defined ACPI name to locate CBMEM TOC.
142 status = acpi_get_handle(NULL, CBMEM_ACPI_NAME, &handle);
143 if (ACPI_SUCCESS(status)) {
144 status = acpi_evaluate_integer(handle, CBMEM_ACPI_NAME,
146 /* Start scan at this address */
147 if (ACPI_SUCCESS(status) && value > 0)
148 scan_addr = (phys_addr_t) value;
152 * Otherwise determine where to start looking for CBMEM signature:
153 * take the top of usable memory and align it up to 128K boundary.
157 top_of_ram = (phys_addr_t) sysi.totalram << PAGE_SHIFT;
158 scan_addr = ALIGN(top_of_ram, CBMEM_ALIGNMENT) +
162 while (scan_addr % MEMORY_BOUNDARY) {
163 struct cbmem_entry __iomem *pcbm;
164 int i, remap_size = sizeof(struct cbmem_entry) * 16;
167 * See if we reached reserved memory. Bail out if so, as it is
168 * not mappable and is above the region where the CBMEM could
171 if (e820_any_mapped(scan_addr,
172 scan_addr + remap_size,
176 pcbm = ioremap(scan_addr, remap_size);
178 scan_addr += CBMEM_ALIGNMENT;
182 if (pcbm->magic != CBMEM_ENTRY_MAGIC) {
184 scan_addr += CBMEM_ALIGNMENT;
188 /* CBMEM found. Is the console log there? */
189 for (i = 1; i < MAX_CBMEM_ENTRIES; i++) {
190 if ((pcbm[i].magic == CBMEM_ENTRY_MAGIC) &&
191 (pcbm[i].id == CBMEM_CONSOLE_ID)) {
192 /* Yes, map the log. */
193 cbmem_console = ioremap(pcbm[i].base,
203 memconsole_baseaddr = cbmem_console->buffer_body;
204 memconsole_length = min(cbmem_console->buffer_cursor,
205 cbmem_console->buffer_size);
209 printk(KERN_INFO "CBMEM console structure not found!\n");
214 * Search through the EBDA for the BIOS Memory Console, and
215 * set the global variables to point to it. Return true if found.
217 static bool found_memconsole(void)
219 unsigned int address;
222 /* Is it communicated through CBMEM? */
223 if (coreboot_system && check_cbmem())
226 address = get_bios_ebda();
228 printk(KERN_INFO "BIOS EBDA non-existent.\n");
232 /* EBDA length is byte 0 of EBDA (in KB) */
233 length = *(u8 *)phys_to_virt(address);
234 length <<= 10; /* convert to bytes */
237 * Search through EBDA for BIOS memory console structure
238 * note: signature is not necessarily dword-aligned
240 for (cur = 0; cur < length; cur++) {
241 struct biosmemcon_ebda *hdr = phys_to_virt(address + cur);
244 if (hdr->signature == BIOS_MEMCONSOLE_V1_MAGIC) {
245 found_v1_header(hdr);
250 if (hdr->signature == BIOS_MEMCONSOLE_V2_MAGIC) {
251 found_v2_header(hdr);
256 printk(KERN_INFO "BIOS console EBDA structure not found!\n");
260 static struct dmi_system_id memconsole_dmi_table[] __initdata = {
262 .ident = "Google Board",
264 DMI_MATCH(DMI_BOARD_VENDOR, "Google, Inc."),
268 .ident = "Google Board",
269 .callback = set_coreboot_system_flag,
271 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
276 MODULE_DEVICE_TABLE(dmi, memconsole_dmi_table);
278 static int __init memconsole_init(void)
282 if (!dmi_check_system(memconsole_dmi_table))
285 if (!found_memconsole())
288 memconsole_bin_attr.size = memconsole_length;
290 ret = sysfs_create_bin_file(firmware_kobj, &memconsole_bin_attr);
295 static void __exit memconsole_exit(void)
298 iounmap(cbmem_console);
300 sysfs_remove_bin_file(firmware_kobj, &memconsole_bin_attr);
303 module_init(memconsole_init);
304 module_exit(memconsole_exit);
306 MODULE_AUTHOR("Google, Inc.");
307 MODULE_LICENSE("GPL");