Aaron Durbin | 49b2383 | 2016-01-22 14:42:54 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2016 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <assert.h> |
| 17 | #include <console/console.h> |
| 18 | #include <ec/google/chromeec/ec.h> |
| 19 | #include <vendorcode/google/chromeos/chromeos.h> |
| 20 | |
| 21 | #define VBOOT_HASH_VSLOT 0 |
| 22 | #define VBOOT_HASH_VSLOT_MASK (1 << (VBOOT_HASH_VSLOT)) |
| 23 | |
| 24 | int vboot_save_hash(void *digest, size_t digest_size) |
| 25 | { |
| 26 | const int slot = VBOOT_HASH_VSLOT; |
| 27 | uint32_t lock_status; |
| 28 | int num_slots; |
| 29 | |
| 30 | /* Ensure the digests being saved match the EC's slot size. */ |
| 31 | assert(digest_size == EC_VSTORE_SLOT_SIZE); |
| 32 | |
| 33 | if (google_chromeec_vstore_write(slot, digest, digest_size)) |
| 34 | return -1; |
| 35 | |
| 36 | /* Assert the slot is locked on successful write. */ |
| 37 | num_slots = google_chromeec_vstore_info(&lock_status); |
| 38 | |
| 39 | /* Normalize to be 0 based. If num_slots returned 0 then it'll be -1. */ |
| 40 | num_slots--; |
| 41 | |
| 42 | if (num_slots < slot) { |
| 43 | printk(BIOS_ERR, "Not enough vstore slots for vboot hash: %d\n", |
| 44 | num_slots + 1); |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | if ((lock_status & VBOOT_HASH_VSLOT_MASK) == 0) { |
| 49 | printk(BIOS_ERR, "Vstore slot not locked after write.\n"); |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | int vboot_retrieve_hash(void *digest, size_t digest_size) |
| 57 | { |
| 58 | /* Ensure the digests being saved match the EC's slot size. */ |
| 59 | assert(digest_size == EC_VSTORE_SLOT_SIZE); |
| 60 | |
| 61 | return google_chromeec_vstore_read(VBOOT_HASH_VSLOT, digest); |
| 62 | } |