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