1*83d290c5STom Rini // SPDX-License-Identifier: BSD-3-Clause
263f559cdSGabe Black /*
363f559cdSGabe Black * This file is part of the libpayload project.
463f559cdSGabe Black *
563f559cdSGabe Black * Copyright (C) 2008 Advanced Micro Devices, Inc.
663f559cdSGabe Black * Copyright (C) 2009 coresystems GmbH
763f559cdSGabe Black */
863f559cdSGabe Black
9402ed004SSimon Glass #include <common.h>
10b18c68d8SSimon Glass #include <net.h>
11378a8634SSimon Glass #include <asm/arch/sysinfo.h>
1263f559cdSGabe Black
1363f559cdSGabe Black /*
1493c1735fSStefan Reinauer * This needs to be in the .data section so that it's copied over during
1593c1735fSStefan Reinauer * relocation. By default it's put in the .bss section which is simply filled
1693c1735fSStefan Reinauer * with zeroes when transitioning from "ROM", which is really RAM, to other
1793c1735fSStefan Reinauer * RAM.
1893c1735fSStefan Reinauer */
1993c1735fSStefan Reinauer struct sysinfo_t lib_sysinfo __attribute__((section(".data")));
2093c1735fSStefan Reinauer
2193c1735fSStefan Reinauer /*
2263f559cdSGabe Black * Some of this is x86 specific, and the rest of it is generic. Right now,
2363f559cdSGabe Black * since we only support x86, we'll avoid trying to make lots of infrastructure
2463f559cdSGabe Black * we don't need. If in the future, we want to use coreboot on some other
2563f559cdSGabe Black * architecture, then take out the generic parsing code and move it elsewhere.
2663f559cdSGabe Black */
2763f559cdSGabe Black
2863f559cdSGabe Black /* === Parsing code === */
2963f559cdSGabe Black /* This is the generic parsing code. */
3063f559cdSGabe Black
cb_parse_memory(unsigned char * ptr,struct sysinfo_t * info)3163f559cdSGabe Black static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
3263f559cdSGabe Black {
3363f559cdSGabe Black struct cb_memory *mem = (struct cb_memory *)ptr;
3463f559cdSGabe Black int count = MEM_RANGE_COUNT(mem);
3563f559cdSGabe Black int i;
3663f559cdSGabe Black
3763f559cdSGabe Black if (count > SYSINFO_MAX_MEM_RANGES)
3863f559cdSGabe Black count = SYSINFO_MAX_MEM_RANGES;
3963f559cdSGabe Black
4063f559cdSGabe Black info->n_memranges = 0;
4163f559cdSGabe Black
4263f559cdSGabe Black for (i = 0; i < count; i++) {
4363f559cdSGabe Black struct cb_memory_range *range =
4463f559cdSGabe Black (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
4563f559cdSGabe Black
4663f559cdSGabe Black info->memrange[info->n_memranges].base =
4763f559cdSGabe Black UNPACK_CB64(range->start);
4863f559cdSGabe Black
4963f559cdSGabe Black info->memrange[info->n_memranges].size =
5063f559cdSGabe Black UNPACK_CB64(range->size);
5163f559cdSGabe Black
5263f559cdSGabe Black info->memrange[info->n_memranges].type = range->type;
5363f559cdSGabe Black
5463f559cdSGabe Black info->n_memranges++;
5563f559cdSGabe Black }
5663f559cdSGabe Black }
5763f559cdSGabe Black
cb_parse_serial(unsigned char * ptr,struct sysinfo_t * info)5863f559cdSGabe Black static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
5963f559cdSGabe Black {
6063f559cdSGabe Black struct cb_serial *ser = (struct cb_serial *)ptr;
61402ed004SSimon Glass info->serial = ser;
6263f559cdSGabe Black }
6363f559cdSGabe Black
cb_parse_vbnv(unsigned char * ptr,struct sysinfo_t * info)64402ed004SSimon Glass static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
6563f559cdSGabe Black {
66402ed004SSimon Glass struct cb_vbnv *vbnv = (struct cb_vbnv *)ptr;
67402ed004SSimon Glass
68402ed004SSimon Glass info->vbnv_start = vbnv->vbnv_start;
69402ed004SSimon Glass info->vbnv_size = vbnv->vbnv_size;
7063f559cdSGabe Black }
7163f559cdSGabe Black
cb_parse_gpios(unsigned char * ptr,struct sysinfo_t * info)72402ed004SSimon Glass static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
7363f559cdSGabe Black {
74402ed004SSimon Glass int i;
75402ed004SSimon Glass struct cb_gpios *gpios = (struct cb_gpios *)ptr;
76402ed004SSimon Glass
77402ed004SSimon Glass info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
78402ed004SSimon Glass (gpios->count) : SYSINFO_MAX_GPIOS;
79402ed004SSimon Glass
80402ed004SSimon Glass for (i = 0; i < info->num_gpios; i++)
81402ed004SSimon Glass info->gpios[i] = gpios->gpios[i];
82402ed004SSimon Glass }
83402ed004SSimon Glass
cb_parse_vdat(unsigned char * ptr,struct sysinfo_t * info)84402ed004SSimon Glass static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
85402ed004SSimon Glass {
86402ed004SSimon Glass struct cb_vdat *vdat = (struct cb_vdat *) ptr;
87402ed004SSimon Glass
88402ed004SSimon Glass info->vdat_addr = vdat->vdat_addr;
89402ed004SSimon Glass info->vdat_size = vdat->vdat_size;
90402ed004SSimon Glass }
91402ed004SSimon Glass
cb_parse_tstamp(unsigned char * ptr,struct sysinfo_t * info)92402ed004SSimon Glass static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
93402ed004SSimon Glass {
94402ed004SSimon Glass info->tstamp_table = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
95402ed004SSimon Glass }
96402ed004SSimon Glass
cb_parse_cbmem_cons(unsigned char * ptr,struct sysinfo_t * info)97402ed004SSimon Glass static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
98402ed004SSimon Glass {
99402ed004SSimon Glass info->cbmem_cons = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
10063f559cdSGabe Black }
10163f559cdSGabe Black
cb_parse_framebuffer(unsigned char * ptr,struct sysinfo_t * info)10263f559cdSGabe Black static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
10363f559cdSGabe Black {
10463f559cdSGabe Black info->framebuffer = (struct cb_framebuffer *)ptr;
10563f559cdSGabe Black }
10663f559cdSGabe Black
cb_parse_string(unsigned char * ptr,char ** info)107402ed004SSimon Glass static void cb_parse_string(unsigned char *ptr, char **info)
108402ed004SSimon Glass {
109402ed004SSimon Glass *info = (char *)((struct cb_string *)ptr)->string;
110402ed004SSimon Glass }
111402ed004SSimon Glass
cb_parse_header(void * addr,int len,struct sysinfo_t * info)11263f559cdSGabe Black static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
11363f559cdSGabe Black {
11463f559cdSGabe Black struct cb_header *header;
11563f559cdSGabe Black unsigned char *ptr = (unsigned char *)addr;
11663f559cdSGabe Black int i;
11763f559cdSGabe Black
11863f559cdSGabe Black for (i = 0; i < len; i += 16, ptr += 16) {
11963f559cdSGabe Black header = (struct cb_header *)ptr;
12063f559cdSGabe Black if (!strncmp((const char *)header->signature, "LBIO", 4))
12163f559cdSGabe Black break;
12263f559cdSGabe Black }
12363f559cdSGabe Black
12463f559cdSGabe Black /* We walked the entire space and didn't find anything. */
12563f559cdSGabe Black if (i >= len)
12663f559cdSGabe Black return -1;
12763f559cdSGabe Black
12863f559cdSGabe Black if (!header->table_bytes)
12963f559cdSGabe Black return 0;
13063f559cdSGabe Black
13163f559cdSGabe Black /* Make sure the checksums match. */
132b18c68d8SSimon Glass if (!ip_checksum_ok(header, sizeof(*header)))
13363f559cdSGabe Black return -1;
13463f559cdSGabe Black
135b18c68d8SSimon Glass if (compute_ip_checksum(ptr + sizeof(*header), header->table_bytes) !=
136b18c68d8SSimon Glass header->table_checksum)
13763f559cdSGabe Black return -1;
13863f559cdSGabe Black
13963f559cdSGabe Black /* Now, walk the tables. */
14063f559cdSGabe Black ptr += header->header_bytes;
14163f559cdSGabe Black
142402ed004SSimon Glass /* Inintialize some fields to sentinel values. */
143402ed004SSimon Glass info->vbnv_start = info->vbnv_size = (uint32_t)(-1);
144402ed004SSimon Glass
14563f559cdSGabe Black for (i = 0; i < header->table_entries; i++) {
14663f559cdSGabe Black struct cb_record *rec = (struct cb_record *)ptr;
14763f559cdSGabe Black
14863f559cdSGabe Black /* We only care about a few tags here (maybe more later). */
14963f559cdSGabe Black switch (rec->tag) {
15063f559cdSGabe Black case CB_TAG_FORWARD:
15163f559cdSGabe Black return cb_parse_header(
15263f559cdSGabe Black (void *)(unsigned long)
15363f559cdSGabe Black ((struct cb_forward *)rec)->forward,
15463f559cdSGabe Black len, info);
15563f559cdSGabe Black continue;
15663f559cdSGabe Black case CB_TAG_MEMORY:
15763f559cdSGabe Black cb_parse_memory(ptr, info);
15863f559cdSGabe Black break;
15963f559cdSGabe Black case CB_TAG_SERIAL:
16063f559cdSGabe Black cb_parse_serial(ptr, info);
16163f559cdSGabe Black break;
162402ed004SSimon Glass case CB_TAG_VERSION:
163402ed004SSimon Glass cb_parse_string(ptr, &info->version);
16463f559cdSGabe Black break;
165402ed004SSimon Glass case CB_TAG_EXTRA_VERSION:
166402ed004SSimon Glass cb_parse_string(ptr, &info->extra_version);
167402ed004SSimon Glass break;
168402ed004SSimon Glass case CB_TAG_BUILD:
169402ed004SSimon Glass cb_parse_string(ptr, &info->build);
170402ed004SSimon Glass break;
171402ed004SSimon Glass case CB_TAG_COMPILE_TIME:
172402ed004SSimon Glass cb_parse_string(ptr, &info->compile_time);
173402ed004SSimon Glass break;
174402ed004SSimon Glass case CB_TAG_COMPILE_BY:
175402ed004SSimon Glass cb_parse_string(ptr, &info->compile_by);
176402ed004SSimon Glass break;
177402ed004SSimon Glass case CB_TAG_COMPILE_HOST:
178402ed004SSimon Glass cb_parse_string(ptr, &info->compile_host);
179402ed004SSimon Glass break;
180402ed004SSimon Glass case CB_TAG_COMPILE_DOMAIN:
181402ed004SSimon Glass cb_parse_string(ptr, &info->compile_domain);
182402ed004SSimon Glass break;
183402ed004SSimon Glass case CB_TAG_COMPILER:
184402ed004SSimon Glass cb_parse_string(ptr, &info->compiler);
185402ed004SSimon Glass break;
186402ed004SSimon Glass case CB_TAG_LINKER:
187402ed004SSimon Glass cb_parse_string(ptr, &info->linker);
188402ed004SSimon Glass break;
189402ed004SSimon Glass case CB_TAG_ASSEMBLER:
190402ed004SSimon Glass cb_parse_string(ptr, &info->assembler);
19163f559cdSGabe Black break;
19263f559cdSGabe Black /*
19363f559cdSGabe Black * FIXME we should warn on serial if coreboot set up a
19463f559cdSGabe Black * framebuffer buf the payload does not know about it.
19563f559cdSGabe Black */
19663f559cdSGabe Black case CB_TAG_FRAMEBUFFER:
19763f559cdSGabe Black cb_parse_framebuffer(ptr, info);
19863f559cdSGabe Black break;
199402ed004SSimon Glass case CB_TAG_GPIO:
200402ed004SSimon Glass cb_parse_gpios(ptr, info);
201402ed004SSimon Glass break;
202402ed004SSimon Glass case CB_TAG_VDAT:
203402ed004SSimon Glass cb_parse_vdat(ptr, info);
204402ed004SSimon Glass break;
205402ed004SSimon Glass case CB_TAG_TIMESTAMPS:
206402ed004SSimon Glass cb_parse_tstamp(ptr, info);
207402ed004SSimon Glass break;
208402ed004SSimon Glass case CB_TAG_CBMEM_CONSOLE:
209402ed004SSimon Glass cb_parse_cbmem_cons(ptr, info);
210402ed004SSimon Glass break;
211402ed004SSimon Glass case CB_TAG_VBNV:
212402ed004SSimon Glass cb_parse_vbnv(ptr, info);
213402ed004SSimon Glass break;
21463f559cdSGabe Black }
21563f559cdSGabe Black
21663f559cdSGabe Black ptr += rec->size;
21763f559cdSGabe Black }
21863f559cdSGabe Black
21963f559cdSGabe Black return 1;
22063f559cdSGabe Black }
22163f559cdSGabe Black
22263f559cdSGabe Black /* == Architecture specific == */
22363f559cdSGabe Black /* This is the x86 specific stuff. */
22463f559cdSGabe Black
get_coreboot_info(struct sysinfo_t * info)22563f559cdSGabe Black int get_coreboot_info(struct sysinfo_t *info)
22663f559cdSGabe Black {
227402ed004SSimon Glass int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
22863f559cdSGabe Black
22963f559cdSGabe Black if (ret != 1)
230402ed004SSimon Glass ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
23163f559cdSGabe Black
23263f559cdSGabe Black return (ret == 1) ? 0 : -1;
23363f559cdSGabe Black }
234