akaros/kern/drivers/dev/coreboot.c
<<
>>
Prefs
   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 <slab.h>
  32#include <kmalloc.h>
  33#include <kref.h>
  34#include <string.h>
  35#include <stdio.h>
  36#include <assert.h>
  37#include <error.h>
  38#include <cpio.h>
  39#include <pmap.h>
  40#include <smp.h>
  41#include <net/ip.h>
  42#include <umem.h>
  43#include <coreboot_tables.h>
  44
  45/*
  46 * Some of this is x86 specific, and the rest of it is generic. Right now,
  47 * since we only support x86, we'll avoid trying to make lots of infrastructure
  48 * we don't need. If in the future, we want to use coreboot on some other
  49 * architecture, then take out the generic parsing code and move it elsewhere.
  50 */
  51
  52/* === Parsing code === */
  53/* This is the generic parsing code. */
  54
  55static void cb_parse_memory(void *ptr, struct sysinfo_t *info)
  56{
  57        struct cb_memory *mem = ptr;
  58        int count = MEM_RANGE_COUNT(mem);
  59        int i;
  60
  61        if (count > SYSINFO_MAX_MEM_RANGES)
  62                count = SYSINFO_MAX_MEM_RANGES;
  63
  64        info->n_memranges = 0;
  65
  66        for (i = 0; i < count; i++) {
  67                struct cb_memory_range *range = MEM_RANGE_PTR(mem, i);
  68
  69#ifdef CONFIG_LP_MEMMAP_RAM_ONLY
  70                if (range->type != CB_MEM_RAM)
  71                        continue;
  72#endif
  73
  74                info->memrange[info->n_memranges].base =
  75                    cb_unpack64(range->start);
  76
  77                info->memrange[info->n_memranges].size =
  78                    cb_unpack64(range->size);
  79
  80                info->memrange[info->n_memranges].type = range->type;
  81
  82                info->n_memranges++;
  83        }
  84}
  85
  86static void cb_parse_serial(void *ptr, struct sysinfo_t *info)
  87{
  88        info->serial = ((struct cb_serial *)ptr);
  89}
  90
  91#ifdef CONFIG_LP_CHROMEOS
  92static void cb_parse_vboot_handoff(unsigned char *ptr, struct sysinfo_t *info)
  93{
  94        struct cb_range *vbho = (struct cb_range *)ptr;
  95
  96        info->vboot_handoff = (void *)(uintptr_t)vbho->range_start;
  97        info->vboot_handoff_size = vbho->range_size;
  98}
  99
 100static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
 101{
 102        struct cb_range *vbnv = (struct cb_range *)ptr;
 103
 104        info->vbnv_start = vbnv->range_start;
 105        info->vbnv_size = vbnv->range_size;
 106}
 107
 108static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
 109{
 110        int i;
 111        struct cb_gpios *gpios = (struct cb_gpios *)ptr;
 112
 113        info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
 114                                (gpios->count) : SYSINFO_MAX_GPIOS;
 115
 116        for (i = 0; i < info->num_gpios; i++)
 117                info->gpios[i] = gpios->gpios[i];
 118}
 119
 120static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
 121{
 122        struct cb_range *vdat = (struct cb_range *) ptr;
 123
 124        info->vdat_addr = phys_to_virt(vdat->range_start);
 125        info->vdat_size = vdat->range_size;
 126}
 127#endif
 128
 129static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
 130{
 131        struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
 132        info->tstamp_table = KADDR(cbmem->cbmem_tab);
 133}
 134
 135static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
 136{
 137        struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
 138        info->cbmem_cons = KADDR(cbmem->cbmem_tab);
 139}
 140
 141static void cb_parse_mrc_cache(unsigned char *ptr, struct sysinfo_t *info)
 142{
 143        struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
 144        info->mrc_cache = KADDR(cbmem->cbmem_tab);
 145}
 146
 147static void cb_parse_acpi_gnvs(unsigned char *ptr, struct sysinfo_t *info)
 148{
 149        struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
 150        info->acpi_gnvs = KADDR(cbmem->cbmem_tab);
 151}
 152
 153#ifdef CONFIG_LP_NVRAM
 154static void cb_parse_optiontable(void *ptr, struct sysinfo_t *info)
 155{
 156        /* ptr points to a coreboot table entry and is already virtual */
 157        info->option_table = ptr;
 158}
 159
 160static void cb_parse_checksum(void *ptr, struct sysinfo_t *info)
 161{
 162        struct cb_cmos_checksum *cmos_cksum = ptr;
 163        info->cmos_range_start = cmos_cksum->range_start;
 164        info->cmos_range_end = cmos_cksum->range_end;
 165        info->cmos_checksum_location = cmos_cksum->location;
 166}
 167#endif
 168
 169#ifdef CONFIG_LP_COREBOOT_VIDEO_CONSOLE
 170static void cb_parse_framebuffer(void *ptr, struct sysinfo_t *info)
 171{
 172        /* ptr points to a coreboot table entry and is already virtual */
 173        info->framebuffer = ptr;
 174}
 175#endif
 176
 177static void cb_parse_x86_rom_var_mtrr(void *ptr, struct sysinfo_t *info)
 178{
 179        struct cb_x86_rom_mtrr *rom_mtrr = ptr;
 180        info->x86_rom_var_mtrr_index = rom_mtrr->index;
 181}
 182
 183static void cb_parse_string(unsigned char *ptr, char **info)
 184{
 185        *info = (char *)((struct cb_string *)ptr)->string;
 186}
 187
 188static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 189{
 190        struct cb_header *header;
 191        unsigned char *ptr = addr;
 192        void *forward;
 193        int i;
 194
 195        for (i = 0; i < len; i += 16, ptr += 16) {
 196                header = (struct cb_header *)ptr;
 197                if (!strncmp((const char *)header->signature, "LBIO", 4))
 198                        break;
 199        }
 200
 201        /* We walked the entire space and didn't find anything. */
 202        if (i >= len)
 203                return -1;
 204
 205        if (!header->table_bytes)
 206                return 0;
 207
 208        /* Make sure the checksums match. */
 209        if (ipchecksum((uint8_t *) header, sizeof(*header)) != 0)
 210                return -1;
 211
 212        if (ipchecksum((uint8_t *) (ptr + sizeof(*header)),
 213                     header->table_bytes) != header->table_checksum)
 214                return -1;
 215
 216        info->header = header;
 217
 218        /* Now, walk the tables. */
 219        ptr += header->header_bytes;
 220
 221        for (i = 0; i < header->table_entries; i++) {
 222                struct cb_record *rec = (struct cb_record *)ptr;
 223
 224                /* We only care about a few tags here (maybe more later). */
 225                switch (rec->tag) {
 226                case CB_TAG_FORWARD:
 227                        forward = KADDR((unsigned long)((struct cb_forward *)rec)->forward);
 228                        return cb_parse_header(forward, len, info);
 229                        continue;
 230                case CB_TAG_MEMORY:
 231                        cb_parse_memory(ptr, info);
 232                        break;
 233                case CB_TAG_SERIAL:
 234                        cb_parse_serial(ptr, info);
 235                        break;
 236                case CB_TAG_VERSION:
 237                        cb_parse_string(ptr, &info->cb_version);
 238                        break;
 239                case CB_TAG_EXTRA_VERSION:
 240                        cb_parse_string(ptr, &info->extra_version);
 241                        break;
 242                case CB_TAG_BUILD:
 243                        cb_parse_string(ptr, &info->build);
 244                        break;
 245                case CB_TAG_COMPILE_TIME:
 246                        cb_parse_string(ptr, &info->compile_time);
 247                        break;
 248                case CB_TAG_COMPILE_BY:
 249                        cb_parse_string(ptr, &info->compile_by);
 250                        break;
 251                case CB_TAG_COMPILE_HOST:
 252                        cb_parse_string(ptr, &info->compile_host);
 253                        break;
 254                case CB_TAG_COMPILE_DOMAIN:
 255                        cb_parse_string(ptr, &info->compile_domain);
 256                        break;
 257                case CB_TAG_COMPILER:
 258                        cb_parse_string(ptr, &info->compiler);
 259                        break;
 260                case CB_TAG_LINKER:
 261                        cb_parse_string(ptr, &info->linker);
 262                        break;
 263                case CB_TAG_ASSEMBLER:
 264                        cb_parse_string(ptr, &info->assembler);
 265                        break;
 266#ifdef CONFIG_LP_NVRAM
 267                case CB_TAG_CMOS_OPTION_TABLE:
 268                        cb_parse_optiontable(ptr, info);
 269                        break;
 270                case CB_TAG_OPTION_CHECKSUM:
 271                        cb_parse_checksum(ptr, info);
 272                        break;
 273#endif
 274#ifdef CONFIG_LP_COREBOOT_VIDEO_CONSOLE
 275                // FIXME we should warn on serial if coreboot set up a
 276                // framebuffer buf the payload does not know about it.
 277                case CB_TAG_FRAMEBUFFER:
 278                        cb_parse_framebuffer(ptr, info);
 279                        break;
 280#endif
 281                case CB_TAG_MAINBOARD:
 282                        info->mainboard = (struct cb_mainboard *)ptr;
 283#ifdef CONFIG_LP_CHROMEOS
 284                case CB_TAG_GPIO:
 285                        cb_parse_gpios(ptr, info);
 286                        break;
 287                case CB_TAG_VDAT:
 288                        cb_parse_vdat(ptr, info);
 289                        break;
 290                case CB_TAG_VBNV:
 291                        cb_parse_vbnv(ptr, info);
 292                        break;
 293                case CB_TAG_VBOOT_HANDOFF:
 294                        cb_parse_vboot_handoff(ptr, info);
 295                        break;
 296#endif
 297                case CB_TAG_TIMESTAMPS:
 298                        cb_parse_tstamp(ptr, info);
 299                        break;
 300                case CB_TAG_CBMEM_CONSOLE:
 301                        cb_parse_cbmem_cons(ptr, info);
 302                        break;
 303                case CB_TAG_MRC_CACHE:
 304                        cb_parse_mrc_cache(ptr, info);
 305                        break;
 306                case CB_TAG_ACPI_GNVS:
 307                        cb_parse_acpi_gnvs(ptr, info);
 308                        break;
 309                case CB_TAG_X86_ROM_MTRR:
 310                        cb_parse_x86_rom_var_mtrr(ptr, info);
 311                        break;
 312                }
 313
 314                ptr += rec->size;
 315        }
 316
 317        return 1;
 318}
 319
 320/* == Architecture specific == */
 321/* This is the x86 specific stuff. */
 322
 323int get_coreboot_info(struct sysinfo_t *info)
 324{
 325        int ret;
 326
 327        /* Ensure the variable range MTRR index covering the ROM is set to
 328         * an invalid value. */
 329        info->x86_rom_var_mtrr_index = -1;
 330
 331        ret = cb_parse_header(KADDR(0x00000000), 0x1000, info);
 332
 333        if (ret != 1)
 334                ret = cb_parse_header(KADDR(0x000f0000), 0x1000, info);
 335        printk("get_coreboot_info: ret %d\n", ret);
 336        return (ret == 1) ? 0 : -1;
 337}
 338