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