1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "common.h"
32#include "layout.h"
33#include "cmos_lowlevel.h"
34
35typedef struct cmos_entry_item_t cmos_entry_item_t;
36
37struct cmos_entry_item_t {
38 cmos_entry_t item;
39 cmos_entry_item_t *next;
40};
41
42typedef struct cmos_enum_item_t cmos_enum_item_t;
43
44struct cmos_enum_item_t {
45 cmos_enum_t item;
46 cmos_enum_item_t *next;
47};
48
49static void default_cmos_layout_get_fn(void);
50static int areas_overlap(unsigned area_0_start, unsigned area_0_length,
51 unsigned area_1_start, unsigned area_1_length);
52static int entries_overlap(const cmos_entry_t * p, const cmos_entry_t * q);
53static const cmos_enum_item_t *find_first_cmos_enum_id(unsigned config_id);
54
55const char checksum_param_name[] = "check_sum";
56
57
58
59
60
61
62
63
64#define CMOS_CHECKSUM_START 49
65
66
67
68
69#define CMOS_CHECKSUM_END 125
70
71
72
73
74#define CMOS_CHECKSUM_INDEX 126
75
76
77unsigned cmos_checksum_start = CMOS_CHECKSUM_START;
78
79
80unsigned cmos_checksum_end = CMOS_CHECKSUM_END;
81
82
83unsigned cmos_checksum_index = CMOS_CHECKSUM_INDEX;
84
85
86
87
88static cmos_entry_item_t *cmos_entry_list = NULL;
89
90
91
92
93static cmos_enum_item_t *cmos_enum_list = NULL;
94
95static cmos_layout_get_fn_t cmos_layout_get_fn = default_cmos_layout_get_fn;
96
97
98
99
100
101
102static inline int entries_overlap(const cmos_entry_t * p,
103 const cmos_entry_t * q)
104{
105 return areas_overlap(p->bit, p->length, q->bit, q->length);
106}
107
108
109
110
111
112
113static inline const cmos_entry_item_t *cmos_entry_to_const_item
114 (const cmos_entry_t * p) {
115 static const cmos_entry_t *pos = &((cmos_entry_item_t *) 0)->item;
116 unsigned long offset, address;
117
118 offset = (unsigned long)pos;
119 address = ((unsigned long)p) - offset;
120 return (const cmos_entry_item_t *)address;
121}
122
123
124
125
126
127
128static inline const cmos_enum_item_t *cmos_enum_to_const_item
129 (const cmos_enum_t * p) {
130 static const cmos_enum_t *pos = &((cmos_enum_item_t *) 0)->item;
131 unsigned long offset, address;
132
133 offset = (unsigned long)pos;
134 address = ((unsigned long)p) - offset;
135 return (const cmos_enum_item_t *)address;
136}
137
138
139
140
141
142
143
144void register_cmos_layout_get_fn(cmos_layout_get_fn_t fn)
145{
146 cmos_layout_get_fn = fn;
147}
148
149
150
151
152
153
154void get_cmos_layout(void)
155{
156 cmos_layout_get_fn();
157}
158
159
160
161
162
163
164
165
166
167int add_cmos_entry(const cmos_entry_t * e, const cmos_entry_t ** conflict)
168{
169 cmos_entry_item_t *item, *prev, *new_entry;
170
171 *conflict = NULL;
172
173 if (e->length < 1)
174 return LAYOUT_ENTRY_BAD_LENGTH;
175
176 if ((new_entry =
177 (cmos_entry_item_t *) malloc(sizeof(*new_entry))) == NULL)
178 out_of_memory();
179
180 new_entry->item = *e;
181
182 if (cmos_entry_list == NULL) {
183 new_entry->next = NULL;
184 cmos_entry_list = new_entry;
185 return OK;
186 }
187
188
189
190
191 for (item = cmos_entry_list, prev = NULL;
192 (item != NULL) && (item->item.bit < e->bit);
193 prev = item, item = item->next) ;
194
195 if (prev == NULL) {
196 if (entries_overlap(e, &cmos_entry_list->item)) {
197 *conflict = &cmos_entry_list->item;
198 goto fail;
199 }
200
201 new_entry->next = cmos_entry_list;
202 cmos_entry_list = new_entry;
203 return OK;
204 }
205
206 if (entries_overlap(&prev->item, e)) {
207 *conflict = &prev->item;
208 goto fail;
209 }
210
211 if ((item != NULL) && entries_overlap(e, &item->item)) {
212 *conflict = &item->item;
213 goto fail;
214 }
215
216 new_entry->next = item;
217 prev->next = new_entry;
218 return OK;
219
220 fail:
221 free(new_entry);
222 return LAYOUT_ENTRY_OVERLAP;
223}
224
225
226
227
228
229
230
231const cmos_entry_t *find_cmos_entry(const char name[])
232{
233 cmos_entry_item_t *item;
234
235 for (item = cmos_entry_list; item != NULL; item = item->next) {
236 if (!strcmp(item->item.name, name))
237 return &item->item;
238 }
239
240 return NULL;
241}
242
243
244
245
246
247
248
249const cmos_entry_t *first_cmos_entry(void)
250{
251 return (cmos_entry_list == NULL) ? NULL : &cmos_entry_list->item;
252}
253
254
255
256
257
258
259
260const cmos_entry_t *next_cmos_entry(const cmos_entry_t * last)
261{
262 const cmos_entry_item_t *last_item, *next_item;
263
264 last_item = cmos_entry_to_const_item(last);
265 next_item = last_item->next;
266 return (next_item == NULL) ? NULL : &next_item->item;
267}
268
269
270
271
272
273
274
275int add_cmos_enum(const cmos_enum_t * e)
276{
277 cmos_enum_item_t *item, *prev, *new_enum;
278
279 if ((new_enum = (cmos_enum_item_t *) malloc(sizeof(*new_enum))) == NULL)
280 out_of_memory();
281
282 new_enum->item = *e;
283
284 if (cmos_enum_list == NULL) {
285 new_enum->next = NULL;
286 cmos_enum_list = new_enum;
287 return OK;
288 }
289
290
291
292
293
294 for (item = cmos_enum_list, prev = NULL;
295 (item != NULL) && (item->item.config_id < e->config_id);
296 prev = item, item = item->next) ;
297
298 if (item == NULL) {
299 new_enum->next = NULL;
300 prev->next = new_enum;
301 return OK;
302 }
303
304 if (item->item.config_id > e->config_id) {
305 new_enum->next = item;
306
307 if (prev == NULL)
308 cmos_enum_list = new_enum;
309 else
310 prev->next = new_enum;
311
312 return OK;
313 }
314
315
316
317
318
319 while (item->item.value < e->value) {
320 prev = item;
321 item = item->next;
322
323 if ((item == NULL) || (item->item.config_id != e->config_id)) {
324 new_enum->next = item;
325 prev->next = new_enum;
326 return OK;
327 }
328 }
329
330 if (item->item.value == e->value) {
331 free(new_enum);
332 return LAYOUT_DUPLICATE_ENUM;
333 }
334
335 new_enum->next = item;
336
337 if (prev == NULL)
338 cmos_enum_list = new_enum;
339 else
340 prev->next = new_enum;
341
342 return OK;
343}
344
345
346
347
348
349
350
351const cmos_enum_t *find_cmos_enum(unsigned config_id, unsigned long long value)
352{
353 const cmos_enum_item_t *item;
354
355 if ((item = find_first_cmos_enum_id(config_id)) == NULL)
356 return NULL;
357
358 while (item->item.value < value) {
359 item = item->next;
360
361 if ((item == NULL) || (item->item.config_id != config_id))
362 return NULL;
363 }
364
365 return (item->item.value == value) ? &item->item : NULL;
366}
367
368
369
370
371
372
373
374const cmos_enum_t *first_cmos_enum(void)
375{
376 return (cmos_enum_list == NULL) ? NULL : &cmos_enum_list->item;
377}
378
379
380
381
382
383
384
385const cmos_enum_t *next_cmos_enum(const cmos_enum_t * last)
386{
387 const cmos_enum_item_t *last_item, *next_item;
388
389 last_item = cmos_enum_to_const_item(last);
390 next_item = last_item->next;
391 return (next_item == NULL) ? NULL : &next_item->item;
392}
393
394
395
396
397
398
399
400const cmos_enum_t *first_cmos_enum_id(unsigned config_id)
401{
402 const cmos_enum_item_t *item;
403
404 item = find_first_cmos_enum_id(config_id);
405 return (item == NULL) ? NULL : &item->item;
406}
407
408
409
410
411
412
413
414const cmos_enum_t *next_cmos_enum_id(const cmos_enum_t * last)
415{
416 const cmos_enum_item_t *item;
417
418 item = cmos_enum_to_const_item(last)->next;
419 return ((item == NULL) || (item->item.config_id != last->config_id)) ?
420 NULL : &item->item;
421}
422
423
424
425
426
427
428
429int is_checksum_name(const char name[])
430{
431 return !strcmp(name, checksum_param_name);
432}
433
434
435
436
437
438
439
440
441
442int checksum_layout_to_bytes(cmos_checksum_layout_t * layout)
443{
444 unsigned start, end, index;
445
446 start = layout->summed_area_start;
447 end = layout->summed_area_end;
448 index = layout->checksum_at;
449
450 if (start % 8)
451 return LAYOUT_SUMMED_AREA_START_NOT_ALIGNED;
452
453 if ((end % 8) != 7)
454 return LAYOUT_SUMMED_AREA_END_NOT_ALIGNED;
455
456 if (index % 8)
457 return LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED;
458
459 if (end <= start)
460 return LAYOUT_INVALID_SUMMED_AREA;
461
462
463 start /= 8;
464 end /= 8;
465 index /= 8;
466
467 if (verify_cmos_byte_index(start) || verify_cmos_byte_index(end))
468 return LAYOUT_SUMMED_AREA_OUT_OF_RANGE;
469
470 if (verify_cmos_byte_index(index))
471 return LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE;
472
473
474 if (areas_overlap(start, end - start + 1, index, index + 1))
475 return LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA;
476
477 layout->summed_area_start = start;
478 layout->summed_area_end = end;
479 layout->checksum_at = index;
480 return OK;
481}
482
483
484
485
486
487
488
489void checksum_layout_to_bits(cmos_checksum_layout_t * layout)
490{
491 layout->summed_area_start *= 8;
492 layout->summed_area_end = (layout->summed_area_end * 8) + 7;
493 layout->checksum_at *= 8;
494}
495
496
497
498
499
500
501
502
503static void default_cmos_layout_get_fn(void)
504{
505 BUG();
506}
507
508
509
510
511
512
513static int areas_overlap(unsigned area_0_start, unsigned area_0_length,
514 unsigned area_1_start, unsigned area_1_length)
515{
516 unsigned area_0_end, area_1_end;
517
518 area_0_end = area_0_start + area_0_length - 1;
519 area_1_end = area_1_start + area_1_length - 1;
520 return ((area_1_start <= area_0_end) && (area_0_start <= area_1_end));
521}
522
523
524
525
526
527
528
529static const cmos_enum_item_t *find_first_cmos_enum_id(unsigned config_id)
530{
531 cmos_enum_item_t *item;
532
533 for (item = cmos_enum_list;
534 (item != NULL) && (item->item.config_id < config_id);
535 item = item->next) ;
536
537 return ((item == NULL) || (item->item.config_id > config_id)) ?
538 NULL : item;
539}
540