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 uint64_t coreboot_table_addr;
50 uint32_t coreboot_table_size;
51 
52 /*
53  * The coreboot table is parsed before the MMU is enabled (i.e. with strongly
54  * ordered memory), so we cannot make unaligned accesses. The table entries
55  * immediately follow one another without padding, so nothing after the header
56  * is guaranteed to be naturally aligned. Therefore, we need to define safety
57  * functions that can read unaligned integers.
58  */
read_le32(uint32_t * p)59 static uint32_t read_le32(uint32_t *p)
60 {
61 	uintptr_t addr = (uintptr_t)p;
62 	return mmio_read_8(addr)		|
63 	       mmio_read_8(addr + 1) << 8	|
64 	       mmio_read_8(addr + 2) << 16	|
65 	       mmio_read_8(addr + 3) << 24;
66 }
read_le64(uint64_t * p)67 static uint64_t read_le64(uint64_t *p)
68 {
69 	return read_le32((void *)p) | (uint64_t)read_le32((void *)p + 4) << 32;
70 }
71 
expand_and_mmap(uintptr_t baseaddr,size_t size)72 static void expand_and_mmap(uintptr_t baseaddr, size_t size)
73 {
74 	uintptr_t pageaddr = round_down(baseaddr, PAGE_SIZE);
75 	size_t expanded = round_up(baseaddr - pageaddr + size, PAGE_SIZE);
76 	mmap_add_region(pageaddr, pageaddr, expanded,
77 			MT_MEMORY | MT_RW | MT_NS | MT_EXECUTE_NEVER);
78 }
79 
setup_cbmem_console(uintptr_t baseaddr)80 static void setup_cbmem_console(uintptr_t baseaddr)
81 {
82 	static console_cbmc_t console;
83 	assert(!console.console.base);	/* should only have one CBMEM console */
84 
85 	/* CBMEM console structure stores its size in first header field. */
86 	uint32_t size = *(uint32_t *)baseaddr;
87 	expand_and_mmap(baseaddr, size);
88 	console_cbmc_register(baseaddr, &console);
89 	console_set_scope(&console.console, CONSOLE_FLAG_BOOT |
90 					    CONSOLE_FLAG_RUNTIME |
91 					    CONSOLE_FLAG_CRASH);
92 }
93 
coreboot_get_memory_type(uintptr_t start,size_t size)94 coreboot_memory_t coreboot_get_memory_type(uintptr_t start, size_t size)
95 {
96 	int i;
97 
98 	for (i = 0; i < COREBOOT_MAX_MEMRANGES; i++) {
99 		coreboot_memrange_t *range = &coreboot_memranges[i];
100 
101 		if (range->type == CB_MEM_NONE)
102 			break;	/* end of table reached */
103 		if ((start >= range->start) &&
104 		    (start - range->start < range->size) &&
105 		    (size <= range->size - (start - range->start))) {
106 			return range->type;
107 		}
108 	}
109 
110 	return CB_MEM_NONE;
111 }
112 
coreboot_get_table_location(uint64_t * address,uint32_t * size)113 void coreboot_get_table_location(uint64_t *address, uint32_t *size)
114 {
115 	*address = coreboot_table_addr;
116 	*size = coreboot_table_size;
117 }
118 
coreboot_table_setup(void * base)119 void coreboot_table_setup(void *base)
120 {
121 	cb_header_t *header = base;
122 	void *ptr;
123 	int i;
124 
125 	if (strncmp(header->signature, "LBIO", 4)) {
126 		ERROR("coreboot table signature corrupt!\n");
127 		return;
128 	}
129 	coreboot_table_addr = (uint64_t) base;
130 	coreboot_table_size = header->header_bytes + header->table_bytes;
131 
132 	ptr = base + header->header_bytes;
133 	for (i = 0; i < header->table_entries; i++) {
134 		size_t size;
135 		cb_entry_t *entry = ptr;
136 
137 		if (ptr - base >= header->header_bytes + header->table_bytes) {
138 			ERROR("coreboot table exceeds its bounds!\n");
139 			break;
140 		}
141 
142 		switch (read_le32(&entry->tag)) {
143 		case CB_TAG_MEMORY:
144 			size = read_le32(&entry->size) -
145 			       offsetof(cb_entry_t, memranges);
146 			if (size > sizeof(coreboot_memranges)) {
147 				ERROR("Need to truncate coreboot memranges!\n");
148 				size = sizeof(coreboot_memranges);
149 			}
150 			memcpy(&coreboot_memranges, &entry->memranges, size);
151 			break;
152 		case CB_TAG_SERIAL:
153 			memcpy(&coreboot_serial, &entry->serial,
154 			       sizeof(coreboot_serial));
155 			break;
156 		case CB_TAG_CBMEM_CONSOLE:
157 			setup_cbmem_console(read_le64(&entry->uint64));
158 			break;
159 		default:
160 			/* There are many tags TF doesn't need to care about. */
161 			break;
162 		}
163 
164 		ptr += read_le32(&entry->size);
165 	}
166 }
167