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