| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <spi-generic.h> |
| 5 |
#include <spi_flash.h> |
| 6 |
#include <string.h> |
| 7 |
#include <timer.h> |
| 8 |
#include <types.h> |
| 9 |
|
| 10 |
#include "spi_flash_internal.h" |
| 11 |
|
| 12 |
void spi_flash_fill_rpmc_caps(struct spi_flash *flash) |
| 13 |
{ |
| 14 |
struct sfdp_rpmc_info rpmc_info; |
| 15 |
|
| 16 |
flash->rpmc_caps.rpmc_available = false; |
| 17 |
|
| 18 |
if (spi_flash_get_sfdp_rpmc(flash, &rpmc_info) != CB_SUCCESS) |
| 19 |
return; |
| 20 |
|
| 21 |
if (rpmc_info.monotonic_counter_size != SFDP_RPMC_COUNTER_BITS_32) { |
| 22 |
printk(BIOS_WARNING, "RPMC: unexpected counter size\n"); |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
flash->rpmc_caps.poll_op2_ext_stat = rpmc_info.busy_polling_method == |
| 27 |
SFDP_RPMC_POLL_OP2_EXTENDED_STATUS; |
| 28 |
flash->rpmc_caps.number_of_counters = rpmc_info.number_of_counters; |
| 29 |
flash->rpmc_caps.op1_write_cmd = rpmc_info.op1_write_command; |
| 30 |
flash->rpmc_caps.op2_read_cmd = rpmc_info.op2_read_command; |
| 31 |
flash->rpmc_caps.rpmc_available = true; |
| 32 |
} |
| 33 |
|
| 34 |
static bool spi_flash_rpmc_is_available(const struct spi_flash *flash) |
| 35 |
{ |
| 36 |
return flash->rpmc_caps.rpmc_available; |
| 37 |
} |
| 38 |
|
| 39 |
#define RPMC_OP2_READ_BUF_EXT_STATUS_OFFSET 0 |
| 40 |
#define RPMC_OP2_READ_BUF_TAG_OFFSET 1 |
| 41 |
#define RPMC_OP2_READ_BUF_COUNTER_DATA_OFFSET (1 + SPI_RPMC_TAG_LEN) |
| 42 |
#define RPMC_OP2_READ_BUF_SIGNATURE_OFFSET (1 + SPI_RPMC_TAG_LEN + SPI_RPMC_COUNTER_DATA_LEN) |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static enum cb_err spi_flash_rpmc_op2_read(const struct spi_flash *flash, |
| 51 |
uint8_t *extended_status, uint8_t *tag, |
| 52 |
uint8_t *counter_data, uint8_t *signature) |
| 53 |
{ |
| 54 |
int cmd_ret; |
| 55 |
const bool only_check_extended_status = extended_status != NULL && tag == NULL && |
| 56 |
counter_data == NULL && signature == NULL; |
| 57 |
|
| 58 |
|
| 59 |
uint8_t write_buf[1 + 1]; |
| 60 |
|
| 61 |
uint8_t read_buf[1 + SPI_RPMC_TAG_LEN + SPI_RPMC_COUNTER_DATA_LEN + SPI_RPMC_SIG_LEN]; |
| 62 |
|
| 63 |
write_buf[0] = flash->rpmc_caps.op2_read_cmd; |
| 64 |
write_buf[1] = 0x00; |
| 65 |
|
| 66 |
cmd_ret = spi_flash_cmd_multi(&flash->spi, write_buf, sizeof(write_buf), read_buf, |
| 67 |
only_check_extended_status ? 1 : sizeof(read_buf)); |
| 68 |
|
| 69 |
if (cmd_ret) |
| 70 |
return CB_ERR; |
| 71 |
|
| 72 |
if (extended_status != NULL) |
| 73 |
*extended_status = read_buf[RPMC_OP2_READ_BUF_EXT_STATUS_OFFSET]; |
| 74 |
|
| 75 |
if (tag != NULL) |
| 76 |
memcpy(tag, &read_buf[RPMC_OP2_READ_BUF_TAG_OFFSET], SPI_RPMC_TAG_LEN); |
| 77 |
|
| 78 |
if (counter_data != NULL) |
| 79 |
memcpy(counter_data, &read_buf[RPMC_OP2_READ_BUF_COUNTER_DATA_OFFSET], |
| 80 |
SPI_RPMC_COUNTER_DATA_LEN); |
| 81 |
|
| 82 |
if (signature != NULL) |
| 83 |
memcpy(signature, &read_buf[RPMC_OP2_READ_BUF_SIGNATURE_OFFSET], |
| 84 |
SPI_RPMC_SIG_LEN); |
| 85 |
|
| 86 |
return CB_SUCCESS; |
| 87 |
} |
| 88 |
|
| 89 |
static enum cb_err spi_flash_rpmc_op2_get_exteded_status(const struct spi_flash *flash, |
| 90 |
uint8_t *extended_status) |
| 91 |
{ |
| 92 |
return spi_flash_rpmc_op2_read(flash, extended_status, NULL, NULL, NULL); |
| 93 |
} |
| 94 |
|
| 95 |
#define RPMC_OP2_EXT_STATUS_SUCCESS BIT(7) |
| 96 |
#define RPMC_OP2_EXT_STATUS_ERR_FATAL BIT(5) |
| 97 |
#define RPMC_OP2_EXT_STATUS_ERR_BAD_COUNTER BIT(4) |
| 98 |
#define RPMC_OP2_EXT_STATUS_ERR_UNINITIALIZED BIT(3) |
| 99 |
#define RPMC_OP2_EXT_STATUS_ERR_INVALID BIT(2) |
| 100 |
#define RPMC_OP2_EXT_STATUS_ERR_OTHER BIT(1) |
| 101 |
#define RPMC_OP2_EXT_STATUS_POLL_BUSY BIT(0) |
| 102 |
|
| 103 |
static bool spi_flash_rpmc_is_extended_status_successful(uint8_t extended_status) |
| 104 |
{ |
| 105 |
return extended_status & RPMC_OP2_EXT_STATUS_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
static void spi_flash_rpmc_print_extended_status_error(uint8_t extended_status) |
| 109 |
{ |
| 110 |
if (extended_status & RPMC_OP2_EXT_STATUS_POLL_BUSY) |
| 111 |
printk(BIOS_WARNING, "SPI flash RPMC is busy\n"); |
| 112 |
|
| 113 |
if (extended_status & RPMC_OP2_EXT_STATUS_ERR_OTHER) |
| 114 |
printk(BIOS_ERR, "SPI flash RPMC other error\n"); |
| 115 |
|
| 116 |
if (extended_status & RPMC_OP2_EXT_STATUS_ERR_INVALID) |
| 117 |
printk(BIOS_ERR, "SPI flash RPMC invalid input\n"); |
| 118 |
|
| 119 |
if (extended_status & RPMC_OP2_EXT_STATUS_ERR_UNINITIALIZED) |
| 120 |
printk(BIOS_ERR, "SPI flash RPMC uninitialized\n"); |
| 121 |
|
| 122 |
if (extended_status & RPMC_OP2_EXT_STATUS_ERR_BAD_COUNTER) |
| 123 |
printk(BIOS_ERR, "SPI flash RPMC counter mismatch\n"); |
| 124 |
|
| 125 |
if (extended_status & RPMC_OP2_EXT_STATUS_ERR_FATAL) |
| 126 |
printk(BIOS_ERR, "SPI flash RPMC fatal error\n"); |
| 127 |
} |
| 128 |
|
| 129 |
static enum cb_err spi_flash_rpmc_check_extended_status(const struct spi_flash *flash) |
| 130 |
{ |
| 131 |
uint8_t extended_status; |
| 132 |
|
| 133 |
if (spi_flash_rpmc_op2_get_exteded_status(flash, &extended_status) != CB_SUCCESS) |
| 134 |
return CB_ERR; |
| 135 |
|
| 136 |
if (spi_flash_rpmc_is_extended_status_successful(extended_status)) |
| 137 |
return CB_SUCCESS; |
| 138 |
|
| 139 |
spi_flash_rpmc_print_extended_status_error(extended_status); |
| 140 |
|
| 141 |
return CB_ERR; |
| 142 |
} |
| 143 |
|
| 144 |
static enum cb_err spi_flash_rpmc_poll_op2(const struct spi_flash *flash, uint64_t timeout_ms) |
| 145 |
{ |
| 146 |
unsigned int attempt = 0; |
| 147 |
uint8_t extended_status; |
| 148 |
struct stopwatch sw; |
| 149 |
|
| 150 |
stopwatch_init_msecs_expire(&sw, timeout_ms); |
| 151 |
do { |
| 152 |
attempt++; |
| 153 |
|
| 154 |
if (spi_flash_rpmc_op2_get_exteded_status(flash, &extended_status)) { |
| 155 |
printk(BIOS_WARNING, |
| 156 |
"SPI command failed on attempt %d\n", attempt); |
| 157 |
return CB_ERR; |
| 158 |
} |
| 159 |
|
| 160 |
if ((extended_status & RPMC_OP2_EXT_STATUS_POLL_BUSY) == 0) |
| 161 |
return CB_SUCCESS; |
| 162 |
} while (!stopwatch_expired(&sw)); |
| 163 |
|
| 164 |
printk(BIOS_WARNING, "SPI RPMC poll timeout at %lld msec after %d attempts\n", |
| 165 |
stopwatch_duration_msecs(&sw), attempt); |
| 166 |
|
| 167 |
return CB_ERR; |
| 168 |
} |
| 169 |
|
| 170 |
static enum cb_err spi_flash_rpmc_wait_ready(const struct spi_flash *flash, |
| 171 |
uint64_t timeout_ms) |
| 172 |
{ |
| 173 |
if (!flash->rpmc_caps.poll_op2_ext_stat) { |
| 174 |
|
| 175 |
if (spi_flash_cmd_wait_ready(flash, timeout_ms)) |
| 176 |
return CB_ERR; |
| 177 |
} else { |
| 178 |
|
| 179 |
if (spi_flash_rpmc_poll_op2(flash, timeout_ms) != CB_SUCCESS) |
| 180 |
return CB_ERR; |
| 181 |
} |
| 182 |
|
| 183 |
return CB_SUCCESS; |
| 184 |
} |
| 185 |
|
| 186 |
static bool spi_flash_rpmc_is_valid_counter_addr(const struct spi_flash *flash, |
| 187 |
uint8_t counter_addr) |
| 188 |
{ |
| 189 |
return counter_addr < flash->rpmc_caps.number_of_counters; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
#define SPI_FLASH_RPMC_TIMEOUT_MS 200 |
| 194 |
|
| 195 |
#define RPMC_OP1_CMD_WRITE_ROOT_KEY_REGISTER 0x00 |
| 196 |
#define RPMC_OP1_CMD_UPDATE_HMAC_KEY_REGISTER 0x01 |
| 197 |
#define RPMC_OP1_CMD_INCREMENT_MONOTONIC_COUNTER 0x02 |
| 198 |
#define RPMC_OP1_CMD_REQUEST_MONOTONIC_COUNTER 0x03 |
| 199 |
|
| 200 |
enum cb_err spi_flash_rpmc_write_root_key(const struct spi_flash *flash, uint8_t counter_addr, |
| 201 |
const uint8_t *root_key, |
| 202 |
const uint8_t *truncated_signature) |
| 203 |
{ |
| 204 |
|
| 205 |
uint8_t buf[1 + 1 + 1 + 1 + SPI_RPMC_ROOT_KEY_LEN + SPI_RPMC_TRUNCTAED_SIG_LEN]; |
| 206 |
|
| 207 |
if (!spi_flash_rpmc_is_available(flash)) |
| 208 |
return CB_ERR; |
| 209 |
|
| 210 |
if (!spi_flash_rpmc_is_valid_counter_addr(flash, counter_addr)) |
| 211 |
return CB_ERR; |
| 212 |
|
| 213 |
buf[0] = flash->rpmc_caps.op1_write_cmd; |
| 214 |
buf[1] = RPMC_OP1_CMD_WRITE_ROOT_KEY_REGISTER; |
| 215 |
buf[2] = counter_addr; |
| 216 |
buf[3] = 0x00; |
| 217 |
memcpy(&buf[4], root_key, SPI_RPMC_ROOT_KEY_LEN); |
| 218 |
memcpy(&buf[4 + SPI_RPMC_ROOT_KEY_LEN], truncated_signature, |
| 219 |
SPI_RPMC_TRUNCTAED_SIG_LEN); |
| 220 |
|
| 221 |
if (spi_flash_cmd_multi(&flash->spi, buf, sizeof(buf), NULL, 0)) |
| 222 |
return CB_ERR; |
| 223 |
|
| 224 |
if (spi_flash_rpmc_wait_ready(flash, SPI_FLASH_RPMC_TIMEOUT_MS) != CB_SUCCESS) |
| 225 |
return CB_ERR; |
| 226 |
|
| 227 |
if (spi_flash_rpmc_check_extended_status(flash) != CB_SUCCESS) |
| 228 |
return CB_ERR; |
| 229 |
|
| 230 |
return CB_SUCCESS; |
| 231 |
} |
| 232 |
|
| 233 |
enum cb_err spi_flash_rpmc_update_hmac_key(const struct spi_flash *flash, uint8_t counter_addr, |
| 234 |
uint8_t *key_data, const uint8_t *signature) |
| 235 |
{ |
| 236 |
|
| 237 |
uint8_t buf[1 + 1 + 1 + 1 + SPI_RPMC_KEY_DATA_LEN + SPI_RPMC_SIG_LEN]; |
| 238 |
|
| 239 |
if (!spi_flash_rpmc_is_available(flash)) |
| 240 |
return CB_ERR; |
| 241 |
|
| 242 |
if (!spi_flash_rpmc_is_valid_counter_addr(flash, counter_addr)) |
| 243 |
return CB_ERR; |
| 244 |
|
| 245 |
buf[0] = flash->rpmc_caps.op1_write_cmd; |
| 246 |
buf[1] = RPMC_OP1_CMD_UPDATE_HMAC_KEY_REGISTER; |
| 247 |
buf[2] = counter_addr; |
| 248 |
buf[3] = 0x00; |
| 249 |
memcpy(&buf[4], key_data, SPI_RPMC_KEY_DATA_LEN); |
| 250 |
memcpy(&buf[4 + SPI_RPMC_KEY_DATA_LEN], signature, SPI_RPMC_SIG_LEN); |
| 251 |
|
| 252 |
if (spi_flash_cmd_multi(&flash->spi, buf, sizeof(buf), NULL, 0)) |
| 253 |
return CB_ERR; |
| 254 |
|
| 255 |
if (spi_flash_rpmc_wait_ready(flash, SPI_FLASH_RPMC_TIMEOUT_MS) != CB_SUCCESS) |
| 256 |
return CB_ERR; |
| 257 |
|
| 258 |
if (spi_flash_rpmc_check_extended_status(flash) != CB_SUCCESS) |
| 259 |
return CB_ERR; |
| 260 |
|
| 261 |
return CB_SUCCESS; |
| 262 |
} |
| 263 |
|
| 264 |
enum cb_err spi_flash_rpmc_increment(const struct spi_flash *flash, uint8_t counter_addr, |
| 265 |
const uint8_t *counter_data, const uint8_t *signature) |
| 266 |
{ |
| 267 |
|
| 268 |
uint8_t buf[1 + 1 + 1 + 1 + SPI_RPMC_COUNTER_DATA_LEN + SPI_RPMC_SIG_LEN]; |
| 269 |
|
| 270 |
if (!spi_flash_rpmc_is_available(flash)) |
| 271 |
return CB_ERR; |
| 272 |
|
| 273 |
if (!spi_flash_rpmc_is_valid_counter_addr(flash, counter_addr)) |
| 274 |
return CB_ERR; |
| 275 |
|
| 276 |
buf[0] = flash->rpmc_caps.op1_write_cmd; |
| 277 |
buf[1] = RPMC_OP1_CMD_INCREMENT_MONOTONIC_COUNTER; |
| 278 |
buf[2] = counter_addr; |
| 279 |
buf[3] = 0x00; |
| 280 |
memcpy(&buf[4], counter_data, SPI_RPMC_COUNTER_DATA_LEN); |
| 281 |
memcpy(&buf[4 + SPI_RPMC_COUNTER_DATA_LEN], signature, SPI_RPMC_SIG_LEN); |
| 282 |
|
| 283 |
if (spi_flash_cmd_multi(&flash->spi, buf, sizeof(buf), NULL, 0)) |
| 284 |
return CB_ERR; |
| 285 |
|
| 286 |
if (spi_flash_rpmc_wait_ready(flash, SPI_FLASH_RPMC_TIMEOUT_MS) != CB_SUCCESS) |
| 287 |
return CB_ERR; |
| 288 |
|
| 289 |
if (spi_flash_rpmc_check_extended_status(flash) != CB_SUCCESS) |
| 290 |
return CB_ERR; |
| 291 |
|
| 292 |
return CB_SUCCESS; |
| 293 |
} |
| 294 |
|
| 295 |
enum cb_err spi_flash_rpmc_request(const struct spi_flash *flash, uint8_t counter_addr, |
| 296 |
const uint8_t *tag, const uint8_t *signature, |
| 297 |
uint8_t *counter_data, uint8_t *signature_out) |
| 298 |
{ |
| 299 |
|
| 300 |
uint8_t buf[1 + 1 + 1 + 1 + SPI_RPMC_TAG_LEN + SPI_RPMC_SIG_LEN]; |
| 301 |
uint8_t extended_status; |
| 302 |
|
| 303 |
if (!spi_flash_rpmc_is_available(flash)) |
| 304 |
return CB_ERR; |
| 305 |
|
| 306 |
if (!spi_flash_rpmc_is_valid_counter_addr(flash, counter_addr)) |
| 307 |
return CB_ERR; |
| 308 |
|
| 309 |
buf[0] = flash->rpmc_caps.op1_write_cmd; |
| 310 |
buf[1] = RPMC_OP1_CMD_REQUEST_MONOTONIC_COUNTER; |
| 311 |
buf[2] = counter_addr; |
| 312 |
buf[3] = 0x00; |
| 313 |
memcpy(&buf[4], tag, SPI_RPMC_TAG_LEN); |
| 314 |
memcpy(&buf[4 + SPI_RPMC_TAG_LEN], signature, SPI_RPMC_SIG_LEN); |
| 315 |
|
| 316 |
if (spi_flash_cmd_multi(&flash->spi, buf, sizeof(buf), NULL, 0)) |
| 317 |
return CB_ERR; |
| 318 |
|
| 319 |
if (spi_flash_rpmc_wait_ready(flash, SPI_FLASH_RPMC_TIMEOUT_MS) != CB_SUCCESS) |
| 320 |
return CB_ERR; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
if (spi_flash_rpmc_op2_read(flash, &extended_status, NULL, counter_data, signature_out) |
| 328 |
!= CB_SUCCESS) |
| 329 |
return CB_ERR; |
| 330 |
|
| 331 |
if (!spi_flash_rpmc_is_extended_status_successful(extended_status)) { |
| 332 |
spi_flash_rpmc_print_extended_status_error(extended_status); |
| 333 |
return CB_ERR; |
| 334 |
} |
| 335 |
|
| 336 |
return CB_SUCCESS; |
| 337 |
} |