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