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) |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 7 | * Copyright (C) 2006-2009 coresystems GmbH |
Stefan Reinauer | 5380d51 | 2007-05-24 09:08:36 +0000 | [diff] [blame] | 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 <stdlib.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <string.h> |
Adam Kaufman | 064b1f2 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 27 | #include "flash.h" |
Stefan Reinauer | 2fbe624 | 2008-01-18 16:17:44 +0000 | [diff] [blame] | 28 | #include "coreboot_tables.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 29 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 30 | char *lb_part = NULL, *lb_vendor = NULL; |
Carl-Daniel Hailfinger | bc25f94 | 2009-07-30 13:30:17 +0000 | [diff] [blame] | 31 | int partvendor_from_cbtable = 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 32 | |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 33 | void lb_vendor_dev_from_string(char *boardstring) |
| 34 | { |
| 35 | char *tempstr2 = NULL; |
| 36 | strtok(boardstring, ":"); |
| 37 | tempstr2 = strtok(NULL, ":"); |
| 38 | if (tempstr2) { |
| 39 | lb_vendor = boardstring; |
| 40 | lb_part = tempstr2; |
| 41 | } else { |
| 42 | lb_vendor = NULL; |
| 43 | lb_part = boardstring; |
| 44 | } |
| 45 | } |
| 46 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 47 | static unsigned long compute_checksum(void *addr, unsigned long length) |
| 48 | { |
| 49 | uint8_t *ptr; |
| 50 | volatile union { |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 51 | uint8_t byte[2]; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 52 | uint16_t word; |
Michael Karcher | e7f3209 | 2010-01-12 15:36:24 +0000 | [diff] [blame] | 53 | } chksum; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 54 | unsigned long sum; |
| 55 | unsigned long i; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 56 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 57 | /* In the most straight forward way possible, |
| 58 | * compute an ip style checksum. |
| 59 | */ |
| 60 | sum = 0; |
| 61 | ptr = addr; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 62 | for (i = 0; i < length; i++) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 63 | unsigned long value; |
| 64 | value = ptr[i]; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 65 | if (i & 1) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 66 | value <<= 8; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 67 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 68 | /* Add the new value */ |
| 69 | sum += value; |
| 70 | /* Wrap around the carry */ |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 71 | if (sum > 0xFFFF) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 72 | sum = (sum + (sum >> 16)) & 0xFFFF; |
Uwe Hermann | ac30934 | 2007-10-10 17:42:20 +0000 | [diff] [blame] | 73 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 74 | } |
Michael Karcher | e7f3209 | 2010-01-12 15:36:24 +0000 | [diff] [blame] | 75 | chksum.byte[0] = sum & 0xff; |
| 76 | chksum.byte[1] = (sum >> 8) & 0xff; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 77 | |
Michael Karcher | e7f3209 | 2010-01-12 15:36:24 +0000 | [diff] [blame] | 78 | return (~chksum.word) & 0xFFFF; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #define for_each_lbrec(head, rec) \ |
| 82 | for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \ |
| 83 | (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \ |
| 84 | (rec->size >= 1) && \ |
| 85 | ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 86 | rec = (struct lb_record *)(((char *)rec) + rec->size)) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 87 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 88 | static int count_lb_records(struct lb_header *head) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 89 | { |
| 90 | struct lb_record *rec; |
| 91 | int count; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 92 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 93 | count = 0; |
| 94 | for_each_lbrec(head, rec) { |
| 95 | count++; |
| 96 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 97 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 98 | return count; |
| 99 | } |
| 100 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 101 | static struct lb_header *find_lb_table(void *base, unsigned long start, |
| 102 | unsigned long end) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 103 | { |
| 104 | unsigned long addr; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 105 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 106 | /* For now be stupid.... */ |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 107 | for (addr = start; addr < end; addr += 16) { |
| 108 | struct lb_header *head = |
| 109 | (struct lb_header *)(((char *)base) + addr); |
| 110 | struct lb_record *recs = |
| 111 | (struct lb_record *)(((char *)base) + addr + sizeof(*head)); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 112 | if (memcmp(head->signature, "LBIO", 4) != 0) |
| 113 | continue; |
Daniel McLellan | d02b73f | 2009-05-14 12:41:00 +0000 | [diff] [blame] | 114 | printf_debug("Found candidate at: %08lx-%08lx\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 115 | addr, addr + head->table_bytes); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 116 | if (head->header_bytes != sizeof(*head)) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 117 | fprintf(stderr, "Header bytes of %d are incorrect.\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 118 | head->header_bytes); |
| 119 | continue; |
| 120 | } |
| 121 | if (count_lb_records(head) != head->table_entries) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 122 | fprintf(stderr, "Bad record count: %d.\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 123 | head->table_entries); |
| 124 | continue; |
| 125 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 126 | if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 127 | fprintf(stderr, "Bad header checksum.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | if (compute_checksum(recs, head->table_bytes) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 131 | != head->table_checksum) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 132 | fprintf(stderr, "Bad table checksum: %04x.\n", |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 133 | head->table_checksum); |
| 134 | continue; |
| 135 | } |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 136 | printf_debug("Found coreboot table at 0x%08lx.\n", addr); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 137 | return head; |
| 138 | |
| 139 | }; |
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 | return 0; |
| 142 | } |
| 143 | |
| 144 | static void find_mainboard(struct lb_record *ptr, unsigned long addr) |
| 145 | { |
| 146 | struct lb_mainboard *rec; |
| 147 | int max_size; |
| 148 | char vendor[256], part[256]; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 149 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 150 | rec = (struct lb_mainboard *)ptr; |
| 151 | max_size = rec->size - sizeof(*rec); |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 152 | printf("Vendor ID: %.*s, part ID: %.*s\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 153 | max_size - rec->vendor_idx, |
| 154 | rec->strings + rec->vendor_idx, |
| 155 | max_size - rec->part_number_idx, |
| 156 | rec->strings + rec->part_number_idx); |
| 157 | snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, |
| 158 | rec->strings + rec->vendor_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 159 | snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 160 | rec->strings + rec->part_number_idx); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 161 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 162 | if (lb_part) { |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 163 | 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] | 164 | } else { |
Carl-Daniel Hailfinger | bc25f94 | 2009-07-30 13:30:17 +0000 | [diff] [blame] | 165 | partvendor_from_cbtable = 1; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 166 | lb_part = strdup(part); |
| 167 | lb_vendor = strdup(vendor); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | static struct lb_record *next_record(struct lb_record *rec) |
| 172 | { |
| 173 | return (struct lb_record *)(((char *)rec) + rec->size); |
| 174 | } |
| 175 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 176 | static void search_lb_records(struct lb_record *rec, struct lb_record *last, |
| 177 | unsigned long addr) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 178 | { |
| 179 | struct lb_record *next; |
| 180 | int count; |
| 181 | count = 0; |
| 182 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 183 | for (next = next_record(rec); (rec < last) && (next <= last); |
| 184 | rec = next, addr += rec->size) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 185 | next = next_record(rec); |
| 186 | count++; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 187 | if (rec->tag == LB_TAG_MAINBOARD) { |
| 188 | find_mainboard(rec, addr); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 194 | #define BYTES_TO_MAP (1024*1024) |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 195 | int coreboot_init(void) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 196 | { |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 197 | uint8_t *table_area; |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 198 | unsigned long addr, start; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 199 | struct lb_header *lb_table; |
| 200 | struct lb_record *rec, *last; |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 201 | |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 202 | #ifdef __DARWIN__ |
| 203 | /* This is a hack. DirectIO fails to map physical address 0x00000000. |
| 204 | * Why? |
| 205 | */ |
| 206 | start = 0x400; |
| 207 | #else |
| 208 | start = 0x0; |
| 209 | #endif |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 210 | table_area = physmap("low megabyte", start, BYTES_TO_MAP); |
Stefan Reinauer | f79edb9 | 2009-01-26 01:23:31 +0000 | [diff] [blame] | 211 | |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 212 | lb_table = find_lb_table(table_area, 0x00000, 0x1000); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 213 | if (!lb_table) |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 214 | lb_table = find_lb_table(table_area, 0xf0000, BYTES_TO_MAP); |
| 215 | if (lb_table) { |
| 216 | struct lb_forward *forward = (struct lb_forward *) |
| 217 | (((char *)lb_table) + lb_table->header_bytes); |
| 218 | if (forward->tag == LB_TAG_FORWARD) { |
| 219 | start = forward->forward; |
Uwe Hermann | 7b2969b | 2009-04-15 10:52:49 +0000 | [diff] [blame] | 220 | start &= ~(getpagesize() - 1); |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 221 | physunmap(table_area, BYTES_TO_MAP); |
| 222 | table_area = physmap("high tables", start, BYTES_TO_MAP); |
| 223 | lb_table = find_lb_table(table_area, 0x00000, 0x1000); |
| 224 | } |
| 225 | } |
| 226 | |
Peter Stuge | 2dc3aaa | 2009-01-26 00:15:56 +0000 | [diff] [blame] | 227 | if (!lb_table) { |
Stefan Reinauer | e3f3e2e | 2008-01-18 15:33:10 +0000 | [diff] [blame] | 228 | printf("No coreboot table found.\n"); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 229 | return -1; |
| 230 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 231 | |
Stefan Reinauer | 2d853bb | 2009-03-17 14:39:25 +0000 | [diff] [blame] | 232 | addr = ((char *)lb_table) - ((char *)table_area) + start; |
| 233 | fprintf(stdout, "coreboot table found at 0x%lx.\n", |
| 234 | (unsigned long)lb_table - (unsigned long)table_area + start); |
Peter Stuge | 2dc3aaa | 2009-01-26 00:15:56 +0000 | [diff] [blame] | 235 | rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes); |
| 236 | last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes); |
Peter Stuge | a0d75a0 | 2009-01-26 00:19:36 +0000 | [diff] [blame] | 237 | printf_debug("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n", |
Peter Stuge | 2dc3aaa | 2009-01-26 00:15:56 +0000 | [diff] [blame] | 238 | lb_table->header_bytes, lb_table->header_checksum, |
| 239 | lb_table->table_bytes, lb_table->table_checksum, |
| 240 | lb_table->table_entries); |
| 241 | search_lb_records(rec, last, addr + lb_table->header_bytes); |
| 242 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 243 | return 0; |
| 244 | } |