Stefan Reinauer | 5380d51 | 2007-05-24 09:08:36 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Stefan Reinauer | 5380d51 | 2007-05-24 09:08:36 +0000 | [diff] [blame] | 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 |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | 5380d51 | 2007-05-24 09:08:36 +0000 | [diff] [blame] | 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" |
Stefan Reinauer | 2fbe624 | 2008-01-18 16:17:44 +0000 | [diff] [blame^] | 34 | #include "coreboot_tables.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 35 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 36 | char *lb_part = NULL, *lb_vendor = NULL; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 37 | |
| 38 | static unsigned long compute_checksum(void *addr, unsigned long length) |
| 39 | { |
| 40 | uint8_t *ptr; |
| 41 | volatile union { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 42 | uint8_t byte[2]; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 43 | uint16_t word; |
| 44 | } value; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 45 | unsigned long sum; |
| 46 | unsigned long i; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 47 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 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]; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 56 | if (i & 1) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 57 | value <<= 8; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 58 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 59 | /* Add the new value */ |
| 60 | sum += value; |
| 61 | /* Wrap around the carry */ |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 62 | if (sum > 0xFFFF) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 63 | sum = (sum + (sum >> 16)) & 0xFFFF; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 64 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 65 | } |
| 66 | value.byte[0] = sum & 0xff; |
| 67 | value.byte[1] = (sum >> 8) & 0xff; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 68 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 69 | return (~value.word) & 0xFFFF; |
| 70 | } |
| 71 | |
| 72 | #define for_each_lbrec(head, rec) \ |
| 73 | for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \ |
| 74 | (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \ |
| 75 | (rec->size >= 1) && \ |
| 76 | ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 77 | rec = (struct lb_record *)(((char *)rec) + rec->size)) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 78 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 79 | static int count_lb_records(struct lb_header *head) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 80 | { |
| 81 | struct lb_record *rec; |
| 82 | int count; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 83 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 84 | count = 0; |
| 85 | for_each_lbrec(head, rec) { |
| 86 | count++; |
| 87 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 88 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 89 | return count; |
| 90 | } |
| 91 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 92 | static struct lb_header *find_lb_table(void *base, unsigned long start, |
| 93 | unsigned long end) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 94 | { |
| 95 | unsigned long addr; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 96 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 97 | /* For now be stupid.... */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 98 | for (addr = start; addr < end; addr += 16) { |
| 99 | struct lb_header *head = |
| 100 | (struct lb_header *)(((char *)base) + addr); |
| 101 | struct lb_record *recs = |
| 102 | (struct lb_record *)(((char *)base) + addr + sizeof(*head)); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 103 | if (memcmp(head->signature, "LBIO", 4) != 0) |
| 104 | continue; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 105 | printf_debug("Found canidate at: %08lx-%08lx\n", |
| 106 | addr, addr + head->table_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 107 | if (head->header_bytes != sizeof(*head)) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 108 | fprintf(stderr, "Header bytes of %d are incorrect.\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 109 | head->header_bytes); |
| 110 | continue; |
| 111 | } |
| 112 | if (count_lb_records(head) != head->table_entries) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 113 | fprintf(stderr, "Bad record count: %d.\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 114 | head->table_entries); |
| 115 | continue; |
| 116 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 117 | if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 118 | fprintf(stderr, "Bad header checksum.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 119 | continue; |
| 120 | } |
| 121 | if (compute_checksum(recs, head->table_bytes) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 122 | != head->table_checksum) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 123 | fprintf(stderr, "Bad table checksum: %04x.\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 124 | head->table_checksum); |
| 125 | continue; |
| 126 | } |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 127 | fprintf(stdout, "Found coreboot table at 0x%08lx.\n", addr); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 128 | return head; |
| 129 | |
| 130 | }; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 131 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static void find_mainboard(struct lb_record *ptr, unsigned long addr) |
| 136 | { |
| 137 | struct lb_mainboard *rec; |
| 138 | int max_size; |
| 139 | char vendor[256], part[256]; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 140 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 141 | rec = (struct lb_mainboard *)ptr; |
| 142 | max_size = rec->size - sizeof(*rec); |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 143 | printf("Vendor ID: %.*s, part ID: %.*s\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 144 | max_size - rec->vendor_idx, |
| 145 | rec->strings + rec->vendor_idx, |
| 146 | max_size - rec->part_number_idx, |
| 147 | rec->strings + rec->part_number_idx); |
| 148 | snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, |
| 149 | rec->strings + rec->vendor_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 150 | snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 151 | rec->strings + rec->part_number_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 152 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 153 | if (lb_part) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 154 | 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] | 155 | } else { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 156 | lb_part = strdup(part); |
| 157 | lb_vendor = strdup(vendor); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | static struct lb_record *next_record(struct lb_record *rec) |
| 162 | { |
| 163 | return (struct lb_record *)(((char *)rec) + rec->size); |
| 164 | } |
| 165 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 166 | static void search_lb_records(struct lb_record *rec, struct lb_record *last, |
| 167 | unsigned long addr) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 168 | { |
| 169 | struct lb_record *next; |
| 170 | int count; |
| 171 | count = 0; |
| 172 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 173 | for (next = next_record(rec); (rec < last) && (next <= last); |
| 174 | rec = next, addr += rec->size) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 175 | next = next_record(rec); |
| 176 | count++; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 177 | if (rec->tag == LB_TAG_MAINBOARD) { |
| 178 | find_mainboard(rec, addr); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 184 | int coreboot_init(void) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 185 | { |
| 186 | uint8_t *low_1MB; |
| 187 | struct lb_header *lb_table; |
| 188 | struct lb_record *rec, *last; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 189 | |
| 190 | low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem, |
| 191 | 0x00000000); |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 192 | if (low_1MB == MAP_FAILED) { |
Stefan Reinauer | 7c1402f | 2007-05-23 18:24:58 +0000 | [diff] [blame] | 193 | perror("Can't mmap memory using " MEM_DEV); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 194 | exit(-2); |
| 195 | } |
| 196 | lb_table = 0; |
| 197 | if (!lb_table) |
| 198 | lb_table = find_lb_table(low_1MB, 0x00000, 0x1000); |
| 199 | if (!lb_table) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 200 | lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 201 | if (lb_table) { |
| 202 | unsigned long addr; |
| 203 | addr = ((char *)lb_table) - ((char *)low_1MB); |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 204 | printf_debug("Coreboot table found at %p.\n", lb_table); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 205 | rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 206 | last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes); |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 207 | printf_debug("Coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 208 | lb_table->header_bytes, lb_table->header_checksum, |
| 209 | lb_table->table_bytes, lb_table->table_checksum, |
| 210 | lb_table->table_entries); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 211 | search_lb_records(rec, last, addr + lb_table->header_bytes); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 212 | } else { |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 213 | printf("No coreboot table found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 214 | return -1; |
| 215 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 216 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 217 | return 0; |
| 218 | } |