Stefan Reinauer | 5380d51 | 2007-05-24 09:08:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * lbtable.c: LinuxBIOS table handling |
| 3 | * |
| 4 | * Copyright (C) 2002 Steven James <pyro@linuxlabs.com> |
| 5 | * Copyright (C) 2002 Linux Networx |
| 6 | * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) |
| 7 | * Copyright (C) 2006-2007 coresystems GmbH |
| 8 | * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 22 | */ |
| 23 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | #include <sys/mman.h> |
Adam Kaufman | 064b1f2 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 33 | #include "flash.h" |
Uwe Hermann | ce1041c | 2007-02-06 19:53:51 +0000 | [diff] [blame] | 34 | #include "linuxbios_tables.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 35 | #include "debug.h" |
| 36 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 37 | char *lb_part = NULL, *lb_vendor = NULL; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 38 | |
| 39 | static unsigned long compute_checksum(void *addr, unsigned long length) |
| 40 | { |
| 41 | uint8_t *ptr; |
| 42 | volatile union { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 43 | uint8_t byte[2]; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 44 | uint16_t word; |
| 45 | } value; |
| 46 | unsigned long sum; |
| 47 | unsigned long i; |
| 48 | /* In the most straight forward way possible, |
| 49 | * compute an ip style checksum. |
| 50 | */ |
| 51 | sum = 0; |
| 52 | ptr = addr; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 53 | for (i = 0; i < length; i++) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 54 | unsigned long value; |
| 55 | value = ptr[i]; |
| 56 | if (i & 1) { |
| 57 | value <<= 8; |
| 58 | } |
| 59 | /* Add the new value */ |
| 60 | sum += value; |
| 61 | /* Wrap around the carry */ |
| 62 | if (sum > 0xFFFF) { |
| 63 | sum = (sum + (sum >> 16)) & 0xFFFF; |
| 64 | } |
| 65 | } |
| 66 | value.byte[0] = sum & 0xff; |
| 67 | value.byte[1] = (sum >> 8) & 0xff; |
| 68 | return (~value.word) & 0xFFFF; |
| 69 | } |
| 70 | |
| 71 | #define for_each_lbrec(head, rec) \ |
| 72 | for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \ |
| 73 | (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \ |
| 74 | (rec->size >= 1) && \ |
| 75 | ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 76 | rec = (struct lb_record *)(((char *)rec) + rec->size)) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 77 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 78 | static int count_lb_records(struct lb_header *head) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 79 | { |
| 80 | struct lb_record *rec; |
| 81 | int count; |
| 82 | count = 0; |
| 83 | for_each_lbrec(head, rec) { |
| 84 | count++; |
| 85 | } |
| 86 | return count; |
| 87 | } |
| 88 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 89 | static struct lb_header *find_lb_table(void *base, unsigned long start, |
| 90 | unsigned long end) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 91 | { |
| 92 | unsigned long addr; |
| 93 | /* For now be stupid.... */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 94 | for (addr = start; addr < end; addr += 16) { |
| 95 | struct lb_header *head = |
| 96 | (struct lb_header *)(((char *)base) + addr); |
| 97 | struct lb_record *recs = |
| 98 | (struct lb_record *)(((char *)base) + addr + sizeof(*head)); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 99 | if (memcmp(head->signature, "LBIO", 4) != 0) |
| 100 | continue; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 101 | printf_debug("Found canidate at: %08lx-%08lx\n", |
| 102 | addr, addr + head->table_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 103 | if (head->header_bytes != sizeof(*head)) { |
| 104 | fprintf(stderr, "Header bytes of %d are incorrect\n", |
| 105 | head->header_bytes); |
| 106 | continue; |
| 107 | } |
| 108 | if (count_lb_records(head) != head->table_entries) { |
| 109 | fprintf(stderr, "bad record count: %d\n", |
| 110 | head->table_entries); |
| 111 | continue; |
| 112 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 113 | if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 114 | fprintf(stderr, "bad header checksum\n"); |
| 115 | continue; |
| 116 | } |
| 117 | if (compute_checksum(recs, head->table_bytes) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 118 | != head->table_checksum) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 119 | fprintf(stderr, "bad table checksum: %04x\n", |
| 120 | head->table_checksum); |
| 121 | continue; |
| 122 | } |
| 123 | fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr); |
| 124 | return head; |
| 125 | |
| 126 | }; |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static void find_mainboard(struct lb_record *ptr, unsigned long addr) |
| 131 | { |
| 132 | struct lb_mainboard *rec; |
| 133 | int max_size; |
| 134 | char vendor[256], part[256]; |
| 135 | rec = (struct lb_mainboard *)ptr; |
| 136 | max_size = rec->size - sizeof(*rec); |
| 137 | printf("vendor id: %.*s part id: %.*s\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 138 | max_size - rec->vendor_idx, |
| 139 | rec->strings + rec->vendor_idx, |
| 140 | max_size - rec->part_number_idx, |
| 141 | rec->strings + rec->part_number_idx); |
| 142 | snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, |
| 143 | rec->strings + rec->vendor_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 144 | snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 145 | rec->strings + rec->part_number_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 146 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 147 | if (lb_part) { |
| 148 | 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] | 149 | } else { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 150 | lb_part = strdup(part); |
| 151 | lb_vendor = strdup(vendor); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | static struct lb_record *next_record(struct lb_record *rec) |
| 156 | { |
| 157 | return (struct lb_record *)(((char *)rec) + rec->size); |
| 158 | } |
| 159 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 160 | static void search_lb_records(struct lb_record *rec, struct lb_record *last, |
| 161 | unsigned long addr) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 162 | { |
| 163 | struct lb_record *next; |
| 164 | int count; |
| 165 | count = 0; |
| 166 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 167 | for (next = next_record(rec); (rec < last) && (next <= last); |
| 168 | rec = next, addr += rec->size) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 169 | next = next_record(rec); |
| 170 | count++; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 171 | if (rec->tag == LB_TAG_MAINBOARD) { |
| 172 | find_mainboard(rec, addr); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 178 | int linuxbios_init(void) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 179 | { |
| 180 | uint8_t *low_1MB; |
| 181 | struct lb_header *lb_table; |
| 182 | struct lb_record *rec, *last; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 183 | |
| 184 | low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem, |
| 185 | 0x00000000); |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 186 | if (low_1MB == MAP_FAILED) { |
Stefan Reinauer | 7c1402f | 2007-05-23 18:24:58 +0000 | [diff] [blame] | 187 | perror("Can't mmap memory using " MEM_DEV); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 188 | exit(-2); |
| 189 | } |
| 190 | lb_table = 0; |
| 191 | if (!lb_table) |
| 192 | lb_table = find_lb_table(low_1MB, 0x00000, 0x1000); |
| 193 | if (!lb_table) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 194 | lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 195 | if (lb_table) { |
| 196 | unsigned long addr; |
| 197 | addr = ((char *)lb_table) - ((char *)low_1MB); |
| 198 | printf_debug("lb_table found at address %p\n", lb_table); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 199 | rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 200 | last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes); |
| 201 | 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] | 202 | lb_table->header_bytes, lb_table->header_checksum, |
| 203 | lb_table->table_bytes, lb_table->table_checksum, |
| 204 | lb_table->table_entries); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 205 | search_lb_records(rec, last, addr + lb_table->header_bytes); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 206 | } else { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 207 | printf("No LinuxBIOS table found.\n"); |
| 208 | return -1; |
| 209 | } |
| 210 | return 0; |
| 211 | } |