libpayload: Refactor highlevel CMOS access
[coreboot.git] / payloads / libpayload / drivers / options.c
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 struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
34         nvram_read,
35         nvram_write
36 };
37
38 struct cb_cmos_option_table *get_system_option_table(void)
39 {
40         return phys_to_virt(lib_sysinfo.option_table);
41 }
42
43 static int options_checksum_valid(const struct nvram_accessor *nvram)
44 {
45         int i;
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;
50
51         for(i = range_start; i <= range_end; i++) {
52                 checksum += nvram->read(i);
53         }
54
55         checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1));
56
57         return (checksum_old == checksum);
58 }
59
60 void fix_options_checksum_with(const struct nvram_accessor *nvram)
61 {
62         int i;
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;
67
68         for(i = range_start; i <= range_end; i++) {
69                 checksum += nvram->read(i);
70         }
71
72         nvram->write((checksum >> 8), checksum_location);
73         nvram->write((checksum & 0xff), checksum_location + 1);
74 }
75
76 void fix_options_checksum(void)
77 {
78         fix_options_checksum_with(use_nvram);
79 }
80
81 static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
82 {
83         u8 *value = (u8 *)valptr;
84         int offs = 0;
85         u32 addr, bit;
86         u8 reg8;
87
88         /* Convert to byte borders */
89         addr=(bitnum / 8);
90         bit=(bitnum % 8);
91
92         /* Handle single byte or less */
93         if(len <= 8) {
94                 reg8 = nvram->read(addr);
95                 reg8 >>= bit;
96                 value[0] = reg8 & ((1 << len) -1);
97                 return 0;
98         }
99
100         /* When handling more than a byte, copy whole bytes */
101         while (len > 0) {
102                 len -= 8;
103                 value[offs++]=nvram->read(addr++);
104         }
105
106         return 0;
107 }
108
109 static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
110 {
111         u8 *value = (u8 *)valptr;
112         int offs = 0;
113         u32 addr, bit;
114         u8 reg8;
115
116         /* Convert to byte borders */
117         addr=(bitnum / 8);
118         bit=(bitnum % 8);
119
120         /* Handle single byte or less */
121         if (len <= 8) {
122                 reg8 = nvram->read(addr);
123                 reg8 &= ~(((1 << len) - 1) << bit);
124                 reg8 |= (value[0] & ((1 << len) - 1)) << bit;
125                 nvram->write(reg8, addr);
126                 return 0;
127         }
128
129         /* When handling more than a byte, copy whole bytes */
130         while (len > 0) {
131                 len -= 8;
132                 nvram->write(value[offs++], addr++);
133         }
134
135         return 0;
136 }
137
138 static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *option_table, char *name)
139 {
140         struct cb_cmos_entries *cmos_entry;
141         int len = strnlen(name, CMOS_MAX_NAME_LENGTH);
142
143         /* cmos entries are located right after the option table */
144
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))
149                         continue;
150                 return cmos_entry;
151         }
152
153         printf("ERROR: No such CMOS option (%s)\n", name);
154         return NULL;
155 }
156
157 int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, char *name)
158 {
159         struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
160         if (cmos_entry) {
161                 if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest))
162                         return 1;
163
164                 if(!options_checksum_valid(nvram))
165                         return 1;
166
167                 return 0;
168         }
169         return 1;
170 }
171
172 int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name)
173 {
174         return get_option_with(use_nvram, option_table, dest, name);
175 }
176
177 int get_option(void *dest, char *name)
178 {
179         return get_option_from(get_system_option_table(), dest, name);
180 }
181
182 int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *value, char *name)
183 {
184         struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
185         if (cmos_entry) {
186                 set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value);
187                 fix_options_checksum_with(nvram);
188                 return 0;
189         }
190         return 1;
191 }
192
193 int set_option(void *value, char *name)
194 {
195         return set_option_with(use_nvram, get_system_option_table(), value, name);
196 }