cbtable.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <unistd.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <strings.h>
  28. #include <string.h>
  29. #include "flash.h"
  30. #include "programmer.h"
  31. #include "coreboot_tables.h"
  32. static char *cb_vendor = NULL, *cb_model = NULL;
  33. /* Tries to find coreboot IDs in the supplied image and compares them to the current IDs.
  34. * Returns...
  35. * -1 if IDs in the image do not match the IDs embedded in the current firmware,
  36. * 0 if the IDs could not be found in the image or if they match correctly.
  37. */
  38. int cb_check_image(uint8_t *image, int size)
  39. {
  40. unsigned int *walk;
  41. unsigned int mb_part_offset, mb_vendor_offset;
  42. char *mb_part, *mb_vendor;
  43. walk = (unsigned int *)(image + size - 0x10);
  44. walk--;
  45. if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
  46. /* Some NVIDIA chipsets store chipset soft straps (IIRC Hypertransport init info etc.) in
  47. * flash at exactly the location where coreboot image size, coreboot vendor name pointer and
  48. * coreboot board name pointer are usually stored. In this case coreboot uses an alternate
  49. * location for the coreboot image data. */
  50. walk = (unsigned int *)(image + size - 0x80);
  51. walk--;
  52. }
  53. /*
  54. * Check if coreboot last image size is 0 or not a multiple of 1k or
  55. * bigger than the chip or if the pointers to vendor ID or mainboard ID
  56. * are outside the image of if the start of ID strings are nonsensical
  57. * (nonprintable and not \0).
  58. */
  59. mb_part_offset = *(walk - 1);
  60. mb_vendor_offset = *(walk - 2);
  61. if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
  62. mb_part_offset > size || mb_vendor_offset > size) {
  63. msg_pdbg("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
  64. return 0;
  65. }
  66. mb_part = (char *)(image + size - mb_part_offset);
  67. mb_vendor = (char *)(image + size - mb_vendor_offset);
  68. if (!isprint((unsigned char)*mb_part) ||
  69. !isprint((unsigned char)*mb_vendor)) {
  70. msg_pdbg("Flash image seems to have garbage in the ID location. "
  71. "Disabling coreboot-related checks.\n");
  72. return 0;
  73. }
  74. msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);
  75. msg_pdbg("Manufacturer: %s\n", mb_vendor);
  76. msg_pdbg("Mainboard ID: %s\n", mb_part);
  77. /* If these are not set, the coreboot table was not found. */
  78. if (!cb_vendor || !cb_model)
  79. return 0;
  80. /* These comparisons are case insensitive to make things a little less user^Werror prone. */
  81. if (!strcasecmp(mb_vendor, cb_vendor) && !strcasecmp(mb_part, cb_model)) {
  82. msg_pdbg2("This coreboot image matches this mainboard.\n");
  83. } else {
  84. msg_perr("This coreboot image (%s:%s) does not appear to\n"
  85. "be correct for the detected mainboard (%s:%s).\n",
  86. mb_vendor, mb_part, cb_vendor, cb_model);
  87. return -1;
  88. }
  89. return 0;
  90. }
  91. static unsigned long compute_checksum(void *addr, unsigned long length)
  92. {
  93. uint8_t *ptr;
  94. volatile union {
  95. uint8_t byte[2];
  96. uint16_t word;
  97. } chksum;
  98. unsigned long sum;
  99. unsigned long i;
  100. /* In the most straight forward way possible,
  101. * compute an ip style checksum.
  102. */
  103. sum = 0;
  104. ptr = addr;
  105. for (i = 0; i < length; i++) {
  106. unsigned long value;
  107. value = ptr[i];
  108. if (i & 1) {
  109. value <<= 8;
  110. }
  111. /* Add the new value */
  112. sum += value;
  113. /* Wrap around the carry */
  114. if (sum > 0xFFFF) {
  115. sum = (sum + (sum >> 16)) & 0xFFFF;
  116. }
  117. }
  118. chksum.byte[0] = sum & 0xff;
  119. chksum.byte[1] = (sum >> 8) & 0xff;
  120. return (~chksum.word) & 0xFFFF;
  121. }
  122. #define for_each_lbrec(head, rec) \
  123. for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
  124. (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
  125. (rec->size >= 1) && \
  126. ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
  127. rec = (struct lb_record *)(((char *)rec) + rec->size))
  128. static int count_lb_records(struct lb_header *head)
  129. {
  130. struct lb_record *rec;
  131. int count;
  132. count = 0;
  133. for_each_lbrec(head, rec) {
  134. count++;
  135. }
  136. return count;
  137. }
  138. static struct lb_header *find_lb_table(void *base, unsigned long start,
  139. unsigned long end)
  140. {
  141. unsigned long addr;
  142. /* For now be stupid.... */
  143. for (addr = start; addr < end; addr += 16) {
  144. struct lb_header *head =
  145. (struct lb_header *)(((char *)base) + addr);
  146. struct lb_record *recs =
  147. (struct lb_record *)(((char *)base) + addr + sizeof(*head));
  148. if (memcmp(head->signature, "LBIO", 4) != 0)
  149. continue;
  150. msg_pdbg("Found candidate at: %08lx-%08lx\n",
  151. addr, addr + head->table_bytes);
  152. if (head->header_bytes != sizeof(*head)) {
  153. msg_perr("Header bytes of %d are incorrect.\n",
  154. head->header_bytes);
  155. continue;
  156. }
  157. if (count_lb_records(head) != head->table_entries) {
  158. msg_perr("Bad record count: %d.\n",
  159. head->table_entries);
  160. continue;
  161. }
  162. if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
  163. msg_perr("Bad header checksum.\n");
  164. continue;
  165. }
  166. if (compute_checksum(recs, head->table_bytes)
  167. != head->table_checksum) {
  168. msg_perr("Bad table checksum: %04x.\n",
  169. head->table_checksum);
  170. continue;
  171. }
  172. msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
  173. return head;
  174. };
  175. return NULL;
  176. }
  177. static void find_mainboard(struct lb_record *ptr, unsigned long addr)
  178. {
  179. struct lb_mainboard *rec;
  180. int max_size;
  181. char vendor[256], part[256];
  182. rec = (struct lb_mainboard *)ptr;
  183. max_size = rec->size - sizeof(*rec);
  184. msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
  185. max_size - rec->vendor_idx,
  186. rec->strings + rec->vendor_idx,
  187. max_size - rec->part_number_idx,
  188. rec->strings + rec->part_number_idx);
  189. snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx, rec->strings + rec->vendor_idx);
  190. snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
  191. cb_vendor = strdup(vendor);
  192. cb_model = strdup(part);
  193. }
  194. static struct lb_record *next_record(struct lb_record *rec)
  195. {
  196. return (struct lb_record *)(((char *)rec) + rec->size);
  197. }
  198. static void search_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr)
  199. {
  200. struct lb_record *next;
  201. int count;
  202. count = 0;
  203. for (next = next_record(rec); (rec < last) && (next <= last);
  204. rec = next, addr += rec->size) {
  205. next = next_record(rec);
  206. count++;
  207. if (rec->tag == LB_TAG_MAINBOARD) {
  208. find_mainboard(rec, addr);
  209. break;
  210. }
  211. }
  212. }
  213. #define BYTES_TO_MAP (1024*1024)
  214. /* returns 0 if the table was parsed successfully and cb_vendor/cb_model have been set. */
  215. int cb_parse_table(const char **vendor, const char **model)
  216. {
  217. uint8_t *table_area;
  218. unsigned long addr, start;
  219. struct lb_header *lb_table;
  220. struct lb_record *rec, *last;
  221. #if defined(__MACH__) && defined(__APPLE__)
  222. /* This is a hack. DirectHW fails to map physical address 0x00000000.
  223. * Why?
  224. */
  225. start = 0x400;
  226. #else
  227. start = 0x0;
  228. #endif
  229. table_area = physmap_ro_unaligned("low megabyte", start, BYTES_TO_MAP - start);
  230. if (ERROR_PTR == table_area) {
  231. msg_perr("Failed getting access to coreboot low tables.\n");
  232. return -1;
  233. }
  234. lb_table = find_lb_table(table_area, 0x00000, 0x1000);
  235. if (!lb_table)
  236. lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
  237. if (lb_table) {
  238. struct lb_forward *forward = (struct lb_forward *)
  239. (((char *)lb_table) + lb_table->header_bytes);
  240. if (forward->tag == LB_TAG_FORWARD) {
  241. start = forward->forward;
  242. start &= ~(getpagesize() - 1);
  243. physunmap_unaligned(table_area, BYTES_TO_MAP);
  244. // FIXME: table_area is never unmapped below, nor is it unmapped above in the no-forward case
  245. table_area = physmap_ro_unaligned("high tables", start, BYTES_TO_MAP);
  246. if (ERROR_PTR == table_area) {
  247. msg_perr("Failed getting access to coreboot high tables.\n");
  248. return -1;
  249. }
  250. lb_table = find_lb_table(table_area, 0x00000, 0x1000);
  251. }
  252. }
  253. if (!lb_table) {
  254. msg_pdbg("No coreboot table found.\n");
  255. return -1;
  256. }
  257. addr = ((char *)lb_table) - ((char *)table_area) + start;
  258. msg_pinfo("coreboot table found at 0x%lx.\n",
  259. (unsigned long)lb_table - (unsigned long)table_area + start);
  260. rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
  261. last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
  262. msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
  263. lb_table->header_bytes, lb_table->header_checksum,
  264. lb_table->table_bytes, lb_table->table_checksum,
  265. lb_table->table_entries);
  266. search_lb_records(rec, last, addr + lb_table->header_bytes);
  267. *vendor = cb_vendor;
  268. *model = cb_model;
  269. return 0;
  270. }