| 1 |
|
| 2 |
|
| 3 |
#if defined(__FreeBSD__) |
| 4 |
#include <fcntl.h> |
| 5 |
#include <unistd.h> |
| 6 |
#endif |
| 7 |
|
| 8 |
#include "common.h" |
| 9 |
#include "cmos_lowlevel.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
extern cmos_access_t cmos_hal, memory_hal; |
| 14 |
static cmos_access_t *current_access = |
| 15 |
#ifdef CMOS_HAL |
| 16 |
&cmos_hal; |
| 17 |
#else |
| 18 |
&memory_hal; |
| 19 |
#endif |
| 20 |
|
| 21 |
void select_hal(hal_t hal, void *data) |
| 22 |
{ |
| 23 |
switch(hal) { |
| 24 |
#ifdef CMOS_HAL |
| 25 |
case HAL_CMOS: |
| 26 |
current_access = &cmos_hal; |
| 27 |
break; |
| 28 |
#endif |
| 29 |
case HAL_MEMORY: |
| 30 |
default: |
| 31 |
current_access = &memory_hal; |
| 32 |
break; |
| 33 |
} |
| 34 |
current_access->init(data); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
typedef struct { |
| 39 |
unsigned byte_index; |
| 40 |
unsigned bit_offset; |
| 41 |
} cmos_bit_op_location_t; |
| 42 |
|
| 43 |
static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 44 |
cmos_bit_op_location_t * where); |
| 45 |
static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 46 |
unsigned nr_bits); |
| 47 |
static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 48 |
unsigned nr_bits, unsigned char value); |
| 49 |
static unsigned char get_bits(unsigned long long value, unsigned bit, |
| 50 |
unsigned nr_bits); |
| 51 |
static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits, |
| 52 |
unsigned long long *result); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static inline unsigned char get_bits(unsigned long long value, unsigned bit, |
| 61 |
unsigned nr_bits) |
| 62 |
{ |
| 63 |
return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1)); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static inline void put_bits(unsigned char value, unsigned bit, |
| 75 |
unsigned nr_bits, unsigned long long *result) |
| 76 |
{ |
| 77 |
*result += ((unsigned long long)(value & |
| 78 |
((unsigned char)((1 << nr_bits) - 1)))) << bit; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
unsigned long long cmos_read(const cmos_entry_t * e) |
| 92 |
{ |
| 93 |
cmos_bit_op_location_t where; |
| 94 |
unsigned bit = e->bit, length = e->length; |
| 95 |
unsigned next_bit, bits_left, nr_bits; |
| 96 |
unsigned long long result = 0; |
| 97 |
unsigned char value; |
| 98 |
|
| 99 |
assert(!verify_cmos_op(bit, length, e->config)); |
| 100 |
|
| 101 |
if (e->config == CMOS_ENTRY_STRING) { |
| 102 |
int strsz = (length + 7) / 8 + 1; |
| 103 |
char *newstring = malloc(strsz); |
| 104 |
unsigned usize = (8 * sizeof(unsigned long long)); |
| 105 |
|
| 106 |
if (!newstring) { |
| 107 |
out_of_memory(); |
| 108 |
} |
| 109 |
|
| 110 |
memset(newstring, 0, strsz); |
| 111 |
|
| 112 |
for (next_bit = 0, bits_left = length; |
| 113 |
bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 114 |
nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 115 |
bits_left > usize ? usize : bits_left, &where); |
| 116 |
value = cmos_read_bits(&where, nr_bits); |
| 117 |
put_bits(value, next_bit % usize, nr_bits, |
| 118 |
&((unsigned long long *)newstring)[next_bit / |
| 119 |
usize]); |
| 120 |
result = (unsigned long)newstring; |
| 121 |
} |
| 122 |
} else { |
| 123 |
for (next_bit = 0, bits_left = length; |
| 124 |
bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 125 |
nr_bits = |
| 126 |
cmos_bit_op_strategy(bit + next_bit, bits_left, |
| 127 |
&where); |
| 128 |
value = cmos_read_bits(&where, nr_bits); |
| 129 |
put_bits(value, next_bit, nr_bits, &result); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
void cmos_write(const cmos_entry_t * e, unsigned long long value) |
| 144 |
{ |
| 145 |
cmos_bit_op_location_t where; |
| 146 |
unsigned bit = e->bit, length = e->length; |
| 147 |
unsigned next_bit, bits_left, nr_bits; |
| 148 |
|
| 149 |
assert(!verify_cmos_op(bit, length, e->config)); |
| 150 |
|
| 151 |
if (e->config == CMOS_ENTRY_STRING) { |
| 152 |
unsigned long long *data = |
| 153 |
(unsigned long long *)(unsigned long)value; |
| 154 |
unsigned usize = (8 * sizeof(unsigned long long)); |
| 155 |
|
| 156 |
for (next_bit = 0, bits_left = length; |
| 157 |
bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 158 |
nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 159 |
bits_left > usize ? usize : bits_left, |
| 160 |
&where); |
| 161 |
value = data[next_bit / usize]; |
| 162 |
cmos_write_bits(&where, nr_bits, |
| 163 |
get_bits(value, next_bit % usize, nr_bits)); |
| 164 |
} |
| 165 |
} else { |
| 166 |
for (next_bit = 0, bits_left = length; |
| 167 |
bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 168 |
nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 169 |
bits_left, &where); |
| 170 |
cmos_write_bits(&where, nr_bits, |
| 171 |
get_bits(value, next_bit, nr_bits)); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
unsigned char cmos_read_byte(unsigned index) |
| 187 |
{ |
| 188 |
return current_access->read(index); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
void cmos_write_byte(unsigned index, unsigned char value) |
| 202 |
{ |
| 203 |
current_access->write(index, value); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
void cmos_read_all(unsigned char data[]) |
| 213 |
{ |
| 214 |
unsigned i; |
| 215 |
|
| 216 |
for (i = 0; i < CMOS_RTC_AREA_SIZE; i++) |
| 217 |
data[i] = 0; |
| 218 |
|
| 219 |
for (; i < CMOS_SIZE; i++) |
| 220 |
data[i] = cmos_read_byte(i); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
void cmos_write_all(unsigned char data[]) |
| 231 |
{ |
| 232 |
unsigned i; |
| 233 |
|
| 234 |
for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++) |
| 235 |
cmos_write_byte(i, data[i]); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
void set_iopl(int level) |
| 248 |
{ |
| 249 |
current_access->set_iopl(level); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config) |
| 262 |
{ |
| 263 |
if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE))) |
| 264 |
return CMOS_AREA_OUT_OF_RANGE; |
| 265 |
|
| 266 |
if (bit < (8 * CMOS_RTC_AREA_SIZE)) |
| 267 |
return CMOS_AREA_OVERLAPS_RTC; |
| 268 |
|
| 269 |
if (config == CMOS_ENTRY_STRING) |
| 270 |
return OK; |
| 271 |
|
| 272 |
if (length > (8 * sizeof(unsigned long long))) |
| 273 |
return CMOS_AREA_TOO_WIDE; |
| 274 |
|
| 275 |
return OK; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 285 |
cmos_bit_op_location_t * where) |
| 286 |
{ |
| 287 |
unsigned max_bits; |
| 288 |
|
| 289 |
where->byte_index = bit >> 3; |
| 290 |
where->bit_offset = bit & 0x07; |
| 291 |
max_bits = 8 - where->bit_offset; |
| 292 |
return (bits_left > max_bits) ? max_bits : bits_left; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 302 |
unsigned nr_bits) |
| 303 |
{ |
| 304 |
return (cmos_read_byte(where->byte_index) >> where->bit_offset) & |
| 305 |
((unsigned char)((1 << nr_bits) - 1)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 315 |
unsigned nr_bits, unsigned char value) |
| 316 |
{ |
| 317 |
unsigned char n, mask; |
| 318 |
|
| 319 |
if (nr_bits == 8) { |
| 320 |
cmos_write_byte(where->byte_index, value); |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
n = cmos_read_byte(where->byte_index); |
| 325 |
mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset; |
| 326 |
n = (n & ~mask) + ((value << where->bit_offset) & mask); |
| 327 |
cmos_write_byte(where->byte_index, n); |
| 328 |
} |