Debian Codegraph @

src/drivers/efi/capsules.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpi.h>
4 #include <boot/coreboot_tables.h>
5 #include <bootmem.h>
6 #include <bootstate.h>
7 #include <cbmem.h>
8 #include <console/console.h>
9 #include <cpu/x86/pae.h>
10 #include <drivers/efi/efivars.h>
11 #include <drivers/efi/capsules.h>
12 #include <memrange.h>
13 #include <smm_call.h>
14 #include <smmstore.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <types.h>
18
19 #include <Uefi/UefiSpec.h>
20 #include <Guid/GlobalVariable.h>
21 #include <Guid/FmpCapsule.h>
22 #include <IndustryStandard/WindowsUxCapsule.h>
23
24 /*
25 * Overview
26 *
27 * SG stands for scatter-gather. SG list consists of SG blocks that describe a
28 * potentially discontinuous sequence of memory blocks while not necessarily
29 * lying in continuous memory themselves.
30 *
31 * SG list is basically a linked list of arrays of block descriptors (SG
32 * blocks). Each of SG blocks can be:
33 * - a data block, which points to capsule's data
34 * - a continuation block, which says where other SG blocks are to be found
35 * - end-of-list block, which indicates there are no more blocks
36 *
37 * Each of the CapsuleUpdateData* EFI variables point to some SG list which
38 * might contain one or more update capsules. SG blocks never contain data of
39 * more than one of the capsules. Boundary between capsules in an SG list is
40 * determined by parsing capsule headers and counting amount of data seen so
41 * far.
42 *
43 * There can be multiple CapsuleUpdateData* variables (CapsuleUpdateData,
44 * CapsuleUpdateData1, etc.) in which case their SG lists are chained together
45 * after sanity checks.
46 */
47
48 /* This should be more than enough. */
49 #define MAX_CAPSULES 32
50
51 /* 4 should be enough, but 8 won't hurt. */
52 #define CAPSULE_ALIGNMENT 8
53
54 /*
55 * A helper structure which bundles physical block address with its data. It's
56 * necessary because 32-bit code can't easily access anything beyond 4 GiB
57 * boundary and this structure allows reading the data, passing it around and,
58 * if necessary, updating it.
59 *
60 * Usage:
61 * 1) Set .self to physical address
62 * 2) Check block's address with is_good_block()
63 * 3) Use load_block() to fetch or store_block() to update data
64 */
65 struct block_descr {
66 /* Where the data comes from. */
67 uint64_t self;
68
69 /*
70 * Data read from the self address above. Three cases:
71 * - len != 0 && addr != 0 => len bytes of capsule data at addr
72 * next block_descr follows this one (self + 16)
73 * - len == 0 && addr != 0 => no data
74 * next block_descr is at addr
75 * - len == 0 && addr == 0 => no data
76 * no next block_descr
77 */
78 uint64_t len;
79 uint64_t addr;
80 };
81
82 /* For passing data from efi_parse_capsules() to bootmem and CBMEM callbacks. */
83 struct memory_range {
84 uint32_t base;
85 uint32_t len;
86 };
87
88 static const EFI_GUID capsule_vendor_guid = {
89 0x711C703F, 0xC285, 0x4B10, { 0xA3, 0xB0, 0x36, 0xEC, 0xBD, 0x3C, 0x8B, 0xE2 }
90 };
91 static const EFI_GUID windows_ux_capsule_guid = WINDOWS_UX_CAPSULE_GUID;
92 static const EFI_GUID edk2_capsule_on_disk_name_guid = {
93 0x98C80A4F, 0xE16B, 0x4D11, { 0x93, 0x9A, 0xAB, 0xE5, 0x61, 0x26, 0x3, 0x30 }
94 };
95 static const EFI_GUID efi_fmp_capsule_guid = EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
96
97 /* Memory map to keep track of unused or reserved ranges. */
98 struct memranges memory_map;
99
100 /* Page tables required for pae_map_2M_page(). */
101 static char pae_page_tables[20 * KiB] __aligned(4 * KiB);
102
103 /* Where all coalesced capsules are located. */
104 struct memory_range coalesce_buffer;
105
106 /* Where individual coalesced capsules are located and their count. */
107 static struct memory_range uefi_capsules[MAX_CAPSULES];
108 static int uefi_capsule_count;
109
110 static bool is_data_block(const struct block_descr *block)
111 {
112 return (block->len != 0);
113 }
114
115 static bool is_final_block(const struct block_descr *block)
116 {
117 return (block->len == 0 && block->addr == 0);
118 }
119
120 static void *map_range(uint64_t base, uint32_t len)
121 {
122 static uint64_t last_mapping_base = UINT64_MAX;
123
124 /* Using MMCONF should be safe as long as we don't do any device
125 initialization during parsing of capsules and don't forget to call
126 paging_disable_pae() at the end. */
127 _Static_assert(IS_ALIGNED(CONFIG_ECAM_MMCONF_BASE_ADDRESS, 2 * MiB));
128 uintptr_t window_base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
129 size_t window_size = 2 * MiB;
130
131 printk(BIOS_SPEW, "capsules: mapping %#010x bytes at %#010llx.\n", len, base);
132
133 if (base + len <= 4ULL * GiB &&
134 (base + len <= window_base || base >= window_base + window_size)) {
135 /* Don't bother with the mapping, the whole range must be
136 already accessible without it. */
137 printk(BIOS_SPEW, "capsules: no need to map anything.\n");
138 return (void *)(uintptr_t)base;
139 }
140
141 uint64_t aligned_base = ALIGN_DOWN(base, 2 * MiB);
142 if (base - aligned_base + len > 2 * MiB)
143 die("capsules: memory range map request can't be satisfied.\n");
144
145 /* No need to map the same data. */
146 if (aligned_base != last_mapping_base) {
147 printk(BIOS_SPEW, "capsules: mapping from %#010llx.\n", aligned_base);
148 pae_map_2M_page(&pae_page_tables, aligned_base, (void *)window_base);
149 last_mapping_base = aligned_base;
150 }
151
152 return (uint8_t *)window_base + (base - aligned_base);
153 }
154
155 /*
156 * Alignment requirement on EFI_CAPSULE_BLOCK_DESCRIPTOR seems to be 8 bytes,
157 * which means that it can be cut in half by a mapping. Could map two 2 MiB
158 * pages instead, but should be easier to simply read those 16 bytes and pass
159 * them around.
160 *
161 * `volatile` is to guard against a hypothetical statement reordering.
162 */
163
164 static void load_block(struct block_descr *block)
165 {
166 volatile uint64_t *len = map_range(block->self, sizeof(uint64_t));
167 block->len = *len;
168
169 volatile uint64_t *addr = map_range(block->self + sizeof(uint64_t), sizeof(uint64_t));
170 block->addr = *addr;
171 }
172
173 static void store_block(const struct block_descr *block)
174 {
175 volatile uint64_t *len = map_range(block->self, sizeof(uint64_t));
176 *len = block->len;
177
178 volatile uint64_t *addr = map_range(block->self + sizeof(uint64_t), sizeof(uint64_t));
179 *addr = block->addr;
180 }
181
182 static void advance_block(struct block_descr *block)
183 {
184 if (is_final_block(block))
185 die("capsules: attempt to advance beyond final SG block of UEFI capsules.\n");
186
187 if (is_data_block(block)) {
188 /* That was at least part of a capsule. */
189 block->self = block->self + sizeof(EFI_CAPSULE_BLOCK_DESCRIPTOR);
190 } else {
191 /* End of continuous sequence of descriptors, but there are more. */
192 block->self = block->addr;
193 }
194 }
195
196 static bool is_good_capsule(const EFI_CAPSULE_HEADER *capsule)
197 {
198 if (capsule->HeaderSize < sizeof(*capsule)) {
199 printk(BIOS_ERR, "capsules: capsule header size is too small: %#010x.\n",
200 capsule->HeaderSize);
201 return false;
202 }
203 if (capsule->CapsuleImageSize <= capsule->HeaderSize) {
204 printk(BIOS_ERR, "capsules: capsule image size is too small: %#010x.\n",
205 capsule->CapsuleImageSize);
206 return false;
207 }
208 if (!(capsule->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET)) {
209 printk(BIOS_ERR,
210 "capsules: this capsule should not have persisted, flags: %#010x.\n",
211 capsule->Flags);
212 return false;
213 }
214
215 const EFI_GUID *guid = &capsule->CapsuleGuid;
216 if (memcmp(guid, &windows_ux_capsule_guid, sizeof(*guid)) == 0)
217 return true;
218 if (memcmp(guid, &edk2_capsule_on_disk_name_guid, sizeof(*guid)) == 0)
219 return true;
220 if (memcmp(guid, &efi_fmp_capsule_guid, sizeof(*guid)) == 0)
221 return true;
222
223 printk(BIOS_ERR, "capsules: unrecognized capsule GUID.\n");
224 return false;
225 }
226
227 static bool is_in_unused_ram(uint64_t base, uint64_t len)
228 {
229 if (len == 0) {
230 die("capsules: %s() was passed an empty range: %#010llx:%#010llx.\n",
231 __func__, base, len);
232 }
233 if (base + len < base) {
234 die("capsules: %s() was passed an invalid range: %#010llx:%#010llx.\n",
235 __func__, base, len);
236 }
237
238 const struct range_entry *r;
239 memranges_each_entry(r, &memory_map) {
240 if (range_entry_tag(r) != BM_MEM_RAM)
241 continue;
242
243 if (base >= range_entry_base(r) && base + len <= range_entry_end(r))
244 return true;
245 }
246
247 return false;
248 }
249
250 static bool is_good_block(struct block_descr *block)
251 {
252 if (!IS_ALIGNED(block->self, sizeof(uint64_t))) {
253 printk(BIOS_ERR, "capsules: misaligned SG block at %#010llx.\n", block->self);
254 return false;
255 }
256
257 if (!is_in_unused_ram(block->self, sizeof(*block))) {
258 printk(BIOS_ERR, "capsules: SG block is not in unused memory.\n");
259 return false;
260 }
261
262 return true;
263 }
264
265 static bool is_good_capsule_head(struct block_descr *block)
266 {
267 if (!is_data_block(block)) {
268 printk(BIOS_ERR, "capsules: first capsule SG block is not a data block.\n");
269 return false;
270 }
271
272 if (block->len < sizeof(EFI_CAPSULE_HEADER)) {
273 printk(BIOS_ERR, "capsules: first SG block of a capsule is too small.\n");
274 return false;
275 }
276
277 if (!is_in_unused_ram(block->addr, block->len)) {
278 printk(BIOS_ERR, "capsules: capsule header is not in unused memory.\n");
279 return false;
280 }
281
282 return true;
283 }
284
285 static bool is_good_capsule_block(struct block_descr *block, uint32_t size_left)
286 {
287 if (is_final_block(block)) {
288 printk(BIOS_ERR, "capsules: not enough SG blocks to cover a capsule.\n");
289 return false;
290 }
291
292 if (!is_data_block(block)) {
293 printk(BIOS_ERR, "capsules: capsule SG block is not a data block.\n");
294 return false;
295 }
296
297 if (block->len > size_left) {
298 printk(BIOS_ERR, "capsules: SG blocks reach beyond a capsule.\n");
299 return false;
300 }
301
302 if (!is_in_unused_ram(block->addr, block->len)) {
303 printk(BIOS_ERR, "capsules: capsule data is not in unused memory.\n");
304 return false;
305 }
306
307 return true;
308 }
309
310 /* Checks a single SG list for sanity. Returns its end-of-list descriptor or
311 an empty one on error. */
312 static struct block_descr check_capsule_block(struct block_descr first_block,
313 uint64_t *total_data_size)
314 {
315 struct block_descr block = first_block;
316 if (!is_good_block(&block)) {
317 printk(BIOS_ERR, "capsules: bad capsule block start.\n");
318 goto error;
319 }
320
321 load_block(&block);
322
323 uint64_t data_size = 0;
324 while (!is_final_block(&block)) {
325 /*
326 * This results in dropping of this capsule block if any of
327 * contained capsule headers looks weird. An alternative is to
328 * cut the capsule block upon finding a bad header. Maybe
329 * could even jump over a broken capsule, temporarily trusting
330 * size field in its header because invalid value should not
331 * break parsing anyway, and then cut it out of the sequence of
332 * blocks. EDK doesn't bother, so only noting the possibility.
333 */
334 if (!is_good_capsule_head(&block)) {
335 printk(BIOS_ERR, "capsules: bad capsule header @ %#010llx.\n",
336 block.addr);
337 goto error;
338 }
339
340 const EFI_CAPSULE_HEADER *capsule_hdr =
341 map_range(block.addr, sizeof(*capsule_hdr));
342 if (!is_good_capsule(capsule_hdr)) {
343 printk(BIOS_ERR, "capsules: bad capsule header @ %#010llx.\n",
344 block.addr);
345 goto error;
346 }
347
348 uint64_t capsule_size =
349 ALIGN_UP((uint64_t)capsule_hdr->CapsuleImageSize, CAPSULE_ALIGNMENT);
350 if (data_size + capsule_size < data_size) { /* overflow detection */
351 printk(BIOS_ERR,
352 "capsules: capsules block size is too large (%#llx + %#llx) for uint64.\n",
353 data_size, capsule_size);
354 goto error;
355 }
356 data_size += capsule_size;
357
358 uint32_t size_left = capsule_hdr->CapsuleImageSize;
359 while (size_left != 0) {
360 /* is_good_block() holds here whether it's the first iteration or
361 not. */
362
363 if (!is_good_capsule_block(&block, size_left))
364 goto error;
365
366 size_left -= block.len;
367
368 advance_block(&block);
369 if (!is_good_block(&block)) {
370 printk(BIOS_ERR, "capsules: capsule body has a bad block.\n");
371 goto error;
372 }
373
374 load_block(&block);
375 if (!is_final_block(&block) && !is_data_block(&block)) {
376 /* Advance to the next page of block descriptors. */
377 advance_block(&block);
378 if (!is_good_block(&block)) {
379 printk(BIOS_ERR, "capsules: bad SG continuation.\n");
380 goto error;
381 }
382
383 load_block(&block);
384 /* Not expecting a continuation to be followed by another
385 continuation or an end-of-list. */
386 if (!is_data_block(&block)) {
387 printk(BIOS_ERR,
388 "capsules: chained SG continuations.\n");
389 goto error;
390 }
391 }
392 }
393 }
394
395 /* Increase the size only on successful parsing of the capsule block. */
396 if (*total_data_size + data_size < *total_data_size) { /* overflow detection */
397 printk(BIOS_ERR,
398 "capsules: total capsule's size is too large (%#llx + %#llx) for uint64.\n",
399 *total_data_size, data_size);
400 goto error;
401 }
402 *total_data_size += data_size;
403
404 return block;
405
406 error:
407 return (struct block_descr){ .self = 0 };
408 }
409
410 /* Fills an array with pointers to capsule blocks. Returns number of
411 discovered capsule blocks or -1 on error. */
412 static int discover_capsule_blocks(struct region_device *rdev,
413 struct block_descr *blocks,
414 int max_blocks)
415 {
416 int block_count = 0;
417 for (int i = 0; block_count < max_blocks; ++i) {
418 char var_name[32];
419 if (i == 0)
420 strcpy(var_name, "CapsuleUpdateData");
421 else
422 snprintf(var_name, sizeof(var_name), "CapsuleUpdateData%d", i);
423
424 struct block_descr block;
425 uint32_t size = sizeof(block.self);
426 enum cb_err ret = efi_fv_get_option(rdev, &capsule_vendor_guid, var_name,
427 &block.self, &size);
428 if (ret != CB_SUCCESS) {
429 /* No more variables. */
430 break;
431 }
432 if (size != sizeof(block.self)) {
433 printk(BIOS_ERR, "capsules: unexpected capsule data size (%d).\n",
434 size);
435 return -1;
436 }
437
438 /*
439 * EDK2 checks for duplicates probably because we'll get into
440 * trouble with chaining if there are any, so do the check.
441 *
442 * This, however, won't handle all possible situations which
443 * lead to loops or processing the same capsule more than once.
444 */
445 int j;
446 for (j = 0; j < block_count; ++j) {
447 if (blocks[j].self == block.self)
448 break;
449 }
450 if (j < block_count) {
451 printk(BIOS_INFO, "capsules: skipping duplicated %s.\n", var_name);
452 continue;
453 }
454
455 printk(BIOS_INFO, "capsules: capsule block #%d at %#010llx.\n",
456 block_count, block.self);
457 blocks[block_count++] = block;
458 }
459
460 return block_count;
461 }
462
463 /*
464 * This function connects tail of one block of descriptors with the head of the
465 * next one and returns pointer to the head of the whole chain. While at it:
466 * - validate structures and pointers for sanity
467 * - compute total amount of memory needed for coalesced capsules
468 *
469 * Returns block that starts at 0 on error.
470 */
471 static struct block_descr verify_and_chain_blocks(struct block_descr *blocks,
472 int block_count,
473 uint64_t *total_data_size)
474 {
475 /* This won't be blocks[0] if there is something wrong with the first capsule block. */
476 struct block_descr head = {0};
477
478 /* End-of-list descriptor of the last chained block. */
479 struct block_descr tail = {0};
480
481 *total_data_size = 0;
482
483 for (int i = 0; i < block_count; ++i) {
484 struct block_descr last_block = check_capsule_block(blocks[i], total_data_size);
485 if (last_block.self == 0) {
486 /* Fail hard instead? EDK just keeps going, as if capsule
487 blocks are always independent. */
488 printk(BIOS_WARNING,
489 "capsules: skipping damaged capsule block #%d @ %#010llx.\n",
490 i, blocks[i].self);
491 continue;
492 }
493
494 if (head.self == 0) {
495 head = blocks[i];
496 } else {
497 tail.addr = blocks[i].self;
498 store_block(&tail);
499 }
500
501 tail = last_block;
502 }
503
504 return head;
505 }
506
507 /* Marks structures and data of SG lists as BM_MEM_RESERVED so we don't step on
508 them when looking for usable memory. */
509 static void reserve_capsules(struct block_descr block_chain)
510 {
511 struct block_descr block = block_chain;
512
513 /* This is the first block of a continuous sequence of blocks. */
514 struct block_descr seq_start = {0};
515
516 /* The code reserves sequences of blocks to avoid invoking
517 memranges_insert() on each of a bunch of adjacent 16-byte blocks. */
518
519 load_block(&block);
520 for (; !is_final_block(&block); advance_block(&block), load_block(&block)) {
521 if (seq_start.self == 0)
522 seq_start = block;
523
524 if (is_data_block(&block)) {
525 /* Reserve capsule data. */
526 memranges_insert(&memory_map, block.addr, block.len, BM_MEM_RESERVED);
527 } else {
528 /* This isn't the final or a data block, so it must be the
529 last block of a continuous sequence. Reserve the whole
530 sequence. */
531 memranges_insert(&memory_map,
532 seq_start.self,
533 block.self - seq_start.self +
534 sizeof(EFI_CAPSULE_BLOCK_DESCRIPTOR),
535 BM_MEM_RESERVED);
536
537 /* Will be set on the next iteration if there is one. */
538 seq_start.self = 0;
539 }
540 }
541
542 /* If continuations never show up in a row as checked by
543 check_capsule_block(), seq_start must be non-NULL here. */
544 memranges_insert(&memory_map,
545 seq_start.self,
546 block.self - seq_start.self + sizeof(EFI_CAPSULE_BLOCK_DESCRIPTOR),
547 BM_MEM_RESERVED);
548 }
549
550 /*
551 * Find a buffer below 4 GiB for coalesced capsules.
552 *
553 * Keeping it simple and allocating a single buffer. However, there is
554 * no requirement to put all the capsules together, only that each of
555 * them is continuous in memory. So if this is bad for some reason,
556 * can allocate a separate block for each.
557 *
558 * Returns buffer that starts at 0 on error.
559 */
560 static struct memory_range pick_buffer(uint64_t total_data_size)
561 {
562 struct memory_range buffer = {0};
563
564 /* 4 * KiB is the alignment set by memranges_init(). */
565 total_data_size = ALIGN_UP(total_data_size, 4 * KiB);
566
567 const struct range_entry *r;
568 memranges_each_entry(r, &memory_map) {
569 if (range_entry_tag(r) != BM_MEM_RAM)
570 continue;
571
572 resource_t base = range_entry_base(r);
573 if (base >= 4ULL * GiB)
574 break;
575
576 /* Possibly reduce size to not deal with ranges that cross 4 GiB boundary. */
577 resource_t size = range_entry_size(r);
578 if (base + size > 4ULL * GiB)
579 size -= base + size - 4ULL * GiB;
580
581 if (size >= total_data_size) {
582 /*
583 * To not create troubles for payloads prefer higher addresses:
584 * - use the top part of a suitable range
585 * - exit the loop only after hitting 4 GiB boundary or end of the list
586 */
587 buffer.base = base + size - total_data_size;
588 buffer.len = total_data_size;
589 }
590 }
591
592 return buffer;
593 }
594
595 /* Puts capsules into continuous physical memory. */
596 static void coalesce_capsules(struct block_descr block_chain, uint8_t *target)
597 {
598 struct block_descr block = block_chain;
599 uint8_t *capsule_start = NULL;
600 uint32_t capsule_size = 0;
601 uint32_t size_left = 0;
602
603 /* No safety checks in this function, as all of them were done earlier. */
604
605 load_block(&block);
606 for (; !is_final_block(&block); advance_block(&block), load_block(&block)) {
607 /* Advance over a continuation. */
608 if (!is_data_block(&block))
609 continue;
610
611 /* This must be the first block of a capsule. */
612 if (size_left == 0) {
613 const EFI_CAPSULE_HEADER *capsule_hdr =
614 map_range(block.addr, sizeof(*capsule_hdr));
615 capsule_size = capsule_hdr->CapsuleImageSize;
616 capsule_start = target;
617
618 size_left = capsule_size;
619 }
620
621 uint64_t addr = block.addr;
622 uint64_t data_left = block.len;
623 while (data_left != 0) {
624 uint64_t piece_len = MIN(data_left, 2 * MiB - (addr % 2 * MiB));
625 void *data = map_range(addr, piece_len);
626
627 memcpy(target, data, piece_len);
628
629 target += piece_len;
630 addr += piece_len;
631 data_left -= piece_len;
632 }
633
634 size_left -= block.len;
635
636 /* This must be the last block of a capsule, record it. */
637 if (size_left == 0) {
638 /* If we can just ignore corrupted capsules, then we can simply
639 drop those which don't fit. */
640 if (uefi_capsule_count == MAX_CAPSULES) {
641 printk(BIOS_WARNING,
642 "capsules: ignoring all capsules after #%d.\n",
643 MAX_CAPSULES);
644 break;
645 }
646
647 uefi_capsules[uefi_capsule_count].base = (uintptr_t)capsule_start;
648 uefi_capsules[uefi_capsule_count].len = capsule_size;
649 uefi_capsule_count++;
650
651 /* This is to align start of the next capsule (assumes that
652 initial value of target was suitably aligned). */
653 if (!IS_ALIGNED(capsule_size, CAPSULE_ALIGNMENT))
654 target += ALIGN_UP(capsule_size, CAPSULE_ALIGNMENT) -
655 capsule_size;
656 }
657 }
658
659 printk(BIOS_INFO, "capsules: found %d capsule(s).\n", uefi_capsule_count);
660 }
661
662 void efi_parse_capsules(void)
663 {
664 /* EDK2 starts with 20 items and then grows the list, but it's unlikely
665 to be necessary in practice. */
666 enum { MAX_CAPSULE_BLOCKS = MAX_CAPSULES };
667
668 struct region_device rdev;
669 if (smmstore_lookup_region(&rdev)) {
670 printk(BIOS_INFO, "capsules: no SMMSTORE region, no update capsules.\n");
671 return;
672 }
673
674 memranges_init(&memory_map, IORESOURCE_MEM | IORESOURCE_FIXED | IORESOURCE_STORED |
675 IORESOURCE_ASSIGNED | IORESOURCE_CACHEABLE, IORESOURCE_MEM |
676 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED |
677 IORESOURCE_CACHEABLE, BM_MEM_RAM);
678
679 init_pae_pagetables(&pae_page_tables);
680
681 /* Blocks are collected here when traversing CapsuleUpdateData*
682 variables, duplicates are skipped. */
683 struct block_descr blocks[MAX_CAPSULE_BLOCKS];
684 int block_count = discover_capsule_blocks(&rdev, blocks, ARRAY_SIZE(blocks));
685 if (block_count <= 0) {
686 if (block_count == 0)
687 printk(BIOS_INFO, "capsules: no UEFI capsules were discovered.\n");
688 goto exit;
689 }
690
691 printk(BIOS_INFO, "capsules: processing %d capsule block(s).\n", block_count);
692
693 /* Broken capsules are ignored, ignore those which didn't fit as well. */
694 if (block_count == ARRAY_SIZE(blocks)) {
695 printk(BIOS_WARNING,
696 "capsules: hit limit on capsule blocks, some might be ignored.\n");
697 }
698
699 /* Chaining is done to not pass around and update an array of pointers. */
700 uint64_t total_data_size;
701 struct block_descr block_chain =
702 verify_and_chain_blocks(blocks, block_count, &total_data_size);
703 if (block_chain.self == 0) {
704 printk(BIOS_ERR, "capsules: no valid capsules to process.\n");
705 goto exit;
706 }
707
708 printk(BIOS_DEBUG, "capsules: chained capsule blocks.\n");
709
710 /* Reserve all blocks and the data they point to to avoid checking for
711 overlaps when looking for a buffer. */
712 reserve_capsules(block_chain);
713
714 printk(BIOS_DEBUG, "capsules: reserved capsule blocks.\n");
715
716 /* Also reserve memory range for cbmem. Since it will still grow in
717 size by an unknown amount, try to account for that by reserving at
718 least 4 MiB more. */
719 void *cbmem_current;
720 size_t cbmem_size;
721 cbmem_get_region(&cbmem_current, &cbmem_size);
722 uintptr_t cbmem_future_base = ALIGN_DOWN((uintptr_t)cbmem_current - 4 * MiB, MiB);
723 memranges_insert(&memory_map,
724 cbmem_future_base,
725 (uintptr_t)cbmem_current + cbmem_size - cbmem_future_base,
726 BM_MEM_RESERVED);
727
728 coalesce_buffer = pick_buffer(total_data_size);
729 if (coalesce_buffer.base == 0) {
730 printk(BIOS_ERR,
731 "capsules: failed to find a buffer (%#llx bytes) for coalesced UEFI capsules.\n",
732 total_data_size);
733 } else {
734 printk(BIOS_DEBUG, "capsules: coalescing capsules data @ %#010x.\n",
735 coalesce_buffer.base);
736 coalesce_capsules(block_chain, (void *)(uintptr_t)coalesce_buffer.base);
737 }
738
739 exit:
740 paging_disable_pae();
741 memranges_teardown(&memory_map);
742 }
743
744 void lb_efi_capsules(struct lb_header *header)
745 {
746 int i;
747 for (i = 0; i < uefi_capsule_count; ++i) {
748 struct lb_range *capsule = (void *)lb_new_record(header);
749
750 printk(BIOS_INFO, "capsules: publishing a capsule @ %#010x.\n",
751 uefi_capsules[i].base);
752
753 capsule->tag = LB_TAG_CAPSULE;
754 capsule->size = sizeof(*capsule);
755 capsule->range_start = uefi_capsules[i].base;
756 capsule->range_size = uefi_capsules[i].len;
757 }
758 }
759
760 void efi_add_capsules_to_bootmem(void)
761 {
762 if (coalesce_buffer.len != 0) {
763 printk(BIOS_INFO, "capsules: reserving capsules data @ %#010x.\n",
764 coalesce_buffer.base);
765 bootmem_add_range(coalesce_buffer.base, coalesce_buffer.len, BM_MEM_RESERVED);
766 }
767 }
768
769 /*
770 * The code from this unit is typically executed by clear_memory() which is run
771 * after DEV_INIT. However, clear_memory() might not be compiled in in which
772 * case we still want to process capsules.
773 *
774 * State machine doesn't enforce any particular ordering for callbacks and
775 * running before DEV_INIT is too early due to MTTRs not being initialized.
776 * Hence invoking code is in two different places that should be mutually
777 * exclusive (can't set a "done" flag due to unknown ordering).
778 */
779 #if !CONFIG(PLATFORM_HAS_DRAM_CLEAR)
780
781 static void parse_capsules(void *unused)
782 {
783 if (!acpi_is_wakeup_s3())
784 efi_parse_capsules();
785 }
786
787 BOOT_STATE_INIT_ENTRY(BS_DEV_INIT, BS_ON_EXIT, parse_capsules, NULL);
788
789 #endif
790
791 static void enable_capsule_smi(void *unused)
792 {
793 uint32_t ret;
794
795 ret = call_smm(APM_CNT_SMMSTORE, SMMSTORE_CMD_USE_FULL_FLASH,
796 (void *)(uintptr_t)uefi_capsule_count);
797
798 printk(BIOS_INFO, "%sabled capsule update SMI handler\n",
799 ret == SMMSTORE_RET_SUCCESS ? "En" : "Dis");
800 }
801
802 BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_ENTRY, enable_capsule_smi, NULL);