blob: 4aa260bc032d2b260757a0c05e79f6087415450c [file] [log] [blame]
Stefan Reinauer5380d512007-05-24 09:08:36 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Stefan Reinauer5380d512007-05-24 09:08:36 +00003 *
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 Reinauer2d853bb2009-03-17 14:39:25 +00007 * Copyright (C) 2006-2009 coresystems GmbH
Stefan Reinauer5380d512007-05-24 09:08:36 +00008 * (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 Hermannd1107642007-08-29 17:52:32 +000021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer5380d512007-05-24 09:08:36 +000022 */
23
Ollie Lho184a4042005-11-26 21:55:36 +000024#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 Kaufman064b1f22007-02-06 19:47:50 +000033#include "flash.h"
Stefan Reinauer2fbe6242008-01-18 16:17:44 +000034#include "coreboot_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000035
Uwe Hermanna7e05482007-05-09 10:17:44 +000036char *lb_part = NULL, *lb_vendor = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000037
38static unsigned long compute_checksum(void *addr, unsigned long length)
39{
40 uint8_t *ptr;
41 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +000042 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +000043 uint16_t word;
44 } value;
Uwe Hermannac309342007-10-10 17:42:20 +000045 unsigned long sum;
46 unsigned long i;
Uwe Hermannffec5f32007-08-23 16:08:21 +000047
Ollie Lho184a4042005-11-26 21:55:36 +000048 /* In the most straight forward way possible,
49 * compute an ip style checksum.
50 */
51 sum = 0;
52 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +000054 unsigned long value;
55 value = ptr[i];
Uwe Hermannac309342007-10-10 17:42:20 +000056 if (i & 1) {
Ollie Lho184a4042005-11-26 21:55:36 +000057 value <<= 8;
Uwe Hermannac309342007-10-10 17:42:20 +000058 }
Ollie Lho184a4042005-11-26 21:55:36 +000059 /* Add the new value */
60 sum += value;
61 /* Wrap around the carry */
Uwe Hermannac309342007-10-10 17:42:20 +000062 if (sum > 0xFFFF) {
Ollie Lho184a4042005-11-26 21:55:36 +000063 sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermannac309342007-10-10 17:42:20 +000064 }
Ollie Lho184a4042005-11-26 21:55:36 +000065 }
66 value.byte[0] = sum & 0xff;
67 value.byte[1] = (sum >> 8) & 0xff;
Uwe Hermannffec5f32007-08-23 16:08:21 +000068
Ollie Lho184a4042005-11-26 21:55:36 +000069 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 Hermanna7e05482007-05-09 10:17:44 +000077 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +000078
Uwe Hermanna7e05482007-05-09 10:17:44 +000079static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +000080{
81 struct lb_record *rec;
82 int count;
Uwe Hermannffec5f32007-08-23 16:08:21 +000083
Ollie Lho184a4042005-11-26 21:55:36 +000084 count = 0;
85 for_each_lbrec(head, rec) {
86 count++;
87 }
Uwe Hermannffec5f32007-08-23 16:08:21 +000088
Ollie Lho184a4042005-11-26 21:55:36 +000089 return count;
90}
91
Uwe Hermanna7e05482007-05-09 10:17:44 +000092static struct lb_header *find_lb_table(void *base, unsigned long start,
93 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +000094{
95 unsigned long addr;
Uwe Hermannffec5f32007-08-23 16:08:21 +000096
Ollie Lho184a4042005-11-26 21:55:36 +000097 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +000098 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 Lho184a4042005-11-26 21:55:36 +0000103 if (memcmp(head->signature, "LBIO", 4) != 0)
104 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000105 printf_debug("Found canidate at: %08lx-%08lx\n",
106 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000107 if (head->header_bytes != sizeof(*head)) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000108 fprintf(stderr, "Header bytes of %d are incorrect.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000109 head->header_bytes);
110 continue;
111 }
112 if (count_lb_records(head) != head->table_entries) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000113 fprintf(stderr, "Bad record count: %d.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000114 head->table_entries);
115 continue;
116 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000117 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000118 fprintf(stderr, "Bad header checksum.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000119 continue;
120 }
121 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000122 != head->table_checksum) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000123 fprintf(stderr, "Bad table checksum: %04x.\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000124 head->table_checksum);
125 continue;
126 }
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000127 printf_debug("Found coreboot table at 0x%08lx.\n", addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000128 return head;
129
130 };
Uwe Hermannffec5f32007-08-23 16:08:21 +0000131
Ollie Lho184a4042005-11-26 21:55:36 +0000132 return 0;
133}
134
135static 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 Hermannffec5f32007-08-23 16:08:21 +0000140
Ollie Lho184a4042005-11-26 21:55:36 +0000141 rec = (struct lb_mainboard *)ptr;
142 max_size = rec->size - sizeof(*rec);
Uwe Hermanna502dce2007-10-17 23:55:15 +0000143 printf("Vendor ID: %.*s, part ID: %.*s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000144 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 Lho184a4042005-11-26 21:55:36 +0000150 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000151 rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000152
Uwe Hermanna7e05482007-05-09 10:17:44 +0000153 if (lb_part) {
Uwe Hermanna502dce2007-10-17 23:55:15 +0000154 printf("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000155 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000156 lb_part = strdup(part);
157 lb_vendor = strdup(vendor);
Ollie Lho184a4042005-11-26 21:55:36 +0000158 }
159}
160
161static struct lb_record *next_record(struct lb_record *rec)
162{
163 return (struct lb_record *)(((char *)rec) + rec->size);
164}
165
Uwe Hermanna7e05482007-05-09 10:17:44 +0000166static void search_lb_records(struct lb_record *rec, struct lb_record *last,
167 unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000168{
169 struct lb_record *next;
170 int count;
171 count = 0;
172
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173 for (next = next_record(rec); (rec < last) && (next <= last);
174 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000175 next = next_record(rec);
176 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000177 if (rec->tag == LB_TAG_MAINBOARD) {
178 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000179 break;
180 }
181 }
182}
183
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000184#define BYTES_TO_MAP (1024*1024)
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000185int coreboot_init(void)
Ollie Lho184a4042005-11-26 21:55:36 +0000186{
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000187 uint8_t *table_area;
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000188 unsigned long addr, start;
Ollie Lho184a4042005-11-26 21:55:36 +0000189 struct lb_header *lb_table;
190 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000191
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000192#ifdef __DARWIN__
193 /* This is a hack. DirectIO fails to map physical address 0x00000000.
194 * Why?
195 */
196 start = 0x400;
197#else
198 start = 0x0;
199#endif
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000200 table_area = physmap("low megabyte", start, BYTES_TO_MAP);
Stefan Reinauerf79edb92009-01-26 01:23:31 +0000201
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000202 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
Ollie Lho184a4042005-11-26 21:55:36 +0000203 if (!lb_table)
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000204 lb_table = find_lb_table(table_area, 0xf0000, BYTES_TO_MAP);
205 if (lb_table) {
206 struct lb_forward *forward = (struct lb_forward *)
207 (((char *)lb_table) + lb_table->header_bytes);
208 if (forward->tag == LB_TAG_FORWARD) {
209 start = forward->forward;
210 start &= ~(getpagesize()-1);
211 physunmap(table_area, BYTES_TO_MAP);
212 table_area = physmap("high tables", start, BYTES_TO_MAP);
213 lb_table = find_lb_table(table_area, 0x00000, 0x1000);
214 }
215 }
216
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000217 if (!lb_table) {
Stefan Reinauere3f3e2e2008-01-18 15:33:10 +0000218 printf("No coreboot table found.\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000219 return -1;
220 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000221
Stefan Reinauer2d853bb2009-03-17 14:39:25 +0000222 addr = ((char *)lb_table) - ((char *)table_area) + start;
223 fprintf(stdout, "coreboot table found at 0x%lx.\n",
224 (unsigned long)lb_table - (unsigned long)table_area + start);
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000225 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
226 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Peter Stugea0d75a02009-01-26 00:19:36 +0000227 printf_debug("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Peter Stuge2dc3aaa2009-01-26 00:15:56 +0000228 lb_table->header_bytes, lb_table->header_checksum,
229 lb_table->table_bytes, lb_table->table_checksum,
230 lb_table->table_entries);
231 search_lb_records(rec, last, addr + lb_table->header_bytes);
232
Ollie Lho184a4042005-11-26 21:55:36 +0000233 return 0;
234}