Debian Codegraph @

payloads/libpayload/libc/coreboot.c

raw symbols SCIP
1 /*
2 *
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
4 * Copyright (C) 2009 coresystems GmbH
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <libpayload-config.h>
31 #include <libpayload.h>
32 #include <commonlib/bsd/cbmem_id.h>
33 #include <coreboot_tables.h>
34 #include <stdint.h>
35
36 /*
37 * The code in this file applies to all coreboot architectures. Some coreboot
38 * table tags are architecture specific, they are handled by their respective
39 * cb_parse_arch_specific() functions.
40 */
41
42 /* === Parsing code === */
43 /* This is the generic parsing code. */
44
45 static void cb_parse_memory(void *ptr, struct sysinfo_t *info)
46 {
47 struct cb_memory *mem = ptr;
48 int count = MEM_RANGE_COUNT(mem);
49 int i;
50
51 if (count > SYSINFO_MAX_MEM_RANGES)
52 count = SYSINFO_MAX_MEM_RANGES;
53
54 info->n_memranges = 0;
55
56 for (i = 0; i < count; i++) {
57 struct cb_memory_range *range = MEM_RANGE_PTR(mem, i);
58
59 #if CONFIG(LP_MEMMAP_RAM_ONLY)
60 if (range->type != CB_MEM_RAM)
61 continue;
62 #endif
63
64 info->memrange[info->n_memranges].base = range->start;
65 info->memrange[info->n_memranges].size = range->size;
66 info->memrange[info->n_memranges].type = range->type;
67
68 info->n_memranges++;
69 }
70 }
71
72 static void cb_parse_serial(void *ptr, struct sysinfo_t *info)
73 {
74 info->cb_serial = virt_to_phys(ptr);
75 }
76
77 static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
78 {
79 struct lb_range *vbnv = (struct lb_range *)ptr;
80
81 info->vbnv_start = vbnv->range_start;
82 info->vbnv_size = vbnv->range_size;
83 }
84
85 static void cb_parse_mmc_info(unsigned char *ptr, struct sysinfo_t *info)
86 {
87 struct cb_mmc_info *mmc_info = (struct cb_mmc_info *)ptr;
88
89 info->mmc_early_wake_status = mmc_info->early_cmd1_status;
90 }
91
92 static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
93 {
94 int i;
95 struct cb_gpios *gpios = (struct cb_gpios *)ptr;
96
97 info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
98 (gpios->count) : SYSINFO_MAX_GPIOS;
99
100 for (i = 0; i < info->num_gpios; i++)
101 info->gpios[i] = gpios->gpios[i];
102 }
103
104 static void cb_parse_mac_addresses(unsigned char *ptr,
105 struct sysinfo_t *info)
106 {
107 struct cb_macs *macs = (struct cb_macs *)ptr;
108 int i;
109
110 info->num_macs = (macs->count < ARRAY_SIZE(info->macs)) ?
111 macs->count : ARRAY_SIZE(info->macs);
112
113 for (i = 0; i < info->num_macs; i++)
114 info->macs[i] = macs->mac_addrs[i];
115 }
116
117 static void cb_parse_board_config(unsigned char *ptr, struct sysinfo_t *info)
118 {
119 struct cb_board_config *const config = (struct cb_board_config *)ptr;
120 info->fw_config = config->fw_config;
121 info->board_id = config->board_id;
122 info->ram_code = config->ram_code;
123 info->sku_id = config->sku_id;
124 }
125
126 #if CONFIG(LP_NVRAM)
127 static void cb_parse_optiontable(void *ptr, struct sysinfo_t *info)
128 {
129 /* ptr is already virtual, but we want to keep physical addresses */
130 info->cmos_option_table = virt_to_phys(ptr);
131 }
132
133 static void cb_parse_checksum(void *ptr, struct sysinfo_t *info)
134 {
135 struct cb_cmos_checksum *cmos_cksum = ptr;
136 info->cmos_range_start = cmos_cksum->range_start;
137 info->cmos_range_end = cmos_cksum->range_end;
138 info->cmos_checksum_location = cmos_cksum->location;
139 }
140 #endif
141
142 #if CONFIG(LP_COREBOOT_VIDEO_CONSOLE)
143 static void cb_parse_framebuffer(void *ptr, struct sysinfo_t *info)
144 {
145 info->framebuffer = *(struct cb_framebuffer *)ptr;
146 }
147 #endif
148
149 static void cb_parse_string(const void *const ptr, uintptr_t *const info)
150 {
151 /* ptr is already virtual (str->string just an offset to that),
152 but we want to keep physical addresses */
153 const struct cb_string *const str = ptr;
154 *info = virt_to_phys(str->string);
155 }
156
157 static void cb_parse_ramoops(void *ptr, struct sysinfo_t *info)
158 {
159 struct lb_range *ramoops = (struct lb_range *)ptr;
160
161 info->ramoops_buffer = ramoops->range_start;
162 info->ramoops_buffer_size = ramoops->range_size;
163 }
164
165 static void cb_parse_mtc(void *ptr, struct sysinfo_t *info)
166 {
167 struct lb_range *mtc = (struct lb_range *)ptr;
168
169 info->mtc_start = mtc->range_start;
170 info->mtc_size = mtc->range_size;
171 }
172
173 static void cb_parse_spi_flash(void *ptr, struct sysinfo_t *info)
174 {
175 struct cb_spi_flash *flash = (struct cb_spi_flash *)ptr;
176
177 info->spi_flash.size = flash->flash_size;
178 info->spi_flash.sector_size = flash->sector_size;
179 info->spi_flash.erase_cmd = flash->erase_cmd;
180 info->spi_flash.flags = flash->flags;
181
182 if (flash->mmap_count == 0)
183 return;
184
185 info->spi_flash.mmap_window_count = MIN(flash->mmap_count, SYSINFO_MAX_MMAP_WINDOWS);
186 memcpy(info->spi_flash.mmap_table, flash->mmap_table,
187 info->spi_flash.mmap_window_count * sizeof(struct flash_mmap_window));
188 }
189
190 static void cb_parse_boot_media_params(unsigned char *ptr,
191 struct sysinfo_t *info)
192 {
193 struct cb_boot_media_params *const bmp =
194 (struct cb_boot_media_params *)ptr;
195 info->fmap_offset = bmp->fmap_offset;
196 info->cbfs_offset = bmp->cbfs_offset;
197 info->cbfs_size = bmp->cbfs_size;
198 info->boot_media_size = bmp->boot_media_size;
199 }
200
201 #if CONFIG(LP_TIMER_RDTSC)
202 static void cb_parse_tsc_info(void *ptr, struct sysinfo_t *info)
203 {
204 const struct cb_tsc_info *tsc_info = ptr;
205
206 if (tsc_info->freq_khz == 0)
207 return;
208
209 /* Honor the TSC frequency passed to the payload. */
210 info->cpu_khz = tsc_info->freq_khz;
211 }
212 #endif
213
214 static void cb_parse_cbmem_entry(void *ptr, struct sysinfo_t *info)
215 {
216 const struct cb_cbmem_entry *cbmem_entry = ptr;
217
218 if (cbmem_entry->size != sizeof(*cbmem_entry))
219 return;
220
221 switch (cbmem_entry->id) {
222 case CBMEM_ID_ACPI_CNVS:
223 info->acpi_cnvs = cbmem_entry->address;
224 break;
225 case CBMEM_ID_ACPI_GNVS:
226 info->acpi_gnvs = cbmem_entry->address;
227 break;
228 case CBMEM_ID_SMBIOS:
229 info->smbios = cbmem_entry->address;
230 break;
231 case CBMEM_ID_CBFS_RO_MCACHE:
232 info->cbfs_ro_mcache_offset = cbmem_entry->address;
233 info->cbfs_ro_mcache_size = cbmem_entry->entry_size;
234 break;
235 case CBMEM_ID_CBFS_RW_MCACHE:
236 info->cbfs_rw_mcache_offset = cbmem_entry->address;
237 info->cbfs_rw_mcache_size = cbmem_entry->entry_size;
238 break;
239 case CBMEM_ID_CONSOLE:
240 info->cbmem_cons = cbmem_entry->address;
241 break;
242 case CBMEM_ID_MRCDATA:
243 info->mrc_cache = cbmem_entry->address;
244 break;
245 case CBMEM_ID_VBOOT_WORKBUF:
246 info->vboot_workbuf = cbmem_entry->address;
247 break;
248 case CBMEM_ID_TIMESTAMP:
249 info->tstamp_table = cbmem_entry->address;
250 break;
251 case CBMEM_ID_VPD:
252 info->chromeos_vpd = cbmem_entry->address;
253 break;
254 case CBMEM_ID_FMAP:
255 info->fmap_cache = cbmem_entry->address;
256 break;
257 case CBMEM_ID_WIFI_CALIBRATION:
258 info->wifi_calibration = cbmem_entry->address;
259 break;
260 case CBMEM_ID_TYPE_C_INFO:
261 info->type_c_info = cbmem_entry->address;
262 break;
263 case CBMEM_ID_MEM_CHIP_INFO:
264 info->mem_chip_base = cbmem_entry->address;
265 break;
266 case CBMEM_ID_CSE_BP_INFO:
267 info->cse_bp_info = cbmem_entry->address;
268 break;
269 case CBMEM_ID_CSE_INFO:
270 info->cse_info = cbmem_entry->address;
271 break;
272 case CBMEM_ID_PVMFW:
273 info->pvmfw = cbmem_entry->address;
274 info->pvmfw_size = cbmem_entry->entry_size;
275 break;
276 default:
277 break;
278 }
279 }
280
281 static void cb_parse_pcie(void *ptr, struct sysinfo_t *info)
282 {
283 const struct cb_pcie *pcie = ptr;
284
285 info->pcie_ctrl_base = pcie->ctrl_base;
286 }
287
288 static void cb_parse_boot_mode(void *ptr, struct sysinfo_t *info)
289 {
290 const struct cb_boot_mode *mode = ptr;
291
292 info->boot_mode = mode->boot_mode;
293 }
294
295 static void cb_parse_rsdp(void *ptr, struct sysinfo_t *info)
296 {
297 const struct cb_acpi_rsdp *cb_acpi_rsdp = ptr;
298 info->acpi_rsdp = cb_acpi_rsdp->rsdp_pointer;
299 }
300
301 int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
302 {
303 struct cb_header *header;
304 unsigned char *ptr = addr;
305 void *forward;
306 int i;
307
308 for (i = 0; i < len; i += 16, ptr += 16) {
309 header = (struct cb_header *)ptr;
310 if (!strncmp((const char *)header->signature, "LBIO", 4))
311 break;
312 }
313
314 /* We walked the entire space and didn't find anything. */
315 if (i >= len)
316 return -1;
317
318 /* Make sure the checksums match. */
319 if (ipchksum((u16 *) header, sizeof(*header)) != 0)
320 return -1;
321
322 if (!header->table_bytes)
323 return 0;
324
325 if (ipchksum((u16 *) (ptr + sizeof(*header)),
326 header->table_bytes) != header->table_checksum)
327 return -1;
328
329 info->cb_header = virt_to_phys(header);
330
331 /* Initialize IDs as undefined in case they don't show up in table. */
332 info->board_id = UNDEFINED_STRAPPING_ID;
333 info->ram_code = UNDEFINED_STRAPPING_ID;
334 info->sku_id = UNDEFINED_STRAPPING_ID;
335 info->fw_config = UNDEFINED_FW_CONFIG;
336
337 /* Now, walk the tables. */
338 ptr += header->header_bytes;
339
340 for (i = 0; i < header->table_entries; i++) {
341 struct cb_record *rec = (struct cb_record *)ptr;
342
343 /* We only care about a few tags here (maybe more later). */
344 switch (rec->tag) {
345 case CB_TAG_FORWARD:
346 forward = phys_to_virt((void *)(unsigned long)
347 ((struct cb_forward *)rec)->forward);
348 return cb_parse_header(forward, len, info);
349 case CB_TAG_MEMORY:
350 cb_parse_memory(ptr, info);
351 break;
352 case CB_TAG_SERIAL:
353 cb_parse_serial(ptr, info);
354 break;
355 case CB_TAG_VERSION:
356 cb_parse_string(ptr, &info->cb_version);
357 break;
358 case CB_TAG_EXTRA_VERSION:
359 cb_parse_string(ptr, &info->extra_version);
360 break;
361 case CB_TAG_BUILD:
362 cb_parse_string(ptr, &info->build);
363 break;
364 case CB_TAG_COMPILE_TIME:
365 cb_parse_string(ptr, &info->compile_time);
366 break;
367 case CB_TAG_COMPILE_BY:
368 cb_parse_string(ptr, &info->compile_by);
369 break;
370 case CB_TAG_COMPILE_HOST:
371 cb_parse_string(ptr, &info->compile_host);
372 break;
373 case CB_TAG_COMPILE_DOMAIN:
374 cb_parse_string(ptr, &info->compile_domain);
375 break;
376 case CB_TAG_COMPILER:
377 cb_parse_string(ptr, &info->compiler);
378 break;
379 case CB_TAG_LINKER:
380 cb_parse_string(ptr, &info->linker);
381 break;
382 case CB_TAG_ASSEMBLER:
383 cb_parse_string(ptr, &info->assembler);
384 break;
385 #if CONFIG(LP_NVRAM)
386 case CB_TAG_CMOS_OPTION_TABLE:
387 cb_parse_optiontable(ptr, info);
388 break;
389 case CB_TAG_OPTION_CHECKSUM:
390 cb_parse_checksum(ptr, info);
391 break;
392 #endif
393 #if CONFIG(LP_COREBOOT_VIDEO_CONSOLE)
394 // FIXME we should warn on serial if coreboot set up a
395 // framebuffer buf the payload does not know about it.
396 case CB_TAG_FRAMEBUFFER:
397 cb_parse_framebuffer(ptr, info);
398 break;
399 #endif
400 case CB_TAG_MAINBOARD:
401 info->cb_mainboard = virt_to_phys(ptr);
402 break;
403 case CB_TAG_GPIO:
404 cb_parse_gpios(ptr, info);
405 break;
406 case CB_TAG_VBNV:
407 cb_parse_vbnv(ptr, info);
408 break;
409 case CB_TAG_MAC_ADDRS:
410 cb_parse_mac_addresses(ptr, info);
411 break;
412 case CB_TAG_SERIALNO:
413 cb_parse_string(ptr, &info->serialno);
414 break;
415 case CB_TAG_BOARD_CONFIG:
416 cb_parse_board_config(ptr, info);
417 break;
418 case CB_TAG_RAM_OOPS:
419 cb_parse_ramoops(ptr, info);
420 break;
421 case CB_TAG_SPI_FLASH:
422 cb_parse_spi_flash(ptr, info);
423 break;
424 case CB_TAG_MMC_INFO:
425 cb_parse_mmc_info(ptr, info);
426 break;
427 case CB_TAG_MTC:
428 cb_parse_mtc(ptr, info);
429 break;
430 case CB_TAG_BOOT_MEDIA_PARAMS:
431 cb_parse_boot_media_params(ptr, info);
432 break;
433 case CB_TAG_CBMEM_ENTRY:
434 cb_parse_cbmem_entry(ptr, info);
435 break;
436 #if CONFIG(LP_TIMER_RDTSC)
437 case CB_TAG_TSC_INFO:
438 cb_parse_tsc_info(ptr, info);
439 break;
440 #endif
441 case CB_TAG_ACPI_RSDP:
442 cb_parse_rsdp(ptr, info);
443 break;
444 case CB_TAG_PCIE:
445 cb_parse_pcie(ptr, info);
446 break;
447 case CB_TAG_BOOT_MODE:
448 cb_parse_boot_mode(ptr, info);
449 break;
450 default:
451 cb_parse_arch_specific(rec, info);
452 break;
453 }
454
455 ptr += rec->size;
456 }
457
458 return 0;
459 }