bloblist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause
  2. /*
  3. * Copyright 2018 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY LOGC_BLOBLIST
  7. #include <common.h>
  8. #include <bloblist.h>
  9. #include <display_options.h>
  10. #include <log.h>
  11. #include <malloc.h>
  12. #include <mapmem.h>
  13. #include <spl.h>
  14. #include <asm/global_data.h>
  15. #include <u-boot/crc.h>
  16. /*
  17. * A bloblist is a single contiguous chunk of memory with a header
  18. * (struct bloblist_hdr) and a number of blobs in it.
  19. *
  20. * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the
  21. * bloblist and consists of a struct bloblist_rec, some padding to the required
  22. * alignment for the blog and then the actual data. The padding ensures that the
  23. * start address of the data in each blob is aligned as required. Note that
  24. * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment
  25. * of the bloblist itself or the blob header.
  26. *
  27. * So far, only BLOBLIST_ALIGN alignment is supported.
  28. */
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static struct tag_name {
  31. enum bloblist_tag_t tag;
  32. const char *name;
  33. } tag_name[] = {
  34. { BLOBLISTT_NONE, "(none)" },
  35. /* BLOBLISTT_AREA_FIRMWARE_TOP */
  36. /* BLOBLISTT_AREA_FIRMWARE */
  37. { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" },
  38. { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" },
  39. { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" },
  40. { BLOBLISTT_TCPA_LOG, "TPM log space" },
  41. { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" },
  42. { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" },
  43. { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" },
  44. /* BLOBLISTT_PROJECT_AREA */
  45. { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" },
  46. { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" },
  47. /* BLOBLISTT_VENDOR_AREA */
  48. };
  49. const char *bloblist_tag_name(enum bloblist_tag_t tag)
  50. {
  51. int i;
  52. for (i = 0; i < ARRAY_SIZE(tag_name); i++) {
  53. if (tag_name[i].tag == tag)
  54. return tag_name[i].name;
  55. }
  56. return "invalid";
  57. }
  58. static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
  59. {
  60. if (hdr->alloced <= hdr->hdr_size)
  61. return NULL;
  62. return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
  63. }
  64. static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr,
  65. struct bloblist_rec *rec)
  66. {
  67. ulong offset;
  68. offset = (void *)rec - (void *)hdr;
  69. offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
  70. return offset;
  71. }
  72. static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
  73. struct bloblist_rec *rec)
  74. {
  75. ulong offset = bloblist_blob_end_ofs(hdr, rec);
  76. if (offset >= hdr->alloced)
  77. return NULL;
  78. return (struct bloblist_rec *)((void *)hdr + offset);
  79. }
  80. #define foreach_rec(_rec, _hdr) \
  81. for (_rec = bloblist_first_blob(_hdr); \
  82. _rec; \
  83. _rec = bloblist_next_blob(_hdr, _rec))
  84. static struct bloblist_rec *bloblist_findrec(uint tag)
  85. {
  86. struct bloblist_hdr *hdr = gd->bloblist;
  87. struct bloblist_rec *rec;
  88. if (!hdr)
  89. return NULL;
  90. foreach_rec(rec, hdr) {
  91. if (rec->tag == tag)
  92. return rec;
  93. }
  94. return NULL;
  95. }
  96. static int bloblist_addrec(uint tag, int size, int align,
  97. struct bloblist_rec **recp)
  98. {
  99. struct bloblist_hdr *hdr = gd->bloblist;
  100. struct bloblist_rec *rec;
  101. int data_start, new_alloced;
  102. if (!align)
  103. align = BLOBLIST_ALIGN;
  104. /* Figure out where the new data will start */
  105. data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec);
  106. /* Align the address and then calculate the offset from ->alloced */
  107. data_start = ALIGN(data_start, align) - map_to_sysmem(hdr);
  108. /* Calculate the new allocated total */
  109. new_alloced = data_start + ALIGN(size, align);
  110. if (new_alloced > hdr->size) {
  111. log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
  112. size, hdr->size, new_alloced);
  113. return log_msg_ret("bloblist add", -ENOSPC);
  114. }
  115. rec = (void *)hdr + hdr->alloced;
  116. rec->tag = tag;
  117. rec->hdr_size = data_start - hdr->alloced;
  118. rec->size = size;
  119. rec->spare = 0;
  120. /* Zero the record data */
  121. memset((void *)rec + rec->hdr_size, '\0', rec->size);
  122. hdr->alloced = new_alloced;
  123. *recp = rec;
  124. return 0;
  125. }
  126. static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size,
  127. int align)
  128. {
  129. struct bloblist_rec *rec;
  130. rec = bloblist_findrec(tag);
  131. if (rec) {
  132. if (size && size != rec->size) {
  133. *recp = rec;
  134. return -ESPIPE;
  135. }
  136. } else {
  137. int ret;
  138. ret = bloblist_addrec(tag, size, align, &rec);
  139. if (ret)
  140. return ret;
  141. }
  142. *recp = rec;
  143. return 0;
  144. }
  145. void *bloblist_find(uint tag, int size)
  146. {
  147. struct bloblist_rec *rec;
  148. rec = bloblist_findrec(tag);
  149. if (!rec)
  150. return NULL;
  151. if (size && size != rec->size)
  152. return NULL;
  153. return (void *)rec + rec->hdr_size;
  154. }
  155. void *bloblist_add(uint tag, int size, int align)
  156. {
  157. struct bloblist_rec *rec;
  158. if (bloblist_addrec(tag, size, align, &rec))
  159. return NULL;
  160. return (void *)rec + rec->hdr_size;
  161. }
  162. int bloblist_ensure_size(uint tag, int size, int align, void **blobp)
  163. {
  164. struct bloblist_rec *rec;
  165. int ret;
  166. ret = bloblist_ensurerec(tag, &rec, size, align);
  167. if (ret)
  168. return ret;
  169. *blobp = (void *)rec + rec->hdr_size;
  170. return 0;
  171. }
  172. void *bloblist_ensure(uint tag, int size)
  173. {
  174. struct bloblist_rec *rec;
  175. if (bloblist_ensurerec(tag, &rec, size, 0))
  176. return NULL;
  177. return (void *)rec + rec->hdr_size;
  178. }
  179. int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
  180. {
  181. struct bloblist_rec *rec;
  182. int ret;
  183. ret = bloblist_ensurerec(tag, &rec, *sizep, 0);
  184. if (ret == -ESPIPE)
  185. *sizep = rec->size;
  186. else if (ret)
  187. return ret;
  188. *blobp = (void *)rec + rec->hdr_size;
  189. return 0;
  190. }
  191. static int bloblist_resize_rec(struct bloblist_hdr *hdr,
  192. struct bloblist_rec *rec,
  193. int new_size)
  194. {
  195. int expand_by; /* Number of bytes to expand by (-ve to contract) */
  196. int new_alloced; /* New value for @hdr->alloced */
  197. ulong next_ofs; /* Offset of the record after @rec */
  198. expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN);
  199. new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN);
  200. if (new_size < 0) {
  201. log_debug("Attempt to shrink blob size below 0 (%x)\n",
  202. new_size);
  203. return log_msg_ret("size", -EINVAL);
  204. }
  205. if (new_alloced > hdr->size) {
  206. log_err("Failed to allocate %x bytes size=%x, need size=%x\n",
  207. new_size, hdr->size, new_alloced);
  208. return log_msg_ret("alloc", -ENOSPC);
  209. }
  210. /* Move the following blobs up or down, if this is not the last */
  211. next_ofs = bloblist_blob_end_ofs(hdr, rec);
  212. if (next_ofs != hdr->alloced) {
  213. memmove((void *)hdr + next_ofs + expand_by,
  214. (void *)hdr + next_ofs, new_alloced - next_ofs);
  215. }
  216. hdr->alloced = new_alloced;
  217. /* Zero the new part of the blob */
  218. if (expand_by > 0) {
  219. memset((void *)rec + rec->hdr_size + rec->size, '\0',
  220. new_size - rec->size);
  221. }
  222. /* Update the size of this blob */
  223. rec->size = new_size;
  224. return 0;
  225. }
  226. int bloblist_resize(uint tag, int new_size)
  227. {
  228. struct bloblist_hdr *hdr = gd->bloblist;
  229. struct bloblist_rec *rec;
  230. int ret;
  231. rec = bloblist_findrec(tag);
  232. if (!rec)
  233. return log_msg_ret("find", -ENOENT);
  234. ret = bloblist_resize_rec(hdr, rec, new_size);
  235. if (ret)
  236. return log_msg_ret("resize", ret);
  237. return 0;
  238. }
  239. static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
  240. {
  241. struct bloblist_rec *rec;
  242. u32 chksum;
  243. chksum = crc32(0, (unsigned char *)hdr,
  244. offsetof(struct bloblist_hdr, chksum));
  245. foreach_rec(rec, hdr) {
  246. chksum = crc32(chksum, (void *)rec, rec->hdr_size);
  247. chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
  248. }
  249. return chksum;
  250. }
  251. int bloblist_new(ulong addr, uint size, uint flags)
  252. {
  253. struct bloblist_hdr *hdr;
  254. if (size < sizeof(*hdr))
  255. return log_ret(-ENOSPC);
  256. if (addr & (BLOBLIST_ALIGN - 1))
  257. return log_ret(-EFAULT);
  258. hdr = map_sysmem(addr, size);
  259. memset(hdr, '\0', sizeof(*hdr));
  260. hdr->version = BLOBLIST_VERSION;
  261. hdr->hdr_size = sizeof(*hdr);
  262. hdr->flags = flags;
  263. hdr->magic = BLOBLIST_MAGIC;
  264. hdr->size = size;
  265. hdr->alloced = hdr->hdr_size;
  266. hdr->chksum = 0;
  267. gd->bloblist = hdr;
  268. return 0;
  269. }
  270. int bloblist_check(ulong addr, uint size)
  271. {
  272. struct bloblist_hdr *hdr;
  273. u32 chksum;
  274. hdr = map_sysmem(addr, sizeof(*hdr));
  275. if (hdr->magic != BLOBLIST_MAGIC)
  276. return log_msg_ret("Bad magic", -ENOENT);
  277. if (hdr->version != BLOBLIST_VERSION)
  278. return log_msg_ret("Bad version", -EPROTONOSUPPORT);
  279. if (size && hdr->size != size)
  280. return log_msg_ret("Bad size", -EFBIG);
  281. chksum = bloblist_calc_chksum(hdr);
  282. if (hdr->chksum != chksum) {
  283. log_err("Checksum %x != %x\n", hdr->chksum, chksum);
  284. return log_msg_ret("Bad checksum", -EIO);
  285. }
  286. gd->bloblist = hdr;
  287. return 0;
  288. }
  289. int bloblist_finish(void)
  290. {
  291. struct bloblist_hdr *hdr = gd->bloblist;
  292. hdr->chksum = bloblist_calc_chksum(hdr);
  293. log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->size,
  294. (ulong)map_to_sysmem(hdr));
  295. return 0;
  296. }
  297. ulong bloblist_get_base(void)
  298. {
  299. return map_to_sysmem(gd->bloblist);
  300. }
  301. ulong bloblist_get_size(void)
  302. {
  303. struct bloblist_hdr *hdr = gd->bloblist;
  304. return hdr->size;
  305. }
  306. void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp)
  307. {
  308. struct bloblist_hdr *hdr = gd->bloblist;
  309. *basep = map_to_sysmem(gd->bloblist);
  310. *sizep = hdr->size;
  311. *allocedp = hdr->alloced;
  312. }
  313. static void show_value(const char *prompt, ulong value)
  314. {
  315. printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value);
  316. print_size(value, "\n");
  317. }
  318. void bloblist_show_stats(void)
  319. {
  320. ulong base, size, alloced;
  321. bloblist_get_stats(&base, &size, &alloced);
  322. printf("base: %lx\n", base);
  323. show_value("size", size);
  324. show_value("alloced", alloced);
  325. show_value("free", size - alloced);
  326. }
  327. void bloblist_show_list(void)
  328. {
  329. struct bloblist_hdr *hdr = gd->bloblist;
  330. struct bloblist_rec *rec;
  331. printf("%-8s %8s Tag Name\n", "Address", "Size");
  332. for (rec = bloblist_first_blob(hdr); rec;
  333. rec = bloblist_next_blob(hdr, rec)) {
  334. printf("%08lx %8x %4x %s\n",
  335. (ulong)map_to_sysmem((void *)rec + rec->hdr_size),
  336. rec->size, rec->tag, bloblist_tag_name(rec->tag));
  337. }
  338. }
  339. void bloblist_reloc(void *to, uint to_size, void *from, uint from_size)
  340. {
  341. struct bloblist_hdr *hdr;
  342. memcpy(to, from, from_size);
  343. hdr = to;
  344. hdr->size = to_size;
  345. }
  346. int bloblist_init(void)
  347. {
  348. bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED);
  349. int ret = -ENOENT;
  350. ulong addr, size;
  351. bool expected;
  352. /**
  353. * We don't expect to find an existing bloblist in the first phase of
  354. * U-Boot that runs. Also we have no way to receive the address of an
  355. * allocated bloblist from a previous stage, so it must be at a fixed
  356. * address.
  357. */
  358. expected = fixed && !u_boot_first_phase();
  359. if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST))
  360. expected = false;
  361. if (fixed)
  362. addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED,
  363. CONFIG_BLOBLIST_ADDR);
  364. size = CONFIG_BLOBLIST_SIZE;
  365. if (expected) {
  366. ret = bloblist_check(addr, size);
  367. if (ret) {
  368. log_warning("Expected bloblist at %lx not found (err=%d)\n",
  369. addr, ret);
  370. } else {
  371. /* Get the real size, if it is not what we expected */
  372. size = gd->bloblist->size;
  373. }
  374. }
  375. if (ret) {
  376. if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) {
  377. void *ptr = memalign(BLOBLIST_ALIGN, size);
  378. if (!ptr)
  379. return log_msg_ret("alloc", -ENOMEM);
  380. addr = map_to_sysmem(ptr);
  381. } else if (!fixed) {
  382. return log_msg_ret("!fixed", ret);
  383. }
  384. log_debug("Creating new bloblist size %lx at %lx\n", size,
  385. addr);
  386. ret = bloblist_new(addr, size, 0);
  387. } else {
  388. log_debug("Found existing bloblist size %lx at %lx\n", size,
  389. addr);
  390. }
  391. return ret;
  392. }