tables.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <asm/arch-coreboot/ipchecksum.h>
  31. #include <asm/arch-coreboot/sysinfo.h>
  32. #include <asm/arch-coreboot/tables.h>
  33. /*
  34. * Some of this is x86 specific, and the rest of it is generic. Right now,
  35. * since we only support x86, we'll avoid trying to make lots of infrastructure
  36. * we don't need. If in the future, we want to use coreboot on some other
  37. * architecture, then take out the generic parsing code and move it elsewhere.
  38. */
  39. /* === Parsing code === */
  40. /* This is the generic parsing code. */
  41. static void cb_parse_memory(unsigned char *ptr, struct sysinfo_t *info)
  42. {
  43. struct cb_memory *mem = (struct cb_memory *)ptr;
  44. int count = MEM_RANGE_COUNT(mem);
  45. int i;
  46. if (count > SYSINFO_MAX_MEM_RANGES)
  47. count = SYSINFO_MAX_MEM_RANGES;
  48. info->n_memranges = 0;
  49. for (i = 0; i < count; i++) {
  50. struct cb_memory_range *range =
  51. (struct cb_memory_range *)MEM_RANGE_PTR(mem, i);
  52. info->memrange[info->n_memranges].base =
  53. UNPACK_CB64(range->start);
  54. info->memrange[info->n_memranges].size =
  55. UNPACK_CB64(range->size);
  56. info->memrange[info->n_memranges].type = range->type;
  57. info->n_memranges++;
  58. }
  59. }
  60. static void cb_parse_serial(unsigned char *ptr, struct sysinfo_t *info)
  61. {
  62. struct cb_serial *ser = (struct cb_serial *)ptr;
  63. if (ser->type != CB_SERIAL_TYPE_IO_MAPPED)
  64. return;
  65. info->ser_ioport = ser->baseaddr;
  66. }
  67. static void cb_parse_optiontable(unsigned char *ptr, struct sysinfo_t *info)
  68. {
  69. info->option_table = (struct cb_cmos_option_table *)ptr;
  70. }
  71. static void cb_parse_checksum(unsigned char *ptr, struct sysinfo_t *info)
  72. {
  73. struct cb_cmos_checksum *cmos_cksum = (struct cb_cmos_checksum *)ptr;
  74. info->cmos_range_start = cmos_cksum->range_start;
  75. info->cmos_range_end = cmos_cksum->range_end;
  76. info->cmos_checksum_location = cmos_cksum->location;
  77. }
  78. static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
  79. {
  80. info->framebuffer = (struct cb_framebuffer *)ptr;
  81. }
  82. static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
  83. {
  84. struct cb_header *header;
  85. unsigned char *ptr = (unsigned char *)addr;
  86. int i;
  87. for (i = 0; i < len; i += 16, ptr += 16) {
  88. header = (struct cb_header *)ptr;
  89. if (!strncmp((const char *)header->signature, "LBIO", 4))
  90. break;
  91. }
  92. /* We walked the entire space and didn't find anything. */
  93. if (i >= len)
  94. return -1;
  95. if (!header->table_bytes)
  96. return 0;
  97. /* Make sure the checksums match. */
  98. if (ipchksum((u16 *) header, sizeof(*header)) != 0)
  99. return -1;
  100. if (ipchksum((u16 *) (ptr + sizeof(*header)),
  101. header->table_bytes) != header->table_checksum)
  102. return -1;
  103. /* Now, walk the tables. */
  104. ptr += header->header_bytes;
  105. for (i = 0; i < header->table_entries; i++) {
  106. struct cb_record *rec = (struct cb_record *)ptr;
  107. /* We only care about a few tags here (maybe more later). */
  108. switch (rec->tag) {
  109. case CB_TAG_FORWARD:
  110. return cb_parse_header(
  111. (void *)(unsigned long)
  112. ((struct cb_forward *)rec)->forward,
  113. len, info);
  114. continue;
  115. case CB_TAG_MEMORY:
  116. cb_parse_memory(ptr, info);
  117. break;
  118. case CB_TAG_SERIAL:
  119. cb_parse_serial(ptr, info);
  120. break;
  121. case CB_TAG_CMOS_OPTION_TABLE:
  122. cb_parse_optiontable(ptr, info);
  123. break;
  124. case CB_TAG_OPTION_CHECKSUM:
  125. cb_parse_checksum(ptr, info);
  126. break;
  127. /*
  128. * FIXME we should warn on serial if coreboot set up a
  129. * framebuffer buf the payload does not know about it.
  130. */
  131. case CB_TAG_FRAMEBUFFER:
  132. cb_parse_framebuffer(ptr, info);
  133. break;
  134. }
  135. ptr += rec->size;
  136. }
  137. return 1;
  138. }
  139. /* == Architecture specific == */
  140. /* This is the x86 specific stuff. */
  141. /* Assume no translation or that memory is identity mapped. */
  142. static void *phys_to_virt(unsigned long virt)
  143. {
  144. return (void *)(uintptr_t)virt;
  145. }
  146. int get_coreboot_info(struct sysinfo_t *info)
  147. {
  148. int ret = cb_parse_header(phys_to_virt(0x00000000), 0x1000, info);
  149. if (ret != 1)
  150. ret = cb_parse_header(phys_to_virt(0x000f0000), 0x1000, info);
  151. return (ret == 1) ? 0 : -1;
  152. }