Simon Glass | 6c9e3d1 | 2022-01-12 19:26:25 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2018 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
Simon Glass | 1d8bbd7 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 7 | #define LOG_CATEGORY LOGC_BLOBLIST |
| 8 | |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <bloblist.h> |
Simon Glass | 4e4bf94 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 11 | #include <display_options.h> |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | d5b6e91 | 2021-11-03 21:09:20 -0600 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 14 | #include <mapmem.h> |
| 15 | #include <spl.h> |
Simon Glass | 997dac6 | 2023-12-27 13:07:05 -0800 | [diff] [blame] | 16 | #include <tables_csum.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 18 | #include <u-boot/crc.h> |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 19 | |
Simon Glass | 751b7c7 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 20 | /* |
| 21 | * A bloblist is a single contiguous chunk of memory with a header |
| 22 | * (struct bloblist_hdr) and a number of blobs in it. |
| 23 | * |
| 24 | * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the |
| 25 | * bloblist and consists of a struct bloblist_rec, some padding to the required |
| 26 | * alignment for the blog and then the actual data. The padding ensures that the |
| 27 | * start address of the data in each blob is aligned as required. Note that |
| 28 | * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment |
| 29 | * of the bloblist itself or the blob header. |
Simon Glass | 751b7c7 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 30 | */ |
| 31 | |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 34 | static struct tag_name { |
| 35 | enum bloblist_tag_t tag; |
| 36 | const char *name; |
| 37 | } tag_name[] = { |
Simon Glass | e748e4b | 2023-12-27 13:06:59 -0800 | [diff] [blame] | 38 | { BLOBLISTT_VOID, "(void)" }, |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 39 | |
| 40 | /* BLOBLISTT_AREA_FIRMWARE_TOP */ |
Simon Glass | e748e4b | 2023-12-27 13:06:59 -0800 | [diff] [blame] | 41 | { BLOBLISTT_CONTROL_FDT, "Control FDT" }, |
| 42 | { BLOBLISTT_HOB_BLOCK, "HOB block" }, |
| 43 | { BLOBLISTT_HOB_LIST, "HOB list" }, |
| 44 | { BLOBLISTT_ACPI_TABLES, "ACPI tables for x86" }, |
| 45 | { BLOBLISTT_TPM_EVLOG, "TPM event log defined by TCG EFI" }, |
| 46 | { BLOBLISTT_TPM_CRB_BASE, "TPM Command Response Buffer address" }, |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 47 | |
| 48 | /* BLOBLISTT_AREA_FIRMWARE */ |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 49 | { BLOBLISTT_TPM2_TCG_LOG, "TPM v2 log space" }, |
| 50 | { BLOBLISTT_TCPA_LOG, "TPM log space" }, |
Simon Glass | e748e4b | 2023-12-27 13:06:59 -0800 | [diff] [blame] | 51 | { BLOBLISTT_ACPI_GNVS, "ACPI GNVS" }, |
| 52 | |
| 53 | /* BLOBLISTT_AREA_TF */ |
| 54 | { BLOBLISTT_OPTEE_PAGABLE_PART, "OP-TEE pagable part" }, |
| 55 | |
| 56 | /* BLOBLISTT_AREA_OTHER */ |
| 57 | { BLOBLISTT_INTEL_VBT, "Intel Video-BIOS table" }, |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 58 | { BLOBLISTT_SMBIOS_TABLES, "SMBIOS tables for x86" }, |
| 59 | { BLOBLISTT_VBOOT_CTX, "Chrome OS vboot context" }, |
| 60 | |
| 61 | /* BLOBLISTT_PROJECT_AREA */ |
| 62 | { BLOBLISTT_U_BOOT_SPL_HANDOFF, "SPL hand-off" }, |
Simon Glass | 14d9f63 | 2023-09-26 08:14:52 -0600 | [diff] [blame] | 63 | { BLOBLISTT_VBE, "VBE" }, |
Simon Glass | 03fe79c | 2023-07-15 21:38:59 -0600 | [diff] [blame] | 64 | { BLOBLISTT_U_BOOT_VIDEO, "SPL video handoff" }, |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 65 | |
| 66 | /* BLOBLISTT_VENDOR_AREA */ |
Simon Glass | 4aed227 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | const char *bloblist_tag_name(enum bloblist_tag_t tag) |
| 70 | { |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 71 | int i; |
Simon Glass | 4aed227 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 72 | |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 73 | for (i = 0; i < ARRAY_SIZE(tag_name); i++) { |
| 74 | if (tag_name[i].tag == tag) |
| 75 | return tag_name[i].name; |
| 76 | } |
| 77 | |
| 78 | return "invalid"; |
Simon Glass | 4aed227 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 82 | { |
| 83 | if (hdr->alloced <= hdr->hdr_size) |
| 84 | return NULL; |
| 85 | return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size); |
| 86 | } |
| 87 | |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 88 | static inline uint rec_hdr_size(struct bloblist_rec *rec) |
| 89 | { |
| 90 | return rec->hdr_size; |
| 91 | } |
| 92 | |
| 93 | static inline uint rec_tag(struct bloblist_rec *rec) |
| 94 | { |
| 95 | return rec->tag; |
| 96 | } |
| 97 | |
Simon Glass | 1fe5937 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 98 | static ulong bloblist_blob_end_ofs(struct bloblist_hdr *hdr, |
| 99 | struct bloblist_rec *rec) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 100 | { |
| 101 | ulong offset; |
| 102 | |
| 103 | offset = (void *)rec - (void *)hdr; |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 104 | offset += rec_hdr_size(rec) + ALIGN(rec->size, BLOBLIST_ALIGN); |
Simon Glass | 1fe5937 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 105 | |
| 106 | return offset; |
| 107 | } |
| 108 | |
| 109 | static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr, |
| 110 | struct bloblist_rec *rec) |
| 111 | { |
| 112 | ulong offset = bloblist_blob_end_ofs(hdr, rec); |
| 113 | |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 114 | if (offset >= hdr->alloced) |
| 115 | return NULL; |
| 116 | return (struct bloblist_rec *)((void *)hdr + offset); |
| 117 | } |
| 118 | |
| 119 | #define foreach_rec(_rec, _hdr) \ |
| 120 | for (_rec = bloblist_first_blob(_hdr); \ |
| 121 | _rec; \ |
| 122 | _rec = bloblist_next_blob(_hdr, _rec)) |
| 123 | |
| 124 | static struct bloblist_rec *bloblist_findrec(uint tag) |
| 125 | { |
| 126 | struct bloblist_hdr *hdr = gd->bloblist; |
| 127 | struct bloblist_rec *rec; |
| 128 | |
| 129 | if (!hdr) |
| 130 | return NULL; |
| 131 | |
| 132 | foreach_rec(rec, hdr) { |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 133 | if (rec_tag(rec) == tag) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 134 | return rec; |
| 135 | } |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 140 | static int bloblist_addrec(uint tag, int size, int align_log2, |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 141 | struct bloblist_rec **recp) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 142 | { |
| 143 | struct bloblist_hdr *hdr = gd->bloblist; |
| 144 | struct bloblist_rec *rec; |
Simon Glass | f9ef9fb | 2023-12-27 13:07:06 -0800 | [diff] [blame^] | 145 | int data_start, aligned_start, new_alloced; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 146 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 147 | if (!align_log2) |
| 148 | align_log2 = BLOBLIST_ALIGN_LOG2; |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 149 | |
Simon Glass | 751b7c7 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 150 | /* Figure out where the new data will start */ |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 151 | data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec); |
| 152 | |
| 153 | /* Align the address and then calculate the offset from ->alloced */ |
Simon Glass | f9ef9fb | 2023-12-27 13:07:06 -0800 | [diff] [blame^] | 154 | aligned_start = ALIGN(data_start, 1U << align_log2) - data_start; |
| 155 | |
| 156 | /* If we need to create a dummy record, create it */ |
| 157 | if (aligned_start) { |
| 158 | int void_size = aligned_start - sizeof(*rec); |
| 159 | struct bloblist_rec *vrec; |
| 160 | int ret; |
| 161 | |
| 162 | ret = bloblist_addrec(BLOBLISTT_VOID, void_size, 0, &vrec); |
| 163 | if (ret) |
| 164 | return log_msg_ret("void", ret); |
| 165 | |
| 166 | /* start the record after that */ |
| 167 | data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*vrec); |
| 168 | } |
Simon Glass | 751b7c7 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 169 | |
| 170 | /* Calculate the new allocated total */ |
Simon Glass | f9ef9fb | 2023-12-27 13:07:06 -0800 | [diff] [blame^] | 171 | new_alloced = data_start - map_to_sysmem(hdr) + |
| 172 | ALIGN(size, 1U << align_log2); |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 173 | |
Simon Glass | 1f618d5 | 2021-07-05 16:32:55 -0600 | [diff] [blame] | 174 | if (new_alloced > hdr->size) { |
Simon Glass | 1d8bbd7 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 175 | log_err("Failed to allocate %x bytes size=%x, need size=%x\n", |
| 176 | size, hdr->size, new_alloced); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 177 | return log_msg_ret("bloblist add", -ENOSPC); |
| 178 | } |
| 179 | rec = (void *)hdr + hdr->alloced; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 180 | |
| 181 | rec->tag = tag; |
Simon Glass | f9ef9fb | 2023-12-27 13:07:06 -0800 | [diff] [blame^] | 182 | rec->hdr_size = sizeof(struct bloblist_rec); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 183 | rec->size = size; |
Simon Glass | b83994d | 2020-01-27 08:49:52 -0700 | [diff] [blame] | 184 | |
| 185 | /* Zero the record data */ |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 186 | memset((void *)rec + rec_hdr_size(rec), '\0', rec->size); |
Simon Glass | 751b7c7 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 187 | |
| 188 | hdr->alloced = new_alloced; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 189 | *recp = rec; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 194 | static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size, |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 195 | int align_log2) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 196 | { |
| 197 | struct bloblist_rec *rec; |
| 198 | |
| 199 | rec = bloblist_findrec(tag); |
| 200 | if (rec) { |
Simon Glass | 5b04454 | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 201 | if (size && size != rec->size) { |
| 202 | *recp = rec; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 203 | return -ESPIPE; |
Simon Glass | 5b04454 | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 204 | } |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 205 | } else { |
| 206 | int ret; |
| 207 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 208 | ret = bloblist_addrec(tag, size, align_log2, &rec); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 209 | if (ret) |
| 210 | return ret; |
| 211 | } |
| 212 | *recp = rec; |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | void *bloblist_find(uint tag, int size) |
| 218 | { |
| 219 | struct bloblist_rec *rec; |
| 220 | |
| 221 | rec = bloblist_findrec(tag); |
| 222 | if (!rec) |
| 223 | return NULL; |
| 224 | if (size && size != rec->size) |
| 225 | return NULL; |
| 226 | |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 227 | return (void *)rec + rec_hdr_size(rec); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 230 | void *bloblist_add(uint tag, int size, int align_log2) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 231 | { |
| 232 | struct bloblist_rec *rec; |
| 233 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 234 | if (bloblist_addrec(tag, size, align_log2, &rec)) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 235 | return NULL; |
| 236 | |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 237 | return (void *)rec + rec_hdr_size(rec); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 240 | int bloblist_ensure_size(uint tag, int size, int align_log2, void **blobp) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 241 | { |
| 242 | struct bloblist_rec *rec; |
| 243 | int ret; |
| 244 | |
Simon Glass | 1a2e02f | 2023-12-27 13:07:00 -0800 | [diff] [blame] | 245 | ret = bloblist_ensurerec(tag, &rec, size, align_log2); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 246 | if (ret) |
| 247 | return ret; |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 248 | *blobp = (void *)rec + rec_hdr_size(rec); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | void *bloblist_ensure(uint tag, int size) |
| 254 | { |
| 255 | struct bloblist_rec *rec; |
| 256 | |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 257 | if (bloblist_ensurerec(tag, &rec, size, 0)) |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 258 | return NULL; |
| 259 | |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 260 | return (void *)rec + rec_hdr_size(rec); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Simon Glass | 5b04454 | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 263 | int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp) |
| 264 | { |
| 265 | struct bloblist_rec *rec; |
| 266 | int ret; |
| 267 | |
Simon Glass | 4c1497e | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 268 | ret = bloblist_ensurerec(tag, &rec, *sizep, 0); |
Simon Glass | 5b04454 | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 269 | if (ret == -ESPIPE) |
| 270 | *sizep = rec->size; |
| 271 | else if (ret) |
| 272 | return ret; |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 273 | *blobp = (void *)rec + rec_hdr_size(rec); |
Simon Glass | 5b04454 | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Simon Glass | 1fe5937 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 278 | static int bloblist_resize_rec(struct bloblist_hdr *hdr, |
| 279 | struct bloblist_rec *rec, |
| 280 | int new_size) |
| 281 | { |
| 282 | int expand_by; /* Number of bytes to expand by (-ve to contract) */ |
| 283 | int new_alloced; /* New value for @hdr->alloced */ |
| 284 | ulong next_ofs; /* Offset of the record after @rec */ |
| 285 | |
| 286 | expand_by = ALIGN(new_size - rec->size, BLOBLIST_ALIGN); |
| 287 | new_alloced = ALIGN(hdr->alloced + expand_by, BLOBLIST_ALIGN); |
| 288 | if (new_size < 0) { |
Simon Glass | 1d8bbd7 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 289 | log_debug("Attempt to shrink blob size below 0 (%x)\n", |
| 290 | new_size); |
Simon Glass | 1fe5937 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 291 | return log_msg_ret("size", -EINVAL); |
| 292 | } |
| 293 | if (new_alloced > hdr->size) { |
Simon Glass | 1d8bbd7 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 294 | log_err("Failed to allocate %x bytes size=%x, need size=%x\n", |
| 295 | new_size, hdr->size, new_alloced); |
Simon Glass | 1fe5937 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 296 | return log_msg_ret("alloc", -ENOSPC); |
| 297 | } |
| 298 | |
| 299 | /* Move the following blobs up or down, if this is not the last */ |
| 300 | next_ofs = bloblist_blob_end_ofs(hdr, rec); |
| 301 | if (next_ofs != hdr->alloced) { |
| 302 | memmove((void *)hdr + next_ofs + expand_by, |
| 303 | (void *)hdr + next_ofs, new_alloced - next_ofs); |
| 304 | } |
| 305 | hdr->alloced = new_alloced; |
| 306 | |
| 307 | /* Zero the new part of the blob */ |
| 308 | if (expand_by > 0) { |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 309 | memset((void *)rec + rec_hdr_size(rec) + rec->size, '\0', |
Simon Glass | 1fe5937 | 2021-07-05 16:32:53 -0600 | [diff] [blame] | 310 | new_size - rec->size); |
| 311 | } |
| 312 | |
| 313 | /* Update the size of this blob */ |
| 314 | rec->size = new_size; |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int bloblist_resize(uint tag, int new_size) |
| 320 | { |
| 321 | struct bloblist_hdr *hdr = gd->bloblist; |
| 322 | struct bloblist_rec *rec; |
| 323 | int ret; |
| 324 | |
| 325 | rec = bloblist_findrec(tag); |
| 326 | if (!rec) |
| 327 | return log_msg_ret("find", -ENOENT); |
| 328 | ret = bloblist_resize_rec(hdr, rec, new_size); |
| 329 | if (ret) |
| 330 | return log_msg_ret("resize", ret); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 335 | static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr) |
| 336 | { |
Simon Glass | 997dac6 | 2023-12-27 13:07:05 -0800 | [diff] [blame] | 337 | u8 chksum; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 338 | |
Simon Glass | 997dac6 | 2023-12-27 13:07:05 -0800 | [diff] [blame] | 339 | chksum = table_compute_checksum(hdr, hdr->alloced); |
| 340 | chksum += hdr->chksum; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 341 | |
| 342 | return chksum; |
| 343 | } |
| 344 | |
| 345 | int bloblist_new(ulong addr, uint size, uint flags) |
| 346 | { |
| 347 | struct bloblist_hdr *hdr; |
| 348 | |
| 349 | if (size < sizeof(*hdr)) |
| 350 | return log_ret(-ENOSPC); |
| 351 | if (addr & (BLOBLIST_ALIGN - 1)) |
| 352 | return log_ret(-EFAULT); |
| 353 | hdr = map_sysmem(addr, size); |
| 354 | memset(hdr, '\0', sizeof(*hdr)); |
| 355 | hdr->version = BLOBLIST_VERSION; |
| 356 | hdr->hdr_size = sizeof(*hdr); |
| 357 | hdr->flags = flags; |
| 358 | hdr->magic = BLOBLIST_MAGIC; |
| 359 | hdr->size = size; |
| 360 | hdr->alloced = hdr->hdr_size; |
| 361 | hdr->chksum = 0; |
| 362 | gd->bloblist = hdr; |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | int bloblist_check(ulong addr, uint size) |
| 368 | { |
| 369 | struct bloblist_hdr *hdr; |
| 370 | u32 chksum; |
| 371 | |
| 372 | hdr = map_sysmem(addr, sizeof(*hdr)); |
| 373 | if (hdr->magic != BLOBLIST_MAGIC) |
| 374 | return log_msg_ret("Bad magic", -ENOENT); |
| 375 | if (hdr->version != BLOBLIST_VERSION) |
| 376 | return log_msg_ret("Bad version", -EPROTONOSUPPORT); |
| 377 | if (size && hdr->size != size) |
| 378 | return log_msg_ret("Bad size", -EFBIG); |
| 379 | chksum = bloblist_calc_chksum(hdr); |
| 380 | if (hdr->chksum != chksum) { |
Simon Glass | 1d8bbd7 | 2022-01-12 19:26:20 -0700 | [diff] [blame] | 381 | log_err("Checksum %x != %x\n", hdr->chksum, chksum); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 382 | return log_msg_ret("Bad checksum", -EIO); |
| 383 | } |
| 384 | gd->bloblist = hdr; |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | int bloblist_finish(void) |
| 390 | { |
| 391 | struct bloblist_hdr *hdr = gd->bloblist; |
| 392 | |
| 393 | hdr->chksum = bloblist_calc_chksum(hdr); |
Simon Glass | 99047f5 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 394 | log_debug("Finished bloblist size %lx at %lx\n", (ulong)hdr->size, |
| 395 | (ulong)map_to_sysmem(hdr)); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Simon Glass | e50a24a | 2022-01-12 19:26:23 -0700 | [diff] [blame] | 400 | ulong bloblist_get_base(void) |
| 401 | { |
| 402 | return map_to_sysmem(gd->bloblist); |
| 403 | } |
| 404 | |
| 405 | ulong bloblist_get_size(void) |
| 406 | { |
| 407 | struct bloblist_hdr *hdr = gd->bloblist; |
| 408 | |
| 409 | return hdr->size; |
| 410 | } |
| 411 | |
Simon Glass | 4aed227 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 412 | void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp) |
| 413 | { |
| 414 | struct bloblist_hdr *hdr = gd->bloblist; |
| 415 | |
| 416 | *basep = map_to_sysmem(gd->bloblist); |
| 417 | *sizep = hdr->size; |
| 418 | *allocedp = hdr->alloced; |
| 419 | } |
| 420 | |
| 421 | static void show_value(const char *prompt, ulong value) |
| 422 | { |
| 423 | printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value); |
| 424 | print_size(value, "\n"); |
| 425 | } |
| 426 | |
| 427 | void bloblist_show_stats(void) |
| 428 | { |
| 429 | ulong base, size, alloced; |
| 430 | |
| 431 | bloblist_get_stats(&base, &size, &alloced); |
| 432 | printf("base: %lx\n", base); |
| 433 | show_value("size", size); |
| 434 | show_value("alloced", alloced); |
| 435 | show_value("free", size - alloced); |
| 436 | } |
| 437 | |
| 438 | void bloblist_show_list(void) |
| 439 | { |
| 440 | struct bloblist_hdr *hdr = gd->bloblist; |
| 441 | struct bloblist_rec *rec; |
| 442 | |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 443 | printf("%-8s %8s Tag Name\n", "Address", "Size"); |
Simon Glass | 4aed227 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 444 | for (rec = bloblist_first_blob(hdr); rec; |
| 445 | rec = bloblist_next_blob(hdr, rec)) { |
Simon Glass | f16ec77 | 2022-01-12 19:26:19 -0700 | [diff] [blame] | 446 | printf("%08lx %8x %4x %s\n", |
Simon Glass | 1f06ed4 | 2023-12-27 13:07:03 -0800 | [diff] [blame] | 447 | (ulong)map_to_sysmem((void *)rec + rec_hdr_size(rec)), |
| 448 | rec->size, rec_tag(rec), |
| 449 | bloblist_tag_name(rec_tag(rec))); |
Simon Glass | 4aed227 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Simon Glass | 9fe0646 | 2021-01-13 20:29:43 -0700 | [diff] [blame] | 453 | void bloblist_reloc(void *to, uint to_size, void *from, uint from_size) |
| 454 | { |
| 455 | struct bloblist_hdr *hdr; |
| 456 | |
| 457 | memcpy(to, from, from_size); |
| 458 | hdr = to; |
| 459 | hdr->size = to_size; |
| 460 | } |
| 461 | |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 462 | int bloblist_init(void) |
| 463 | { |
Simon Glass | ce3e75d | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 464 | bool fixed = IS_ENABLED(CONFIG_BLOBLIST_FIXED); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 465 | int ret = -ENOENT; |
Simon Glass | 99047f5 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 466 | ulong addr, size; |
| 467 | bool expected; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 468 | |
| 469 | /** |
Simon Glass | ce3e75d | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 470 | * We don't expect to find an existing bloblist in the first phase of |
| 471 | * U-Boot that runs. Also we have no way to receive the address of an |
| 472 | * allocated bloblist from a previous stage, so it must be at a fixed |
| 473 | * address. |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 474 | */ |
Simon Glass | ce3e75d | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 475 | expected = fixed && !u_boot_first_phase(); |
Simon Glass | 9fe0646 | 2021-01-13 20:29:43 -0700 | [diff] [blame] | 476 | if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST)) |
| 477 | expected = false; |
Simon Glass | ce3e75d | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 478 | if (fixed) |
| 479 | addr = IF_ENABLED_INT(CONFIG_BLOBLIST_FIXED, |
| 480 | CONFIG_BLOBLIST_ADDR); |
Simon Glass | 99047f5 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 481 | size = CONFIG_BLOBLIST_SIZE; |
| 482 | if (expected) { |
| 483 | ret = bloblist_check(addr, size); |
| 484 | if (ret) { |
| 485 | log_warning("Expected bloblist at %lx not found (err=%d)\n", |
| 486 | addr, ret); |
| 487 | } else { |
| 488 | /* Get the real size, if it is not what we expected */ |
| 489 | size = gd->bloblist->size; |
| 490 | } |
| 491 | } |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 492 | if (ret) { |
Simon Glass | 99047f5 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 493 | if (CONFIG_IS_ENABLED(BLOBLIST_ALLOC)) { |
| 494 | void *ptr = memalign(BLOBLIST_ALIGN, size); |
Simon Glass | d5b6e91 | 2021-11-03 21:09:20 -0600 | [diff] [blame] | 495 | |
| 496 | if (!ptr) |
| 497 | return log_msg_ret("alloc", -ENOMEM); |
| 498 | addr = map_to_sysmem(ptr); |
Simon Glass | ce3e75d | 2022-01-22 05:07:27 -0700 | [diff] [blame] | 499 | } else if (!fixed) { |
| 500 | return log_msg_ret("!fixed", ret); |
Simon Glass | d5b6e91 | 2021-11-03 21:09:20 -0600 | [diff] [blame] | 501 | } |
Simon Glass | 99047f5 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 502 | log_debug("Creating new bloblist size %lx at %lx\n", size, |
| 503 | addr); |
| 504 | ret = bloblist_new(addr, size, 0); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 505 | } else { |
Simon Glass | 99047f5 | 2022-01-12 19:26:22 -0700 | [diff] [blame] | 506 | log_debug("Found existing bloblist size %lx at %lx\n", size, |
| 507 | addr); |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 508 | } |
Simon Glass | 3d65318 | 2023-09-26 08:14:51 -0600 | [diff] [blame] | 509 | if (ret) |
| 510 | return log_msg_ret("ini", ret); |
| 511 | gd->flags |= GD_FLG_BLOBLIST_READY; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 512 | |
Simon Glass | 3d65318 | 2023-09-26 08:14:51 -0600 | [diff] [blame] | 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | int bloblist_maybe_init(void) |
| 517 | { |
| 518 | if (CONFIG_IS_ENABLED(BLOBLIST) && !(gd->flags & GD_FLG_BLOBLIST_READY)) |
| 519 | return bloblist_init(); |
| 520 | |
| 521 | return 0; |
Simon Glass | 9f407d4 | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 522 | } |