devcoreboot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #define I_AM_HERE print("Core 0 is in %s() at %s:%d\n", \
  46. __FUNCTION__, __FILE__, __LINE__);
  47. static void cb_parse_memory(void *ptr, struct sysinfo_t *info)
  48. { print("%s\n", __func__);
  49. struct cb_memory *mem = ptr;
  50. int count = MEM_RANGE_COUNT(mem);
  51. int i;
  52. if (count > SYSINFO_MAX_MEM_RANGES)
  53. count = SYSINFO_MAX_MEM_RANGES;
  54. info->n_memranges = 0;
  55. for (i = 0; i < count; i++) {
  56. struct cb_memory_range *range = MEM_RANGE_PTR(mem, i);
  57. info->memrange[info->n_memranges].base =
  58. cb_unpack64(range->start);
  59. info->memrange[info->n_memranges].size =
  60. cb_unpack64(range->size);
  61. info->memrange[info->n_memranges].type = range->type;
  62. info->n_memranges++;
  63. }
  64. }
  65. static void cb_parse_serial(void *ptr, struct sysinfo_t *info)
  66. { print("%s\n", __func__);
  67. info->serial = ((struct cb_serial *)ptr);
  68. }
  69. static void cb_parse_vboot_handoff(unsigned char *ptr, struct sysinfo_t *info)
  70. { print("%s\n", __func__);
  71. struct cb_range *vbho = (struct cb_range *)ptr;
  72. info->vboot_handoff = (void *)(uintptr_t)vbho->range_start;
  73. info->vboot_handoff_size = vbho->range_size;
  74. }
  75. static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
  76. { print("%s\n", __func__);
  77. struct cb_range *vbnv = (struct cb_range *)ptr;
  78. info->vbnv_start = vbnv->range_start;
  79. info->vbnv_size = vbnv->range_size;
  80. }
  81. static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
  82. { print("%s\n", __func__);
  83. int i;
  84. struct cb_gpios *gpios = (struct cb_gpios *)ptr;
  85. info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
  86. (gpios->count) : SYSINFO_MAX_GPIOS;
  87. for (i = 0; i < info->num_gpios; i++)
  88. info->gpios[i] = gpios->gpios[i];
  89. }
  90. static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
  91. { print("%s\n", __func__);
  92. struct cb_range *vdat = (struct cb_range *) ptr;
  93. info->vdat_addr = KADDR(vdat->range_start);
  94. info->vdat_size = vdat->range_size;
  95. }
  96. static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
  97. { print("%s\n", __func__);
  98. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  99. info->tstamp_table = KADDR(cbmem->cbmem_tab);
  100. }
  101. static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
  102. { print("%s\n", __func__);
  103. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  104. info->cbmem_cons = KADDR(cbmem->cbmem_tab);
  105. }
  106. static void cb_parse_mrc_cache(unsigned char *ptr, struct sysinfo_t *info)
  107. { print("%s\n", __func__);
  108. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  109. info->mrc_cache = KADDR(cbmem->cbmem_tab);
  110. }
  111. static void cb_parse_acpi_gnvs(unsigned char *ptr, struct sysinfo_t *info)
  112. { print("%s\n", __func__);
  113. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  114. info->acpi_gnvs = KADDR(cbmem->cbmem_tab);
  115. }
  116. static void cb_parse_optiontable(void *ptr, struct sysinfo_t *info)
  117. { print("%s\n", __func__);
  118. /* ptr points to a coreboot table entry and is already virtual */
  119. info->option_table = ptr;
  120. }
  121. static void cb_parse_checksum(void *ptr, struct sysinfo_t *info)
  122. { print("%s\n", __func__);
  123. struct cb_cmos_checksum *cmos_cksum = ptr;
  124. info->cmos_range_start = cmos_cksum->range_start;
  125. info->cmos_range_end = cmos_cksum->range_end;
  126. info->cmos_checksum_location = cmos_cksum->location;
  127. }
  128. static void cb_parse_framebuffer(void *ptr, struct sysinfo_t *info)
  129. { print("%s\n", __func__);
  130. /* ptr points to a coreboot table entry and is already virtual */
  131. info->framebuffer = ptr;
  132. }
  133. static void cb_parse_x86_rom_var_mtrr(void *ptr, struct sysinfo_t *info)
  134. {
  135. print("%s, ignoring MTRR information.\n", __func__);
  136. }
  137. static void cb_parse_string(unsigned char *ptr, char **info)
  138. { print("%s\n", __func__);
  139. *info = (char *)((struct cb_string *)ptr)->string;
  140. }
  141. int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  142. { print("%s\n", __func__);
  143. struct cb_header *header;
  144. unsigned char *ptr = addr;
  145. void *forward;
  146. int i;
  147. for (i = 0; i < len; i += 16, ptr += 16) {
  148. header = (struct cb_header *)ptr;
  149. if (0)
  150. print("Check header %p sig %p val %02x %02x %02x %02x\n", header, header->signature,
  151. header->signature[0],
  152. header->signature[1],
  153. header->signature[2],
  154. header->signature[3]);
  155. if (!strncmp((char *)header->signature, "LBIO", 4))
  156. break;
  157. }
  158. print("found ? i %d len %d\n", i, len);
  159. /* We walked the entire space and didn't find anything. */
  160. if (i >= len)
  161. return -1;
  162. I_AM_HERE;
  163. if (!header->table_bytes)
  164. return 0;
  165. I_AM_HERE;
  166. /* Make sure the checksums match. */
  167. if (ipchksum((uint8_t *) header, sizeof(*header)) != 0)
  168. return -1;
  169. I_AM_HERE;
  170. if (ipchksum((uint8_t *) (ptr + sizeof(*header)),
  171. header->table_bytes) != header->table_checksum)
  172. return -1;
  173. I_AM_HERE;
  174. info->header = header;
  175. /* Now, walk the tables. */
  176. ptr += header->header_bytes;
  177. I_AM_HERE;
  178. for (i = 0; i < header->table_entries; i++) {
  179. struct cb_record *rec = (struct cb_record *)ptr;
  180. I_AM_HERE;
  181. print("rec %p tag %p\n", rec, rec->tag);
  182. /* We only care about a few tags here (maybe more later). */
  183. switch (rec->tag) {
  184. case CB_TAG_FORWARD:
  185. forward = KADDR((unsigned long)((struct cb_forward *)rec)->forward);
  186. print("FORWARD: %p %p\n", (unsigned long)((struct cb_forward *)rec)->forward,
  187. forward);
  188. return cb_parse_header(forward, len, info);
  189. continue;
  190. case CB_TAG_MEMORY:
  191. cb_parse_memory(ptr, info);
  192. break;
  193. case CB_TAG_SERIAL:
  194. cb_parse_serial(ptr, info);
  195. break;
  196. case CB_TAG_VERSION:
  197. cb_parse_string(ptr, &info->cb_version);
  198. break;
  199. case CB_TAG_EXTRA_VERSION:
  200. cb_parse_string(ptr, &info->extra_version);
  201. break;
  202. case CB_TAG_BUILD:
  203. cb_parse_string(ptr, &info->build);
  204. break;
  205. case CB_TAG_COMPILE_TIME:
  206. cb_parse_string(ptr, &info->compile_time);
  207. break;
  208. case CB_TAG_COMPILE_BY:
  209. cb_parse_string(ptr, &info->compile_by);
  210. break;
  211. case CB_TAG_COMPILE_HOST:
  212. cb_parse_string(ptr, &info->compile_host);
  213. break;
  214. case CB_TAG_COMPILE_DOMAIN:
  215. cb_parse_string(ptr, &info->compile_domain);
  216. break;
  217. case CB_TAG_COMPILER:
  218. cb_parse_string(ptr, &info->compiler);
  219. break;
  220. case CB_TAG_LINKER:
  221. cb_parse_string(ptr, &info->linker);
  222. break;
  223. case CB_TAG_ASSEMBLER:
  224. cb_parse_string(ptr, &info->assembler);
  225. break;
  226. case CB_TAG_CMOS_OPTION_TABLE:
  227. cb_parse_optiontable(ptr, info);
  228. break;
  229. case CB_TAG_OPTION_CHECKSUM:
  230. cb_parse_checksum(ptr, info);
  231. break;
  232. // FIXME we should warn on serial if coreboot set up a
  233. // framebuffer buf the payload does not know about it.
  234. case CB_TAG_FRAMEBUFFER:
  235. cb_parse_framebuffer(ptr, info);
  236. break;
  237. case CB_TAG_MAINBOARD:
  238. info->mainboard = (struct cb_mainboard *)ptr;
  239. case CB_TAG_GPIO:
  240. cb_parse_gpios(ptr, info);
  241. break;
  242. case CB_TAG_VDAT:
  243. cb_parse_vdat(ptr, info);
  244. break;
  245. case CB_TAG_VBNV:
  246. cb_parse_vbnv(ptr, info);
  247. break;
  248. case CB_TAG_VBOOT_HANDOFF:
  249. cb_parse_vboot_handoff(ptr, info);
  250. break;
  251. case CB_TAG_TIMESTAMPS:
  252. cb_parse_tstamp(ptr, info);
  253. break;
  254. case CB_TAG_CBMEM_CONSOLE:
  255. cb_parse_cbmem_cons(ptr, info);
  256. break;
  257. case CB_TAG_MRC_CACHE:
  258. cb_parse_mrc_cache(ptr, info);
  259. break;
  260. case CB_TAG_ACPI_GNVS:
  261. cb_parse_acpi_gnvs(ptr, info);
  262. break;
  263. case CB_TAG_X86_ROM_MTRR:
  264. cb_parse_x86_rom_var_mtrr(ptr, info);
  265. break;
  266. }
  267. ptr += rec->size;
  268. }
  269. return 1;
  270. }
  271. enum{
  272. Qdir,
  273. Qtable,
  274. };
  275. static Dirtab corebootdir[]={
  276. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  277. "table", {Qtable}, 0, 0444,
  278. };
  279. static struct sysinfo_t cbinfo;
  280. static int
  281. corebootdevgen(Chan *c, char *name, Dirtab *tab, int ntab, int i, Dir *dp)
  282. {
  283. int rc;
  284. rc = devgen(c, name, tab, ntab, i, dp);
  285. return rc;
  286. }
  287. static void
  288. corebootinit(void)
  289. {
  290. get_coreboot_info(&cbinfo);
  291. }
  292. static Chan*
  293. corebootattach(char *spec)
  294. {
  295. return devattach('Y', spec);
  296. }
  297. static Walkqid*
  298. corebootwalk(Chan *c, Chan *nc, char **name, int nname)
  299. {
  300. Walkqid *wq;
  301. wq = devwalk(c, nc, name, nname, corebootdir, nelem(corebootdir), devgen);
  302. /* todo: cover any cases that need locking. There are none yet. */
  303. return wq;
  304. }
  305. static int
  306. corebootstat(Chan *c, unsigned char *db, int n)
  307. {
  308. return devstat(c, db, n, corebootdir, nelem(corebootdir), corebootdevgen);
  309. }
  310. static Chan*
  311. corebootopen(Chan *c, int omode)
  312. {
  313. switch((uint32_t)c->qid.path){
  314. case Qdir:
  315. if(omode != OREAD)
  316. error(Eperm);
  317. break;
  318. /* default are tables, which are unchanging, and readonly, so no need to lock them.
  319. * They live in reserved memory.
  320. */
  321. default:
  322. break;
  323. }
  324. c->mode = openmode(omode);
  325. c->flag |= COPEN;
  326. c->offset = 0;
  327. return c;
  328. }
  329. static void
  330. corebootcreate(Chan *c, char *j, int i, int u)
  331. {
  332. error(Eperm);
  333. }
  334. static void
  335. corebootclose(Chan *c)
  336. {
  337. /* anything to do? */
  338. }
  339. static int32_t
  340. corebootread(Chan *c, void *va, int32_t n, int64_t off)
  341. {
  342. switch((uint32_t)c->qid.path){
  343. case Qdir:
  344. return devdirread(c, va, n, corebootdir, nelem(corebootdir), corebootdevgen);
  345. case Qtable:
  346. /* todo: do something */
  347. break;
  348. }
  349. return 0;
  350. }
  351. static int32_t
  352. corebootwrite(Chan *c, void *va, int32_t n, int64_t r)
  353. {
  354. switch((uint32_t)c->qid.path){
  355. case Qdir:
  356. error(Eisdir);
  357. break;
  358. default:
  359. error(Eperm);
  360. break;
  361. }
  362. error(Egreg);
  363. return -1;
  364. }
  365. Dev corebootdevtab = {
  366. 'Y',
  367. "coreboot",
  368. devreset,
  369. corebootinit,
  370. devshutdown,
  371. corebootattach,
  372. corebootwalk,
  373. corebootstat,
  374. corebootopen,
  375. corebootcreate,
  376. corebootclose,
  377. corebootread,
  378. devbread,
  379. corebootwrite,
  380. devbwrite,
  381. devremove,
  382. devwstat,
  383. };