bloblist.c 11 KB

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