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 u8 *mem_accessor_base;
35 static u8 mem_read(u8 reg)
37 return mem_accessor_base[reg];
40 static void mem_write(u8 val, u8 reg)
42 mem_accessor_base[reg] = val;
45 struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
50 struct nvram_accessor *use_mem = &(struct nvram_accessor) {
55 struct cb_cmos_option_table *get_system_option_table(void)
57 return phys_to_virt(lib_sysinfo.option_table);
60 int options_checksum_valid(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;
66 u16 checksum = 0, checksum_old;
68 for(i = range_start; i <= range_end; i++) {
69 checksum += nvram->read(i);
72 checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1));
74 return (checksum_old == checksum);
77 void fix_options_checksum_with(const struct nvram_accessor *nvram)
80 int range_start = lib_sysinfo.cmos_range_start / 8;
81 int range_end = lib_sysinfo.cmos_range_end / 8;
82 int checksum_location = lib_sysinfo.cmos_checksum_location / 8;
85 for(i = range_start; i <= range_end; i++) {
86 checksum += nvram->read(i);
89 nvram->write((checksum >> 8), checksum_location);
90 nvram->write((checksum & 0xff), checksum_location + 1);
93 void fix_options_checksum(void)
95 fix_options_checksum_with(use_nvram);
98 static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
100 u8 *value = (u8 *)valptr;
105 /* Convert to byte borders */
109 /* Handle single byte or less */
111 reg8 = nvram->read(addr);
113 value[0] = reg8 & ((1 << len) -1);
117 /* When handling more than a byte, copy whole bytes */
120 value[offs++]=nvram->read(addr++);
126 static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
128 u8 *value = (u8 *)valptr;
133 /* Convert to byte borders */
137 /* Handle single byte or less */
139 reg8 = nvram->read(addr);
140 reg8 &= ~(((1 << len) - 1) << bit);
141 reg8 |= (value[0] & ((1 << len) - 1)) << bit;
142 nvram->write(reg8, addr);
146 /* When handling more than a byte, copy whole bytes */
149 nvram->write(value[offs++], addr++);
155 static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *option_table, char *name)
157 struct cb_cmos_entries *cmos_entry;
158 int len = strnlen(name, CMOS_MAX_NAME_LENGTH);
160 /* cmos entries are located right after the option table */
162 for ( cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
163 cmos_entry->tag == CB_TAG_OPTION;
164 cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size)) {
165 if (memcmp((const char*)cmos_entry->name, name, len))
170 printf("ERROR: No such CMOS option (%s)\n", name);
174 struct cb_cmos_entries *first_cmos_entry(struct cb_cmos_option_table *option_table)
176 return lookup_cmos_entry(option_table, "");
179 struct cb_cmos_entries *next_cmos_entry(struct cb_cmos_entries *cmos_entry)
181 struct cb_cmos_entries *next = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
182 if (next->tag == CB_TAG_OPTION)
188 /* Either value or text must be NULL. Returns the field that matches "the other" for a given config_id */
189 static struct cb_cmos_enums *lookup_cmos_enum_core(struct cb_cmos_option_table *option_table, int config_id, u8 *value, char *text)
191 struct cb_cmos_entries *cmos_entry;
192 int len = strnlen(text, CMOS_MAX_TEXT_LENGTH);
194 /* cmos entries are located right after the option table. Skip them */
195 cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
196 while (cmos_entry->tag == CB_TAG_OPTION)
197 cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
199 /* cmos enums are located after cmos entries. */
200 struct cb_cmos_enums *cmos_enum;
201 for ( cmos_enum = (struct cb_cmos_enums*)cmos_entry;
202 cmos_enum->tag == CB_TAG_OPTION_ENUM;
203 cmos_enum = (struct cb_cmos_enums*)((unsigned char *)cmos_enum + cmos_enum->size)) {
204 if ((cmos_enum->config_id == config_id)
205 && ((value == NULL) || (cmos_enum->value == *value))
206 && ((text == NULL) || (memcmp((const char*)cmos_enum->text, text, len)))) {
214 static struct cb_cmos_enums *lookup_cmos_enum_by_value(struct cb_cmos_option_table *option_table, int config_id, u8 *value)
216 return lookup_cmos_enum_core(option_table, config_id, value, NULL);
219 static struct cb_cmos_enums *lookup_cmos_enum_by_label(struct cb_cmos_option_table *option_table, int config_id, char *label)
221 return lookup_cmos_enum_core(option_table, config_id, NULL, label);
224 int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, char *name)
226 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
228 if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest))
231 if(!options_checksum_valid(nvram))
239 int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name)
241 return get_option_with(use_nvram, option_table, dest, name);
244 int get_option(void *dest, char *name)
246 return get_option_from(get_system_option_table(), dest, name);
249 int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *value, char *name)
251 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
253 set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value);
254 fix_options_checksum_with(nvram);
260 int set_option(void *value, char *name)
262 return set_option_with(use_nvram, get_system_option_table(), value, name);
265 int get_option_as_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char **dest, char *name)
268 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
271 int cmos_length = (cmos_entry->length+7)/8;
273 /* extra byte to ensure 0-terminated strings */
274 raw = malloc(cmos_length+1);
275 memset(raw, 0, cmos_length+1);
277 int ret = get_option_with(nvram, option_table, raw, name);
279 struct cb_cmos_enums *cmos_enum;
280 switch (cmos_entry->config) {
282 /* only works on little endian.
283 26 bytes is enough for a 64bit value in decimal */
285 sprintf(*dest, "%ull", *(u64*)raw);
291 cmos_enum = lookup_cmos_enum_by_value(option_table, cmos_entry->config_id, (u8*)raw);
292 *dest = strdup((const char*)cmos_enum->text);
301 int set_option_from_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char *value, char *name)
304 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
308 struct cb_cmos_enums *cmos_enum;
309 switch (cmos_entry->config) {
311 /* only works on little endian */
313 *(u64*)raw = strtoull(value, NULL, 0);
319 cmos_enum = lookup_cmos_enum_by_label(option_table, cmos_entry->config_id, value);
320 raw = malloc(sizeof(u32));
321 *(u32*)raw = cmos_enum->value;
327 int ret = set_option_with(nvram, option_table, raw, name);