tables.c 5.7 KB

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