Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <string.h> |
| 8 | #include <errno.h> |
| 9 | #include <sys/mman.h> |
Adam Kaufman | 064b1f2 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 10 | #include "flash.h" |
Uwe Hermann | ce1041c | 2007-02-06 19:53:51 +0000 | [diff] [blame] | 11 | #include "linuxbios_tables.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 12 | #include "debug.h" |
| 13 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 14 | char *lb_part = NULL, *lb_vendor = NULL; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 15 | |
| 16 | static unsigned long compute_checksum(void *addr, unsigned long length) |
| 17 | { |
| 18 | uint8_t *ptr; |
| 19 | volatile union { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 20 | uint8_t byte[2]; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 21 | uint16_t word; |
| 22 | } value; |
| 23 | unsigned long sum; |
| 24 | unsigned long i; |
| 25 | /* In the most straight forward way possible, |
| 26 | * compute an ip style checksum. |
| 27 | */ |
| 28 | sum = 0; |
| 29 | ptr = addr; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 30 | for (i = 0; i < length; i++) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 31 | unsigned long value; |
| 32 | value = ptr[i]; |
| 33 | if (i & 1) { |
| 34 | value <<= 8; |
| 35 | } |
| 36 | /* Add the new value */ |
| 37 | sum += value; |
| 38 | /* Wrap around the carry */ |
| 39 | if (sum > 0xFFFF) { |
| 40 | sum = (sum + (sum >> 16)) & 0xFFFF; |
| 41 | } |
| 42 | } |
| 43 | value.byte[0] = sum & 0xff; |
| 44 | value.byte[1] = (sum >> 8) & 0xff; |
| 45 | return (~value.word) & 0xFFFF; |
| 46 | } |
| 47 | |
| 48 | #define for_each_lbrec(head, rec) \ |
| 49 | for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \ |
| 50 | (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \ |
| 51 | (rec->size >= 1) && \ |
| 52 | ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 53 | rec = (struct lb_record *)(((char *)rec) + rec->size)) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 54 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 55 | static int count_lb_records(struct lb_header *head) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 56 | { |
| 57 | struct lb_record *rec; |
| 58 | int count; |
| 59 | count = 0; |
| 60 | for_each_lbrec(head, rec) { |
| 61 | count++; |
| 62 | } |
| 63 | return count; |
| 64 | } |
| 65 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 66 | static struct lb_header *find_lb_table(void *base, unsigned long start, |
| 67 | unsigned long end) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned long addr; |
| 70 | /* For now be stupid.... */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 71 | for (addr = start; addr < end; addr += 16) { |
| 72 | struct lb_header *head = |
| 73 | (struct lb_header *)(((char *)base) + addr); |
| 74 | struct lb_record *recs = |
| 75 | (struct lb_record *)(((char *)base) + addr + sizeof(*head)); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 76 | if (memcmp(head->signature, "LBIO", 4) != 0) |
| 77 | continue; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 78 | printf_debug("Found canidate at: %08lx-%08lx\n", |
| 79 | addr, addr + head->table_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 80 | if (head->header_bytes != sizeof(*head)) { |
| 81 | fprintf(stderr, "Header bytes of %d are incorrect\n", |
| 82 | head->header_bytes); |
| 83 | continue; |
| 84 | } |
| 85 | if (count_lb_records(head) != head->table_entries) { |
| 86 | fprintf(stderr, "bad record count: %d\n", |
| 87 | head->table_entries); |
| 88 | continue; |
| 89 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 90 | if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 91 | fprintf(stderr, "bad header checksum\n"); |
| 92 | continue; |
| 93 | } |
| 94 | if (compute_checksum(recs, head->table_bytes) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 95 | != head->table_checksum) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 96 | fprintf(stderr, "bad table checksum: %04x\n", |
| 97 | head->table_checksum); |
| 98 | continue; |
| 99 | } |
| 100 | fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr); |
| 101 | return head; |
| 102 | |
| 103 | }; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static void find_mainboard(struct lb_record *ptr, unsigned long addr) |
| 108 | { |
| 109 | struct lb_mainboard *rec; |
| 110 | int max_size; |
| 111 | char vendor[256], part[256]; |
| 112 | rec = (struct lb_mainboard *)ptr; |
| 113 | max_size = rec->size - sizeof(*rec); |
| 114 | printf("vendor id: %.*s part id: %.*s\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 115 | max_size - rec->vendor_idx, |
| 116 | rec->strings + rec->vendor_idx, |
| 117 | max_size - rec->part_number_idx, |
| 118 | rec->strings + rec->part_number_idx); |
| 119 | snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, |
| 120 | rec->strings + rec->vendor_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 121 | snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 122 | rec->strings + rec->part_number_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 123 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 124 | if (lb_part) { |
| 125 | printf("overwritten by command line, vendor id: %s part id: %s\n", lb_vendor, lb_part); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 126 | } else { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 127 | lb_part = strdup(part); |
| 128 | lb_vendor = strdup(vendor); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | static struct lb_record *next_record(struct lb_record *rec) |
| 133 | { |
| 134 | return (struct lb_record *)(((char *)rec) + rec->size); |
| 135 | } |
| 136 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 137 | static void search_lb_records(struct lb_record *rec, struct lb_record *last, |
| 138 | unsigned long addr) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 139 | { |
| 140 | struct lb_record *next; |
| 141 | int count; |
| 142 | count = 0; |
| 143 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 144 | for (next = next_record(rec); (rec < last) && (next <= last); |
| 145 | rec = next, addr += rec->size) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 146 | next = next_record(rec); |
| 147 | count++; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 148 | if (rec->tag == LB_TAG_MAINBOARD) { |
| 149 | find_mainboard(rec, addr); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 155 | int linuxbios_init(void) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 156 | { |
| 157 | uint8_t *low_1MB; |
| 158 | struct lb_header *lb_table; |
| 159 | struct lb_record *rec, *last; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 160 | |
| 161 | low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem, |
| 162 | 0x00000000); |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 163 | if (low_1MB == MAP_FAILED) { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 164 | fprintf(stderr, |
| 165 | "Can not mmap " MEM_DEV " at %08lx errno(%d):%s\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 166 | 0x00000000UL, errno, strerror(errno)); |
| 167 | exit(-2); |
| 168 | } |
| 169 | lb_table = 0; |
| 170 | if (!lb_table) |
| 171 | lb_table = find_lb_table(low_1MB, 0x00000, 0x1000); |
| 172 | if (!lb_table) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 173 | lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 174 | if (lb_table) { |
| 175 | unsigned long addr; |
| 176 | addr = ((char *)lb_table) - ((char *)low_1MB); |
| 177 | printf_debug("lb_table found at address %p\n", lb_table); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 178 | rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 179 | last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes); |
| 180 | printf_debug("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 181 | lb_table->header_bytes, lb_table->header_checksum, |
| 182 | lb_table->table_bytes, lb_table->table_checksum, |
| 183 | lb_table->table_entries); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 184 | search_lb_records(rec, last, addr + lb_table->header_bytes); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame^] | 185 | } else { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 186 | printf("No LinuxBIOS table found.\n"); |
| 187 | return -1; |
| 188 | } |
| 189 | return 0; |
| 190 | } |