|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | + |
| 3 | +#include "system76_ec.h" |
| 4 | +#include <bootstate.h> |
| 5 | +#include <commonlib/region.h> |
| 6 | +#include <fmap.h> |
| 7 | +#include <spi_flash.h> |
| 8 | + |
| 9 | +static int protect_region_by_name(const char *name) |
| 10 | +{ |
| 11 | + int res; |
| 12 | + struct region region; |
| 13 | + |
| 14 | + res = fmap_locate_area(name, ®ion); |
| 15 | + if (res < 0) { |
| 16 | + printk(BIOS_ERR, "fmap_locate_area '%s' failed: %d\n", name, res); |
| 17 | + return res; |
| 18 | + } |
| 19 | + |
| 20 | + res = spi_flash_ctrlr_protect_region( |
| 21 | + boot_device_spi_flash(), |
| 22 | + ®ion, |
| 23 | + WRITE_PROTECT |
| 24 | + ); |
| 25 | + if (res < 0) { |
| 26 | + printk(BIOS_ERR, "spi_flash_ctrlr_protect_region '%s' failed: %d\n", name, res); |
| 27 | + return res; |
| 28 | + } |
| 29 | + |
| 30 | + printk(BIOS_INFO, "protected '%s'\n", name); |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +static void lock(void *unused) |
| 35 | +{ |
| 36 | + uint8_t state = SYSTEM76_EC_SECURITY_STATE_UNLOCK; |
| 37 | + if (!system76_ec_security_get(&state)) { |
| 38 | + printk(BIOS_INFO, "failed to get security state, assuming unlocked\n"); |
| 39 | + state = SYSTEM76_EC_SECURITY_STATE_UNLOCK; |
| 40 | + } |
| 41 | + |
| 42 | + printk(BIOS_INFO, "security state: %d\n", state); |
| 43 | + if (state != SYSTEM76_EC_SECURITY_STATE_UNLOCK) { |
| 44 | + // Protect WP_RO region, which should contain FMAP and COREBOOT |
| 45 | + protect_region_by_name("WP_RO"); |
| 46 | + // Protect RW_MRC_CACHE region, this must be done after it is written |
| 47 | + protect_region_by_name("RW_MRC_CACHE"); |
| 48 | + //TODO: protect entire flash except when in SMM? |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/* |
| 53 | + * Keep in sync with mrc_cache.c |
| 54 | + */ |
| 55 | +#if CONFIG(MRC_WRITE_NV_LATE) |
| 56 | +BOOT_STATE_INIT_ENTRY(BS_OS_RESUME_CHECK, BS_ON_EXIT, lock, NULL); |
| 57 | +#else |
| 58 | +BOOT_STATE_INIT_ENTRY(BS_DEV_RESOURCES, BS_ON_ENTRY, lock, NULL); |
| 59 | +#endif |
0 commit comments