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