| 1 | /* |
|---|
| 2 | * This file is part of the flashrom project. |
|---|
| 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-2009 coresystems GmbH |
|---|
| 8 | * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) |
|---|
| 9 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
|---|
| 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 |
|---|
| 22 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 23 | */ |
|---|
| 24 | |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | #include <stdio.h> |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include "flash.h" |
|---|
| 30 | #include "programmer.h" |
|---|
| 31 | #include "coreboot_tables.h" |
|---|
| 32 | |
|---|
| 33 | char *lb_part = NULL, *lb_vendor = NULL; |
|---|
| 34 | int partvendor_from_cbtable = 0; |
|---|
| 35 | |
|---|
| 36 | /* Parse the [<vendor>:]<board> string specified by the user as part of |
|---|
| 37 | * -p internal:mainboard=[<vendor>:]<board> and set lb_vendor and lb_part |
|---|
| 38 | * to the extracted values. |
|---|
| 39 | * Note: strtok modifies the original string, so we work on a copy and allocate |
|---|
| 40 | * memory for lb_vendor and lb_part with strdup. |
|---|
| 41 | */ |
|---|
| 42 | void lb_vendor_dev_from_string(const char *boardstring) |
|---|
| 43 | { |
|---|
| 44 | /* strtok may modify the original string. */ |
|---|
| 45 | char *tempstr = strdup(boardstring); |
|---|
| 46 | char *tempstr2 = NULL; |
|---|
| 47 | strtok(tempstr, ":"); |
|---|
| 48 | tempstr2 = strtok(NULL, ":"); |
|---|
| 49 | if (tempstr2) { |
|---|
| 50 | lb_vendor = strdup(tempstr); |
|---|
| 51 | lb_part = strdup(tempstr2); |
|---|
| 52 | } else { |
|---|
| 53 | lb_vendor = NULL; |
|---|
| 54 | lb_part = strdup(tempstr); |
|---|
| 55 | } |
|---|
| 56 | free(tempstr); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | static unsigned long compute_checksum(void *addr, unsigned long length) |
|---|
| 60 | { |
|---|
| 61 | uint8_t *ptr; |
|---|
| 62 | volatile union { |
|---|
| 63 | uint8_t byte[2]; |
|---|
| 64 | uint16_t word; |
|---|
| 65 | } chksum; |
|---|
| 66 | unsigned long sum; |
|---|
| 67 | unsigned long i; |
|---|
| 68 | |
|---|
| 69 | /* In the most straight forward way possible, |
|---|
| 70 | * compute an ip style checksum. |
|---|
| 71 | */ |
|---|
| 72 | sum = 0; |
|---|
| 73 | ptr = addr; |
|---|
| 74 | for (i = 0; i < length; i++) { |
|---|
| 75 | unsigned long value; |
|---|
| 76 | value = ptr[i]; |
|---|
| 77 | if (i & 1) { |
|---|
| 78 | value <<= 8; |
|---|
| 79 | } |
|---|
| 80 | /* Add the new value */ |
|---|
| 81 | sum += value; |
|---|
| 82 | /* Wrap around the carry */ |
|---|
| 83 | if (sum > 0xFFFF) { |
|---|
| 84 | sum = (sum + (sum >> 16)) & 0xFFFF; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | chksum.byte[0] = sum & 0xff; |
|---|
| 88 | chksum.byte[1] = (sum >> 8) & 0xff; |
|---|
| 89 | |
|---|
| 90 | return (~chksum.word) & 0xFFFF; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | #define for_each_lbrec(head, rec) \ |
|---|
| 94 | for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \ |
|---|
| 95 | (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \ |
|---|
| 96 | (rec->size >= 1) && \ |
|---|
| 97 | ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \ |
|---|
| 98 | rec = (struct lb_record *)(((char *)rec) + rec->size)) |
|---|
| 99 | |
|---|
| 100 | static int count_lb_records(struct lb_header *head) |
|---|
| 101 | { |
|---|
| 102 | struct lb_record *rec; |
|---|
| 103 | int count; |
|---|
| 104 | |
|---|
| 105 | count = 0; |
|---|
| 106 | for_each_lbrec(head, rec) { |
|---|
| 107 | count++; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | return count; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | static struct lb_header *find_lb_table(void *base, unsigned long start, |
|---|
| 114 | unsigned long end) |
|---|
| 115 | { |
|---|
| 116 | unsigned long addr; |
|---|
| 117 | |
|---|
| 118 | /* For now be stupid.... */ |
|---|
| 119 | for (addr = start; addr < end; addr += 16) { |
|---|
| 120 | struct lb_header *head = |
|---|
| 121 | (struct lb_header *)(((char *)base) + addr); |
|---|
| 122 | struct lb_record *recs = |
|---|
| 123 | (struct lb_record *)(((char *)base) + addr + sizeof(*head)); |
|---|
| 124 | if (memcmp(head->signature, "LBIO", 4) != 0) |
|---|
| 125 | continue; |
|---|
| 126 | msg_pdbg("Found candidate at: %08lx-%08lx\n", |
|---|
| 127 | addr, addr + head->table_bytes); |
|---|
| 128 | if (head->header_bytes != sizeof(*head)) { |
|---|
| 129 | msg_perr("Header bytes of %d are incorrect.\n", |
|---|
| 130 | head->header_bytes); |
|---|
| 131 | continue; |
|---|
| 132 | } |
|---|
| 133 | if (count_lb_records(head) != head->table_entries) { |
|---|
| 134 | msg_perr("Bad record count: %d.\n", |
|---|
| 135 | head->table_entries); |
|---|
| 136 | continue; |
|---|
| 137 | } |
|---|
| 138 | if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) { |
|---|
| 139 | msg_perr("Bad header checksum.\n"); |
|---|
| 140 | continue; |
|---|
| 141 | } |
|---|
| 142 | if (compute_checksum(recs, head->table_bytes) |
|---|
| 143 | != head->table_checksum) { |
|---|
| 144 | msg_perr("Bad table checksum: %04x.\n", |
|---|
| 145 | head->table_checksum); |
|---|
| 146 | continue; |
|---|
| 147 | } |
|---|
| 148 | msg_pdbg("Found coreboot table at 0x%08lx.\n", addr); |
|---|
| 149 | return head; |
|---|
| 150 | |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | return NULL; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | static void find_mainboard(struct lb_record *ptr, unsigned long addr) |
|---|
| 157 | { |
|---|
| 158 | struct lb_mainboard *rec; |
|---|
| 159 | int max_size; |
|---|
| 160 | char vendor[256], part[256]; |
|---|
| 161 | |
|---|
| 162 | rec = (struct lb_mainboard *)ptr; |
|---|
| 163 | max_size = rec->size - sizeof(*rec); |
|---|
| 164 | msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n", |
|---|
| 165 | max_size - rec->vendor_idx, |
|---|
| 166 | rec->strings + rec->vendor_idx, |
|---|
| 167 | max_size - rec->part_number_idx, |
|---|
| 168 | rec->strings + rec->part_number_idx); |
|---|
| 169 | snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, |
|---|
| 170 | rec->strings + rec->vendor_idx); |
|---|
| 171 | snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, |
|---|
| 172 | rec->strings + rec->part_number_idx); |
|---|
| 173 | |
|---|
| 174 | if (lb_part) { |
|---|
| 175 | msg_pdbg("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part); |
|---|
| 176 | } else { |
|---|
| 177 | partvendor_from_cbtable = 1; |
|---|
| 178 | lb_part = strdup(part); |
|---|
| 179 | lb_vendor = strdup(vendor); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | static struct lb_record *next_record(struct lb_record *rec) |
|---|
| 184 | { |
|---|
| 185 | return (struct lb_record *)(((char *)rec) + rec->size); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | static void search_lb_records(struct lb_record *rec, struct lb_record *last, |
|---|
| 189 | unsigned long addr) |
|---|
| 190 | { |
|---|
| 191 | struct lb_record *next; |
|---|
| 192 | int count; |
|---|
| 193 | count = 0; |
|---|
| 194 | |
|---|
| 195 | for (next = next_record(rec); (rec < last) && (next <= last); |
|---|
| 196 | rec = next, addr += rec->size) { |
|---|
| 197 | next = next_record(rec); |
|---|
| 198 | count++; |
|---|
| 199 | if (rec->tag == LB_TAG_MAINBOARD) { |
|---|
| 200 | find_mainboard(rec, addr); |
|---|
| 201 | break; |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | #define BYTES_TO_MAP (1024*1024) |
|---|
| 207 | int coreboot_init(void) |
|---|
| 208 | { |
|---|
| 209 | uint8_t *table_area; |
|---|
| 210 | unsigned long addr, start; |
|---|
| 211 | struct lb_header *lb_table; |
|---|
| 212 | struct lb_record *rec, *last; |
|---|
| 213 | |
|---|
| 214 | #ifdef __DARWIN__ |
|---|
| 215 | /* This is a hack. DirectHW fails to map physical address 0x00000000. |
|---|
| 216 | * Why? |
|---|
| 217 | */ |
|---|
| 218 | start = 0x400; |
|---|
| 219 | #else |
|---|
| 220 | start = 0x0; |
|---|
| 221 | #endif |
|---|
| 222 | table_area = physmap_try_ro("low megabyte", start, BYTES_TO_MAP - start); |
|---|
| 223 | if (ERROR_PTR == table_area) { |
|---|
| 224 | msg_perr("Failed getting access to coreboot low tables.\n"); |
|---|
| 225 | return -1; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | lb_table = find_lb_table(table_area, 0x00000, 0x1000); |
|---|
| 229 | if (!lb_table) |
|---|
| 230 | lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start); |
|---|
| 231 | if (lb_table) { |
|---|
| 232 | struct lb_forward *forward = (struct lb_forward *) |
|---|
| 233 | (((char *)lb_table) + lb_table->header_bytes); |
|---|
| 234 | if (forward->tag == LB_TAG_FORWARD) { |
|---|
| 235 | start = forward->forward; |
|---|
| 236 | start &= ~(getpagesize() - 1); |
|---|
| 237 | physunmap(table_area, BYTES_TO_MAP); |
|---|
| 238 | table_area = physmap_try_ro("high tables", start, BYTES_TO_MAP); |
|---|
| 239 | if (ERROR_PTR == table_area) { |
|---|
| 240 | msg_perr("Failed getting access to coreboot " |
|---|
| 241 | "high tables.\n"); |
|---|
| 242 | return -1; |
|---|
| 243 | } |
|---|
| 244 | lb_table = find_lb_table(table_area, 0x00000, 0x1000); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | if (!lb_table) { |
|---|
| 249 | msg_pdbg("No coreboot table found.\n"); |
|---|
| 250 | return -1; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | addr = ((char *)lb_table) - ((char *)table_area) + start; |
|---|
| 254 | msg_pinfo("coreboot table found at 0x%lx.\n", |
|---|
| 255 | (unsigned long)lb_table - (unsigned long)table_area + start); |
|---|
| 256 | rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes); |
|---|
| 257 | last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes); |
|---|
| 258 | msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n", |
|---|
| 259 | lb_table->header_bytes, lb_table->header_checksum, |
|---|
| 260 | lb_table->table_bytes, lb_table->table_checksum, |
|---|
| 261 | lb_table->table_entries); |
|---|
| 262 | search_lb_records(rec, last, addr + lb_table->header_bytes); |
|---|
| 263 | |
|---|
| 264 | return 0; |
|---|
| 265 | } |
|---|