tables.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #include <common.h>
  10. #include <asm/arch-coreboot/ipchecksum.h>
  11. #include <asm/arch-coreboot/sysinfo.h>
  12. #include <asm/arch-coreboot/tables.h>
  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_gpios(unsigned char *ptr, struct sysinfo_t *info)
  59. {
  60. int i;
  61. struct cb_gpios *gpios = (struct cb_gpios *)ptr;
  62. info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
  63. (gpios->count) : SYSINFO_MAX_GPIOS;
  64. for (i = 0; i < info->num_gpios; i++)
  65. info->gpios[i] = gpios->gpios[i];
  66. }
  67. static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
  68. {
  69. struct cb_vdat *vdat = (struct cb_vdat *) ptr;
  70. info->vdat_addr = vdat->vdat_addr;
  71. info->vdat_size = vdat->vdat_size;
  72. }
  73. static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
  74. {
  75. info->tstamp_table = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
  76. }
  77. static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
  78. {
  79. info->cbmem_cons = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
  80. }
  81. static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
  82. {
  83. info->framebuffer = (struct cb_framebuffer *)ptr;
  84. }
  85. static void cb_parse_string(unsigned char *ptr, char **info)
  86. {
  87. *info = (char *)((struct cb_string *)ptr)->string;
  88. }
  89. static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  90. {
  91. struct cb_header *header;
  92. unsigned char *ptr = (unsigned char *)addr;
  93. int i;
  94. for (i = 0; i < len; i += 16, ptr += 16) {
  95. header = (struct cb_header *)ptr;
  96. if (!strncmp((const char *)header->signature, "LBIO", 4))
  97. break;
  98. }
  99. /* We walked the entire space and didn't find anything. */
  100. if (i >= len)
  101. return -1;
  102. if (!header->table_bytes)
  103. return 0;
  104. /* Make sure the checksums match. */
  105. if (ipchksum((u16 *) header, sizeof(*header)) != 0)
  106. return -1;
  107. if (ipchksum((u16 *) (ptr + sizeof(*header)),
  108. header->table_bytes) != header->table_checksum)
  109. return -1;
  110. /* Now, walk the tables. */
  111. ptr += header->header_bytes;
  112. /* Inintialize some fields to sentinel values. */
  113. info->vbnv_start = info->vbnv_size = (uint32_t)(-1);
  114. for (i = 0; i < header->table_entries; i++) {
  115. struct cb_record *rec = (struct cb_record *)ptr;
  116. /* We only care about a few tags here (maybe more later). */
  117. switch (rec->tag) {
  118. case CB_TAG_FORWARD:
  119. return cb_parse_header(
  120. (void *)(unsigned long)
  121. ((struct cb_forward *)rec)->forward,
  122. len, info);
  123. continue;
  124. case CB_TAG_MEMORY:
  125. cb_parse_memory(ptr, info);
  126. break;
  127. case CB_TAG_SERIAL:
  128. cb_parse_serial(ptr, info);
  129. break;
  130. case CB_TAG_VERSION:
  131. cb_parse_string(ptr, &info->version);
  132. break;
  133. case CB_TAG_EXTRA_VERSION:
  134. cb_parse_string(ptr, &info->extra_version);
  135. break;
  136. case CB_TAG_BUILD:
  137. cb_parse_string(ptr, &info->build);
  138. break;
  139. case CB_TAG_COMPILE_TIME:
  140. cb_parse_string(ptr, &info->compile_time);
  141. break;
  142. case CB_TAG_COMPILE_BY:
  143. cb_parse_string(ptr, &info->compile_by);
  144. break;
  145. case CB_TAG_COMPILE_HOST:
  146. cb_parse_string(ptr, &info->compile_host);
  147. break;
  148. case CB_TAG_COMPILE_DOMAIN:
  149. cb_parse_string(ptr, &info->compile_domain);
  150. break;
  151. case CB_TAG_COMPILER:
  152. cb_parse_string(ptr, &info->compiler);
  153. break;
  154. case CB_TAG_LINKER:
  155. cb_parse_string(ptr, &info->linker);
  156. break;
  157. case CB_TAG_ASSEMBLER:
  158. cb_parse_string(ptr, &info->assembler);
  159. break;
  160. /*
  161. * FIXME we should warn on serial if coreboot set up a
  162. * framebuffer buf the payload does not know about it.
  163. */
  164. case CB_TAG_FRAMEBUFFER:
  165. cb_parse_framebuffer(ptr, info);
  166. break;
  167. case CB_TAG_GPIO:
  168. cb_parse_gpios(ptr, info);
  169. break;
  170. case CB_TAG_VDAT:
  171. cb_parse_vdat(ptr, info);
  172. break;
  173. case CB_TAG_TIMESTAMPS:
  174. cb_parse_tstamp(ptr, info);
  175. break;
  176. case CB_TAG_CBMEM_CONSOLE:
  177. cb_parse_cbmem_cons(ptr, info);
  178. break;
  179. case CB_TAG_VBNV:
  180. cb_parse_vbnv(ptr, info);
  181. break;
  182. }
  183. ptr += rec->size;
  184. }
  185. return 1;
  186. }
  187. /* == Architecture specific == */
  188. /* This is the x86 specific stuff. */
  189. int get_coreboot_info(struct sysinfo_t *info)
  190. {
  191. int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
  192. if (ret != 1)
  193. ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
  194. return (ret == 1) ? 0 : -1;
  195. }