bloblist.c 8.3 KB

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