bloblist.c 4.9 KB

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