Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2018 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <bloblist.h> |
| 9 | #include <log.h> |
| 10 | #include <mapmem.h> |
| 11 | #include <spl.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 13 | #include <u-boot/crc.h> |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 14 | |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 15 | /* |
| 16 | * A bloblist is a single contiguous chunk of memory with a header |
| 17 | * (struct bloblist_hdr) and a number of blobs in it. |
| 18 | * |
| 19 | * Each blob starts on a BLOBLIST_ALIGN boundary relative to the start of the |
| 20 | * bloblist and consists of a struct bloblist_rec, some padding to the required |
| 21 | * alignment for the blog and then the actual data. The padding ensures that the |
| 22 | * start address of the data in each blob is aligned as required. Note that |
| 23 | * each blob's *data* is aligned to BLOBLIST_ALIGN regardless of the alignment |
| 24 | * of the bloblist itself or the blob header. |
| 25 | * |
| 26 | * So far, only BLOBLIST_ALIGN alignment is supported. |
| 27 | */ |
| 28 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 31 | static const char *const tag_name[] = { |
| 32 | [BLOBLISTT_NONE] = "(none)", |
| 33 | [BLOBLISTT_EC_HOSTEVENT] = "EC host event", |
| 34 | [BLOBLISTT_SPL_HANDOFF] = "SPL hand-off", |
| 35 | [BLOBLISTT_VBOOT_CTX] = "Chrome OS vboot context", |
| 36 | [BLOBLISTT_VBOOT_HANDOFF] = "Chrome OS vboot hand-off", |
Simon Glass | 0da2c79 | 2021-01-13 20:29:44 -0700 | [diff] [blame] | 37 | [BLOBLISTT_ACPI_GNVS] = "ACPI GNVS", |
| 38 | [BLOBLISTT_INTEL_VBT] = "Intel Video-BIOS table", |
| 39 | [BLOBLISTT_TPM2_TCG_LOG] = "TPM v2 log space", |
| 40 | [BLOBLISTT_TCPA_LOG] = "TPM log space", |
| 41 | [BLOBLISTT_ACPI_TABLES] = "ACPI tables for x86", |
| 42 | [BLOBLISTT_SMBIOS_TABLES] = "SMBIOS tables for x86", |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | const char *bloblist_tag_name(enum bloblist_tag_t tag) |
| 46 | { |
| 47 | if (tag < 0 || tag >= BLOBLISTT_COUNT) |
| 48 | return "invalid"; |
| 49 | |
| 50 | return tag_name[tag]; |
| 51 | } |
| 52 | |
| 53 | static struct bloblist_rec *bloblist_first_blob(struct bloblist_hdr *hdr) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 54 | { |
| 55 | if (hdr->alloced <= hdr->hdr_size) |
| 56 | return NULL; |
| 57 | return (struct bloblist_rec *)((void *)hdr + hdr->hdr_size); |
| 58 | } |
| 59 | |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 60 | static struct bloblist_rec *bloblist_next_blob(struct bloblist_hdr *hdr, |
| 61 | struct bloblist_rec *rec) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 62 | { |
| 63 | ulong offset; |
| 64 | |
| 65 | offset = (void *)rec - (void *)hdr; |
| 66 | offset += rec->hdr_size + ALIGN(rec->size, BLOBLIST_ALIGN); |
| 67 | if (offset >= hdr->alloced) |
| 68 | return NULL; |
| 69 | return (struct bloblist_rec *)((void *)hdr + offset); |
| 70 | } |
| 71 | |
| 72 | #define foreach_rec(_rec, _hdr) \ |
| 73 | for (_rec = bloblist_first_blob(_hdr); \ |
| 74 | _rec; \ |
| 75 | _rec = bloblist_next_blob(_hdr, _rec)) |
| 76 | |
| 77 | static struct bloblist_rec *bloblist_findrec(uint tag) |
| 78 | { |
| 79 | struct bloblist_hdr *hdr = gd->bloblist; |
| 80 | struct bloblist_rec *rec; |
| 81 | |
| 82 | if (!hdr) |
| 83 | return NULL; |
| 84 | |
| 85 | foreach_rec(rec, hdr) { |
| 86 | if (rec->tag == tag) |
| 87 | return rec; |
| 88 | } |
| 89 | |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 93 | static int bloblist_addrec(uint tag, int size, int align, |
| 94 | struct bloblist_rec **recp) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 95 | { |
| 96 | struct bloblist_hdr *hdr = gd->bloblist; |
| 97 | struct bloblist_rec *rec; |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 98 | int data_start, new_alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 99 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 100 | if (!align) |
| 101 | align = BLOBLIST_ALIGN; |
| 102 | |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 103 | /* Figure out where the new data will start */ |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 104 | data_start = map_to_sysmem(hdr) + hdr->alloced + sizeof(*rec); |
| 105 | |
| 106 | /* Align the address and then calculate the offset from ->alloced */ |
| 107 | data_start = ALIGN(data_start, align) - map_to_sysmem(hdr); |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 108 | |
| 109 | /* Calculate the new allocated total */ |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 110 | new_alloced = data_start + ALIGN(size, align); |
| 111 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 112 | if (new_alloced >= hdr->size) { |
| 113 | log(LOGC_BLOBLIST, LOGL_ERR, |
Simon Glass | 32ec7c6 | 2020-01-27 08:49:51 -0700 | [diff] [blame] | 114 | "Failed to allocate %x bytes size=%x, need size=%x\n", |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 115 | size, hdr->size, new_alloced); |
| 116 | return log_msg_ret("bloblist add", -ENOSPC); |
| 117 | } |
| 118 | rec = (void *)hdr + hdr->alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 119 | |
| 120 | rec->tag = tag; |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 121 | rec->hdr_size = data_start - hdr->alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 122 | rec->size = size; |
| 123 | rec->spare = 0; |
Simon Glass | 3e85ea0 | 2020-01-27 08:49:52 -0700 | [diff] [blame] | 124 | |
| 125 | /* Zero the record data */ |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 126 | memset((void *)rec + rec->hdr_size, '\0', rec->size); |
| 127 | |
| 128 | hdr->alloced = new_alloced; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 129 | *recp = rec; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 134 | static int bloblist_ensurerec(uint tag, struct bloblist_rec **recp, int size, |
| 135 | int align) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 136 | { |
| 137 | struct bloblist_rec *rec; |
| 138 | |
| 139 | rec = bloblist_findrec(tag); |
| 140 | if (rec) { |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 141 | if (size && size != rec->size) { |
| 142 | *recp = rec; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 143 | return -ESPIPE; |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 144 | } |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 145 | } else { |
| 146 | int ret; |
| 147 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 148 | ret = bloblist_addrec(tag, size, align, &rec); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 149 | if (ret) |
| 150 | return ret; |
| 151 | } |
| 152 | *recp = rec; |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | void *bloblist_find(uint tag, int size) |
| 158 | { |
| 159 | struct bloblist_rec *rec; |
| 160 | |
| 161 | rec = bloblist_findrec(tag); |
| 162 | if (!rec) |
| 163 | return NULL; |
| 164 | if (size && size != rec->size) |
| 165 | return NULL; |
| 166 | |
| 167 | return (void *)rec + rec->hdr_size; |
| 168 | } |
| 169 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 170 | void *bloblist_add(uint tag, int size, int align) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 171 | { |
| 172 | struct bloblist_rec *rec; |
| 173 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 174 | if (bloblist_addrec(tag, size, align, &rec)) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 175 | return NULL; |
| 176 | |
Simon Glass | d7985e6 | 2020-09-19 18:49:28 -0600 | [diff] [blame] | 177 | return (void *)rec + rec->hdr_size; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 180 | int bloblist_ensure_size(uint tag, int size, int align, void **blobp) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 181 | { |
| 182 | struct bloblist_rec *rec; |
| 183 | int ret; |
| 184 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 185 | ret = bloblist_ensurerec(tag, &rec, size, align); |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 186 | if (ret) |
| 187 | return ret; |
| 188 | *blobp = (void *)rec + rec->hdr_size; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | void *bloblist_ensure(uint tag, int size) |
| 194 | { |
| 195 | struct bloblist_rec *rec; |
| 196 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 197 | if (bloblist_ensurerec(tag, &rec, size, 0)) |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 198 | return NULL; |
| 199 | |
| 200 | return (void *)rec + rec->hdr_size; |
| 201 | } |
| 202 | |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 203 | int bloblist_ensure_size_ret(uint tag, int *sizep, void **blobp) |
| 204 | { |
| 205 | struct bloblist_rec *rec; |
| 206 | int ret; |
| 207 | |
Simon Glass | 0637397 | 2020-09-19 18:49:29 -0600 | [diff] [blame] | 208 | ret = bloblist_ensurerec(tag, &rec, *sizep, 0); |
Simon Glass | 1e0304e | 2020-01-27 08:49:50 -0700 | [diff] [blame] | 209 | if (ret == -ESPIPE) |
| 210 | *sizep = rec->size; |
| 211 | else if (ret) |
| 212 | return ret; |
| 213 | *blobp = (void *)rec + rec->hdr_size; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 218 | static u32 bloblist_calc_chksum(struct bloblist_hdr *hdr) |
| 219 | { |
| 220 | struct bloblist_rec *rec; |
| 221 | u32 chksum; |
| 222 | |
| 223 | chksum = crc32(0, (unsigned char *)hdr, |
| 224 | offsetof(struct bloblist_hdr, chksum)); |
| 225 | foreach_rec(rec, hdr) { |
| 226 | chksum = crc32(chksum, (void *)rec, rec->hdr_size); |
| 227 | chksum = crc32(chksum, (void *)rec + rec->hdr_size, rec->size); |
| 228 | } |
| 229 | |
| 230 | return chksum; |
| 231 | } |
| 232 | |
| 233 | int bloblist_new(ulong addr, uint size, uint flags) |
| 234 | { |
| 235 | struct bloblist_hdr *hdr; |
| 236 | |
| 237 | if (size < sizeof(*hdr)) |
| 238 | return log_ret(-ENOSPC); |
| 239 | if (addr & (BLOBLIST_ALIGN - 1)) |
| 240 | return log_ret(-EFAULT); |
| 241 | hdr = map_sysmem(addr, size); |
| 242 | memset(hdr, '\0', sizeof(*hdr)); |
| 243 | hdr->version = BLOBLIST_VERSION; |
| 244 | hdr->hdr_size = sizeof(*hdr); |
| 245 | hdr->flags = flags; |
| 246 | hdr->magic = BLOBLIST_MAGIC; |
| 247 | hdr->size = size; |
| 248 | hdr->alloced = hdr->hdr_size; |
| 249 | hdr->chksum = 0; |
| 250 | gd->bloblist = hdr; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int bloblist_check(ulong addr, uint size) |
| 256 | { |
| 257 | struct bloblist_hdr *hdr; |
| 258 | u32 chksum; |
| 259 | |
| 260 | hdr = map_sysmem(addr, sizeof(*hdr)); |
| 261 | if (hdr->magic != BLOBLIST_MAGIC) |
| 262 | return log_msg_ret("Bad magic", -ENOENT); |
| 263 | if (hdr->version != BLOBLIST_VERSION) |
| 264 | return log_msg_ret("Bad version", -EPROTONOSUPPORT); |
| 265 | if (size && hdr->size != size) |
| 266 | return log_msg_ret("Bad size", -EFBIG); |
| 267 | chksum = bloblist_calc_chksum(hdr); |
| 268 | if (hdr->chksum != chksum) { |
| 269 | log(LOGC_BLOBLIST, LOGL_ERR, "Checksum %x != %x\n", hdr->chksum, |
| 270 | chksum); |
| 271 | return log_msg_ret("Bad checksum", -EIO); |
| 272 | } |
| 273 | gd->bloblist = hdr; |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | int bloblist_finish(void) |
| 279 | { |
| 280 | struct bloblist_hdr *hdr = gd->bloblist; |
| 281 | |
| 282 | hdr->chksum = bloblist_calc_chksum(hdr); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
Simon Glass | b936a97 | 2020-09-19 18:49:26 -0600 | [diff] [blame] | 287 | void bloblist_get_stats(ulong *basep, ulong *sizep, ulong *allocedp) |
| 288 | { |
| 289 | struct bloblist_hdr *hdr = gd->bloblist; |
| 290 | |
| 291 | *basep = map_to_sysmem(gd->bloblist); |
| 292 | *sizep = hdr->size; |
| 293 | *allocedp = hdr->alloced; |
| 294 | } |
| 295 | |
| 296 | static void show_value(const char *prompt, ulong value) |
| 297 | { |
| 298 | printf("%s:%*s %-5lx ", prompt, 8 - (int)strlen(prompt), "", value); |
| 299 | print_size(value, "\n"); |
| 300 | } |
| 301 | |
| 302 | void bloblist_show_stats(void) |
| 303 | { |
| 304 | ulong base, size, alloced; |
| 305 | |
| 306 | bloblist_get_stats(&base, &size, &alloced); |
| 307 | printf("base: %lx\n", base); |
| 308 | show_value("size", size); |
| 309 | show_value("alloced", alloced); |
| 310 | show_value("free", size - alloced); |
| 311 | } |
| 312 | |
| 313 | void bloblist_show_list(void) |
| 314 | { |
| 315 | struct bloblist_hdr *hdr = gd->bloblist; |
| 316 | struct bloblist_rec *rec; |
| 317 | |
| 318 | printf("%-8s %8s Tag Name\n", "Address", "Size"); |
| 319 | for (rec = bloblist_first_blob(hdr); rec; |
| 320 | rec = bloblist_next_blob(hdr, rec)) { |
| 321 | printf("%08lx %8x %3d %s\n", |
| 322 | (ulong)map_to_sysmem((void *)rec + rec->hdr_size), |
| 323 | rec->size, rec->tag, bloblist_tag_name(rec->tag)); |
| 324 | } |
| 325 | } |
| 326 | |
Simon Glass | ab7e746 | 2021-01-13 20:29:43 -0700 | [diff] [blame] | 327 | void bloblist_reloc(void *to, uint to_size, void *from, uint from_size) |
| 328 | { |
| 329 | struct bloblist_hdr *hdr; |
| 330 | |
| 331 | memcpy(to, from, from_size); |
| 332 | hdr = to; |
| 333 | hdr->size = to_size; |
| 334 | } |
| 335 | |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 336 | int bloblist_init(void) |
| 337 | { |
| 338 | bool expected; |
| 339 | int ret = -ENOENT; |
| 340 | |
| 341 | /** |
| 342 | * Wed expect to find an existing bloblist in the first phase of U-Boot |
| 343 | * that runs |
| 344 | */ |
| 345 | expected = !u_boot_first_phase(); |
Simon Glass | ab7e746 | 2021-01-13 20:29:43 -0700 | [diff] [blame] | 346 | if (spl_prev_phase() == PHASE_TPL && !IS_ENABLED(CONFIG_TPL_BLOBLIST)) |
| 347 | expected = false; |
Simon Glass | 712bd2d | 2018-11-15 18:43:50 -0700 | [diff] [blame] | 348 | if (expected) |
| 349 | ret = bloblist_check(CONFIG_BLOBLIST_ADDR, |
| 350 | CONFIG_BLOBLIST_SIZE); |
| 351 | if (ret) { |
| 352 | log(LOGC_BLOBLIST, expected ? LOGL_WARNING : LOGL_DEBUG, |
| 353 | "Existing bloblist not found: creating new bloblist\n"); |
| 354 | ret = bloblist_new(CONFIG_BLOBLIST_ADDR, CONFIG_BLOBLIST_SIZE, |
| 355 | 0); |
| 356 | } else { |
| 357 | log(LOGC_BLOBLIST, LOGL_DEBUG, "Found existing bloblist\n"); |
| 358 | } |
| 359 | |
| 360 | return ret; |
| 361 | } |