bloblist.c 4.9 KB

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