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