| 1 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#define MAX_CAPSULES 32 |
| 50 |
|
| 51 |
|
| 52 |
#define CAPSULE_ALIGNMENT 8 |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
struct block_descr { |
| 66 |
|
| 67 |
uint64_t self; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
uint64_t len; |
| 79 |
uint64_t addr; |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 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 |
|
| 98 |
struct memranges memory_map; |
| 99 |
|
| 100 |
|
| 101 |
static char pae_page_tables[20 * KiB] __aligned(4 * KiB); |
| 102 |
|
| 103 |
|
| 104 |
struct memory_range coalesce_buffer; |
| 105 |
|
| 106 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 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 |
|
| 136 |
|
| 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 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 189 |
block->self = block->self + sizeof(EFI_CAPSULE_BLOCK_DESCRIPTOR); |
| 190 |
} else { |
| 191 |
|
| 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 |
|
| 311 |
|
| 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 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 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) { |
| 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 |
|
| 361 |
|
| 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 |
|
| 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 |
|
| 385 |
|
| 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 |
|
| 396 |
if (*total_data_size + data_size < *total_data_size) { |
| 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 |
|
| 411 |
|
| 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 |
|
| 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 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 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 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 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 |
|
| 476 |
struct block_descr head = {0}; |
| 477 |
|
| 478 |
|
| 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 |
|
| 487 |
|
| 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 |
|
| 508 |
|
| 509 |
static void reserve_capsules(struct block_descr block_chain) |
| 510 |
{ |
| 511 |
struct block_descr block = block_chain; |
| 512 |
|
| 513 |
|
| 514 |
struct block_descr seq_start = {0}; |
| 515 |
|
| 516 |
|
| 517 |
|
| 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 |
|
| 526 |
memranges_insert(&memory_map, block.addr, block.len, BM_MEM_RESERVED); |
| 527 |
} else { |
| 528 |
|
| 529 |
|
| 530 |
|
| 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 |
|
| 538 |
seq_start.self = 0; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 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 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static struct memory_range pick_buffer(uint64_t total_data_size) |
| 561 |
{ |
| 562 |
struct memory_range buffer = {0}; |
| 563 |
|
| 564 |
|
| 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 |
|
| 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 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
buffer.base = base + size - total_data_size; |
| 588 |
buffer.len = total_data_size; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
return buffer; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 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 |
|
| 604 |
|
| 605 |
load_block(&block); |
| 606 |
for (; !is_final_block(&block); advance_block(&block), load_block(&block)) { |
| 607 |
|
| 608 |
if (!is_data_block(&block)) |
| 609 |
continue; |
| 610 |
|
| 611 |
|
| 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 |
|
| 637 |
if (size_left == 0) { |
| 638 |
|
| 639 |
|
| 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 |
|
| 652 |
|
| 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 |
|
| 665 |
|
| 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 |
|
| 682 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 711 |
|
| 712 |
reserve_capsules(block_chain); |
| 713 |
|
| 714 |
printk(BIOS_DEBUG, "capsules: reserved capsule blocks.\n"); |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 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 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 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); |