Use void pointers for untyped memory
[coreboot.git] / payloads / libpayload / arch / i386 / coreboot.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  * 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
31 #include <libpayload-config.h>
32 #include <libpayload.h>
33 #include <coreboot_tables.h>
34
35 /*
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  */
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 #ifdef CONFIG_MEMMAP_RAM_ONLY
60                 if (range->type != CB_MEM_RAM)
61                         continue;
62 #endif
63
64                 info->memrange[info->n_memranges].base =
65                     cb_unpack64(range->start);
66
67                 info->memrange[info->n_memranges].size =
68                     cb_unpack64(range->size);
69
70                 info->memrange[info->n_memranges].type = range->type;
71
72                 info->n_memranges++;
73         }
74 }
75
76 static void cb_parse_serial(void *ptr, struct sysinfo_t *info)
77 {
78         struct cb_serial *ser = ptr;
79         if (ser->type != CB_SERIAL_TYPE_IO_MAPPED)
80                 return;
81         info->ser_ioport = ser->baseaddr;
82 }
83
84 static void cb_parse_version(void *ptr, struct sysinfo_t *info)
85 {
86         struct cb_string *ver = ptr;
87         info->cb_version = (char *)ver->string;
88 }
89
90 #ifdef CONFIG_NVRAM
91 static void cb_parse_optiontable(void *ptr, struct sysinfo_t *info)
92 {
93         info->option_table = ptr;
94 }
95
96 static void cb_parse_checksum(void *ptr, struct sysinfo_t *info)
97 {
98         struct cb_cmos_checksum *cmos_cksum = ptr;
99         info->cmos_range_start = cmos_cksum->range_start;
100         info->cmos_range_end = cmos_cksum->range_end;
101         info->cmos_checksum_location = cmos_cksum->location;
102 }
103 #endif
104
105 #ifdef CONFIG_COREBOOT_VIDEO_CONSOLE
106 static void cb_parse_framebuffer(void *ptr, struct sysinfo_t *info)
107 {
108         info->framebuffer = ptr;
109 }
110 #endif
111
112 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
113 {
114         struct cb_header *header;
115         unsigned char *ptr = addr;
116         int i;
117
118         for (i = 0; i < len; i += 16, ptr += 16) {
119                 header = (struct cb_header *)ptr;
120                 if (!strncmp((const char *)header->signature, "LBIO", 4))
121                         break;
122         }
123
124         /* We walked the entire space and didn't find anything. */
125         if (i >= len)
126                 return -1;
127
128         if (!header->table_bytes)
129                 return 0;
130
131         /* Make sure the checksums match. */
132         if (ipchksum((u16 *) header, sizeof(*header)) != 0)
133                 return -1;
134
135         if (ipchksum((u16 *) (ptr + sizeof(*header)),
136                      header->table_bytes) != header->table_checksum)
137                 return -1;
138
139         /* Now, walk the tables. */
140         ptr += header->header_bytes;
141
142         for (i = 0; i < header->table_entries; i++) {
143                 struct cb_record *rec = (struct cb_record *)ptr;
144
145                 /* We only care about a few tags here (maybe more later). */
146                 switch (rec->tag) {
147                 case CB_TAG_FORWARD:
148                         return cb_parse_header((void *)(unsigned long)((struct cb_forward *)rec)->forward, len, info);
149                         continue;
150                 case CB_TAG_MEMORY:
151                         cb_parse_memory(ptr, info);
152                         break;
153                 case CB_TAG_SERIAL:
154                         cb_parse_serial(ptr, info);
155                         break;
156                 case CB_TAG_VERSION:
157                         cb_parse_version(ptr, info);
158                         break;
159 #ifdef CONFIG_NVRAM
160                 case CB_TAG_CMOS_OPTION_TABLE:
161                         cb_parse_optiontable(ptr, info);
162                         break;
163                 case CB_TAG_OPTION_CHECKSUM:
164                         cb_parse_checksum(ptr, info);
165                         break;
166 #endif
167 #ifdef CONFIG_COREBOOT_VIDEO_CONSOLE
168                 // FIXME we should warn on serial if coreboot set up a
169                 // framebuffer buf the payload does not know about it.
170                 case CB_TAG_FRAMEBUFFER:
171                         cb_parse_framebuffer(ptr, info);
172                         break;
173 #endif
174                 }
175
176                 ptr += rec->size;
177         }
178
179         return 1;
180 }
181
182 /* == Architecture specific == */
183 /* This is the x86 specific stuff. */
184
185 int get_coreboot_info(struct sysinfo_t *info)
186 {
187         int ret = cb_parse_header(phys_to_virt(0x00000000), 0x1000, info);
188
189         if (ret != 1)
190                 ret = cb_parse_header(phys_to_virt(0x000f0000), 0x1000, info);
191
192         return (ret == 1) ? 0 : -1;
193 }