blob: 66e02f11b72936cfe8889b7f4fa0898beaa8ad45 [file] [log] [blame]
Jordan Crousef6145c32008-03-19 23:56:58 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Stefan Reinauer5f7d5062009-03-17 16:41:01 +00005 * Copyright (C) 2009 coresystems GmbH
Jordan Crousef6145c32008-03-19 23:56:58 +00006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
Jordan Crousedb8c0ab2008-11-24 17:54:46 +000031#include <libpayload-config.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000032#include <libpayload.h>
Jordan Crousef6145c32008-03-19 23:56:58 +000033#include <coreboot_tables.h>
34
Uwe Hermann6a441bf2008-03-20 19:54:59 +000035/*
36 * Some of this is x86 specific, and the rest of it is generic. Right now,
37 * since we only support x86, we'll avoid trying to make lots of infrastructure
38 * we don't need. If in the future, we want to use coreboot on some other
39 * architecture, then take out the generic parsing code and move it elsewhere.
40 */
Jordan Crousef6145c32008-03-19 23:56:58 +000041
42/* === Parsing code === */
Uwe Hermann6a441bf2008-03-20 19:54:59 +000043/* This is the generic parsing code. */
Jordan Crousef6145c32008-03-19 23:56:58 +000044
45static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
46{
Uwe Hermann6a441bf2008-03-20 19:54:59 +000047 struct cb_memory *mem = (struct cb_memory *)ptr;
Jordan Crousef6145c32008-03-19 23:56:58 +000048 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
Uwe Hermann6a441bf2008-03-20 19:54:59 +000056 for (i = 0; i < count; i++) {
57 struct cb_memory_range *range =
58 (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
59
Jordan Crousef6145c32008-03-19 23:56:58 +000060 if (range->type != CB_MEM_RAM)
61 continue;
62
63 info->memrange[info->n_memranges].base =
Uwe Hermann6a441bf2008-03-20 19:54:59 +000064 UNPACK_CB64(range->start);
Jordan Crousef6145c32008-03-19 23:56:58 +000065
66 info->memrange[info->n_memranges].size =
Uwe Hermann6a441bf2008-03-20 19:54:59 +000067 UNPACK_CB64(range->size);
Jordan Crousef6145c32008-03-19 23:56:58 +000068
69 info->n_memranges++;
70 }
71}
72
73static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
74{
Uwe Hermann6a441bf2008-03-20 19:54:59 +000075 struct cb_serial *ser = (struct cb_serial *)ptr;
Jordan Crousef6145c32008-03-19 23:56:58 +000076 info->ser_ioport = ser->ioport;
77}
78
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000079#ifdef CONFIG_NVRAM
80static void cb_parse_optiontable(unsigned char *ptr, struct sysinfo_t *info)
81{
82 info->option_table = (struct cb_cmos_option_table *)ptr;
83}
84
85static void cb_parse_checksum(unsigned char *ptr, struct sysinfo_t *info)
86{
87 struct cb_cmos_checksum *cmos_cksum = (struct cb_cmos_checksum *)ptr;
88 info->cmos_range_start = cmos_cksum->range_start;
89 info->cmos_range_end = cmos_cksum->range_end;
90 info->cmos_checksum_location = cmos_cksum->location;
91}
92#endif
93
Jordan Crousef6145c32008-03-19 23:56:58 +000094static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
95{
96 struct cb_header *header;
Uwe Hermann6a441bf2008-03-20 19:54:59 +000097 unsigned char *ptr = (unsigned char *)addr;
Jordan Crousef6145c32008-03-19 23:56:58 +000098 int i;
99
100 for (i = 0; i < len; i += 16, ptr += 16) {
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000101 header = (struct cb_header *)ptr;
Uwe Hermann14a3feb2008-03-20 20:46:44 +0000102 if (!strncmp((const char *)header->signature, "LBIO", 4))
Jordan Crousef6145c32008-03-19 23:56:58 +0000103 break;
104 }
Jordan Crousef6145c32008-03-19 23:56:58 +0000105
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000106 /* We walked the entire space and didn't find anything. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000107 if (i >= len)
108 return -1;
109
110 if (!header->table_bytes)
111 return 0;
112
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000113 /* Make sure the checksums match. */
Uwe Hermannfad8c2b2008-04-11 18:01:50 +0000114 if (ipchksum((u16 *) header, sizeof(*header)) != 0)
Jordan Crousef6145c32008-03-19 23:56:58 +0000115 return -1;
116
Uwe Hermannfad8c2b2008-04-11 18:01:50 +0000117 if (ipchksum((u16 *) (ptr + sizeof(*header)),
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000118 header->table_bytes) != header->table_checksum)
Jordan Crousef6145c32008-03-19 23:56:58 +0000119 return -1;
120
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000121 /* Now, walk the tables. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000122 ptr += header->header_bytes;
Jordan Crousef6145c32008-03-19 23:56:58 +0000123
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000124 for (i = 0; i < header->table_entries; i++) {
125 struct cb_record *rec = (struct cb_record *)ptr;
Jordan Crousef6145c32008-03-19 23:56:58 +0000126
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000127 /* We only care about a few tags here (maybe more later). */
128 switch (rec->tag) {
Stefan Reinauer5f7d5062009-03-17 16:41:01 +0000129 case CB_TAG_FORWARD:
130 return cb_parse_header((void *)(unsigned long)((struct cb_forward *)rec)->forward, len, info);
131 continue;
Jordan Crousef6145c32008-03-19 23:56:58 +0000132 case CB_TAG_MEMORY:
133 cb_parse_memory(ptr, info);
134 break;
Jordan Crousef6145c32008-03-19 23:56:58 +0000135 case CB_TAG_SERIAL:
136 cb_parse_serial(ptr, info);
137 break;
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000138#ifdef CONFIG_NVRAM
139 case CB_TAG_CMOS_OPTION_TABLE:
140 cb_parse_optiontable(ptr, info);
141 break;
142 case CB_TAG_OPTION_CHECKSUM:
143 cb_parse_checksum(ptr, info);
144 break;
145#endif
Jordan Crousef6145c32008-03-19 23:56:58 +0000146 }
147
148 ptr += rec->size;
149 }
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000150
Jordan Crousef6145c32008-03-19 23:56:58 +0000151 return 1;
152}
153
Uwe Hermann6a441bf2008-03-20 19:54:59 +0000154/* == Architecture specific == */
155/* This is the x86 specific stuff. */
Jordan Crousef6145c32008-03-19 23:56:58 +0000156
157int get_coreboot_info(struct sysinfo_t *info)
158{
Stefan Reinauer99c08562008-08-19 17:49:53 +0000159 int ret = cb_parse_header(phys_to_virt(0x00000000), 0x1000, info);
Jordan Crousef6145c32008-03-19 23:56:58 +0000160
161 if (ret != 1)
Stefan Reinauer99c08562008-08-19 17:49:53 +0000162 ret = cb_parse_header(phys_to_virt(0x000f0000), 0x1000, info);
Jordan Crousef6145c32008-03-19 23:56:58 +0000163
164 return (ret == 1) ? 0 : -1;
165}