| 1 |
|
| 2 |
|
| 3 |
#include <bootstate.h> |
| 4 |
#include <boot/coreboot_tables.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <types.h> |
| 7 |
#include <pc80/mc146818rtc.h> |
| 8 |
#include <security/vboot/vbnv.h> |
| 9 |
#include <security/vboot/vbnv_layout.h> |
| 10 |
|
| 11 |
static void clear_vbnv_battery_cutoff_flag(uint8_t *vbnv_copy) |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
if (vbnv_copy[MISC_FLAGS_OFFSET] & MISC_FLAGS_BATTERY_CUTOFF_MASK) { |
| 21 |
printk(BIOS_INFO, "VBNV: Remove battery cut-off request.\n"); |
| 22 |
vbnv_copy[MISC_FLAGS_OFFSET] &= ~MISC_FLAGS_BATTERY_CUTOFF_MASK; |
| 23 |
regen_vbnv_crc(vbnv_copy); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
static int restore_from_backup(uint8_t *vbnv_copy) |
| 29 |
{ |
| 30 |
if (!CONFIG(VBOOT_VBNV_CMOS_BACKUP_TO_FLASH)) |
| 31 |
return 0; |
| 32 |
|
| 33 |
printk(BIOS_INFO, "VBNV: CMOS invalid, restoring from flash\n"); |
| 34 |
read_vbnv_flash(vbnv_copy); |
| 35 |
|
| 36 |
if (verify_vbnv(vbnv_copy)) { |
| 37 |
clear_vbnv_battery_cutoff_flag(vbnv_copy); |
| 38 |
save_vbnv_cmos(vbnv_copy); |
| 39 |
printk(BIOS_INFO, "VBNV: Flash backup restored\n"); |
| 40 |
return 1; |
| 41 |
} |
| 42 |
|
| 43 |
printk(BIOS_INFO, "VBNV: Restore from flash failed\n"); |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
void read_vbnv_cmos(uint8_t *vbnv_copy) |
| 49 |
{ |
| 50 |
int i; |
| 51 |
|
| 52 |
for (i = 0; i < VBOOT_VBNV_BLOCK_SIZE; i++) |
| 53 |
vbnv_copy[i] = cmos_read(CONFIG_VBOOT_VBNV_OFFSET + 14 + i); |
| 54 |
|
| 55 |
|
| 56 |
if (verify_vbnv(vbnv_copy)) |
| 57 |
return; |
| 58 |
|
| 59 |
restore_from_backup(vbnv_copy); |
| 60 |
} |
| 61 |
|
| 62 |
void save_vbnv_cmos(const uint8_t *vbnv_copy) |
| 63 |
{ |
| 64 |
int i; |
| 65 |
|
| 66 |
for (i = 0; i < VBOOT_VBNV_BLOCK_SIZE; i++) |
| 67 |
cmos_write(vbnv_copy[i], CONFIG_VBOOT_VBNV_OFFSET + 14 + i); |
| 68 |
} |
| 69 |
|
| 70 |
void __weak vbnv_platform_init_cmos(void) |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
void vbnv_init_cmos(uint8_t *vbnv_copy) |
| 75 |
{ |
| 76 |
vbnv_platform_init_cmos(); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
if (!vbnv_cmos_failed()) |
| 81 |
return; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if (!restore_from_backup(vbnv_copy)) { |
| 86 |
vbnv_reset(vbnv_copy); |
| 87 |
|
| 88 |
vbnv_copy[HEADER_OFFSET] = HEADER_SIGNATURE | |
| 89 |
HEADER_FIRMWARE_SETTINGS_RESET | |
| 90 |
HEADER_KERNEL_SETTINGS_RESET; |
| 91 |
regen_vbnv_crc(vbnv_copy); |
| 92 |
save_vbnv_cmos(vbnv_copy); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void lb_table_add_vbnv_cmos(struct lb_header *header) |
| 97 |
{ |
| 98 |
struct lb_range *vbnv; |
| 99 |
|
| 100 |
vbnv = (struct lb_range *)lb_new_record(header); |
| 101 |
vbnv->tag = LB_TAG_VBNV; |
| 102 |
vbnv->size = sizeof(*vbnv); |
| 103 |
vbnv->range_start = CONFIG_VBOOT_VBNV_OFFSET + 14; |
| 104 |
vbnv->range_size = VBOOT_VBNV_BLOCK_SIZE; |
| 105 |
} |
| 106 |
|
| 107 |
#if CONFIG(VBOOT_VBNV_CMOS_BACKUP_TO_FLASH) |
| 108 |
static void back_up_vbnv_cmos(void *unused) |
| 109 |
{ |
| 110 |
uint8_t vbnv_cmos[VBOOT_VBNV_BLOCK_SIZE]; |
| 111 |
|
| 112 |
|
| 113 |
read_vbnv_cmos(vbnv_cmos); |
| 114 |
|
| 115 |
|
| 116 |
save_vbnv_flash(vbnv_cmos); |
| 117 |
} |
| 118 |
|
| 119 |
#if CONFIG(POSTPONE_SPI_ACCESS) |
| 120 |
BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, back_up_vbnv_cmos, NULL); |
| 121 |
#else |
| 122 |
BOOT_STATE_INIT_ENTRY(BS_POST_DEVICE, BS_ON_EXIT, back_up_vbnv_cmos, NULL); |
| 123 |
#endif |
| 124 |
#endif |