devcoreboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * This file is part of the libpayload project.
  3. *
  4. * Copyright (C) 2008 Advanced Micro Devices, Inc.
  5. * Copyright (C) 2009 coresystems GmbH
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include "u.h"
  31. #include "../port/lib.h"
  32. #include "mem.h"
  33. #include "dat.h"
  34. #include "fns.h"
  35. #include "../port/error.h"
  36. #include "coreboot.h"
  37. /*
  38. * Some of this is x86 specific, and the rest of it is generic. Right now,
  39. * since we only support x86, we'll avoid trying to make lots of infrastructure
  40. * we don't need. If in the future, we want to use coreboot on some other
  41. * architecture, then take out the generic parsing code and move it elsewhere.
  42. */
  43. /* === Parsing code === */
  44. /* This is the generic parsing code. */
  45. static void cb_parse_memory(void *ptr, struct sysinfo_t *info)
  46. { kmprint("%s\n", __func__);
  47. struct cb_memory *mem = ptr;
  48. int count = MEM_RANGE_COUNT(mem);
  49. int i;
  50. if (count > SYSINFO_MAX_MEM_RANGES)
  51. count = SYSINFO_MAX_MEM_RANGES;
  52. info->n_memranges = 0;
  53. for (i = 0; i < count; i++) {
  54. struct cb_memory_range *range = MEM_RANGE_PTR(mem, i);
  55. info->memrange[info->n_memranges].base =
  56. cb_unpack64(range->start);
  57. info->memrange[info->n_memranges].size =
  58. cb_unpack64(range->size);
  59. info->memrange[info->n_memranges].type = range->type;
  60. info->n_memranges++;
  61. }
  62. }
  63. static void cb_parse_serial(void *ptr, struct sysinfo_t *info)
  64. { kmprint("%s\n", __func__);
  65. info->serial = ((struct cb_serial *)ptr);
  66. }
  67. static void cb_parse_vboot_handoff(unsigned char *ptr, struct sysinfo_t *info)
  68. { kmprint("%s\n", __func__);
  69. struct cb_range *vbho = (struct cb_range *)ptr;
  70. info->vboot_handoff = (void *)(uintptr_t)vbho->range_start;
  71. info->vboot_handoff_size = vbho->range_size;
  72. }
  73. static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
  74. { kmprint("%s\n", __func__);
  75. struct cb_range *vbnv = (struct cb_range *)ptr;
  76. info->vbnv_start = vbnv->range_start;
  77. info->vbnv_size = vbnv->range_size;
  78. }
  79. static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
  80. { kmprint("%s\n", __func__);
  81. int i;
  82. struct cb_gpios *gpios = (struct cb_gpios *)ptr;
  83. info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
  84. (gpios->count) : SYSINFO_MAX_GPIOS;
  85. for (i = 0; i < info->num_gpios; i++)
  86. info->gpios[i] = gpios->gpios[i];
  87. }
  88. static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
  89. { kmprint("%s\n", __func__);
  90. struct cb_range *vdat = (struct cb_range *) ptr;
  91. info->vdat_addr = KADDR(vdat->range_start);
  92. info->vdat_size = vdat->range_size;
  93. }
  94. static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
  95. { kmprint("%s\n", __func__);
  96. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  97. info->tstamp_table = KADDR(cbmem->cbmem_tab);
  98. }
  99. static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
  100. { kmprint("%s\n", __func__);
  101. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  102. info->cbmem_cons = KADDR(cbmem->cbmem_tab);
  103. }
  104. static void cb_parse_mrc_cache(unsigned char *ptr, struct sysinfo_t *info)
  105. { kmprint("%s\n", __func__);
  106. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  107. info->mrc_cache = KADDR(cbmem->cbmem_tab);
  108. }
  109. static void cb_parse_acpi_gnvs(unsigned char *ptr, struct sysinfo_t *info)
  110. { kmprint("%s\n", __func__);
  111. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  112. info->acpi_gnvs = KADDR(cbmem->cbmem_tab);
  113. }
  114. static void cb_parse_optiontable(void *ptr, struct sysinfo_t *info)
  115. { kmprint("%s\n", __func__);
  116. /* ptr points to a coreboot table entry and is already virtual */
  117. info->option_table = ptr;
  118. }
  119. static void cb_parse_checksum(void *ptr, struct sysinfo_t *info)
  120. { kmprint("%s\n", __func__);
  121. struct cb_cmos_checksum *cmos_cksum = ptr;
  122. info->cmos_range_start = cmos_cksum->range_start;
  123. info->cmos_range_end = cmos_cksum->range_end;
  124. info->cmos_checksum_location = cmos_cksum->location;
  125. }
  126. static void cb_parse_framebuffer(void *ptr, struct sysinfo_t *info)
  127. { kmprint("%s\n", __func__);
  128. /* ptr points to a coreboot table entry and is already virtual */
  129. info->framebuffer = ptr;
  130. }
  131. static void cb_parse_x86_rom_var_mtrr(void *ptr, struct sysinfo_t *info)
  132. {
  133. kmprint("%s, ignoring MTRR information.\n", __func__);
  134. }
  135. static void cb_parse_string(unsigned char *ptr, char **info)
  136. { kmprint("%s\n", __func__);
  137. *info = (char *)((struct cb_string *)ptr)->string;
  138. }
  139. int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  140. { kmprint("%s\n", __func__);
  141. struct cb_header *header;
  142. unsigned char *ptr = addr;
  143. void *forward;
  144. int i;
  145. for (i = 0; i < len; i += 16, ptr += 16) {
  146. header = (struct cb_header *)ptr;
  147. if (0)
  148. kmprint("Check header %p sig %p val %02x %02x %02x %02x\n", header, header->signature,
  149. header->signature[0],
  150. header->signature[1],
  151. header->signature[2],
  152. header->signature[3]);
  153. if (!strncmp((char *)header->signature, "LBIO", 4))
  154. break;
  155. }
  156. kmprint("found ? i %d len %d\n", i, len);
  157. /* We walked the entire space and didn't find anything. */
  158. if (i >= len)
  159. return -1;
  160. I_AM_HERE;
  161. if (!header->table_bytes)
  162. return 0;
  163. I_AM_HERE;
  164. /* Make sure the checksums match. */
  165. if (ipchksum((uint8_t *) header, sizeof(*header)) != 0)
  166. return -1;
  167. I_AM_HERE;
  168. if (ipchksum((uint8_t *) (ptr + sizeof(*header)),
  169. header->table_bytes) != header->table_checksum)
  170. return -1;
  171. I_AM_HERE;
  172. info->header = header;
  173. /* Now, walk the tables. */
  174. ptr += header->header_bytes;
  175. I_AM_HERE;
  176. for (i = 0; i < header->table_entries; i++) {
  177. struct cb_record *rec = (struct cb_record *)ptr;
  178. I_AM_HERE;
  179. kmprint("rec %p tag %p\n", rec, rec->tag);
  180. /* We only care about a few tags here (maybe more later). */
  181. switch (rec->tag) {
  182. case CB_TAG_FORWARD:
  183. forward = KADDR((unsigned long)((struct cb_forward *)rec)->forward);
  184. kmprint("FORWARD: %p %p\n", (unsigned long)((struct cb_forward *)rec)->forward,
  185. forward);
  186. return cb_parse_header(forward, len, info);
  187. continue;
  188. case CB_TAG_MEMORY:
  189. cb_parse_memory(ptr, info);
  190. break;
  191. case CB_TAG_SERIAL:
  192. cb_parse_serial(ptr, info);
  193. break;
  194. case CB_TAG_VERSION:
  195. cb_parse_string(ptr, &info->cb_version);
  196. break;
  197. case CB_TAG_EXTRA_VERSION:
  198. cb_parse_string(ptr, &info->extra_version);
  199. break;
  200. case CB_TAG_BUILD:
  201. cb_parse_string(ptr, &info->build);
  202. break;
  203. case CB_TAG_COMPILE_TIME:
  204. cb_parse_string(ptr, &info->compile_time);
  205. break;
  206. case CB_TAG_COMPILE_BY:
  207. cb_parse_string(ptr, &info->compile_by);
  208. break;
  209. case CB_TAG_COMPILE_HOST:
  210. cb_parse_string(ptr, &info->compile_host);
  211. break;
  212. case CB_TAG_COMPILE_DOMAIN:
  213. cb_parse_string(ptr, &info->compile_domain);
  214. break;
  215. case CB_TAG_COMPILER:
  216. cb_parse_string(ptr, &info->compiler);
  217. break;
  218. case CB_TAG_LINKER:
  219. cb_parse_string(ptr, &info->linker);
  220. break;
  221. case CB_TAG_ASSEMBLER:
  222. cb_parse_string(ptr, &info->assembler);
  223. break;
  224. case CB_TAG_CMOS_OPTION_TABLE:
  225. cb_parse_optiontable(ptr, info);
  226. break;
  227. case CB_TAG_OPTION_CHECKSUM:
  228. cb_parse_checksum(ptr, info);
  229. break;
  230. // FIXME we should warn on serial if coreboot set up a
  231. // framebuffer buf the payload does not know about it.
  232. case CB_TAG_FRAMEBUFFER:
  233. cb_parse_framebuffer(ptr, info);
  234. break;
  235. case CB_TAG_MAINBOARD:
  236. info->mainboard = (struct cb_mainboard *)ptr;
  237. case CB_TAG_GPIO:
  238. cb_parse_gpios(ptr, info);
  239. break;
  240. case CB_TAG_VDAT:
  241. cb_parse_vdat(ptr, info);
  242. break;
  243. case CB_TAG_VBNV:
  244. cb_parse_vbnv(ptr, info);
  245. break;
  246. case CB_TAG_VBOOT_HANDOFF:
  247. cb_parse_vboot_handoff(ptr, info);
  248. break;
  249. case CB_TAG_TIMESTAMPS:
  250. cb_parse_tstamp(ptr, info);
  251. break;
  252. case CB_TAG_CBMEM_CONSOLE:
  253. cb_parse_cbmem_cons(ptr, info);
  254. break;
  255. case CB_TAG_MRC_CACHE:
  256. cb_parse_mrc_cache(ptr, info);
  257. break;
  258. case CB_TAG_ACPI_GNVS:
  259. cb_parse_acpi_gnvs(ptr, info);
  260. break;
  261. case CB_TAG_X86_ROM_MTRR:
  262. cb_parse_x86_rom_var_mtrr(ptr, info);
  263. break;
  264. }
  265. ptr += rec->size;
  266. }
  267. return 1;
  268. }
  269. enum{
  270. Qdir,
  271. Qtable,
  272. };
  273. static Dirtab corebootdir[]={
  274. {".", {Qdir, 0, QTDIR}, 0, DMDIR|0555},
  275. {"table", {Qtable}, 0, 0444},
  276. };
  277. struct sysinfo_t cbinfo;
  278. static int
  279. corebootdevgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
  280. {
  281. int rc;
  282. rc = devgen(c, name, tab, ntab, i, dp);
  283. return rc;
  284. }
  285. static void
  286. corebootinit(void)
  287. {
  288. get_coreboot_info(&cbinfo);
  289. }
  290. static Chan*
  291. corebootattach(char *spec)
  292. {
  293. return devattach('Y', spec);
  294. }
  295. static Walkqid*
  296. corebootwalk(Chan *c, Chan *nc, char **name, int nname)
  297. {
  298. Walkqid *wq;
  299. wq = devwalk(c, nc, name, nname, corebootdir, nelem(corebootdir), devgen);
  300. /* todo: cover any cases that need locking. There are none yet. */
  301. return wq;
  302. }
  303. static int
  304. corebootstat(Chan *c, unsigned char *db, int n)
  305. {
  306. return devstat(c, db, n, corebootdir, nelem(corebootdir), corebootdevgen);
  307. }
  308. static Chan*
  309. corebootopen(Chan *c, int omode)
  310. {
  311. switch((uint32_t)c->qid.path){
  312. case Qdir:
  313. if(omode != OREAD)
  314. error(Eperm);
  315. break;
  316. /* default are tables, which are unchanging, and readonly, so no need to lock them.
  317. * They live in reserved memory.
  318. */
  319. default:
  320. break;
  321. }
  322. c->mode = openmode(omode);
  323. c->flag |= COPEN;
  324. c->offset = 0;
  325. return c;
  326. }
  327. static void
  328. corebootcreate(Chan *c, char *j, int i, int u)
  329. {
  330. error(Eperm);
  331. }
  332. static void
  333. corebootclose(Chan *c)
  334. {
  335. /* anything to do? */
  336. }
  337. static int32_t
  338. corebootread(Chan *c, void *va, int32_t n, int64_t off)
  339. {
  340. switch((uint32_t)c->qid.path){
  341. case Qdir:
  342. return devdirread(c, va, n, corebootdir, nelem(corebootdir), corebootdevgen);
  343. case Qtable:
  344. /* todo: do something */
  345. break;
  346. }
  347. return 0;
  348. }
  349. static int32_t
  350. corebootwrite(Chan *c, void *va, int32_t n, int64_t r)
  351. {
  352. switch((uint32_t)c->qid.path){
  353. case Qdir:
  354. error(Eisdir);
  355. break;
  356. default:
  357. error(Eperm);
  358. break;
  359. }
  360. error(Egreg);
  361. return -1;
  362. }
  363. Dev corebootdevtab = {
  364. .dc = 'Y',
  365. .name = "coreboot",
  366. .reset = devreset,
  367. .init = corebootinit,
  368. .shutdown = devshutdown,
  369. .attach = corebootattach,
  370. .walk = corebootwalk,
  371. .stat = corebootstat,
  372. .open = corebootopen,
  373. .create = corebootcreate,
  374. .close = corebootclose,
  375. .read = corebootread,
  376. .bread = devbread,
  377. .write = corebootwrite,
  378. .bwrite = devbwrite,
  379. .remove = devremove,
  380. .wstat = devwstat,
  381. };