tables.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <net.h>
  10. #include <asm/cb_sysinfo.h>
  11. #include <asm/global_data.h>
  12. DECLARE_GLOBAL_DATA_PTR;
  13. /*
  14. * This needs to be in the .data section so that it's copied over during
  15. * relocation. By default it's put in the .bss section which is simply filled
  16. * with zeroes when transitioning from "ROM", which is really RAM, to other
  17. * RAM.
  18. */
  19. struct sysinfo_t lib_sysinfo __attribute__((section(".data")));
  20. /*
  21. * Some of this is x86 specific, and the rest of it is generic. Right now,
  22. * since we only support x86, we'll avoid trying to make lots of infrastructure
  23. * we don't need. If in the future, we want to use coreboot on some other
  24. * architecture, then take out the generic parsing code and move it elsewhere.
  25. */
  26. /* === Parsing code === */
  27. /* This is the generic parsing code. */
  28. static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
  29. {
  30. struct cb_memory *mem = (struct cb_memory *)ptr;
  31. int count = MEM_RANGE_COUNT(mem);
  32. int i;
  33. if (count > SYSINFO_MAX_MEM_RANGES)
  34. count = SYSINFO_MAX_MEM_RANGES;
  35. info->n_memranges = 0;
  36. for (i = 0; i < count; i++) {
  37. struct cb_memory_range *range =
  38. (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
  39. info->memrange[info->n_memranges].base =
  40. UNPACK_CB64(range->start);
  41. info->memrange[info->n_memranges].size =
  42. UNPACK_CB64(range->size);
  43. info->memrange[info->n_memranges].type = range->type;
  44. info->n_memranges++;
  45. }
  46. }
  47. static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
  48. {
  49. struct cb_serial *ser = (struct cb_serial *)ptr;
  50. info->serial = ser;
  51. }
  52. static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
  53. {
  54. struct cb_vbnv *vbnv = (struct cb_vbnv *)ptr;
  55. info->vbnv_start = vbnv->vbnv_start;
  56. info->vbnv_size = vbnv->vbnv_size;
  57. }
  58. static void cb_parse_cbmem_entry(unsigned char *ptr, struct sysinfo_t *info)
  59. {
  60. struct cb_cbmem_entry *entry = (struct cb_cbmem_entry *)ptr;
  61. if (entry->id != CBMEM_ID_SMBIOS)
  62. return;
  63. info->smbios_start = entry->address;
  64. info->smbios_size = entry->entry_size;
  65. }
  66. static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
  67. {
  68. int i;
  69. struct cb_gpios *gpios = (struct cb_gpios *)ptr;
  70. info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
  71. (gpios->count) : SYSINFO_MAX_GPIOS;
  72. for (i = 0; i < info->num_gpios; i++)
  73. info->gpios[i] = gpios->gpios[i];
  74. }
  75. static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
  76. {
  77. struct cb_vdat *vdat = (struct cb_vdat *) ptr;
  78. info->vdat_addr = vdat->vdat_addr;
  79. info->vdat_size = vdat->vdat_size;
  80. }
  81. static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
  82. {
  83. info->tstamp_table = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
  84. }
  85. static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
  86. {
  87. info->cbmem_cons = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
  88. }
  89. static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
  90. {
  91. info->framebuffer = (struct cb_framebuffer *)ptr;
  92. }
  93. static void cb_parse_string(unsigned char *ptr, char **info)
  94. {
  95. *info = (char *)((struct cb_string *)ptr)->string;
  96. }
  97. __weak void cb_parse_unhandled(u32 tag, unsigned char *ptr)
  98. {
  99. }
  100. static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  101. {
  102. unsigned char *ptr = addr;
  103. struct cb_header *header;
  104. int i;
  105. header = (struct cb_header *)ptr;
  106. if (!header->table_bytes)
  107. return 0;
  108. /* Make sure the checksums match. */
  109. if (!ip_checksum_ok(header, sizeof(*header)))
  110. return -1;
  111. if (compute_ip_checksum(ptr + sizeof(*header), header->table_bytes) !=
  112. header->table_checksum)
  113. return -1;
  114. /* Now, walk the tables. */
  115. ptr += header->header_bytes;
  116. /* Inintialize some fields to sentinel values. */
  117. info->vbnv_start = info->vbnv_size = (uint32_t)(-1);
  118. for (i = 0; i < header->table_entries; i++) {
  119. struct cb_record *rec = (struct cb_record *)ptr;
  120. /* We only care about a few tags here (maybe more later). */
  121. switch (rec->tag) {
  122. case CB_TAG_FORWARD:
  123. return cb_parse_header(
  124. (void *)(unsigned long)
  125. ((struct cb_forward *)rec)->forward,
  126. len, info);
  127. continue;
  128. case CB_TAG_MEMORY:
  129. cb_parse_memory(ptr, info);
  130. break;
  131. case CB_TAG_SERIAL:
  132. cb_parse_serial(ptr, info);
  133. break;
  134. case CB_TAG_VERSION:
  135. cb_parse_string(ptr, &info->version);
  136. break;
  137. case CB_TAG_EXTRA_VERSION:
  138. cb_parse_string(ptr, &info->extra_version);
  139. break;
  140. case CB_TAG_BUILD:
  141. cb_parse_string(ptr, &info->build);
  142. break;
  143. case CB_TAG_COMPILE_TIME:
  144. cb_parse_string(ptr, &info->compile_time);
  145. break;
  146. case CB_TAG_COMPILE_BY:
  147. cb_parse_string(ptr, &info->compile_by);
  148. break;
  149. case CB_TAG_COMPILE_HOST:
  150. cb_parse_string(ptr, &info->compile_host);
  151. break;
  152. case CB_TAG_COMPILE_DOMAIN:
  153. cb_parse_string(ptr, &info->compile_domain);
  154. break;
  155. case CB_TAG_COMPILER:
  156. cb_parse_string(ptr, &info->compiler);
  157. break;
  158. case CB_TAG_LINKER:
  159. cb_parse_string(ptr, &info->linker);
  160. break;
  161. case CB_TAG_ASSEMBLER:
  162. cb_parse_string(ptr, &info->assembler);
  163. break;
  164. /*
  165. * FIXME we should warn on serial if coreboot set up a
  166. * framebuffer buf the payload does not know about it.
  167. */
  168. case CB_TAG_FRAMEBUFFER:
  169. cb_parse_framebuffer(ptr, info);
  170. break;
  171. case CB_TAG_GPIO:
  172. cb_parse_gpios(ptr, info);
  173. break;
  174. case CB_TAG_VDAT:
  175. cb_parse_vdat(ptr, info);
  176. break;
  177. case CB_TAG_TIMESTAMPS:
  178. cb_parse_tstamp(ptr, info);
  179. break;
  180. case CB_TAG_CBMEM_CONSOLE:
  181. cb_parse_cbmem_cons(ptr, info);
  182. break;
  183. case CB_TAG_VBNV:
  184. cb_parse_vbnv(ptr, info);
  185. break;
  186. case CB_TAG_CBMEM_ENTRY:
  187. cb_parse_cbmem_entry(ptr, info);
  188. break;
  189. default:
  190. cb_parse_unhandled(rec->tag, ptr);
  191. break;
  192. }
  193. ptr += rec->size;
  194. }
  195. return 1;
  196. }
  197. /* == Architecture specific == */
  198. /* This is the x86 specific stuff. */
  199. int get_coreboot_info(struct sysinfo_t *info)
  200. {
  201. long addr;
  202. int ret;
  203. addr = locate_coreboot_table();
  204. if (addr < 0)
  205. return addr;
  206. ret = cb_parse_header((void *)addr, 0x1000, info);
  207. if (!ret)
  208. return -ENOENT;
  209. gd->arch.coreboot_table = addr;
  210. gd->flags |= GD_FLG_SKIP_LL_INIT;
  211. return 0;
  212. }