bloblist.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. [BLOBLISTT_ACPI_GNVS] = "ACPI GNVS",
  33. [BLOBLISTT_INTEL_VBT] = "Intel Video-BIOS table",
  34. [BLOBLISTT_TPM2_TCG_LOG] = "TPM v2 log space",
  35. [BLOBLISTT_TCPA_LOG] = "TPM log space",
  36. [BLOBLISTT_ACPI_TABLES] = "ACPI tables for x86",
  37. [BLOBLISTT_SMBIOS_TABLES] = "SMBIOS tables for x86",
  38. };
  39. const char *bloblist_tag_name(enum bloblist_tag_t tag)
  40. {
  41. if (tag < 0 || tag >= BLOBLISTT_COUNT)
  42. return "invalid";
  43. return tag_name[tag];
  44. }
  45. static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
  46. {
  47. if (hdr->alloced <= hdr->hdr_size)
  48. return NULL;
  49. return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
  50. }
  51. static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
  52. struct bloblist_rec *rec)
  53. {
  54. ulong offset;
  55. offset = (void *)rec - (void *)hdr;
  56. offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
  57. if (offset >= hdr->alloced)
  58. return NULL;
  59. return (struct bloblist_rec *)((void *)hdr + offset);
  60. }
  61. #define foreach_rec(_rec, _hdr) \
  62. for (_rec = bloblist_first_blob(_hdr); \
  63. _rec; \
  64. _rec = bloblist_next_blob(_hdr, _rec))
  65. static struct bloblist_rec *bloblist_findrec(uint tag)
  66. {
  67. struct bloblist_hdr *hdr = gd->bloblist;
  68. struct bloblist_rec *rec;
  69. if (!hdr)
  70. return NULL;
  71. foreach_rec(rec, hdr) {
  72. if (rec->tag == tag)
  73. return rec;
  74. }
  75. return NULL;
  76. }
  77. static int bloblist_addrec(uint tag, int size, int align,
  78. struct bloblist_rec **recp)
  79. {
  80. struct bloblist_hdr *hdr = gd->bloblist;
  81. struct bloblist_rec *rec;
  82. int data_start, new_alloced;
  83. if (!align)
  84. align = BLOBLIST_ALIGN;
  85. /* Figure out where the new data will start */
  86. data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
  87. /* Align the address and then calculate the offset from ->alloced */
  88. data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
  89. /* Calculate the new allocated total */
  90. new_alloced = data_start + ALIGN(size, align);
  91. if (new_alloced >= hdr->size) {
  92. log(LOGC_BLOBLIST, LOGL_ERR,
  93. "Failed to allocate %x bytes size=%x, need size=%x\n",
  94. size, hdr->size, new_alloced);
  95. return log_msg_ret("bloblist add", -ENOSPC);
  96. }
  97. rec = (void *)hdr + hdr->alloced;
  98. rec->tag = tag;
  99. rec->hdr_size = data_start - hdr->alloced;
  100. rec->size = size;
  101. rec->spare = 0;
  102. /* Zero the record data */
  103. memset((void *)rec + rec->hdr_size, '\0', rec->size);
  104. hdr->alloced = new_alloced;
  105. *recp = rec;
  106. return 0;
  107. }
  108. static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
  109. int align)
  110. {
  111. struct bloblist_rec *rec;
  112. rec = bloblist_findrec(tag);
  113. if (rec) {
  114. if (size && size != rec->size) {
  115. *recp = rec;
  116. return -ESPIPE;
  117. }
  118. } else {
  119. int ret;
  120. ret = bloblist_addrec(tag, size, align, &rec);
  121. if (ret)
  122. return ret;
  123. }
  124. *recp = rec;
  125. return 0;
  126. }
  127. void *bloblist_find(uint tag, int size)
  128. {
  129. struct bloblist_rec *rec;
  130. rec = bloblist_findrec(tag);
  131. if (!rec)
  132. return NULL;
  133. if (size && size != rec->size)
  134. return NULL;
  135. return (void *)rec + rec->hdr_size;
  136. }
  137. void *bloblist_add(uint tag, int size, int align)
  138. {
  139. struct bloblist_rec *rec;
  140. if (bloblist_addrec(tag, size, align, &rec))
  141. return NULL;
  142. return (void *)rec + rec->hdr_size;
  143. }
  144. int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
  145. {
  146. struct bloblist_rec *rec;
  147. int ret;
  148. ret = bloblist_ensurerec(tag, &rec, size, align);
  149. if (ret)
  150. return ret;
  151. *blobp = (void *)rec + rec->hdr_size;
  152. return 0;
  153. }
  154. void *bloblist_ensure(uint tag, int size)
  155. {
  156. struct bloblist_rec *rec;
  157. if (bloblist_ensurerec(tag, &rec, size, 0))
  158. return NULL;
  159. return (void *)rec + rec->hdr_size;
  160. }
  161. int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
  162. {
  163. struct bloblist_rec *rec;
  164. int ret;
  165. ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
  166. if (ret == -ESPIPE)
  167. *sizep = rec->size;
  168. else if (ret)
  169. return ret;
  170. *blobp = (void *)rec + rec->hdr_size;
  171. return 0;
  172. }
  173. static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
  174. {
  175. struct bloblist_rec *rec;
  176. u32 chksum;
  177. chksum = crc32(0, (unsigned char *)hdr,
  178. offsetof(struct bloblist_hdr, chksum));
  179. foreach_rec(rec, hdr) {
  180. chksum = crc32(chksum, (void *)rec, rec->hdr_size);
  181. chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
  182. }
  183. return chksum;
  184. }
  185. int bloblist_new(ulong addr, uint size, uint flags)
  186. {
  187. struct bloblist_hdr *hdr;
  188. if (size < sizeof(*hdr))
  189. return log_ret(-ENOSPC);
  190. if (addr & (BLOBLIST_ALIGN - 1))
  191. return log_ret(-EFAULT);
  192. hdr = map_sysmem(addr, size);
  193. memset(hdr, '\0', sizeof(*hdr));
  194. hdr->version = BLOBLIST_VERSION;
  195. hdr->hdr_size = sizeof(*hdr);
  196. hdr->flags = flags;
  197. hdr->magic = BLOBLIST_MAGIC;
  198. hdr->size = size;
  199. hdr->alloced = hdr->hdr_size;
  200. hdr->chksum = 0;
  201. gd->bloblist = hdr;
  202. return 0;
  203. }
  204. int bloblist_check(ulong addr, uint size)
  205. {
  206. struct bloblist_hdr *hdr;
  207. u32 chksum;
  208. hdr = map_sysmem(addr, sizeof(*hdr));
  209. if (hdr->magic != BLOBLIST_MAGIC)
  210. return log_msg_ret("Bad magic", -ENOENT);
  211. if (hdr->version != BLOBLIST_VERSION)
  212. return log_msg_ret("Bad version", -EPROTONOSUPPORT);
  213. if (size && hdr->size != size)
  214. return log_msg_ret("Bad size", -EFBIG);
  215. chksum = bloblist_calc_chksum(hdr);
  216. if (hdr->chksum != chksum) {
  217. log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
  218. chksum);
  219. return log_msg_ret("Bad checksum", -EIO);
  220. }
  221. gd->bloblist = hdr;
  222. return 0;
  223. }
  224. int bloblist_finish(void)
  225. {
  226. struct bloblist_hdr *hdr = gd->bloblist;
  227. hdr->chksum = bloblist_calc_chksum(hdr);
  228. return 0;
  229. }
  230. void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
  231. {
  232. struct bloblist_hdr *hdr = gd->bloblist;
  233. *basep = map_to_sysmem(gd->bloblist);
  234. *sizep = hdr->size;
  235. *allocedp = hdr->alloced;
  236. }
  237. static void show_value(const char *prompt, ulong value)
  238. {
  239. printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
  240. print_size(value, "\n");
  241. }
  242. void bloblist_show_stats(void)
  243. {
  244. ulong base, size, alloced;
  245. bloblist_get_stats(&base, &size, &alloced);
  246. printf("base: %lx\n", base);
  247. show_value("size", size);
  248. show_value("alloced", alloced);
  249. show_value("free", size - alloced);
  250. }
  251. void bloblist_show_list(void)
  252. {
  253. struct bloblist_hdr *hdr = gd->bloblist;
  254. struct bloblist_rec *rec;
  255. printf("%-8s %8s Tag Name\n", "Address", "Size");
  256. for (rec = bloblist_first_blob(hdr); rec;
  257. rec = bloblist_next_blob(hdr, rec)) {
  258. printf("%08lx %8x %3d %s\n",
  259. (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
  260. rec->size, rec->tag, bloblist_tag_name(rec->tag));
  261. }
  262. }
  263. void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
  264. {
  265. struct bloblist_hdr *hdr;
  266. memcpy(to, from, from_size);
  267. hdr = to;
  268. hdr->size = to_size;
  269. }
  270. int bloblist_init(void)
  271. {
  272. bool expected;
  273. int ret = -ENOENT;
  274. /**
  275. * Wed expect to find an existing bloblist in the first phase of U-Boot
  276. * that runs
  277. */
  278. expected = !u_boot_first_phase();
  279. if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
  280. expected = false;
  281. if (expected)
  282. ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
  283. CONFIG_BLOBLIST_SIZE);
  284. if (ret) {
  285. log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
  286. "Existing bloblist not found: creating new bloblist\n");
  287. ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
  288. 0);
  289. } else {
  290. log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
  291. }
  292. return ret;
  293. }