1 /*
2  * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <drivers/coreboot/cbmem_console.h>
11 #include <common/debug.h>
12 #include <lib/coreboot.h>
13 #include <lib/mmio.h>
14 #include <lib/xlat_tables/xlat_tables_v2.h>
15 
16 /*
17  * Structures describing coreboot's in-memory descriptor tables. See
18  * <coreboot>/src/commonlib/include/commonlib/coreboot_tables.h for
19  * canonical implementation.
20  */
21 
22 typedef struct {
23 	char signature[4];
24 	uint32_t header_bytes;
25 	uint32_t header_checksum;
26 	uint32_t table_bytes;
27 	uint32_t table_checksum;
28 	uint32_t table_entries;
29 } cb_header_t;
30 
31 typedef enum {
32 	CB_TAG_MEMORY = 0x1,
33 	CB_TAG_SERIAL = 0xf,
34 	CB_TAG_CBMEM_CONSOLE = 0x17,
35 } cb_tag_t;
36 
37 typedef struct {
38 	uint32_t tag;
39 	uint32_t size;
40 	union {
41 		coreboot_memrange_t memranges[COREBOOT_MAX_MEMRANGES];
42 		coreboot_serial_t serial;
43 		uint64_t uint64;
44 	};
45 } cb_entry_t;
46 
47 coreboot_memrange_t coreboot_memranges[COREBOOT_MAX_MEMRANGES];
48 coreboot_serial_t coreboot_serial;
49 
50 /*
51  * The coreboot table is parsed before the MMU is enabled (i.e. with strongly
52  * ordered memory), so we cannot make unaligned accesses. The table entries
53  * immediately follow one another without padding, so nothing after the header
54  * is guaranteed to be naturally aligned. Therefore, we need to define safety
55  * functions that can read unaligned integers.
56  */
read_le32(uint32_t * p)57 static uint32_t read_le32(uint32_t *p)
58 {
59 	uintptr_t addr = (uintptr_t)p;
60 	return mmio_read_8(addr)		|
61 	       mmio_read_8(addr + 1) << 8	|
62 	       mmio_read_8(addr + 2) << 16	|
63 	       mmio_read_8(addr + 3) << 24;
64 }
read_le64(uint64_t * p)65 static uint64_t read_le64(uint64_t *p)
66 {
67 	return read_le32((void *)p) | (uint64_t)read_le32((void *)p + 4) << 32;
68 }
69 
expand_and_mmap(uintptr_t baseaddr,size_t size)70 static void expand_and_mmap(uintptr_t baseaddr, size_t size)
71 {
72 	uintptr_t pageaddr = round_down(baseaddr, PAGE_SIZE);
73 	size_t expanded = round_up(baseaddr - pageaddr + size, PAGE_SIZE);
74 	mmap_add_region(pageaddr, pageaddr, expanded,
75 			MT_MEMORY | MT_RW | MT_NS | MT_EXECUTE_NEVER);
76 }
77 
setup_cbmem_console(uintptr_t baseaddr)78 static void setup_cbmem_console(uintptr_t baseaddr)
79 {
80 	static console_cbmc_t console;
81 	assert(!console.console.base);	/* should only have one CBMEM console */
82 
83 	/* CBMEM console structure stores its size in first header field. */
84 	uint32_t size = *(uint32_t *)baseaddr;
85 	expand_and_mmap(baseaddr, size);
86 	console_cbmc_register(baseaddr, &console);
87 	console_set_scope(&console.console, CONSOLE_FLAG_BOOT |
88 					    CONSOLE_FLAG_RUNTIME |
89 					    CONSOLE_FLAG_CRASH);
90 }
91 
coreboot_get_memory_type(uintptr_t start,size_t size)92 coreboot_memory_t coreboot_get_memory_type(uintptr_t start, size_t size)
93 {
94 	int i;
95 
96 	for (i = 0; i < COREBOOT_MAX_MEMRANGES; i++) {
97 		coreboot_memrange_t *range = &coreboot_memranges[i];
98 
99 		if (range->type == CB_MEM_NONE)
100 			break;	/* end of table reached */
101 		if ((start >= range->start) &&
102 		    (start - range->start < range->size) &&
103 		    (size <= range->size - (start - range->start))) {
104 			return range->type;
105 		}
106 	}
107 
108 	return CB_MEM_NONE;
109 }
110 
coreboot_table_setup(void * base)111 void coreboot_table_setup(void *base)
112 {
113 	cb_header_t *header = base;
114 	void *ptr;
115 	int i;
116 
117 	if (strncmp(header->signature, "LBIO", 4)) {
118 		ERROR("coreboot table signature corrupt!\n");
119 		return;
120 	}
121 
122 	ptr = base + header->header_bytes;
123 	for (i = 0; i < header->table_entries; i++) {
124 		size_t size;
125 		cb_entry_t *entry = ptr;
126 
127 		if (ptr - base >= header->header_bytes + header->table_bytes) {
128 			ERROR("coreboot table exceeds its bounds!\n");
129 			break;
130 		}
131 
132 		switch (read_le32(&entry->tag)) {
133 		case CB_TAG_MEMORY:
134 			size = read_le32(&entry->size) -
135 			       offsetof(cb_entry_t, memranges);
136 			if (size > sizeof(coreboot_memranges)) {
137 				ERROR("Need to truncate coreboot memranges!\n");
138 				size = sizeof(coreboot_memranges);
139 			}
140 			memcpy(&coreboot_memranges, &entry->memranges, size);
141 			break;
142 		case CB_TAG_SERIAL:
143 			memcpy(&coreboot_serial, &entry->serial,
144 			       sizeof(coreboot_serial));
145 			break;
146 		case CB_TAG_CBMEM_CONSOLE:
147 			setup_cbmem_console(read_le64(&entry->uint64));
148 			break;
149 		default:
150 			/* There are many tags TF doesn't need to care about. */
151 			break;
152 		}
153 
154 		ptr += read_le32(&entry->size);
155 	}
156 }
157