tables.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <common.h>
  31. #include <asm/arch-coreboot/ipchecksum.h>
  32. #include <asm/arch-coreboot/sysinfo.h>
  33. #include <asm/arch-coreboot/tables.h>
  34. /*
  35. * This needs to be in the .data section so that it's copied over during
  36. * relocation. By default it's put in the .bss section which is simply filled
  37. * with zeroes when transitioning from "ROM", which is really RAM, to other
  38. * RAM.
  39. */
  40. struct sysinfo_t lib_sysinfo __attribute__((section(".data")));
  41. /*
  42. * Some of this is x86 specific, and the rest of it is generic. Right now,
  43. * since we only support x86, we'll avoid trying to make lots of infrastructure
  44. * we don't need. If in the future, we want to use coreboot on some other
  45. * architecture, then take out the generic parsing code and move it elsewhere.
  46. */
  47. /* === Parsing code === */
  48. /* This is the generic parsing code. */
  49. static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
  50. {
  51. struct cb_memory *mem = (struct cb_memory *)ptr;
  52. int count = MEM_RANGE_COUNT(mem);
  53. int i;
  54. if (count > SYSINFO_MAX_MEM_RANGES)
  55. count = SYSINFO_MAX_MEM_RANGES;
  56. info->n_memranges = 0;
  57. for (i = 0; i < count; i++) {
  58. struct cb_memory_range *range =
  59. (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
  60. info->memrange[info->n_memranges].base =
  61. UNPACK_CB64(range->start);
  62. info->memrange[info->n_memranges].size =
  63. UNPACK_CB64(range->size);
  64. info->memrange[info->n_memranges].type = range->type;
  65. info->n_memranges++;
  66. }
  67. }
  68. static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
  69. {
  70. struct cb_serial *ser = (struct cb_serial *)ptr;
  71. info->serial = ser;
  72. }
  73. static void cb_parse_vbnv(unsigned char *ptr, struct sysinfo_t *info)
  74. {
  75. struct cb_vbnv *vbnv = (struct cb_vbnv *)ptr;
  76. info->vbnv_start = vbnv->vbnv_start;
  77. info->vbnv_size = vbnv->vbnv_size;
  78. }
  79. static void cb_parse_gpios(unsigned char *ptr, struct sysinfo_t *info)
  80. {
  81. int i;
  82. struct cb_gpios *gpios = (struct cb_gpios *)ptr;
  83. info->num_gpios = (gpios->count < SYSINFO_MAX_GPIOS) ?
  84. (gpios->count) : SYSINFO_MAX_GPIOS;
  85. for (i = 0; i < info->num_gpios; i++)
  86. info->gpios[i] = gpios->gpios[i];
  87. }
  88. static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
  89. {
  90. struct cb_vdat *vdat = (struct cb_vdat *) ptr;
  91. info->vdat_addr = vdat->vdat_addr;
  92. info->vdat_size = vdat->vdat_size;
  93. }
  94. static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
  95. {
  96. info->tstamp_table = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
  97. }
  98. static void cb_parse_cbmem_cons(unsigned char *ptr, struct sysinfo_t *info)
  99. {
  100. info->cbmem_cons = ((struct cb_cbmem_tab *)ptr)->cbmem_tab;
  101. }
  102. static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
  103. {
  104. info->framebuffer = (struct cb_framebuffer *)ptr;
  105. }
  106. static void cb_parse_string(unsigned char *ptr, char **info)
  107. {
  108. *info = (char *)((struct cb_string *)ptr)->string;
  109. }
  110. static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  111. {
  112. struct cb_header *header;
  113. unsigned char *ptr = (unsigned char *)addr;
  114. int i;
  115. for (i = 0; i < len; i += 16, ptr += 16) {
  116. header = (struct cb_header *)ptr;
  117. if (!strncmp((const char *)header->signature, "LBIO", 4))
  118. break;
  119. }
  120. /* We walked the entire space and didn't find anything. */
  121. if (i >= len)
  122. return -1;
  123. if (!header->table_bytes)
  124. return 0;
  125. /* Make sure the checksums match. */
  126. if (ipchksum((u16 *) header, sizeof(*header)) != 0)
  127. return -1;
  128. if (ipchksum((u16 *) (ptr + sizeof(*header)),
  129. header->table_bytes) != header->table_checksum)
  130. return -1;
  131. /* Now, walk the tables. */
  132. ptr += header->header_bytes;
  133. /* Inintialize some fields to sentinel values. */
  134. info->vbnv_start = info->vbnv_size = (uint32_t)(-1);
  135. for (i = 0; i < header->table_entries; i++) {
  136. struct cb_record *rec = (struct cb_record *)ptr;
  137. /* We only care about a few tags here (maybe more later). */
  138. switch (rec->tag) {
  139. case CB_TAG_FORWARD:
  140. return cb_parse_header(
  141. (void *)(unsigned long)
  142. ((struct cb_forward *)rec)->forward,
  143. len, info);
  144. continue;
  145. case CB_TAG_MEMORY:
  146. cb_parse_memory(ptr, info);
  147. break;
  148. case CB_TAG_SERIAL:
  149. cb_parse_serial(ptr, info);
  150. break;
  151. case CB_TAG_VERSION:
  152. cb_parse_string(ptr, &info->version);
  153. break;
  154. case CB_TAG_EXTRA_VERSION:
  155. cb_parse_string(ptr, &info->extra_version);
  156. break;
  157. case CB_TAG_BUILD:
  158. cb_parse_string(ptr, &info->build);
  159. break;
  160. case CB_TAG_COMPILE_TIME:
  161. cb_parse_string(ptr, &info->compile_time);
  162. break;
  163. case CB_TAG_COMPILE_BY:
  164. cb_parse_string(ptr, &info->compile_by);
  165. break;
  166. case CB_TAG_COMPILE_HOST:
  167. cb_parse_string(ptr, &info->compile_host);
  168. break;
  169. case CB_TAG_COMPILE_DOMAIN:
  170. cb_parse_string(ptr, &info->compile_domain);
  171. break;
  172. case CB_TAG_COMPILER:
  173. cb_parse_string(ptr, &info->compiler);
  174. break;
  175. case CB_TAG_LINKER:
  176. cb_parse_string(ptr, &info->linker);
  177. break;
  178. case CB_TAG_ASSEMBLER:
  179. cb_parse_string(ptr, &info->assembler);
  180. break;
  181. /*
  182. * FIXME we should warn on serial if coreboot set up a
  183. * framebuffer buf the payload does not know about it.
  184. */
  185. case CB_TAG_FRAMEBUFFER:
  186. cb_parse_framebuffer(ptr, info);
  187. break;
  188. case CB_TAG_GPIO:
  189. cb_parse_gpios(ptr, info);
  190. break;
  191. case CB_TAG_VDAT:
  192. cb_parse_vdat(ptr, info);
  193. break;
  194. case CB_TAG_TIMESTAMPS:
  195. cb_parse_tstamp(ptr, info);
  196. break;
  197. case CB_TAG_CBMEM_CONSOLE:
  198. cb_parse_cbmem_cons(ptr, info);
  199. break;
  200. case CB_TAG_VBNV:
  201. cb_parse_vbnv(ptr, info);
  202. break;
  203. }
  204. ptr += rec->size;
  205. }
  206. return 1;
  207. }
  208. /* == Architecture specific == */
  209. /* This is the x86 specific stuff. */
  210. int get_coreboot_info(struct sysinfo_t *info)
  211. {
  212. int ret = cb_parse_header((void *)0x00000000, 0x1000, info);
  213. if (ret != 1)
  214. ret = cb_parse_header((void *)0x000f0000, 0x1000, info);
  215. return (ret == 1) ? 0 : -1;
  216. }