xref: /openbmc/u-boot/arch/x86/cpu/coreboot/tables.c (revision 87a62bce)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * This file is part of the libpayload project.
4  *
5  * Copyright (C) 2008 Advanced Micro Devices, Inc.
6  * Copyright (C) 2009 coresystems GmbH
7  */
8 
9 #include <common.h>
10 #include <net.h>
11 #include <asm/arch/sysinfo.h>
12 
13 /*
14  * This needs to be in the .data section so that it's copied over during
15  * relocation. By default it's put in the .bss section which is simply filled
16  * with zeroes when transitioning from "ROM", which is really RAM, to other
17  * RAM.
18  */
19 struct sysinfo_t lib_sysinfo __attribute__((section(".data")));
20 
21 /*
22  * Some of this is x86 specific, and the rest of it is generic. Right now,
23  * since we only support x86, we'll avoid trying to make lots of infrastructure
24  * we don't need. If in the future, we want to use coreboot on some other
25  * architecture, then take out the generic parsing code and move it elsewhere.
26  */
27 
28 /* === Parsing code === */
29 /* This is the generic parsing code. */
30 
31 static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
32 {
33 	struct cb_memory *mem = (struct cb_memory *)ptr;
34 	int count = MEM_RANGE_COUNT(mem);
35 	int i;
36 
37 	if (count > SYSINFO_MAX_MEM_RANGES)
38 		count = SYSINFO_MAX_MEM_RANGES;
39 
40 	info->n_memranges = 0;
41 
42 	for (i = 0; i < count; i++) {
43 		struct cb_memory_range *range =
44 		    (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
45 
46 		info->memrange[info->n_memranges].base =
47 		    UNPACK_CB64(range->start);
48 
49 		info->memrange[info->n_memranges].size =
50 		    UNPACK_CB64(range->size);
51 
52 		info->memrange[info->n_memranges].type = range->type;
53 
54 		info->n_memranges++;
55 	}
56 }
57 
58 static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
59 {
60 	struct cb_serial *ser = (struct cb_serial *)ptr;
61 	info->serial = ser;
62 }
63 
64 static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
65 {
66 	struct cb_vbnv *vbnv = (struct cb_vbnv *)ptr;
67 
68 	info->vbnv_start = vbnv->vbnv_start;
69 	info->vbnv_size = vbnv->vbnv_size;
70 }
71 
72 static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
73 {
74 	int i;
75 	struct cb_gpios *gpios = (struct cb_gpios *)ptr;
76 
77 	info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
78 				(gpios->count) : SYSINFO_MAX_GPIOS;
79 
80 	for (i = 0; i < info->num_gpios; i++)
81 		info->gpios[i] = gpios->gpios[i];
82 }
83 
84 static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
85 {
86 	struct cb_vdat *vdat = (struct cb_vdat *) ptr;
87 
88 	info->vdat_addr = vdat->vdat_addr;
89 	info->vdat_size = vdat->vdat_size;
90 }
91 
92 static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
93 {
94 	info->tstamp_table = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
95 }
96 
97 static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
98 {
99 	info->cbmem_cons = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
100 }
101 
102 static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
103 {
104 	info->framebuffer = (struct cb_framebuffer *)ptr;
105 }
106 
107 static void cb_parse_string(unsigned char *ptr, char **info)
108 {
109 	*info = (char *)((struct cb_string *)ptr)->string;
110 }
111 
112 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
113 {
114 	struct cb_header *header;
115 	unsigned char *ptr = (unsigned char *)addr;
116 	int i;
117 
118 	for (i = 0; i < len; i += 16, ptr += 16) {
119 		header = (struct cb_header *)ptr;
120 		if (!strncmp((const char *)header->signature, "LBIO", 4))
121 			break;
122 	}
123 
124 	/* We walked the entire space and didn't find anything. */
125 	if (i >= len)
126 		return -1;
127 
128 	if (!header->table_bytes)
129 		return 0;
130 
131 	/* Make sure the checksums match. */
132 	if (!ip_checksum_ok(header, sizeof(*header)))
133 		return -1;
134 
135 	if (compute_ip_checksum(ptr + sizeof(*header), header->table_bytes) !=
136 	    header->table_checksum)
137 		return -1;
138 
139 	/* Now, walk the tables. */
140 	ptr += header->header_bytes;
141 
142 	/* Inintialize some fields to sentinel values. */
143 	info->vbnv_start = info->vbnv_size = (uint32_t)(-1);
144 
145 	for (i = 0; i < header->table_entries; i++) {
146 		struct cb_record *rec = (struct cb_record *)ptr;
147 
148 		/* We only care about a few tags here (maybe more later). */
149 		switch (rec->tag) {
150 		case CB_TAG_FORWARD:
151 			return cb_parse_header(
152 				(void *)(unsigned long)
153 				((struct cb_forward *)rec)->forward,
154 				len, info);
155 			continue;
156 		case CB_TAG_MEMORY:
157 			cb_parse_memory(ptr, info);
158 			break;
159 		case CB_TAG_SERIAL:
160 			cb_parse_serial(ptr, info);
161 			break;
162 		case CB_TAG_VERSION:
163 			cb_parse_string(ptr, &info->version);
164 			break;
165 		case CB_TAG_EXTRA_VERSION:
166 			cb_parse_string(ptr, &info->extra_version);
167 			break;
168 		case CB_TAG_BUILD:
169 			cb_parse_string(ptr, &info->build);
170 			break;
171 		case CB_TAG_COMPILE_TIME:
172 			cb_parse_string(ptr, &info->compile_time);
173 			break;
174 		case CB_TAG_COMPILE_BY:
175 			cb_parse_string(ptr, &info->compile_by);
176 			break;
177 		case CB_TAG_COMPILE_HOST:
178 			cb_parse_string(ptr, &info->compile_host);
179 			break;
180 		case CB_TAG_COMPILE_DOMAIN:
181 			cb_parse_string(ptr, &info->compile_domain);
182 			break;
183 		case CB_TAG_COMPILER:
184 			cb_parse_string(ptr, &info->compiler);
185 			break;
186 		case CB_TAG_LINKER:
187 			cb_parse_string(ptr, &info->linker);
188 			break;
189 		case CB_TAG_ASSEMBLER:
190 			cb_parse_string(ptr, &info->assembler);
191 			break;
192 		/*
193 		 * FIXME we should warn on serial if coreboot set up a
194 		 * framebuffer buf the payload does not know about it.
195 		 */
196 		case CB_TAG_FRAMEBUFFER:
197 			cb_parse_framebuffer(ptr, info);
198 			break;
199 		case CB_TAG_GPIO:
200 			cb_parse_gpios(ptr, info);
201 			break;
202 		case CB_TAG_VDAT:
203 			cb_parse_vdat(ptr, info);
204 			break;
205 		case CB_TAG_TIMESTAMPS:
206 			cb_parse_tstamp(ptr, info);
207 			break;
208 		case CB_TAG_CBMEM_CONSOLE:
209 			cb_parse_cbmem_cons(ptr, info);
210 			break;
211 		case CB_TAG_VBNV:
212 			cb_parse_vbnv(ptr, info);
213 			break;
214 		}
215 
216 		ptr += rec->size;
217 	}
218 
219 	return 1;
220 }
221 
222 /* == Architecture specific == */
223 /* This is the x86 specific stuff. */
224 
225 int get_coreboot_info(struct sysinfo_t *info)
226 {
227 	int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
228 
229 	if (ret != 1)
230 		ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
231 
232 	return (ret == 1) ? 0 : -1;
233 }
234