bloblist.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 ulong bloblist_blob_end_ofs(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. return offset;
  59. }
  60. static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
  61. struct bloblist_rec *rec)
  62. {
  63. ulong offset = bloblist_blob_end_ofs(hdr, rec);
  64. if (offset >= hdr->alloced)
  65. return NULL;
  66. return (struct bloblist_rec *)((void *)hdr + offset);
  67. }
  68. #define foreach_rec(_rec, _hdr) \
  69. for (_rec = bloblist_first_blob(_hdr); \
  70. _rec; \
  71. _rec = bloblist_next_blob(_hdr, _rec))
  72. static struct bloblist_rec *bloblist_findrec(uint tag)
  73. {
  74. struct bloblist_hdr *hdr = gd->bloblist;
  75. struct bloblist_rec *rec;
  76. if (!hdr)
  77. return NULL;
  78. foreach_rec(rec, hdr) {
  79. if (rec->tag == tag)
  80. return rec;
  81. }
  82. return NULL;
  83. }
  84. static int bloblist_addrec(uint tag, int size, int align,
  85. struct bloblist_rec **recp)
  86. {
  87. struct bloblist_hdr *hdr = gd->bloblist;
  88. struct bloblist_rec *rec;
  89. int data_start, new_alloced;
  90. if (!align)
  91. align = BLOBLIST_ALIGN;
  92. /* Figure out where the new data will start */
  93. data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
  94. /* Align the address and then calculate the offset from ->alloced */
  95. data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
  96. /* Calculate the new allocated total */
  97. new_alloced = data_start + ALIGN(size, align);
  98. if (new_alloced > hdr->size) {
  99. log(LOGC_BLOBLIST, LOGL_ERR,
  100. "Failed to allocate %x bytes size=%x, need size=%x\n",
  101. size, hdr->size, new_alloced);
  102. return log_msg_ret("bloblist add", -ENOSPC);
  103. }
  104. rec = (void *)hdr + hdr->alloced;
  105. rec->tag = tag;
  106. rec->hdr_size = data_start - hdr->alloced;
  107. rec->size = size;
  108. rec->spare = 0;
  109. /* Zero the record data */
  110. memset((void *)rec + rec->hdr_size, '\0', rec->size);
  111. hdr->alloced = new_alloced;
  112. *recp = rec;
  113. return 0;
  114. }
  115. static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
  116. int align)
  117. {
  118. struct bloblist_rec *rec;
  119. rec = bloblist_findrec(tag);
  120. if (rec) {
  121. if (size && size != rec->size) {
  122. *recp = rec;
  123. return -ESPIPE;
  124. }
  125. } else {
  126. int ret;
  127. ret = bloblist_addrec(tag, size, align, &rec);
  128. if (ret)
  129. return ret;
  130. }
  131. *recp = rec;
  132. return 0;
  133. }
  134. void *bloblist_find(uint tag, int size)
  135. {
  136. struct bloblist_rec *rec;
  137. rec = bloblist_findrec(tag);
  138. if (!rec)
  139. return NULL;
  140. if (size && size != rec->size)
  141. return NULL;
  142. return (void *)rec + rec->hdr_size;
  143. }
  144. void *bloblist_add(uint tag, int size, int align)
  145. {
  146. struct bloblist_rec *rec;
  147. if (bloblist_addrec(tag, size, align, &rec))
  148. return NULL;
  149. return (void *)rec + rec->hdr_size;
  150. }
  151. int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
  152. {
  153. struct bloblist_rec *rec;
  154. int ret;
  155. ret = bloblist_ensurerec(tag, &rec, size, align);
  156. if (ret)
  157. return ret;
  158. *blobp = (void *)rec + rec->hdr_size;
  159. return 0;
  160. }
  161. void *bloblist_ensure(uint tag, int size)
  162. {
  163. struct bloblist_rec *rec;
  164. if (bloblist_ensurerec(tag, &rec, size, 0))
  165. return NULL;
  166. return (void *)rec + rec->hdr_size;
  167. }
  168. int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
  169. {
  170. struct bloblist_rec *rec;
  171. int ret;
  172. ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
  173. if (ret == -ESPIPE)
  174. *sizep = rec->size;
  175. else if (ret)
  176. return ret;
  177. *blobp = (void *)rec + rec->hdr_size;
  178. return 0;
  179. }
  180. static int bloblist_resize_rec(struct bloblist_hdr *hdr,
  181. struct bloblist_rec *rec,
  182. int new_size)
  183. {
  184. int expand_by; /* Number of bytes to expand by (-ve to contract) */
  185. int new_alloced; /* New value for @hdr->alloced */
  186. ulong next_ofs; /* Offset of the record after @rec */
  187. expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
  188. new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
  189. if (new_size < 0) {
  190. log(LOGC_BLOBLIST, LOGL_DEBUG,
  191. "Attempt to shrink blob size below 0 (%x)\n", new_size);
  192. return log_msg_ret("size", -EINVAL);
  193. }
  194. if (new_alloced > hdr->size) {
  195. log(LOGC_BLOBLIST, LOGL_ERR,
  196. "Failed to allocate %x bytes size=%x, need size=%x\n",
  197. new_size, hdr->size, new_alloced);
  198. return log_msg_ret("alloc", -ENOSPC);
  199. }
  200. /* Move the following blobs up or down, if this is not the last */
  201. next_ofs = bloblist_blob_end_ofs(hdr, rec);
  202. if (next_ofs != hdr->alloced) {
  203. memmove((void *)hdr + next_ofs + expand_by,
  204. (void *)hdr + next_ofs, new_alloced - next_ofs);
  205. }
  206. hdr->alloced = new_alloced;
  207. /* Zero the new part of the blob */
  208. if (expand_by > 0) {
  209. memset((void *)rec + rec->hdr_size + rec->size, '\0',
  210. new_size - rec->size);
  211. }
  212. /* Update the size of this blob */
  213. rec->size = new_size;
  214. return 0;
  215. }
  216. int bloblist_resize(uint tag, int new_size)
  217. {
  218. struct bloblist_hdr *hdr = gd->bloblist;
  219. struct bloblist_rec *rec;
  220. int ret;
  221. rec = bloblist_findrec(tag);
  222. if (!rec)
  223. return log_msg_ret("find", -ENOENT);
  224. ret = bloblist_resize_rec(hdr, rec, new_size);
  225. if (ret)
  226. return log_msg_ret("resize", ret);
  227. return 0;
  228. }
  229. static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
  230. {
  231. struct bloblist_rec *rec;
  232. u32 chksum;
  233. chksum = crc32(0, (unsigned char *)hdr,
  234. offsetof(struct bloblist_hdr, chksum));
  235. foreach_rec(rec, hdr) {
  236. chksum = crc32(chksum, (void *)rec, rec->hdr_size);
  237. chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
  238. }
  239. return chksum;
  240. }
  241. int bloblist_new(ulong addr, uint size, uint flags)
  242. {
  243. struct bloblist_hdr *hdr;
  244. if (size < sizeof(*hdr))
  245. return log_ret(-ENOSPC);
  246. if (addr & (BLOBLIST_ALIGN - 1))
  247. return log_ret(-EFAULT);
  248. hdr = map_sysmem(addr, size);
  249. memset(hdr, '\0', sizeof(*hdr));
  250. hdr->version = BLOBLIST_VERSION;
  251. hdr->hdr_size = sizeof(*hdr);
  252. hdr->flags = flags;
  253. hdr->magic = BLOBLIST_MAGIC;
  254. hdr->size = size;
  255. hdr->alloced = hdr->hdr_size;
  256. hdr->chksum = 0;
  257. gd->bloblist = hdr;
  258. return 0;
  259. }
  260. int bloblist_check(ulong addr, uint size)
  261. {
  262. struct bloblist_hdr *hdr;
  263. u32 chksum;
  264. hdr = map_sysmem(addr, sizeof(*hdr));
  265. if (hdr->magic != BLOBLIST_MAGIC)
  266. return log_msg_ret("Bad magic", -ENOENT);
  267. if (hdr->version != BLOBLIST_VERSION)
  268. return log_msg_ret("Bad version", -EPROTONOSUPPORT);
  269. if (size && hdr->size != size)
  270. return log_msg_ret("Bad size", -EFBIG);
  271. chksum = bloblist_calc_chksum(hdr);
  272. if (hdr->chksum != chksum) {
  273. log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
  274. chksum);
  275. return log_msg_ret("Bad checksum", -EIO);
  276. }
  277. gd->bloblist = hdr;
  278. return 0;
  279. }
  280. int bloblist_finish(void)
  281. {
  282. struct bloblist_hdr *hdr = gd->bloblist;
  283. hdr->chksum = bloblist_calc_chksum(hdr);
  284. return 0;
  285. }
  286. void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
  287. {
  288. struct bloblist_hdr *hdr = gd->bloblist;
  289. *basep = map_to_sysmem(gd->bloblist);
  290. *sizep = hdr->size;
  291. *allocedp = hdr->alloced;
  292. }
  293. static void show_value(const char *prompt, ulong value)
  294. {
  295. printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
  296. print_size(value, "\n");
  297. }
  298. void bloblist_show_stats(void)
  299. {
  300. ulong base, size, alloced;
  301. bloblist_get_stats(&base, &size, &alloced);
  302. printf("base: %lx\n", base);
  303. show_value("size", size);
  304. show_value("alloced", alloced);
  305. show_value("free", size - alloced);
  306. }
  307. void bloblist_show_list(void)
  308. {
  309. struct bloblist_hdr *hdr = gd->bloblist;
  310. struct bloblist_rec *rec;
  311. printf("%-8s %8s Tag Name\n", "Address", "Size");
  312. for (rec = bloblist_first_blob(hdr); rec;
  313. rec = bloblist_next_blob(hdr, rec)) {
  314. printf("%08lx %8x %3d %s\n",
  315. (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
  316. rec->size, rec->tag, bloblist_tag_name(rec->tag));
  317. }
  318. }
  319. void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
  320. {
  321. struct bloblist_hdr *hdr;
  322. memcpy(to, from, from_size);
  323. hdr = to;
  324. hdr->size = to_size;
  325. }
  326. int bloblist_init(void)
  327. {
  328. bool expected;
  329. int ret = -ENOENT;
  330. /**
  331. * Wed expect to find an existing bloblist in the first phase of U-Boot
  332. * that runs
  333. */
  334. expected = !u_boot_first_phase();
  335. if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
  336. expected = false;
  337. if (expected)
  338. ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
  339. CONFIG_BLOBLIST_SIZE);
  340. if (ret) {
  341. log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
  342. "Existing bloblist not found: creating new bloblist\n");
  343. ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
  344. 0);
  345. } else {
  346. log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
  347. }
  348. return ret;
  349. }