Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 4 | * Copyright (C) 2014 Google Inc. |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 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. |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <stddef.h> |
Lee Leahy | ff76709 | 2016-04-12 13:01:02 -0700 | [diff] [blame] | 18 | #include <bootmode.h> |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 19 | #include <console/console.h> |
| 20 | #include <string.h> |
| 21 | #include <spi-generic.h> |
| 22 | #include <spi_flash.h> |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 23 | #include <soc/spi.h> |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 24 | #include <vendorcode/google/chromeos/chromeos.h> |
Duncan Laurie | d8c4f2b | 2014-04-22 10:46:06 -0700 | [diff] [blame] | 25 | #include "nvm.h" |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 26 | |
| 27 | /* This module assumes the flash is memory mapped just below 4GiB in the |
| 28 | * address space for reading. Also this module assumes an area it erased |
| 29 | * when all bytes read as all 0xff's. */ |
| 30 | |
| 31 | static struct spi_flash *flash; |
| 32 | |
| 33 | static int nvm_init(void) |
| 34 | { |
| 35 | if (flash != NULL) |
| 36 | return 0; |
| 37 | |
| 38 | spi_init(); |
Gabe Black | 1e18735 | 2014-03-27 20:37:03 -0700 | [diff] [blame] | 39 | flash = spi_flash_probe(0, 0); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 40 | if (!flash) { |
| 41 | printk(BIOS_DEBUG, "Could not find SPI device\n"); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Alexandru Gagniuc | 9220641 | 2016-03-03 10:58:30 -0800 | [diff] [blame] | 48 | /* |
| 49 | * Convert memory mapped pointer to flash offset. |
| 50 | * |
| 51 | * This is weak because not every platforms memory-maps the NVM media in the |
| 52 | * same manner. This is a stop-gap solution. |
| 53 | * |
| 54 | * The root of the problem is that users of this API work in memory space for |
| 55 | * both reads and writes, but erase and write must be done in flash space. This |
| 56 | * also only works when the media is memory-mapped, which is no longer |
| 57 | * universally true. The long-term approach should be to rewrite this and its |
| 58 | * users to work in flash space, while using rdev_read() instead of rdev_mmap(). |
| 59 | */ |
| 60 | __attribute__((weak)) |
| 61 | uint32_t nvm_mmio_to_flash_offset(void *p) |
Kyösti Mälkki | 9b29aad | 2015-01-06 07:08:46 +0100 | [diff] [blame] | 62 | { |
| 63 | return CONFIG_ROM_SIZE + (uintptr_t)p; |
| 64 | } |
| 65 | |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 66 | int nvm_is_erased(const void *start, size_t size) |
| 67 | { |
Aaron Durbin | b013fff | 2014-01-13 11:39:04 -0600 | [diff] [blame] | 68 | const uint8_t *cur = start; |
| 69 | const uint8_t erased_value = 0xff; |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 70 | |
| 71 | while (size > 0) { |
Aaron Durbin | b013fff | 2014-01-13 11:39:04 -0600 | [diff] [blame] | 72 | if (*cur != erased_value) |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 73 | return 0; |
| 74 | cur++; |
| 75 | size--; |
| 76 | } |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | int nvm_erase(void *start, size_t size) |
| 81 | { |
| 82 | if (nvm_init() < 0) |
| 83 | return -1; |
Alexandru Gagniuc | 9220641 | 2016-03-03 10:58:30 -0800 | [diff] [blame] | 84 | return flash->erase(flash, nvm_mmio_to_flash_offset(start), size); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Write data to NVM. Returns 0 on success < 0 on error. */ |
| 88 | int nvm_write(void *start, const void *data, size_t size) |
| 89 | { |
| 90 | if (nvm_init() < 0) |
| 91 | return -1; |
Alexandru Gagniuc | 9220641 | 2016-03-03 10:58:30 -0800 | [diff] [blame] | 92 | return flash->write(flash, nvm_mmio_to_flash_offset(start), size, data); |
Aaron Durbin | 9a7d7bc | 2013-09-07 00:41:48 -0500 | [diff] [blame] | 93 | } |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 94 | |
| 95 | /* Read flash status register to determine if write protect is active */ |
| 96 | int nvm_is_write_protected(void) |
| 97 | { |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 98 | if (nvm_init() < 0) |
| 99 | return -1; |
| 100 | |
Lee Leahy | ff76709 | 2016-04-12 13:01:02 -0700 | [diff] [blame] | 101 | if (IS_ENABLED(CONFIG_CHROMEOS)) { |
| 102 | u8 sr1; |
| 103 | u8 wp_gpio; |
| 104 | u8 wp_spi; |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 105 | |
Lee Leahy | ff76709 | 2016-04-12 13:01:02 -0700 | [diff] [blame] | 106 | /* Read Write Protect GPIO if available */ |
| 107 | wp_gpio = get_write_protect_state(); |
| 108 | |
| 109 | /* Read Status Register 1 */ |
| 110 | if (flash->status(flash, &sr1) < 0) { |
| 111 | printk(BIOS_ERR, |
| 112 | "Failed to read SPI status register 1\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | wp_spi = !!(sr1 & 0x80); |
| 116 | |
| 117 | printk(BIOS_DEBUG, "SPI flash protection: WPSW=%d SRP0=%d\n", |
| 118 | wp_gpio, wp_spi); |
| 119 | |
| 120 | return wp_gpio && wp_spi; |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 121 | } |
Lee Leahy | ff76709 | 2016-04-12 13:01:02 -0700 | [diff] [blame] | 122 | return 0; |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /* Apply protection to a range of flash */ |
| 126 | int nvm_protect(void *start, size_t size) |
| 127 | { |
| 128 | #if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT) |
| 129 | if (nvm_init() < 0) |
| 130 | return -1; |
Alexandru Gagniuc | 9220641 | 2016-03-03 10:58:30 -0800 | [diff] [blame] | 131 | return spi_flash_protect(nvm_mmio_to_flash_offset(start), size); |
Duncan Laurie | a32b6b9 | 2015-01-15 15:49:07 -0800 | [diff] [blame] | 132 | #else |
| 133 | return -1; |
| 134 | #endif |
| 135 | } |