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