fdtmap.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2013, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * Alternatively, this software may be distributed under the terms of the
  32. * GNU General Public License ("GPL") version 2 as published by the Free
  33. * Software Foundation.
  34. */
  35. #include <ctype.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <zlib.h>
  39. #include "flash.h"
  40. #include "fdtmap.h"
  41. #include "flash.h"
  42. #include "libfdt.h"
  43. #include "layout.h"
  44. #ifdef DEBUG
  45. #define debug(a, b...) printf(a, ##b)
  46. #else
  47. #define debug(a, b...)
  48. #endif
  49. /* An entry in the flashmap - we only care about the offset and length */
  50. struct fmap_entry {
  51. uint32_t offset;
  52. uint32_t length;
  53. };
  54. /**
  55. * Look up a property in a node and check that it has a minimum length.
  56. *
  57. * @blob: FDT blob
  58. * @node: node to examine
  59. * @prop_name: name of property to find
  60. * @min_len: minimum property length in bytes
  61. * @err: 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  62. found, or -FDT_ERR_BADLAYOUT if not enough data
  63. * @return pointer to cell, which is only valid if err == 0
  64. */
  65. static const void *get_prop_check_min_len(const void *blob, int node,
  66. const char *prop_name, int min_len, int *err)
  67. {
  68. const void *cell;
  69. int len;
  70. debug("%s: %s\n", __func__, prop_name);
  71. cell = fdt_getprop(blob, node, prop_name, &len);
  72. if (!cell)
  73. *err = -FDT_ERR_NOTFOUND;
  74. else if (len < min_len)
  75. *err = -FDT_ERR_BADLAYOUT;
  76. else
  77. *err = 0;
  78. return cell;
  79. }
  80. static int fdtdec_get_int_array(const void *blob, int node,
  81. const char *prop_name, uint32_t *array, int count)
  82. {
  83. const uint32_t *cell;
  84. int i, err = 0;
  85. debug("%s: %s\n", __func__, prop_name);
  86. cell = get_prop_check_min_len(blob, node, prop_name,
  87. sizeof(uint32_t) * count, &err);
  88. if (!err) {
  89. for (i = 0; i < count; i++)
  90. array[i] = fdt32_to_cpu(cell[i]);
  91. }
  92. return err;
  93. }
  94. /**
  95. * Read a flash entry from the fdt
  96. *
  97. * @blob: FDT blob
  98. * @node: Offset of node to read
  99. * @name: Name of node being read
  100. * @entry: Place to put offset and size of this node
  101. * @return 0 if ok, -ve on error
  102. */
  103. static int read_entry(const void *blob, int node, const char *name,
  104. struct fmap_entry *entry)
  105. {
  106. uint32_t reg[2];
  107. int ret;
  108. ret = fdtdec_get_int_array(blob, node, "reg", reg, 2);
  109. if (ret) {
  110. debug("Node '%s' has bad/missing 'reg' property\n", name);
  111. return ret;
  112. }
  113. entry->offset = reg[0];
  114. entry->length = reg[1];
  115. return 0;
  116. }
  117. static int scan_flashmap(const void *blob, romlayout_t *rom_entries,
  118. int max_entries)
  119. {
  120. int romimages;
  121. int offset;
  122. int depth;
  123. int node;
  124. offset = fdt_node_offset_by_compatible(blob, -1,
  125. "chromeos,flashmap");
  126. if (offset < 0)
  127. return offset;
  128. for (depth = romimages = 0; offset > 0 && depth >= 0; offset = node) {
  129. struct fmap_entry entry;
  130. const char *name;
  131. romlayout_t *rl;
  132. char *s;
  133. int ret;
  134. node = fdt_next_node(blob, offset, &depth);
  135. if (node < 0)
  136. return node;
  137. if (depth != 1)
  138. continue;
  139. name = fdt_getprop(blob, node, "label", NULL);
  140. ret = read_entry(blob, node, name, &entry);
  141. if (ret)
  142. return ret;
  143. if (romimages >= max_entries) {
  144. msg_gerr("ROM image contains too many regions\n");
  145. return -1;
  146. }
  147. rl = &rom_entries[romimages];
  148. rl->start = entry.offset;
  149. /*
  150. * Flashrom rom entries use absolute addresses. So for non-zero
  151. * length entries, we need to subtract 1 from offset + size to
  152. * determine the end address.
  153. */
  154. rl->end = entry.offset + entry.length;
  155. if (entry.length)
  156. rl->end--;
  157. /* Use an upper case name for flashrom, with _ instead of - */
  158. strncpy(rl->name, name, sizeof(rl->name));
  159. rl->name[sizeof(rl->name) - 1] = '\0';
  160. for (s = rl->name; *s; s++)
  161. if (*s == '-')
  162. *s = '_';
  163. else if (*s == '@')
  164. break;
  165. else
  166. *s = toupper(*s);
  167. *s = '\0';
  168. *rl->file = '\0';
  169. rl->included = 0;
  170. msg_gdbg("added fdtmap region \"%s\" (file=\"%s\") as %sincluded,"
  171. " offset: 0x%08x, end: 0x%08x\n",
  172. rl->name,
  173. rl->file,
  174. rl->included ? "" : "not ",
  175. rl->start,
  176. rl->end);
  177. romimages++;
  178. }
  179. msg_gdbg("Found %d regions\n", romimages);
  180. return romimages;
  181. }
  182. int fdtmap_add_entries_from_buf(const void *blob, romlayout_t *rom_entries,
  183. int max_entries)
  184. {
  185. int count;
  186. count = scan_flashmap(blob, rom_entries, max_entries);
  187. if (count < 0) {
  188. msg_gerr("Failed to read flashmap: '%s'\n", fdt_strerror(count));
  189. return -1;
  190. }
  191. return count;
  192. }
  193. int fdtmap_find(struct flashctx *flash, struct fdtmap_hdr *hdr, loff_t offset,
  194. uint8_t **buf)
  195. {
  196. int fmap_size;
  197. uint32_t crc;
  198. if (memcmp(hdr->sig, FDTMAP_SIGNATURE, sizeof(hdr->sig)))
  199. return 0;
  200. msg_gdbg("%s: found possible fdtmap at offset %#lx\n",
  201. __func__, (unsigned long)offset);
  202. fmap_size = hdr->size;
  203. *buf = malloc(fmap_size);
  204. msg_gdbg("%s: fdtmap size %#x\n", __func__, fmap_size);
  205. /* We may as well just read it here, to simplify the code */
  206. if (flash->read(flash, *buf, offset + sizeof(*hdr), fmap_size)) {
  207. msg_gdbg("[L%d] failed to read %d bytes at offset %#lx\n",
  208. __LINE__, fmap_size, (unsigned long)offset);
  209. return 0;
  210. }
  211. /* Sanity check, the FDT total size should equal fmap_size */
  212. if (fdt_totalsize(*buf) != fmap_size) {
  213. msg_gdbg("[L%d] FDT size %#x did not match header size %#x at %#lx\n",
  214. __LINE__, fdt_totalsize(*buf),
  215. fmap_size, (unsigned long)offset);
  216. return 0;
  217. }
  218. crc = crc32(0, Z_NULL, 0);
  219. crc = crc32(crc, *buf, fmap_size);
  220. /* Sanity check, the FDT total size should equal fmap_size */
  221. if (crc != hdr->crc32) {
  222. msg_gdbg("[L%d] CRC32 %#08x did not match expected %#08x at %#lx\n",
  223. __LINE__, crc, hdr->crc32, (unsigned long)offset);
  224. return 0;
  225. }
  226. return 1;
  227. }