libpayload: The initial chunk of code writen by AMD
[coreboot.git] / payloads / libpayload / i386 / coreboot.c
1 /*
2  * This file is part of the libpayload project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
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.h>
31 #include <sysinfo.h>
32 #include <ipchecksum.h>
33 #include <coreboot_tables.h>
34
35 /* Some of this is x86 specific, and the rest of it
36    is generic.  Right now, since we only support x86,
37    we'll avoid trying to make lots of infrastructure
38    we don't need.  If in the future, we want to use
39    coreboot on some other architecture, then take out
40    the generic parsing code and move it elsewhere
41 */
42
43 /* === Parsing code === */
44 /* This is the generic parsing code */
45
46 static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
47 {
48         struct cb_memory *mem = (struct cb_memory *) ptr;
49         int count = MEM_RANGE_COUNT(mem);
50         int i;
51
52         if (count > SYSINFO_MAX_MEM_RANGES)
53                 count = SYSINFO_MAX_MEM_RANGES;
54
55         info->n_memranges = 0;
56
57         for(i = 0; i < count; i++) {
58                 struct cb_memory_range *range = 
59                         (struct cb_memory_range *) MEM_RANGE_PTR(mem, i);
60                 
61                 if (range->type != CB_MEM_RAM)
62                         continue;
63
64                 info->memrange[info->n_memranges].base =
65                         UNPACK_CB64(range->start);
66
67                 info->memrange[info->n_memranges].size =
68                         UNPACK_CB64(range->size);
69
70                 info->n_memranges++;
71         }
72 }
73
74 static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
75 {
76         struct cb_serial *ser = (struct cb_serial *) ptr;
77         info->ser_ioport = ser->ioport;
78 }
79
80 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
81 {
82         struct cb_header *header;
83         unsigned char *ptr = (unsigned char *) addr;
84         int i;
85
86         for (i = 0; i < len; i += 16, ptr += 16) {
87                 header = (struct cb_header *) ptr;
88
89                 if (!strncmp(header->signature, "LBIO", 4))
90                         break;
91         }
92         
93         /* We walked the entire space and didn't find anything */
94
95         if (i >= len)
96                 return -1;
97
98         if (!header->table_bytes)
99                 return 0;
100
101         /* Make sure the checksums match */
102
103         if (ipchksum((uint16_t *) header, sizeof(*header)) != 0)
104                 return -1;
105
106         if (ipchksum((uint16_t *) (ptr + sizeof(*header)),
107                 header->table_bytes) != header->table_checksum)
108                 return -1;
109
110         /* Now, walk the tables */
111         ptr += header->header_bytes;
112     
113         for(i = 0; i < header->table_entries; i++) {
114                 struct cb_record *rec = (struct cb_record *) ptr;
115
116                 /* We only care about a few tags here - maybe
117                    more will be interesting later 
118                 */
119
120                 switch(rec->tag) {
121                 case CB_TAG_MEMORY:
122                         cb_parse_memory(ptr, info);
123                         break;
124                         
125                 case CB_TAG_SERIAL:
126                         cb_parse_serial(ptr, info);
127                         break;
128                 }
129
130                 ptr += rec->size;
131         }
132         
133         return 1;
134 }
135
136 /* == Architecture specific ==*/
137 /* This is the x86 specific stuff */
138
139 int get_coreboot_info(struct sysinfo_t *info)
140 {
141         int ret = cb_parse_header((void *) 0x0, 0x1000, info);
142
143         if (ret != 1)
144                 ret = cb_parse_header((void *) 0xf0000, 0x1000, info);
145
146         return (ret == 1) ? 0 : -1;
147 }