2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 coresystems GmbH
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
30 #include <libpayload.h>
31 #include <coreboot_tables.h>
33 struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
38 struct cb_cmos_option_table *get_system_option_table(void)
40 return phys_to_virt(lib_sysinfo.option_table);
43 static int options_checksum_valid(const struct nvram_accessor *nvram)
46 int range_start = lib_sysinfo.cmos_range_start / 8;
47 int range_end = lib_sysinfo.cmos_range_end / 8;
48 int checksum_location = lib_sysinfo.cmos_checksum_location / 8;
49 u16 checksum = 0, checksum_old;
51 for(i = range_start; i <= range_end; i++) {
52 checksum += nvram->read(i);
55 checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1));
57 return (checksum_old == checksum);
60 void fix_options_checksum_with(const struct nvram_accessor *nvram)
63 int range_start = lib_sysinfo.cmos_range_start / 8;
64 int range_end = lib_sysinfo.cmos_range_end / 8;
65 int checksum_location = lib_sysinfo.cmos_checksum_location / 8;
68 for(i = range_start; i <= range_end; i++) {
69 checksum += nvram->read(i);
72 nvram->write((checksum >> 8), checksum_location);
73 nvram->write((checksum & 0xff), checksum_location + 1);
76 void fix_options_checksum(void)
78 fix_options_checksum_with(use_nvram);
81 static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
83 u8 *value = (u8 *)valptr;
88 /* Convert to byte borders */
92 /* Handle single byte or less */
94 reg8 = nvram->read(addr);
96 value[0] = reg8 & ((1 << len) -1);
100 /* When handling more than a byte, copy whole bytes */
103 value[offs++]=nvram->read(addr++);
109 static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
111 u8 *value = (u8 *)valptr;
116 /* Convert to byte borders */
120 /* Handle single byte or less */
122 reg8 = nvram->read(addr);
123 reg8 &= ~(((1 << len) - 1) << bit);
124 reg8 |= (value[0] & ((1 << len) - 1)) << bit;
125 nvram->write(reg8, addr);
129 /* When handling more than a byte, copy whole bytes */
132 nvram->write(value[offs++], addr++);
138 static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *option_table, char *name)
140 struct cb_cmos_entries *cmos_entry;
141 int len = strnlen(name, CMOS_MAX_NAME_LENGTH);
143 /* cmos entries are located right after the option table */
145 for ( cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
146 cmos_entry->tag == CB_TAG_OPTION;
147 cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size)) {
148 if (memcmp((const char*)cmos_entry->name, name, len))
153 printf("ERROR: No such CMOS option (%s)\n", name);
157 int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, char *name)
159 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
161 if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest))
164 if(!options_checksum_valid(nvram))
172 int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name)
174 return get_option_with(use_nvram, option_table, dest, name);
177 int get_option(void *dest, char *name)
179 return get_option_from(get_system_option_table(), dest, name);
182 int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *value, char *name)
184 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
186 set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value);
187 fix_options_checksum_with(nvram);
193 int set_option(void *value, char *name)
195 return set_option_with(use_nvram, get_system_option_table(), value, name);