Skip to content

Commit a5061d0

Browse files
jwerner-chromiumgregkh
authored andcommitted
firmware: google: memconsole: Adapt to new coreboot ring buffer format
The upstream coreboot implementation of memconsole was enhanced from a single-boot console to a persistent ring buffer (https://review.coreboot.org/#/c/18301). This patch changes the kernel memconsole driver to be able to read the new format in all cases. Signed-off-by: Julius Werner <jwerner@chromium.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7918cfc commit a5061d0

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

‎drivers/firmware/google/memconsole-coreboot.c‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,50 @@
2626

2727
/* CBMEM firmware console log descriptor. */
2828
struct cbmem_cons {
29-
u32 buffer_size;
30-
u32 buffer_cursor;
31-
u8 buffer_body[0];
29+
u32 size;
30+
u32 cursor;
31+
u8 body[0];
3232
} __packed;
3333

34+
#define CURSOR_MASK ((1 << 28) - 1)
35+
#define OVERFLOW (1 << 31)
36+
3437
static struct cbmem_cons __iomem *cbmem_console;
3538

39+
/*
40+
* The cbmem_console structure is read again on every access because it may
41+
* change at any time if runtime firmware logs new messages. This may rarely
42+
* lead to race conditions where the firmware overwrites the beginning of the
43+
* ring buffer with more lines after we have already read |cursor|. It should be
44+
* rare and harmless enough that we don't spend extra effort working around it.
45+
*/
3646
static ssize_t memconsole_coreboot_read(char *buf, loff_t pos, size_t count)
3747
{
38-
return memory_read_from_buffer(buf, count, &pos,
39-
cbmem_console->buffer_body,
40-
min(cbmem_console->buffer_cursor,
41-
cbmem_console->buffer_size));
48+
u32 cursor = cbmem_console->cursor & CURSOR_MASK;
49+
u32 flags = cbmem_console->cursor & ~CURSOR_MASK;
50+
u32 size = cbmem_console->size;
51+
struct seg { /* describes ring buffer segments in logical order */
52+
u32 phys; /* physical offset from start of mem buffer */
53+
u32 len; /* length of segment */
54+
} seg[2] = { {0}, {0} };
55+
size_t done = 0;
56+
int i;
57+
58+
if (flags & OVERFLOW) {
59+
if (cursor > size) /* Shouldn't really happen, but... */
60+
cursor = 0;
61+
seg[0] = (struct seg){.phys = cursor, .len = size - cursor};
62+
seg[1] = (struct seg){.phys = 0, .len = cursor};
63+
} else {
64+
seg[0] = (struct seg){.phys = 0, .len = min(cursor, size)};
65+
}
66+
67+
for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) {
68+
done += memory_read_from_buffer(buf + done, count - done, &pos,
69+
cbmem_console->body + seg[i].phys, seg[i].len);
70+
pos -= seg[i].len;
71+
}
72+
return done;
4273
}
4374

4475
static int memconsole_coreboot_init(phys_addr_t physaddr)
@@ -51,7 +82,7 @@ static int memconsole_coreboot_init(phys_addr_t physaddr)
5182
return -ENOMEM;
5283

5384
cbmem_console = memremap(physaddr,
54-
tmp_cbmc->buffer_size + sizeof(*cbmem_console),
85+
tmp_cbmc->size + sizeof(*cbmem_console),
5586
MEMREMAP_WB);
5687
memunmap(tmp_cbmc);
5788

0 commit comments

Comments
 (0)