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