Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | static int options_checksum_valid(void) |
| 34 | { |
| 35 | int i; |
Stefan Reinauer | 71c006f | 2008-09-26 18:39:06 +0000 | [diff] [blame] | 36 | int range_start = lib_sysinfo.cmos_range_start / 8; |
| 37 | int range_end = lib_sysinfo.cmos_range_end / 8; |
| 38 | int checksum_location = lib_sysinfo.cmos_checksum_location / 8; |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 39 | u16 checksum = 0, checksum_old; |
| 40 | |
| 41 | for(i = range_start; i <= range_end; i++) { |
| 42 | checksum += nvram_read(i); |
| 43 | } |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 44 | |
| 45 | checksum_old = ((nvram_read(checksum_location)<<8) | nvram_read(checksum_location+1)); |
| 46 | |
| 47 | return (checksum_old == checksum); |
| 48 | } |
| 49 | |
Stefan Reinauer | 59a1b7f | 2010-08-17 10:14:50 +0000 | [diff] [blame] | 50 | void fix_options_checksum(void) |
| 51 | { |
| 52 | int i; |
| 53 | int range_start = lib_sysinfo.cmos_range_start / 8; |
| 54 | int range_end = lib_sysinfo.cmos_range_end / 8; |
| 55 | int checksum_location = lib_sysinfo.cmos_checksum_location / 8; |
| 56 | u16 checksum = 0; |
| 57 | |
| 58 | for(i = range_start; i <= range_end; i++) { |
| 59 | checksum += nvram_read(i); |
| 60 | } |
Stefan Reinauer | 59a1b7f | 2010-08-17 10:14:50 +0000 | [diff] [blame] | 61 | |
| 62 | nvram_write((checksum >> 8), checksum_location); |
| 63 | nvram_write((checksum & 0xff), checksum_location + 1); |
| 64 | } |
| 65 | |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 66 | static int get_cmos_value(u32 bitnum, u32 len, void *valptr) |
| 67 | { |
| 68 | u8 *value = (u8 *)valptr; |
| 69 | int offs = 0; |
| 70 | u32 addr, bit; |
| 71 | u8 reg8; |
| 72 | |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 73 | /* Convert to byte borders */ |
| 74 | addr=(bitnum / 8); |
| 75 | bit=(bitnum % 8); |
| 76 | |
| 77 | /* Handle single byte or less */ |
| 78 | if(len <= 8) { |
| 79 | reg8 = nvram_read(addr); |
| 80 | reg8 >>= bit; |
| 81 | value[0] = reg8 & ((1 << len) -1); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* When handling more than a byte, copy whole bytes */ |
| 86 | while (len > 0) { |
| 87 | len -= 8; |
| 88 | value[offs++]=nvram_read(addr++); |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Patrick Georgi | c230058 | 2011-07-12 11:40:29 +0200 | [diff] [blame] | 94 | int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 95 | { |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 96 | struct cb_cmos_entries *cmos_entry; |
| 97 | int len = strnlen(name, CMOS_MAX_NAME_LENGTH); |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 98 | |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 99 | /* cmos entries are located right after the option table */ |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 100 | |
| 101 | for ( cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length); |
| 102 | cmos_entry->tag == CB_TAG_OPTION; |
| 103 | cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size)) { |
Stefan Reinauer | 34b9f86 | 2008-08-19 17:51:30 +0000 | [diff] [blame] | 104 | if (memcmp((const char*)cmos_entry->name, name, len)) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 105 | continue; |
| 106 | if(get_cmos_value(cmos_entry->bit, cmos_entry->length, dest)) |
| 107 | return 1; |
| 108 | |
| 109 | if(!options_checksum_valid()) |
| 110 | return 1; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | printf("ERROR: No such CMOS option (%s)\n", name); |
| 116 | return 1; |
| 117 | } |
Patrick Georgi | c230058 | 2011-07-12 11:40:29 +0200 | [diff] [blame] | 118 | |
| 119 | int get_option(void *dest, char *name) |
| 120 | { |
| 121 | struct cb_cmos_option_table *option_table = phys_to_virt(lib_sysinfo.option_table); |
| 122 | return get_option_from(option_table, dest, name); |
| 123 | } |