cb_sysinfo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * This file is part of the libpayload project.
  4. *
  5. * Copyright (C) 2008 Advanced Micro Devices, Inc.
  6. * Copyright (C) 2009 coresystems GmbH
  7. */
  8. #include <common.h>
  9. #include <asm/cb_sysinfo.h>
  10. #include <init.h>
  11. #include <mapmem.h>
  12. #include <net.h>
  13. #include <asm/global_data.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. /*
  16. * This needs to be in the .data section so that it's copied over during
  17. * relocation. By default it's put in the .bss section which is simply filled
  18. * with zeroes when transitioning from "ROM", which is really RAM, to other
  19. * RAM.
  20. */
  21. struct sysinfo_t lib_sysinfo __section(".data");
  22. /*
  23. * Some of this is x86 specific, and the rest of it is generic. Right now,
  24. * since we only support x86, we'll avoid trying to make lots of infrastructure
  25. * we don't need. If in the future, we want to use coreboot on some other
  26. * architecture, then take out the generic parsing code and move it elsewhere.
  27. */
  28. /* === Parsing code === */
  29. /* This is the generic parsing code */
  30. static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
  31. {
  32. struct cb_memory *mem = (struct cb_memory *)ptr;
  33. int count = MEM_RANGE_COUNT(mem);
  34. int i;
  35. if (count > SYSINFO_MAX_MEM_RANGES)
  36. count = SYSINFO_MAX_MEM_RANGES;
  37. info->n_memranges = 0;
  38. for (i = 0; i < count; i++) {
  39. struct cb_memory_range *range =
  40. (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
  41. info->memrange[info->n_memranges].base =
  42. UNPACK_CB64(range->start);
  43. info->memrange[info->n_memranges].size =
  44. UNPACK_CB64(range->size);
  45. info->memrange[info->n_memranges].type = range->type;
  46. info->n_memranges++;
  47. }
  48. }
  49. static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
  50. {
  51. struct cb_serial *ser = (struct cb_serial *)ptr;
  52. info->serial = ser;
  53. }
  54. static void cb_parse_vboot_handoff(unsigned char *ptr, struct sysinfo_t *info)
  55. {
  56. struct lb_range *vbho = (struct lb_range *)ptr;
  57. info->vboot_handoff = (void *)(uintptr_t)vbho->range_start;
  58. info->vboot_handoff_size = vbho->range_size;
  59. }
  60. static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
  61. {
  62. struct lb_range *vbnv = (struct lb_range *)ptr;
  63. info->vbnv_start = vbnv->range_start;
  64. info->vbnv_size = vbnv->range_size;
  65. }
  66. static void cb_parse_cbmem_entry(unsigned char *ptr, struct sysinfo_t *info)
  67. {
  68. struct cb_cbmem_entry *entry = (struct cb_cbmem_entry *)ptr;
  69. if (entry->id != CBMEM_ID_SMBIOS)
  70. return;
  71. info->smbios_start = entry->address;
  72. info->smbios_size = entry->entry_size;
  73. }
  74. static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
  75. {
  76. int i;
  77. struct cb_gpios *gpios = (struct cb_gpios *)ptr;
  78. info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
  79. (gpios->count) : SYSINFO_MAX_GPIOS;
  80. for (i = 0; i < info->num_gpios; i++)
  81. info->gpios[i] = gpios->gpios[i];
  82. }
  83. static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
  84. {
  85. struct lb_range *vdat = (struct lb_range *)ptr;
  86. info->vdat_addr = map_sysmem(vdat->range_start, vdat->range_size);
  87. info->vdat_size = vdat->range_size;
  88. }
  89. static void cb_parse_mac_addresses(unsigned char *ptr,
  90. struct sysinfo_t *info)
  91. {
  92. struct cb_macs *macs = (struct cb_macs *)ptr;
  93. int i;
  94. info->num_macs = (macs->count < ARRAY_SIZE(info->macs)) ?
  95. macs->count : ARRAY_SIZE(info->macs);
  96. for (i = 0; i < info->num_macs; i++)
  97. info->macs[i] = macs->mac_addrs[i];
  98. }
  99. static void cb_parse_tstamp(void *ptr, struct sysinfo_t *info)
  100. {
  101. struct cb_cbmem_tab *const cbmem = ptr;
  102. info->tstamp_table = map_sysmem(cbmem->cbmem_tab, 0);
  103. }
  104. static void cb_parse_cbmem_cons(void *ptr, struct sysinfo_t *info)
  105. {
  106. struct cb_cbmem_tab *const cbmem = ptr;
  107. info->cbmem_cons = map_sysmem(cbmem->cbmem_tab, 0);
  108. }
  109. static void cb_parse_acpi_gnvs(unsigned char *ptr, struct sysinfo_t *info)
  110. {
  111. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  112. info->acpi_gnvs = map_sysmem(cbmem->cbmem_tab, 0);
  113. }
  114. static void cb_parse_board_id(unsigned char *ptr, struct sysinfo_t *info)
  115. {
  116. struct cb_board_id *const cbbid = (struct cb_board_id *)ptr;
  117. info->board_id = cbbid->board_id;
  118. }
  119. static void cb_parse_ram_code(unsigned char *ptr, struct sysinfo_t *info)
  120. {
  121. struct cb_ram_code *const ram_code = (struct cb_ram_code *)ptr;
  122. info->ram_code = ram_code->ram_code;
  123. }
  124. static void cb_parse_optiontable(void *ptr, struct sysinfo_t *info)
  125. {
  126. /* ptr points to a coreboot table entry and is already virtual */
  127. info->option_table = ptr;
  128. }
  129. static void cb_parse_checksum(void *ptr, struct sysinfo_t *info)
  130. {
  131. struct cb_cmos_checksum *cmos_cksum = ptr;
  132. info->cmos_range_start = cmos_cksum->range_start;
  133. info->cmos_range_end = cmos_cksum->range_end;
  134. info->cmos_checksum_location = cmos_cksum->location;
  135. }
  136. static void cb_parse_framebuffer(void *ptr, struct sysinfo_t *info)
  137. {
  138. /* ptr points to a coreboot table entry and is already virtual */
  139. info->framebuffer = ptr;
  140. }
  141. static void cb_parse_string(unsigned char *ptr, char **info)
  142. {
  143. *info = (char *)((struct cb_string *)ptr)->string;
  144. }
  145. static void cb_parse_wifi_calibration(void *ptr, struct sysinfo_t *info)
  146. {
  147. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  148. info->wifi_calibration = map_sysmem(cbmem->cbmem_tab, 0);
  149. }
  150. static void cb_parse_ramoops(void *ptr, struct sysinfo_t *info)
  151. {
  152. struct lb_range *ramoops = (struct lb_range *)ptr;
  153. info->ramoops_buffer = ramoops->range_start;
  154. info->ramoops_buffer_size = ramoops->range_size;
  155. }
  156. static void cb_parse_mtc(void *ptr, struct sysinfo_t *info)
  157. {
  158. struct lb_range *mtc = (struct lb_range *)ptr;
  159. info->mtc_start = mtc->range_start;
  160. info->mtc_size = mtc->range_size;
  161. }
  162. static void cb_parse_spi_flash(void *ptr, struct sysinfo_t *info)
  163. {
  164. struct cb_spi_flash *flash = (struct cb_spi_flash *)ptr;
  165. info->spi_flash.size = flash->flash_size;
  166. info->spi_flash.sector_size = flash->sector_size;
  167. info->spi_flash.erase_cmd = flash->erase_cmd;
  168. }
  169. static void cb_parse_boot_media_params(unsigned char *ptr,
  170. struct sysinfo_t *info)
  171. {
  172. struct cb_boot_media_params *const bmp =
  173. (struct cb_boot_media_params *)ptr;
  174. info->fmap_offset = bmp->fmap_offset;
  175. info->cbfs_offset = bmp->cbfs_offset;
  176. info->cbfs_size = bmp->cbfs_size;
  177. info->boot_media_size = bmp->boot_media_size;
  178. }
  179. static void cb_parse_vpd(void *ptr, struct sysinfo_t *info)
  180. {
  181. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  182. info->chromeos_vpd = map_sysmem(cbmem->cbmem_tab, 0);
  183. }
  184. static void cb_parse_tsc_info(void *ptr, struct sysinfo_t *info)
  185. {
  186. const struct cb_tsc_info *tsc_info = ptr;
  187. if (tsc_info->freq_khz == 0)
  188. return;
  189. /* Honor the TSC frequency passed to the payload */
  190. info->cpu_khz = tsc_info->freq_khz;
  191. }
  192. static void cb_parse_x86_rom_var_mtrr(void *ptr, struct sysinfo_t *info)
  193. {
  194. struct cb_x86_rom_mtrr *rom_mtrr = ptr;
  195. info->x86_rom_var_mtrr_index = rom_mtrr->index;
  196. }
  197. static void cb_parse_mrc_cache(void *ptr, struct sysinfo_t *info)
  198. {
  199. struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
  200. info->mrc_cache = map_sysmem(cbmem->cbmem_tab, 0);
  201. }
  202. __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr)
  203. {
  204. }
  205. static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  206. {
  207. unsigned char *ptr = addr;
  208. struct cb_header *header;
  209. int i;
  210. header = (struct cb_header *)ptr;
  211. if (!header->table_bytes)
  212. return 0;
  213. /* Make sure the checksums match */
  214. if (!ip_checksum_ok(header, sizeof(*header)))
  215. return -1;
  216. if (compute_ip_checksum(ptr + sizeof(*header), header->table_bytes) !=
  217. header->table_checksum)
  218. return -1;
  219. info->header = header;
  220. /*
  221. * Board straps represented by numerical values are small numbers.
  222. * Preset them to an invalid value in case the firmware does not
  223. * supply the info.
  224. */
  225. info->board_id = ~0;
  226. info->ram_code = ~0;
  227. /* Now, walk the tables */
  228. ptr += header->header_bytes;
  229. /* Inintialize some fields to sentinel values */
  230. info->vbnv_start = info->vbnv_size = (uint32_t)(-1);
  231. for (i = 0; i < header->table_entries; i++) {
  232. struct cb_record *rec = (struct cb_record *)ptr;
  233. /* We only care about a few tags here (maybe more later) */
  234. switch (rec->tag) {
  235. case CB_TAG_FORWARD:
  236. return cb_parse_header(
  237. (void *)(unsigned long)
  238. ((struct cb_forward *)rec)->forward,
  239. len, info);
  240. continue;
  241. case CB_TAG_MEMORY:
  242. cb_parse_memory(ptr, info);
  243. break;
  244. case CB_TAG_SERIAL:
  245. cb_parse_serial(ptr, info);
  246. break;
  247. case CB_TAG_VERSION:
  248. cb_parse_string(ptr, &info->cb_version);
  249. break;
  250. case CB_TAG_EXTRA_VERSION:
  251. cb_parse_string(ptr, &info->extra_version);
  252. break;
  253. case CB_TAG_BUILD:
  254. cb_parse_string(ptr, &info->build);
  255. break;
  256. case CB_TAG_COMPILE_TIME:
  257. cb_parse_string(ptr, &info->compile_time);
  258. break;
  259. case CB_TAG_COMPILE_BY:
  260. cb_parse_string(ptr, &info->compile_by);
  261. break;
  262. case CB_TAG_COMPILE_HOST:
  263. cb_parse_string(ptr, &info->compile_host);
  264. break;
  265. case CB_TAG_COMPILE_DOMAIN:
  266. cb_parse_string(ptr, &info->compile_domain);
  267. break;
  268. case CB_TAG_COMPILER:
  269. cb_parse_string(ptr, &info->compiler);
  270. break;
  271. case CB_TAG_LINKER:
  272. cb_parse_string(ptr, &info->linker);
  273. break;
  274. case CB_TAG_ASSEMBLER:
  275. cb_parse_string(ptr, &info->assembler);
  276. break;
  277. case CB_TAG_CMOS_OPTION_TABLE:
  278. cb_parse_optiontable(ptr, info);
  279. break;
  280. case CB_TAG_OPTION_CHECKSUM:
  281. cb_parse_checksum(ptr, info);
  282. break;
  283. /*
  284. * FIXME we should warn on serial if coreboot set up a
  285. * framebuffer buf the payload does not know about it.
  286. */
  287. case CB_TAG_FRAMEBUFFER:
  288. cb_parse_framebuffer(ptr, info);
  289. break;
  290. case CB_TAG_MAINBOARD:
  291. info->mainboard = (struct cb_mainboard *)ptr;
  292. break;
  293. case CB_TAG_GPIO:
  294. cb_parse_gpios(ptr, info);
  295. break;
  296. case CB_TAG_VDAT:
  297. cb_parse_vdat(ptr, info);
  298. break;
  299. case CB_TAG_VBNV:
  300. cb_parse_vbnv(ptr, info);
  301. break;
  302. case CB_TAG_VBOOT_HANDOFF:
  303. cb_parse_vboot_handoff(ptr, info);
  304. break;
  305. case CB_TAG_MAC_ADDRS:
  306. cb_parse_mac_addresses(ptr, info);
  307. break;
  308. case CB_TAG_SERIALNO:
  309. cb_parse_string(ptr, &info->serialno);
  310. break;
  311. case CB_TAG_TIMESTAMPS:
  312. cb_parse_tstamp(ptr, info);
  313. break;
  314. case CB_TAG_CBMEM_CONSOLE:
  315. cb_parse_cbmem_cons(ptr, info);
  316. break;
  317. case CB_TAG_ACPI_GNVS:
  318. cb_parse_acpi_gnvs(ptr, info);
  319. break;
  320. case CB_TAG_CBMEM_ENTRY:
  321. cb_parse_cbmem_entry(ptr, info);
  322. break;
  323. case CB_TAG_BOARD_ID:
  324. cb_parse_board_id(ptr, info);
  325. break;
  326. case CB_TAG_RAM_CODE:
  327. cb_parse_ram_code(ptr, info);
  328. break;
  329. case CB_TAG_WIFI_CALIBRATION:
  330. cb_parse_wifi_calibration(ptr, info);
  331. break;
  332. case CB_TAG_RAM_OOPS:
  333. cb_parse_ramoops(ptr, info);
  334. break;
  335. case CB_TAG_SPI_FLASH:
  336. cb_parse_spi_flash(ptr, info);
  337. break;
  338. case CB_TAG_MTC:
  339. cb_parse_mtc(ptr, info);
  340. break;
  341. case CB_TAG_BOOT_MEDIA_PARAMS:
  342. cb_parse_boot_media_params(ptr, info);
  343. break;
  344. case CB_TAG_TSC_INFO:
  345. cb_parse_tsc_info(ptr, info);
  346. break;
  347. case CB_TAG_VPD:
  348. cb_parse_vpd(ptr, info);
  349. break;
  350. case CB_TAG_X86_ROM_MTRR:
  351. cb_parse_x86_rom_var_mtrr(rec, info);
  352. break;
  353. case CB_TAG_MRC_CACHE:
  354. cb_parse_mrc_cache(rec, info);
  355. break;
  356. default:
  357. cb_parse_unhandled(rec->tag, ptr);
  358. break;
  359. }
  360. ptr += rec->size;
  361. }
  362. return 1;
  363. }
  364. /* == Architecture specific == */
  365. /* This is the x86 specific stuff */
  366. int get_coreboot_info(struct sysinfo_t *info)
  367. {
  368. long addr;
  369. int ret;
  370. addr = locate_coreboot_table();
  371. if (addr < 0)
  372. return addr;
  373. ret = cb_parse_header((void *)addr, 0x1000, info);
  374. if (!ret)
  375. return -ENOENT;
  376. gd->arch.coreboot_table = addr;
  377. gd->flags |= GD_FLG_SKIP_LL_INIT;
  378. return 0;
  379. }
  380. const struct sysinfo_t *cb_get_sysinfo(void)
  381. {
  382. if (!ll_boot_init())
  383. return &lib_sysinfo;
  384. return NULL;
  385. }