2008-08-07 12:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the libpayload project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 coresystems GmbH
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libpayload.h>
|
|
|
|
#include <coreboot_tables.h>
|
|
|
|
|
2012-02-02 15:51:29 +01:00
|
|
|
u8 *mem_accessor_base;
|
|
|
|
|
|
|
|
static u8 mem_read(u8 reg)
|
|
|
|
{
|
|
|
|
return mem_accessor_base[reg];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mem_write(u8 val, u8 reg)
|
|
|
|
{
|
|
|
|
mem_accessor_base[reg] = val;
|
|
|
|
}
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
|
|
|
|
nvram_read,
|
|
|
|
nvram_write
|
|
|
|
};
|
|
|
|
|
2012-02-02 15:51:29 +01:00
|
|
|
struct nvram_accessor *use_mem = &(struct nvram_accessor) {
|
|
|
|
mem_read,
|
|
|
|
mem_write
|
|
|
|
};
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
struct cb_cmos_option_table *get_system_option_table(void)
|
|
|
|
{
|
|
|
|
return phys_to_virt(lib_sysinfo.option_table);
|
|
|
|
}
|
|
|
|
|
2012-01-16 15:03:11 +01:00
|
|
|
int options_checksum_valid(const struct nvram_accessor *nvram)
|
2008-08-07 12:21:05 +02:00
|
|
|
{
|
|
|
|
int i;
|
2008-09-26 20:39:06 +02:00
|
|
|
int range_start = lib_sysinfo.cmos_range_start / 8;
|
|
|
|
int range_end = lib_sysinfo.cmos_range_end / 8;
|
|
|
|
int checksum_location = lib_sysinfo.cmos_checksum_location / 8;
|
2008-08-07 12:21:05 +02:00
|
|
|
u16 checksum = 0, checksum_old;
|
|
|
|
|
|
|
|
for(i = range_start; i <= range_end; i++) {
|
2012-01-16 10:14:24 +01:00
|
|
|
checksum += nvram->read(i);
|
2008-08-07 12:21:05 +02:00
|
|
|
}
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1));
|
2008-08-07 12:21:05 +02:00
|
|
|
|
|
|
|
return (checksum_old == checksum);
|
|
|
|
}
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
void fix_options_checksum_with(const struct nvram_accessor *nvram)
|
2010-08-17 12:14:50 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int range_start = lib_sysinfo.cmos_range_start / 8;
|
|
|
|
int range_end = lib_sysinfo.cmos_range_end / 8;
|
|
|
|
int checksum_location = lib_sysinfo.cmos_checksum_location / 8;
|
|
|
|
u16 checksum = 0;
|
|
|
|
|
|
|
|
for(i = range_start; i <= range_end; i++) {
|
2012-01-16 10:14:24 +01:00
|
|
|
checksum += nvram->read(i);
|
2010-08-17 12:14:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
nvram->write((checksum >> 8), checksum_location);
|
|
|
|
nvram->write((checksum & 0xff), checksum_location + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fix_options_checksum(void)
|
|
|
|
{
|
|
|
|
fix_options_checksum_with(use_nvram);
|
2010-08-17 12:14:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
|
2008-08-07 12:21:05 +02:00
|
|
|
{
|
2012-02-17 12:02:47 +01:00
|
|
|
u8 *value = valptr;
|
2008-08-07 12:21:05 +02:00
|
|
|
int offs = 0;
|
|
|
|
u32 addr, bit;
|
|
|
|
u8 reg8;
|
|
|
|
|
|
|
|
/* Convert to byte borders */
|
|
|
|
addr=(bitnum / 8);
|
|
|
|
bit=(bitnum % 8);
|
|
|
|
|
|
|
|
/* Handle single byte or less */
|
|
|
|
if(len <= 8) {
|
2012-01-16 10:14:24 +01:00
|
|
|
reg8 = nvram->read(addr);
|
2008-08-07 12:21:05 +02:00
|
|
|
reg8 >>= bit;
|
|
|
|
value[0] = reg8 & ((1 << len) -1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When handling more than a byte, copy whole bytes */
|
|
|
|
while (len > 0) {
|
|
|
|
len -= 8;
|
2012-01-16 10:14:24 +01:00
|
|
|
value[offs++]=nvram->read(addr++);
|
2008-08-07 12:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, const void *valptr)
|
2011-11-22 13:07:45 +01:00
|
|
|
{
|
2012-02-17 12:02:47 +01:00
|
|
|
const u8 *value = valptr;
|
2011-11-22 13:07:45 +01:00
|
|
|
int offs = 0;
|
|
|
|
u32 addr, bit;
|
|
|
|
u8 reg8;
|
|
|
|
|
|
|
|
/* Convert to byte borders */
|
|
|
|
addr=(bitnum / 8);
|
|
|
|
bit=(bitnum % 8);
|
|
|
|
|
|
|
|
/* Handle single byte or less */
|
|
|
|
if (len <= 8) {
|
2012-01-16 10:14:24 +01:00
|
|
|
reg8 = nvram->read(addr);
|
2011-11-22 13:07:45 +01:00
|
|
|
reg8 &= ~(((1 << len) - 1) << bit);
|
|
|
|
reg8 |= (value[0] & ((1 << len) - 1)) << bit;
|
2012-01-16 10:14:24 +01:00
|
|
|
nvram->write(reg8, addr);
|
2011-11-22 13:07:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When handling more than a byte, copy whole bytes */
|
|
|
|
while (len > 0) {
|
|
|
|
len -= 8;
|
2012-01-16 10:14:24 +01:00
|
|
|
nvram->write(value[offs++], addr++);
|
2011-11-22 13:07:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *option_table, const char *name)
|
2008-08-07 12:21:05 +02:00
|
|
|
{
|
|
|
|
struct cb_cmos_entries *cmos_entry;
|
2012-02-17 11:53:28 +01:00
|
|
|
int len = name ? strnlen(name, CMOS_MAX_NAME_LENGTH) : 0;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2008-08-07 12:21:05 +02:00
|
|
|
/* cmos entries are located right after the option table */
|
|
|
|
|
|
|
|
for ( cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
|
|
|
|
cmos_entry->tag == CB_TAG_OPTION;
|
|
|
|
cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size)) {
|
2008-08-19 19:51:30 +02:00
|
|
|
if (memcmp((const char*)cmos_entry->name, name, len))
|
2008-08-07 12:21:05 +02:00
|
|
|
continue;
|
2011-11-22 13:07:45 +01:00
|
|
|
return cmos_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("ERROR: No such CMOS option (%s)\n", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-16 15:39:57 +01:00
|
|
|
struct cb_cmos_entries *first_cmos_entry(struct cb_cmos_option_table *option_table)
|
|
|
|
{
|
2012-02-17 11:53:28 +01:00
|
|
|
return lookup_cmos_entry(option_table, NULL);
|
2012-01-16 15:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct cb_cmos_entries *next_cmos_entry(struct cb_cmos_entries *cmos_entry)
|
|
|
|
{
|
|
|
|
struct cb_cmos_entries *next = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
|
|
|
|
if (next->tag == CB_TAG_OPTION)
|
|
|
|
return next;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-24 20:06:27 +02:00
|
|
|
struct cb_cmos_enums *first_cmos_enum(struct cb_cmos_option_table *option_table)
|
2012-01-16 13:47:33 +01:00
|
|
|
{
|
|
|
|
struct cb_cmos_entries *cmos_entry;
|
|
|
|
/* cmos entries are located right after the option table. Skip them */
|
2012-09-24 20:06:27 +02:00
|
|
|
cmos_entry = (struct cb_cmos_entries *)((unsigned char *)option_table + option_table->header_length);
|
2012-01-16 13:47:33 +01:00
|
|
|
while (cmos_entry->tag == CB_TAG_OPTION)
|
|
|
|
cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
|
|
|
|
|
2012-09-24 20:06:27 +02:00
|
|
|
/* cmos enums are located after cmos entries. */
|
|
|
|
return (struct cb_cmos_enums *)cmos_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cb_cmos_enums *next_cmos_enum(struct cb_cmos_enums *cmos_enum)
|
|
|
|
{
|
|
|
|
if (!cmos_enum) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmos_enum = (struct cb_cmos_enums*)((unsigned char *)cmos_enum + cmos_enum->size);
|
|
|
|
if (cmos_enum->tag == CB_TAG_OPTION_ENUM) {
|
|
|
|
return cmos_enum;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cb_cmos_enums *next_cmos_enum_of_id(struct cb_cmos_enums *cmos_enum, int id)
|
|
|
|
{
|
|
|
|
while ((cmos_enum = next_cmos_enum(cmos_enum))) {
|
|
|
|
if (cmos_enum->config_id == id) {
|
|
|
|
return cmos_enum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cb_cmos_enums *first_cmos_enum_of_id(struct cb_cmos_option_table *option_table, int id)
|
|
|
|
{
|
|
|
|
struct cb_cmos_enums *cmos_enum = first_cmos_enum(option_table);
|
|
|
|
if (!cmos_enum) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (cmos_enum->config_id == id) {
|
|
|
|
return cmos_enum;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next_cmos_enum_of_id(cmos_enum, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Either value or text must be NULL. Returns the field that matches "the other" for a given config_id */
|
|
|
|
static struct cb_cmos_enums *lookup_cmos_enum_core(struct cb_cmos_option_table *option_table, int config_id, const u8 *value, const char *text)
|
|
|
|
{
|
|
|
|
int len = strnlen(text, CMOS_MAX_TEXT_LENGTH);
|
|
|
|
|
2012-01-16 13:47:33 +01:00
|
|
|
/* cmos enums are located after cmos entries. */
|
|
|
|
struct cb_cmos_enums *cmos_enum;
|
2012-09-24 20:06:27 +02:00
|
|
|
for ( cmos_enum = first_cmos_enum_of_id(option_table, config_id);
|
|
|
|
cmos_enum;
|
|
|
|
cmos_enum = next_cmos_enum_of_id(cmos_enum, config_id)) {
|
|
|
|
if (((value == NULL) || (cmos_enum->value == *value)) &&
|
|
|
|
((text == NULL) || (memcmp((const char*)cmos_enum->text, text, len)))) {
|
2012-01-16 13:47:33 +01:00
|
|
|
return cmos_enum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
static struct cb_cmos_enums *lookup_cmos_enum_by_value(struct cb_cmos_option_table *option_table, int config_id, const u8 *value)
|
2012-01-16 13:47:33 +01:00
|
|
|
{
|
|
|
|
return lookup_cmos_enum_core(option_table, config_id, value, NULL);
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
static struct cb_cmos_enums *lookup_cmos_enum_by_label(struct cb_cmos_option_table *option_table, int config_id, const char *label)
|
2012-01-16 13:47:33 +01:00
|
|
|
{
|
|
|
|
return lookup_cmos_enum_core(option_table, config_id, NULL, label);
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, const char *name)
|
2011-11-22 13:07:45 +01:00
|
|
|
{
|
|
|
|
struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
|
|
|
|
if (cmos_entry) {
|
2012-01-16 10:14:24 +01:00
|
|
|
if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest))
|
2008-08-07 12:21:05 +02:00
|
|
|
return 1;
|
|
|
|
|
2012-01-16 10:14:24 +01:00
|
|
|
if(!options_checksum_valid(nvram))
|
2008-08-07 12:21:05 +02:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2011-07-12 11:40:29 +02:00
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int get_option_from(struct cb_cmos_option_table *option_table, void *dest, const char *name)
|
2012-01-16 10:14:24 +01:00
|
|
|
{
|
|
|
|
return get_option_with(use_nvram, option_table, dest, name);
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int get_option(void *dest, const char *name)
|
2011-07-12 11:40:29 +02:00
|
|
|
{
|
2012-01-16 10:14:24 +01:00
|
|
|
return get_option_from(get_system_option_table(), dest, name);
|
2011-07-12 11:40:29 +02:00
|
|
|
}
|
2011-11-22 13:07:45 +01:00
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const void *value, const char *name)
|
2011-11-22 13:07:45 +01:00
|
|
|
{
|
|
|
|
struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
|
|
|
|
if (cmos_entry) {
|
2012-01-16 10:14:24 +01:00
|
|
|
set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value);
|
|
|
|
fix_options_checksum_with(nvram);
|
2011-11-22 13:07:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int set_option(const void *value, const char *name)
|
2012-01-16 10:14:24 +01:00
|
|
|
{
|
|
|
|
return set_option_with(use_nvram, get_system_option_table(), value, name);
|
|
|
|
}
|
2012-01-16 13:47:33 +01:00
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int get_option_as_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char **dest, const char *name)
|
2012-01-16 13:47:33 +01:00
|
|
|
{
|
|
|
|
void *raw;
|
|
|
|
struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
|
|
|
|
if (!cmos_entry)
|
|
|
|
return 1;
|
|
|
|
int cmos_length = (cmos_entry->length+7)/8;
|
|
|
|
|
|
|
|
/* extra byte to ensure 0-terminated strings */
|
|
|
|
raw = malloc(cmos_length+1);
|
|
|
|
memset(raw, 0, cmos_length+1);
|
|
|
|
|
|
|
|
int ret = get_option_with(nvram, option_table, raw, name);
|
|
|
|
|
|
|
|
struct cb_cmos_enums *cmos_enum;
|
|
|
|
switch (cmos_entry->config) {
|
|
|
|
case 'h':
|
|
|
|
/* only works on little endian.
|
|
|
|
26 bytes is enough for a 64bit value in decimal */
|
|
|
|
*dest = malloc(26);
|
2012-09-26 15:11:48 +02:00
|
|
|
sprintf(*dest, "%llu", *(u64*)raw);
|
2012-01-16 13:47:33 +01:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
*dest = strdup(raw);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
cmos_enum = lookup_cmos_enum_by_value(option_table, cmos_entry->config_id, (u8*)raw);
|
|
|
|
*dest = strdup((const char*)cmos_enum->text);
|
|
|
|
break;
|
|
|
|
default: /* fail */
|
2012-02-08 10:31:42 +01:00
|
|
|
ret = 1;
|
2012-01-16 13:47:33 +01:00
|
|
|
}
|
|
|
|
free(raw);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:02:47 +01:00
|
|
|
int set_option_from_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const char *value, const char *name)
|
2012-01-16 13:47:33 +01:00
|
|
|
{
|
|
|
|
void *raw;
|
|
|
|
struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
|
|
|
|
if (!cmos_entry)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
struct cb_cmos_enums *cmos_enum;
|
|
|
|
switch (cmos_entry->config) {
|
|
|
|
case 'h':
|
|
|
|
/* only works on little endian */
|
2012-02-08 10:32:57 +01:00
|
|
|
raw = malloc(sizeof(u64));
|
2012-01-16 13:47:33 +01:00
|
|
|
*(u64*)raw = strtoull(value, NULL, 0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
raw = strdup(value);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
cmos_enum = lookup_cmos_enum_by_label(option_table, cmos_entry->config_id, value);
|
|
|
|
raw = malloc(sizeof(u32));
|
|
|
|
*(u32*)raw = cmos_enum->value;
|
|
|
|
break;
|
|
|
|
default: /* fail */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = set_option_with(nvram, option_table, raw, name);
|
|
|
|
free(raw);
|
|
|
|
return ret;
|
|
|
|
}
|