tables.c 5.7 KB

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