bloblist.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. DECLARE_GLOBAL_DATA_PTR;
  13. struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr)
  14. {
  15. if (hdr->alloced <= hdr->hdr_size)
  16. return NULL;
  17. return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size);
  18. }
  19. struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr,
  20. struct bloblist_rec *rec)
  21. {
  22. ulong offset;
  23. offset = (void *)rec - (void *)hdr;
  24. offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN);
  25. if (offset >= hdr->alloced)
  26. return NULL;
  27. return (struct bloblist_rec *)((void *)hdr + offset);
  28. }
  29. #define foreach_rec(_rec, _hdr) \
  30. for (_rec = bloblist_first_blob(_hdr); \
  31. _rec; \
  32. _rec = bloblist_next_blob(_hdr, _rec))
  33. static struct bloblist_rec *bloblist_findrec(uint tag)
  34. {
  35. struct bloblist_hdr *hdr = gd->bloblist;
  36. struct bloblist_rec *rec;
  37. if (!hdr)
  38. return NULL;
  39. foreach_rec(rec, hdr) {
  40. if (rec->tag == tag)
  41. return rec;
  42. }
  43. return NULL;
  44. }
  45. static int bloblist_addrec(uint tag, int size, struct bloblist_rec **recp)
  46. {
  47. struct bloblist_hdr *hdr = gd->bloblist;
  48. struct bloblist_rec *rec;
  49. int new_alloced;
  50. new_alloced = hdr->alloced + sizeof(*rec) + ALIGN(size, BLOBLIST_ALIGN);
  51. if (new_alloced >= hdr->size) {
  52. log(LOGC_BLOBLIST, LOGL_ERR,
  53. "Failed to allocate %x bytes size=%x, need size=%x\n",
  54. size, hdr->size, new_alloced);
  55. return log_msg_ret("bloblist add", -ENOSPC);
  56. }
  57. rec = (void *)hdr + hdr->alloced;
  58. hdr->alloced = new_alloced;
  59. rec->tag = tag;
  60. rec->hdr_size = sizeof(*rec);
  61. rec->size = size;
  62. rec->spare = 0;
  63. /* Zero the record data */
  64. memset(rec + 1, '\0', rec->size);
  65. *recp = rec;
  66. return 0;
  67. }
  68. static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size)
  69. {
  70. struct bloblist_rec *rec;
  71. rec = bloblist_findrec(tag);
  72. if (rec) {
  73. if (size && size != rec->size) {
  74. *recp = rec;
  75. return -ESPIPE;
  76. }
  77. } else {
  78. int ret;
  79. ret = bloblist_addrec(tag, size, &rec);
  80. if (ret)
  81. return ret;
  82. }
  83. *recp = rec;
  84. return 0;
  85. }
  86. void *bloblist_find(uint tag, int size)
  87. {
  88. struct bloblist_rec *rec;
  89. rec = bloblist_findrec(tag);
  90. if (!rec)
  91. return NULL;
  92. if (size && size != rec->size)
  93. return NULL;
  94. return (void *)rec + rec->hdr_size;
  95. }
  96. void *bloblist_add(uint tag, int size)
  97. {
  98. struct bloblist_rec *rec;
  99. if (bloblist_addrec(tag, size, &rec))
  100. return NULL;
  101. return rec + 1;
  102. }
  103. int bloblist_ensure_size(uint tag, int size, void **blobp)
  104. {
  105. struct bloblist_rec *rec;
  106. int ret;
  107. ret = bloblist_ensurerec(tag, &rec, size);
  108. if (ret)
  109. return ret;
  110. *blobp = (void *)rec + rec->hdr_size;
  111. return 0;
  112. }
  113. void *bloblist_ensure(uint tag, int size)
  114. {
  115. struct bloblist_rec *rec;
  116. if (bloblist_ensurerec(tag, &rec, size))
  117. return NULL;
  118. return (void *)rec + rec->hdr_size;
  119. }
  120. int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp)
  121. {
  122. struct bloblist_rec *rec;
  123. int ret;
  124. ret = bloblist_ensurerec(tag, &rec, *sizep);
  125. if (ret == -ESPIPE)
  126. *sizep = rec->size;
  127. else if (ret)
  128. return ret;
  129. *blobp = (void *)rec + rec->hdr_size;
  130. return 0;
  131. }
  132. static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr)
  133. {
  134. struct bloblist_rec *rec;
  135. u32 chksum;
  136. chksum = crc32(0, (unsigned char *)hdr,
  137. offsetof(struct bloblist_hdr, chksum));
  138. foreach_rec(rec, hdr) {
  139. chksum = crc32(chksum, (void *)rec, rec->hdr_size);
  140. chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size);
  141. }
  142. return chksum;
  143. }
  144. int bloblist_new(ulong addr, uint size, uint flags)
  145. {
  146. struct bloblist_hdr *hdr;
  147. if (size < sizeof(*hdr))
  148. return log_ret(-ENOSPC);
  149. if (addr & (BLOBLIST_ALIGN - 1))
  150. return log_ret(-EFAULT);
  151. hdr = map_sysmem(addr, size);
  152. memset(hdr, '\0', sizeof(*hdr));
  153. hdr->version = BLOBLIST_VERSION;
  154. hdr->hdr_size = sizeof(*hdr);
  155. hdr->flags = flags;
  156. hdr->magic = BLOBLIST_MAGIC;
  157. hdr->size = size;
  158. hdr->alloced = hdr->hdr_size;
  159. hdr->chksum = 0;
  160. gd->bloblist = hdr;
  161. return 0;
  162. }
  163. int bloblist_check(ulong addr, uint size)
  164. {
  165. struct bloblist_hdr *hdr;
  166. u32 chksum;
  167. hdr = map_sysmem(addr, sizeof(*hdr));
  168. if (hdr->magic != BLOBLIST_MAGIC)
  169. return log_msg_ret("Bad magic", -ENOENT);
  170. if (hdr->version != BLOBLIST_VERSION)
  171. return log_msg_ret("Bad version", -EPROTONOSUPPORT);
  172. if (size && hdr->size != size)
  173. return log_msg_ret("Bad size", -EFBIG);
  174. chksum = bloblist_calc_chksum(hdr);
  175. if (hdr->chksum != chksum) {
  176. log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum,
  177. chksum);
  178. return log_msg_ret("Bad checksum", -EIO);
  179. }
  180. gd->bloblist = hdr;
  181. return 0;
  182. }
  183. int bloblist_finish(void)
  184. {
  185. struct bloblist_hdr *hdr = gd->bloblist;
  186. hdr->chksum = bloblist_calc_chksum(hdr);
  187. return 0;
  188. }
  189. int bloblist_init(void)
  190. {
  191. bool expected;
  192. int ret = -ENOENT;
  193. /**
  194. * Wed expect to find an existing bloblist in the first phase of U-Boot
  195. * that runs
  196. */
  197. expected = !u_boot_first_phase();
  198. if (expected)
  199. ret = bloblist_check(CONFIG_BLOBLIST_ADDR,
  200. CONFIG_BLOBLIST_SIZE);
  201. if (ret) {
  202. log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG,
  203. "Existing bloblist not found: creating new bloblist\n");
  204. ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE,
  205. 0);
  206. } else {
  207. log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n");
  208. }
  209. return ret;
  210. }