bloblist.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <bloblist.h>
  8. #include <log.h>
  9. #include <mapmem.h>
  10. #include <spl.h>
  11. #include <u-boot/crc.h>
  12. /*
  13. * A bloblist is a single contiguous chunk of memory with a header
  14. * (struct bloblist_hdr) and a number of blobs in it.
  15. *
  16. * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
  17. * bloblist and consists of a struct bloblist_rec, some padding to the required
  18. * alignment for the blog and then the actual data. The padding ensures that the
  19. * start address of the data in each blob is aligned as required. Note that
  20. * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
  21. * of the bloblist itself or the blob header.
  22. *
  23. * So far, only BLOBLIST_ALIGN alignment is supported.
  24. */
  25. DECLARE_GLOBAL_DATA_PTR;
  26. static const char *const tag_name[] = {
  27. [BLOBLISTT_NONE] = "(none)",
  28. [BLOBLISTT_EC_HOSTEVENT] = "EC host event",
  29. [BLOBLISTT_SPL_HANDOFF] = "SPL hand-off",
  30. [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context",
  31. [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off",
  32. };
  33. const char *bloblist_tag_name(enum bloblist_tag_t tag)
  34. {
  35. if (tag < 0 || tag >= BLOBLISTT_COUNT)
  36. return "invalid";
  37. return tag_name[tag];
  38. }
  39. static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
  40. {
  41. if (hdr->alloced <= hdr->hdr_size)
  42. return NULL;
  43. return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
  44. }
  45. static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
  46. struct bloblist_rec *rec)
  47. {
  48. ulong offset;
  49. offset = (void *)rec - (void *)hdr;
  50. offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
  51. if (offset >= hdr->alloced)
  52. return NULL;
  53. return (struct bloblist_rec *)((void *)hdr + offset);
  54. }
  55. #define foreach_rec(_rec, _hdr) \
  56. for (_rec = bloblist_first_blob(_hdr); \
  57. _rec; \
  58. _rec = bloblist_next_blob(_hdr, _rec))
  59. static struct bloblist_rec *bloblist_findrec(uint tag)
  60. {
  61. struct bloblist_hdr *hdr = gd->bloblist;
  62. struct bloblist_rec *rec;
  63. if (!hdr)
  64. return NULL;
  65. foreach_rec(rec, hdr) {
  66. if (rec->tag == tag)
  67. return rec;
  68. }
  69. return NULL;
  70. }
  71. static int bloblist_addrec(uint tag, int size, int align,
  72. struct bloblist_rec **recp)
  73. {
  74. struct bloblist_hdr *hdr = gd->bloblist;
  75. struct bloblist_rec *rec;
  76. int data_start, new_alloced;
  77. if (!align)
  78. align = BLOBLIST_ALIGN;
  79. /* Figure out where the new data will start */
  80. data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
  81. /* Align the address and then calculate the offset from ->alloced */
  82. data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
  83. /* Calculate the new allocated total */
  84. new_alloced = data_start + ALIGN(size, align);
  85. if (new_alloced >= hdr->size) {
  86. log(LOGC_BLOBLIST, LOGL_ERR,
  87. "Failed to allocate %x bytes size=%x, need size=%x\n",
  88. size, hdr->size, new_alloced);
  89. return log_msg_ret("bloblist add", -ENOSPC);
  90. }
  91. rec = (void *)hdr + hdr->alloced;
  92. rec->tag = tag;
  93. rec->hdr_size = data_start - hdr->alloced;
  94. rec->size = size;
  95. rec->spare = 0;
  96. /* Zero the record data */
  97. memset((void *)rec + rec->hdr_size, '\0', rec->size);
  98. hdr->alloced = new_alloced;
  99. *recp = rec;
  100. return 0;
  101. }
  102. static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
  103. int align)
  104. {
  105. struct bloblist_rec *rec;
  106. rec = bloblist_findrec(tag);
  107. if (rec) {
  108. if (size && size != rec->size) {
  109. *recp = rec;
  110. return -ESPIPE;
  111. }
  112. } else {
  113. int ret;
  114. ret = bloblist_addrec(tag, size, align, &rec);
  115. if (ret)
  116. return ret;
  117. }
  118. *recp = rec;
  119. return 0;
  120. }
  121. void *bloblist_find(uint tag, int size)
  122. {
  123. struct bloblist_rec *rec;
  124. rec = bloblist_findrec(tag);
  125. if (!rec)
  126. return NULL;
  127. if (size && size != rec->size)
  128. return NULL;
  129. return (void *)rec + rec->hdr_size;
  130. }
  131. void *bloblist_add(uint tag, int size, int align)
  132. {
  133. struct bloblist_rec *rec;
  134. if (bloblist_addrec(tag, size, align, &rec))
  135. return NULL;
  136. return (void *)rec + rec->hdr_size;
  137. }
  138. int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
  139. {
  140. struct bloblist_rec *rec;
  141. int ret;
  142. ret = bloblist_ensurerec(tag, &rec, size, align);
  143. if (ret)
  144. return ret;
  145. *blobp = (void *)rec + rec->hdr_size;
  146. return 0;
  147. }
  148. void *bloblist_ensure(uint tag, int size)
  149. {
  150. struct bloblist_rec *rec;
  151. if (bloblist_ensurerec(tag, &rec, size, 0))
  152. return NULL;
  153. return (void *)rec + rec->hdr_size;
  154. }
  155. int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
  156. {
  157. struct bloblist_rec *rec;
  158. int ret;
  159. ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
  160. if (ret == -ESPIPE)
  161. *sizep = rec->size;
  162. else if (ret)
  163. return ret;
  164. *blobp = (void *)rec + rec->hdr_size;
  165. return 0;
  166. }
  167. static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
  168. {
  169. struct bloblist_rec *rec;
  170. u32 chksum;
  171. chksum = crc32(0, (unsigned char *)hdr,
  172. offsetof(struct bloblist_hdr, chksum));
  173. foreach_rec(rec, hdr) {
  174. chksum = crc32(chksum, (void *)rec, rec->hdr_size);
  175. chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
  176. }
  177. return chksum;
  178. }
  179. int bloblist_new(ulong addr, uint size, uint flags)
  180. {
  181. struct bloblist_hdr *hdr;
  182. if (size < sizeof(*hdr))
  183. return log_ret(-ENOSPC);
  184. if (addr & (BLOBLIST_ALIGN - 1))
  185. return log_ret(-EFAULT);
  186. hdr = map_sysmem(addr, size);
  187. memset(hdr, '\0', sizeof(*hdr));
  188. hdr->version = BLOBLIST_VERSION;
  189. hdr->hdr_size = sizeof(*hdr);
  190. hdr->flags = flags;
  191. hdr->magic = BLOBLIST_MAGIC;
  192. hdr->size = size;
  193. hdr->alloced = hdr->hdr_size;
  194. hdr->chksum = 0;
  195. gd->bloblist = hdr;
  196. return 0;
  197. }
  198. int bloblist_check(ulong addr, uint size)
  199. {
  200. struct bloblist_hdr *hdr;
  201. u32 chksum;
  202. hdr = map_sysmem(addr, sizeof(*hdr));
  203. if (hdr->magic != BLOBLIST_MAGIC)
  204. return log_msg_ret("Bad magic", -ENOENT);
  205. if (hdr->version != BLOBLIST_VERSION)
  206. return log_msg_ret("Bad version", -EPROTONOSUPPORT);
  207. if (size && hdr->size != size)
  208. return log_msg_ret("Bad size", -EFBIG);
  209. chksum = bloblist_calc_chksum(hdr);
  210. if (hdr->chksum != chksum) {
  211. log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
  212. chksum);
  213. return log_msg_ret("Bad checksum", -EIO);
  214. }
  215. gd->bloblist = hdr;
  216. return 0;
  217. }
  218. int bloblist_finish(void)
  219. {
  220. struct bloblist_hdr *hdr = gd->bloblist;
  221. hdr->chksum = bloblist_calc_chksum(hdr);
  222. return 0;
  223. }
  224. void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
  225. {
  226. struct bloblist_hdr *hdr = gd->bloblist;
  227. *basep = map_to_sysmem(gd->bloblist);
  228. *sizep = hdr->size;
  229. *allocedp = hdr->alloced;
  230. }
  231. static void show_value(const char *prompt, ulong value)
  232. {
  233. printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
  234. print_size(value, "\n");
  235. }
  236. void bloblist_show_stats(void)
  237. {
  238. ulong base, size, alloced;
  239. bloblist_get_stats(&base, &size, &alloced);
  240. printf("base: %lx\n", base);
  241. show_value("size", size);
  242. show_value("alloced", alloced);
  243. show_value("free", size - alloced);
  244. }
  245. void bloblist_show_list(void)
  246. {
  247. struct bloblist_hdr *hdr = gd->bloblist;
  248. struct bloblist_rec *rec;
  249. printf("%-8s %8s Tag Name\n", "Address", "Size");
  250. for (rec = bloblist_first_blob(hdr); rec;
  251. rec = bloblist_next_blob(hdr, rec)) {
  252. printf("%08lx %8x %3d %s\n",
  253. (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
  254. rec->size, rec->tag, bloblist_tag_name(rec->tag));
  255. }
  256. }
  257. int bloblist_init(void)
  258. {
  259. bool expected;
  260. int ret = -ENOENT;
  261. /**
  262. * Wed expect to find an existing bloblist in the first phase of U-Boot
  263. * that runs
  264. */
  265. expected = !u_boot_first_phase();
  266. if (expected)
  267. ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
  268. CONFIG_BLOBLIST_SIZE);
  269. if (ret) {
  270. log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
  271. "Existing bloblist not found: creating new bloblist\n");
  272. ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
  273. 0);
  274. } else {
  275. log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
  276. }
  277. return ret;
  278. }