Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * memconsole-coreboot.c
4 *
5 * Memory based BIOS console accessed through coreboot table.
6 *
7 * Copyright 2017 Google Inc.
8 */
9
10#include <linux/device.h>
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14
15#include "memconsole.h"
16#include "coreboot_table.h"
17
18#define CB_TAG_CBMEM_CONSOLE 0x17
19
20/* CBMEM firmware console log descriptor. */
21struct cbmem_cons {
22 u32 size_dont_access_after_boot;
23 u32 cursor;
24 u8 body[];
25} __packed;
26
27#define CURSOR_MASK ((1 << 28) - 1)
28#define OVERFLOW (1 << 31)
29
30static struct cbmem_cons *cbmem_console;
31static u32 cbmem_console_size;
32
33/*
34 * The cbmem_console structure is read again on every access because it may
35 * change at any time if runtime firmware logs new messages. This may rarely
36 * lead to race conditions where the firmware overwrites the beginning of the
37 * ring buffer with more lines after we have already read |cursor|. It should be
38 * rare and harmless enough that we don't spend extra effort working around it.
39 */
40static ssize_t memconsole_coreboot_read(char *buf, loff_t pos, size_t count)
41{
42 u32 cursor = cbmem_console->cursor & CURSOR_MASK;
43 u32 flags = cbmem_console->cursor & ~CURSOR_MASK;
44 u32 size = cbmem_console_size;
45 struct seg { /* describes ring buffer segments in logical order */
46 u32 phys; /* physical offset from start of mem buffer */
47 u32 len; /* length of segment */
48 } seg[2] = { {0}, {0} };
49 size_t done = 0;
50 int i;
51
52 if (flags & OVERFLOW) {
53 if (cursor > size) /* Shouldn't really happen, but... */
54 cursor = 0;
55 seg[0] = (struct seg){.phys = cursor, .len = size - cursor};
56 seg[1] = (struct seg){.phys = 0, .len = cursor};
57 } else {
58 seg[0] = (struct seg){.phys = 0, .len = min(cursor, size)};
59 }
60
61 for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) {
62 done += memory_read_from_buffer(buf + done, count - done, &pos,
63 cbmem_console->body + seg[i].phys, seg[i].len);
64 pos -= seg[i].len;
65 }
66 return done;
67}
68
69static int memconsole_probe(struct coreboot_device *dev)
70{
71 struct cbmem_cons *tmp_cbmc;
72
73 tmp_cbmc = memremap(dev->cbmem_ref.cbmem_addr,
74 sizeof(*tmp_cbmc), MEMREMAP_WB);
75
76 if (!tmp_cbmc)
77 return -ENOMEM;
78
79 /* Read size only once to prevent overrun attack through /dev/mem. */
80 cbmem_console_size = tmp_cbmc->size_dont_access_after_boot;
81 cbmem_console = devm_memremap(&dev->dev, dev->cbmem_ref.cbmem_addr,
82 cbmem_console_size + sizeof(*cbmem_console),
83 MEMREMAP_WB);
84 memunmap(tmp_cbmc);
85
86 if (IS_ERR(cbmem_console))
87 return PTR_ERR(cbmem_console);
88
89 memconsole_setup(memconsole_coreboot_read);
90
91 return memconsole_sysfs_init();
92}
93
94static void memconsole_remove(struct coreboot_device *dev)
95{
96 memconsole_exit();
97}
98
99static const struct coreboot_device_id memconsole_ids[] = {
100 { .tag = CB_TAG_CBMEM_CONSOLE },
101 { /* sentinel */ }
102};
103MODULE_DEVICE_TABLE(coreboot, memconsole_ids);
104
105static struct coreboot_driver memconsole_driver = {
106 .probe = memconsole_probe,
107 .remove = memconsole_remove,
108 .drv = {
109 .name = "memconsole",
110 },
111 .id_table = memconsole_ids,
112};
113module_coreboot_driver(memconsole_driver);
114
115MODULE_AUTHOR("Google, Inc.");
116MODULE_DESCRIPTION("Memory based BIOS console accessed through coreboot table");
117MODULE_LICENSE("GPL");
1/*
2 * memconsole-coreboot.c
3 *
4 * Memory based BIOS console accessed through coreboot table.
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/platform_device.h>
21
22#include "memconsole.h"
23#include "coreboot_table.h"
24
25#define CB_TAG_CBMEM_CONSOLE 0x17
26
27/* CBMEM firmware console log descriptor. */
28struct cbmem_cons {
29 u32 size_dont_access_after_boot;
30 u32 cursor;
31 u8 body[0];
32} __packed;
33
34#define CURSOR_MASK ((1 << 28) - 1)
35#define OVERFLOW (1 << 31)
36
37static struct cbmem_cons __iomem *cbmem_console;
38static u32 cbmem_console_size;
39
40/*
41 * The cbmem_console structure is read again on every access because it may
42 * change at any time if runtime firmware logs new messages. This may rarely
43 * lead to race conditions where the firmware overwrites the beginning of the
44 * ring buffer with more lines after we have already read |cursor|. It should be
45 * rare and harmless enough that we don't spend extra effort working around it.
46 */
47static ssize_t memconsole_coreboot_read(char *buf, loff_t pos, size_t count)
48{
49 u32 cursor = cbmem_console->cursor & CURSOR_MASK;
50 u32 flags = cbmem_console->cursor & ~CURSOR_MASK;
51 u32 size = cbmem_console_size;
52 struct seg { /* describes ring buffer segments in logical order */
53 u32 phys; /* physical offset from start of mem buffer */
54 u32 len; /* length of segment */
55 } seg[2] = { {0}, {0} };
56 size_t done = 0;
57 int i;
58
59 if (flags & OVERFLOW) {
60 if (cursor > size) /* Shouldn't really happen, but... */
61 cursor = 0;
62 seg[0] = (struct seg){.phys = cursor, .len = size - cursor};
63 seg[1] = (struct seg){.phys = 0, .len = cursor};
64 } else {
65 seg[0] = (struct seg){.phys = 0, .len = min(cursor, size)};
66 }
67
68 for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) {
69 done += memory_read_from_buffer(buf + done, count - done, &pos,
70 cbmem_console->body + seg[i].phys, seg[i].len);
71 pos -= seg[i].len;
72 }
73 return done;
74}
75
76static int memconsole_coreboot_init(phys_addr_t physaddr)
77{
78 struct cbmem_cons __iomem *tmp_cbmc;
79
80 tmp_cbmc = memremap(physaddr, sizeof(*tmp_cbmc), MEMREMAP_WB);
81
82 if (!tmp_cbmc)
83 return -ENOMEM;
84
85 /* Read size only once to prevent overrun attack through /dev/mem. */
86 cbmem_console_size = tmp_cbmc->size_dont_access_after_boot;
87 cbmem_console = memremap(physaddr,
88 cbmem_console_size + sizeof(*cbmem_console),
89 MEMREMAP_WB);
90 memunmap(tmp_cbmc);
91
92 if (!cbmem_console)
93 return -ENOMEM;
94
95 memconsole_setup(memconsole_coreboot_read);
96 return 0;
97}
98
99static int memconsole_probe(struct platform_device *pdev)
100{
101 int ret;
102 struct lb_cbmem_ref entry;
103
104 ret = coreboot_table_find(CB_TAG_CBMEM_CONSOLE, &entry, sizeof(entry));
105 if (ret)
106 return ret;
107
108 ret = memconsole_coreboot_init(entry.cbmem_addr);
109 if (ret)
110 return ret;
111
112 return memconsole_sysfs_init();
113}
114
115static int memconsole_remove(struct platform_device *pdev)
116{
117 memconsole_exit();
118
119 if (cbmem_console)
120 memunmap(cbmem_console);
121
122 return 0;
123}
124
125static struct platform_driver memconsole_driver = {
126 .probe = memconsole_probe,
127 .remove = memconsole_remove,
128 .driver = {
129 .name = "memconsole",
130 },
131};
132
133static int __init platform_memconsole_init(void)
134{
135 struct platform_device *pdev;
136
137 pdev = platform_device_register_simple("memconsole", -1, NULL, 0);
138 if (IS_ERR(pdev))
139 return PTR_ERR(pdev);
140
141 platform_driver_register(&memconsole_driver);
142
143 return 0;
144}
145
146module_init(platform_memconsole_init);
147
148MODULE_AUTHOR("Google, Inc.");
149MODULE_LICENSE("GPL");