From 271bb8c30baa07e97b5dd08b1f1fd587e571ddcd Mon Sep 17 00:00:00 2001 From: Vadim Bendebury Date: Mon, 20 Sep 2021 21:26:52 -0700 Subject: usb_spi: add API for reading arbitrary AP flash locations This API will provide support to the AP RO verification implementation. The size of data read in one transaction is limited by SPI_HASH_CHUNK size. BUG=b:199904580, b:200736744 TEST=tested along with AP RO verification implementation. Signed-off-by: Vadim Bendebury Change-Id: Id4da2add2ce1202d979627dde40325b583004fc5 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3172254 Reviewed-by: Andrey Pronin --- board/cr50/usb_spi.c | 20 ++++++++++++++++++++ board/cr50/usb_spi_board.h | 11 +++++++++++ 2 files changed, 31 insertions(+) (limited to 'board') diff --git a/board/cr50/usb_spi.c b/board/cr50/usb_spi.c index 6876272297..4aede92f19 100644 --- a/board/cr50/usb_spi.c +++ b/board/cr50/usb_spi.c @@ -683,6 +683,26 @@ int usb_spi_sha256_start(struct sha256_ctx *ctx) return EC_SUCCESS; } +int usb_spi_read_buffer(void *buf, unsigned int offset, size_t bytes) +{ + uint8_t *p = buf; + + while (bytes) { + const int this_chunk = MIN(bytes, SPI_HASH_CHUNK_SIZE); + + /* Read the data */ + if (spi_read_chunk(p, offset, this_chunk) != EC_SUCCESS) { + CPRINTS("%s: read error at 0x%x", __func__, offset); + return VENDOR_RC_READ_FLASH_FAIL; + } + + bytes -= this_chunk; + offset += this_chunk; + p += this_chunk; + } + return EC_SUCCESS; +} + int usb_spi_sha256_update(struct sha256_ctx *ctx, uint32_t offset, uint32_t size) { diff --git a/board/cr50/usb_spi_board.h b/board/cr50/usb_spi_board.h index 1c40f0814d..7549b98f7f 100644 --- a/board/cr50/usb_spi_board.h +++ b/board/cr50/usb_spi_board.h @@ -9,3 +9,14 @@ int usb_spi_sha256_update(struct sha256_ctx *ctx, uint32_t offset, uint32_t size); void usb_spi_sha256_final(struct sha256_ctx *ctx, void *digest, size_t digest_size); + +/** + * Returns the content of SPI flash + * + * @param buf Buffer to write flash contents + * @param offset Flash offset to start reading from + * @param bytes Number of bytes to read. + * + * @return EC_SUCCESS, or non-zero if any error. + */ +int usb_spi_read_buffer(void *buf, unsigned int offset, size_t bytes); -- cgit v1.2.1