blob: eac4a0c51967ba365208fdeb72e3d9033b963e3f [file] [log] [blame]
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2008 coresystems GmbH
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30#include <libpayload.h>
31#include <coreboot_tables.h>
32
Patrick Georgi5febb002012-02-02 15:51:29 +010033u8 *mem_accessor_base;
34
35static u8 mem_read(u8 reg)
36{
37 return mem_accessor_base[reg];
38}
39
40static void mem_write(u8 val, u8 reg)
41{
42 mem_accessor_base[reg] = val;
43}
44
Patrick Georgi317ca0d2012-01-16 10:14:24 +010045struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
46 nvram_read,
47 nvram_write
48};
49
Patrick Georgi5febb002012-02-02 15:51:29 +010050struct nvram_accessor *use_mem = &(struct nvram_accessor) {
51 mem_read,
52 mem_write
53};
54
Patrick Georgi317ca0d2012-01-16 10:14:24 +010055struct cb_cmos_option_table *get_system_option_table(void)
56{
57 return phys_to_virt(lib_sysinfo.option_table);
58}
59
Patrick Georgi56f468d2012-01-16 15:03:11 +010060int options_checksum_valid(const struct nvram_accessor *nvram)
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000061{
62 int i;
Stefan Reinauer71c006f2008-09-26 18:39:06 +000063 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;
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000066 u16 checksum = 0, checksum_old;
67
68 for(i = range_start; i <= range_end; i++) {
Patrick Georgi317ca0d2012-01-16 10:14:24 +010069 checksum += nvram->read(i);
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000070 }
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000071
Patrick Georgi317ca0d2012-01-16 10:14:24 +010072 checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1));
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000073
74 return (checksum_old == checksum);
75}
76
Patrick Georgi317ca0d2012-01-16 10:14:24 +010077void fix_options_checksum_with(const struct nvram_accessor *nvram)
Stefan Reinauer59a1b7f2010-08-17 10:14:50 +000078{
79 int i;
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;
83 u16 checksum = 0;
84
85 for(i = range_start; i <= range_end; i++) {
Patrick Georgi317ca0d2012-01-16 10:14:24 +010086 checksum += nvram->read(i);
Stefan Reinauer59a1b7f2010-08-17 10:14:50 +000087 }
Stefan Reinauer59a1b7f2010-08-17 10:14:50 +000088
Patrick Georgi317ca0d2012-01-16 10:14:24 +010089 nvram->write((checksum >> 8), checksum_location);
90 nvram->write((checksum & 0xff), checksum_location + 1);
Stefan Reinauer59a1b7f2010-08-17 10:14:50 +000091}
92
Patrick Georgi317ca0d2012-01-16 10:14:24 +010093void fix_options_checksum(void)
94{
95 fix_options_checksum_with(use_nvram);
96}
97
98static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +000099{
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100100 u8 *value = valptr;
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000101 int offs = 0;
102 u32 addr, bit;
103 u8 reg8;
104
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000105 /* Convert to byte borders */
106 addr=(bitnum / 8);
107 bit=(bitnum % 8);
108
109 /* Handle single byte or less */
110 if(len <= 8) {
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100111 reg8 = nvram->read(addr);
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000112 reg8 >>= bit;
113 value[0] = reg8 & ((1 << len) -1);
114 return 0;
115 }
116
117 /* When handling more than a byte, copy whole bytes */
118 while (len > 0) {
119 len -= 8;
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100120 value[offs++]=nvram->read(addr++);
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000121 }
122
123 return 0;
124}
125
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100126static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, const void *valptr)
Patrick Georgidbde8092011-11-22 13:07:45 +0100127{
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100128 const u8 *value = valptr;
Patrick Georgidbde8092011-11-22 13:07:45 +0100129 int offs = 0;
130 u32 addr, bit;
131 u8 reg8;
132
133 /* Convert to byte borders */
134 addr=(bitnum / 8);
135 bit=(bitnum % 8);
136
137 /* Handle single byte or less */
138 if (len <= 8) {
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100139 reg8 = nvram->read(addr);
Patrick Georgidbde8092011-11-22 13:07:45 +0100140 reg8 &= ~(((1 << len) - 1) << bit);
141 reg8 |= (value[0] & ((1 << len) - 1)) << bit;
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100142 nvram->write(reg8, addr);
Patrick Georgidbde8092011-11-22 13:07:45 +0100143 return 0;
144 }
145
146 /* When handling more than a byte, copy whole bytes */
147 while (len > 0) {
148 len -= 8;
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100149 nvram->write(value[offs++], addr++);
Patrick Georgidbde8092011-11-22 13:07:45 +0100150 }
151
152 return 0;
153}
154
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100155static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *option_table, const char *name)
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000156{
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000157 struct cb_cmos_entries *cmos_entry;
Mathias Krause632ab1f2012-02-17 11:53:28 +0100158 int len = name ? strnlen(name, CMOS_MAX_NAME_LENGTH) : 0;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000159
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000160 /* cmos entries are located right after the option table */
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000161
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)) {
Stefan Reinauer34b9f862008-08-19 17:51:30 +0000165 if (memcmp((const char*)cmos_entry->name, name, len))
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000166 continue;
Patrick Georgidbde8092011-11-22 13:07:45 +0100167 return cmos_entry;
168 }
169
170 printf("ERROR: No such CMOS option (%s)\n", name);
171 return NULL;
172}
173
Patrick Georgi0a595112012-01-16 15:39:57 +0100174struct cb_cmos_entries *first_cmos_entry(struct cb_cmos_option_table *option_table)
175{
Mathias Krause632ab1f2012-02-17 11:53:28 +0100176 return lookup_cmos_entry(option_table, NULL);
Patrick Georgi0a595112012-01-16 15:39:57 +0100177}
178
179struct cb_cmos_entries *next_cmos_entry(struct cb_cmos_entries *cmos_entry)
180{
181 struct cb_cmos_entries *next = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
182 if (next->tag == CB_TAG_OPTION)
183 return next;
184 else
185 return NULL;
186}
187
Patrick Georgi08ed5a82012-09-24 20:06:27 +0200188struct cb_cmos_enums *first_cmos_enum(struct cb_cmos_option_table *option_table)
Patrick Georgida59f9a2012-01-16 13:47:33 +0100189{
190 struct cb_cmos_entries *cmos_entry;
Patrick Georgida59f9a2012-01-16 13:47:33 +0100191 /* cmos entries are located right after the option table. Skip them */
Patrick Georgi08ed5a82012-09-24 20:06:27 +0200192 cmos_entry = (struct cb_cmos_entries *)((unsigned char *)option_table + option_table->header_length);
Patrick Georgida59f9a2012-01-16 13:47:33 +0100193 while (cmos_entry->tag == CB_TAG_OPTION)
194 cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
195
196 /* cmos enums are located after cmos entries. */
Patrick Georgi08ed5a82012-09-24 20:06:27 +0200197 return (struct cb_cmos_enums *)cmos_entry;
198}
199
200struct cb_cmos_enums *next_cmos_enum(struct cb_cmos_enums *cmos_enum)
201{
202 if (!cmos_enum) {
203 return NULL;
204 }
205
206 cmos_enum = (struct cb_cmos_enums*)((unsigned char *)cmos_enum + cmos_enum->size);
207 if (cmos_enum->tag == CB_TAG_OPTION_ENUM) {
208 return cmos_enum;
209 } else {
210 return NULL;
211 }
212}
213
214struct cb_cmos_enums *next_cmos_enum_of_id(struct cb_cmos_enums *cmos_enum, int id)
215{
216 while ((cmos_enum = next_cmos_enum(cmos_enum))) {
217 if (cmos_enum->config_id == id) {
218 return cmos_enum;
219 }
220 }
221 return NULL;
222}
223
224struct cb_cmos_enums *first_cmos_enum_of_id(struct cb_cmos_option_table *option_table, int id)
225{
226 struct cb_cmos_enums *cmos_enum = first_cmos_enum(option_table);
227 if (!cmos_enum) {
228 return NULL;
229 }
230 if (cmos_enum->config_id == id) {
231 return cmos_enum;
232 }
233
234 return next_cmos_enum_of_id(cmos_enum, id);
235}
236
237/* Either value or text must be NULL. Returns the field that matches "the other" for a given config_id */
238static struct cb_cmos_enums *lookup_cmos_enum_core(struct cb_cmos_option_table *option_table, int config_id, const u8 *value, const char *text)
239{
240 int len = strnlen(text, CMOS_MAX_TEXT_LENGTH);
241
242 /* cmos enums are located after cmos entries. */
Patrick Georgida59f9a2012-01-16 13:47:33 +0100243 struct cb_cmos_enums *cmos_enum;
Patrick Georgi08ed5a82012-09-24 20:06:27 +0200244 for ( cmos_enum = first_cmos_enum_of_id(option_table, config_id);
245 cmos_enum;
246 cmos_enum = next_cmos_enum_of_id(cmos_enum, config_id)) {
247 if (((value == NULL) || (cmos_enum->value == *value)) &&
248 ((text == NULL) || (memcmp((const char*)cmos_enum->text, text, len)))) {
Patrick Georgida59f9a2012-01-16 13:47:33 +0100249 return cmos_enum;
250 }
251 }
252
253 return NULL;
254}
255
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100256static struct cb_cmos_enums *lookup_cmos_enum_by_value(struct cb_cmos_option_table *option_table, int config_id, const u8 *value)
Patrick Georgida59f9a2012-01-16 13:47:33 +0100257{
258 return lookup_cmos_enum_core(option_table, config_id, value, NULL);
259}
260
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100261static struct cb_cmos_enums *lookup_cmos_enum_by_label(struct cb_cmos_option_table *option_table, int config_id, const char *label)
Patrick Georgida59f9a2012-01-16 13:47:33 +0100262{
263 return lookup_cmos_enum_core(option_table, config_id, NULL, label);
264}
265
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100266int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, const char *name)
Patrick Georgidbde8092011-11-22 13:07:45 +0100267{
268 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
269 if (cmos_entry) {
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100270 if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest))
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000271 return 1;
272
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100273 if(!options_checksum_valid(nvram))
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000274 return 1;
275
276 return 0;
277 }
Stefan Reinauer95a6e1c2008-08-07 10:21:05 +0000278 return 1;
279}
Patrick Georgic2300582011-07-12 11:40:29 +0200280
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100281int get_option_from(struct cb_cmos_option_table *option_table, void *dest, const char *name)
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100282{
283 return get_option_with(use_nvram, option_table, dest, name);
284}
285
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100286int get_option(void *dest, const char *name)
Patrick Georgic2300582011-07-12 11:40:29 +0200287{
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100288 return get_option_from(get_system_option_table(), dest, name);
Patrick Georgic2300582011-07-12 11:40:29 +0200289}
Patrick Georgidbde8092011-11-22 13:07:45 +0100290
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100291int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const void *value, const char *name)
Patrick Georgidbde8092011-11-22 13:07:45 +0100292{
Patrick Georgidbde8092011-11-22 13:07:45 +0100293 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
294 if (cmos_entry) {
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100295 set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value);
296 fix_options_checksum_with(nvram);
Patrick Georgidbde8092011-11-22 13:07:45 +0100297 return 0;
298 }
299 return 1;
300}
301
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100302int set_option(const void *value, const char *name)
Patrick Georgi317ca0d2012-01-16 10:14:24 +0100303{
304 return set_option_with(use_nvram, get_system_option_table(), value, name);
305}
Patrick Georgida59f9a2012-01-16 13:47:33 +0100306
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100307int get_option_as_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char **dest, const char *name)
Patrick Georgida59f9a2012-01-16 13:47:33 +0100308{
309 void *raw;
310 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
311 if (!cmos_entry)
312 return 1;
313 int cmos_length = (cmos_entry->length+7)/8;
314
315 /* extra byte to ensure 0-terminated strings */
316 raw = malloc(cmos_length+1);
317 memset(raw, 0, cmos_length+1);
318
319 int ret = get_option_with(nvram, option_table, raw, name);
320
321 struct cb_cmos_enums *cmos_enum;
322 switch (cmos_entry->config) {
323 case 'h':
324 /* only works on little endian.
325 26 bytes is enough for a 64bit value in decimal */
326 *dest = malloc(26);
327 sprintf(*dest, "%ull", *(u64*)raw);
328 break;
329 case 's':
330 *dest = strdup(raw);
331 break;
332 case 'e':
333 cmos_enum = lookup_cmos_enum_by_value(option_table, cmos_entry->config_id, (u8*)raw);
334 *dest = strdup((const char*)cmos_enum->text);
335 break;
336 default: /* fail */
Mathias Krause9a4114a2012-02-08 10:31:42 +0100337 ret = 1;
Patrick Georgida59f9a2012-01-16 13:47:33 +0100338 }
339 free(raw);
340 return ret;
341}
342
Mathias Kraused6a6eef2012-02-17 12:02:47 +0100343int set_option_from_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const char *value, const char *name)
Patrick Georgida59f9a2012-01-16 13:47:33 +0100344{
345 void *raw;
346 struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
347 if (!cmos_entry)
348 return 1;
349
350 struct cb_cmos_enums *cmos_enum;
351 switch (cmos_entry->config) {
352 case 'h':
353 /* only works on little endian */
Mathias Krause50759ed2012-02-08 10:32:57 +0100354 raw = malloc(sizeof(u64));
Patrick Georgida59f9a2012-01-16 13:47:33 +0100355 *(u64*)raw = strtoull(value, NULL, 0);
356 break;
357 case 's':
358 raw = strdup(value);
359 break;
360 case 'e':
361 cmos_enum = lookup_cmos_enum_by_label(option_table, cmos_entry->config_id, value);
362 raw = malloc(sizeof(u32));
363 *(u32*)raw = cmos_enum->value;
364 break;
365 default: /* fail */
366 return 1;
367 }
368
369 int ret = set_option_with(nvram, option_table, raw, name);
370 free(raw);
371 return ret;
372}