Andrew Geissler | 2daf84b | 2023-03-31 09:57:23 -0500 | [diff] [blame] | 1 | From 25924b6c0504faae0b0ed680c09fb8996b6aaba6 Mon Sep 17 00:00:00 2001 |
| 2 | From: Mohamed Omar Asaker <mohamed.omarasaker@arm.com> |
| 3 | Date: Wed, 21 Dec 2022 15:42:21 +0000 |
| 4 | Subject: [PATCH 5/10] Platform:corstone1000:fwu: metadata_read validate crc |
| 5 | |
| 6 | Add validation logic to metadata_read function. |
| 7 | Also, add metadata_read_without_validation |
| 8 | |
| 9 | Signed-off-by: Mohamed Omar Asaker <mohamed.omarasaker@arm.com> |
| 10 | Upstream-Status: Pending [Not submitted to upstream yet] |
| 11 | --- |
| 12 | .../target/arm/corstone1000/CMakeLists.txt | 1 + |
| 13 | .../corstone1000/fw_update_agent/fwu_agent.c | 51 +++++++++++++++++++ |
| 14 | 2 files changed, 52 insertions(+) |
| 15 | |
| 16 | diff --git a/platform/ext/target/arm/corstone1000/CMakeLists.txt b/platform/ext/target/arm/corstone1000/CMakeLists.txt |
| 17 | index 554fc51b21..9db2864033 100644 |
| 18 | --- a/platform/ext/target/arm/corstone1000/CMakeLists.txt |
| 19 | +++ b/platform/ext/target/arm/corstone1000/CMakeLists.txt |
| 20 | @@ -75,6 +75,7 @@ target_sources(platform_s |
| 21 | fw_update_agent/uefi_capsule_parser.c |
| 22 | fw_update_agent/fwu_agent.c |
| 23 | fw_update_agent/uefi_fmp.c |
| 24 | + soft_crc/soft_crc.c |
| 25 | $<$<NOT:$<BOOL:${PLATFORM_DEFAULT_OTP}>>:${PLATFORM_DIR}/ext/accelerator/cc312/otp_cc312.c> |
| 26 | ) |
| 27 | |
| 28 | diff --git a/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c b/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c |
| 29 | index 1a42c72bd5..eb17c3a377 100644 |
| 30 | --- a/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c |
| 31 | +++ b/platform/ext/target/arm/corstone1000/fw_update_agent/fwu_agent.c |
| 32 | @@ -193,6 +193,53 @@ static enum fwu_agent_error_t private_metadata_write( |
| 33 | return FWU_AGENT_SUCCESS; |
| 34 | } |
| 35 | |
| 36 | +static enum fwu_agent_error_t metadata_validate(struct fwu_metadata *p_metadata) |
| 37 | +{ |
| 38 | + int ret; |
| 39 | + |
| 40 | + FWU_LOG_MSG("%s: enter:\n\r", __func__); |
| 41 | + |
| 42 | + if (!p_metadata) { |
| 43 | + return FWU_AGENT_ERROR; |
| 44 | + } |
| 45 | + |
| 46 | + uint32_t calculated_crc32 = crc32((uint8_t *)&(p_metadata->version), |
| 47 | + sizeof(struct fwu_metadata) - sizeof(uint32_t)); |
| 48 | + |
| 49 | + if (p_metadata->crc_32 != calculated_crc32) { |
| 50 | + FWU_LOG_MSG("%s: failed: crc32 calculated: 0x%x, given: 0x%x\n\r", __func__, |
| 51 | + calculated_crc32, p_metadata->crc_32); |
| 52 | + return FWU_AGENT_ERROR; |
| 53 | + } |
| 54 | + |
| 55 | + FWU_LOG_MSG("%s: success\n\r", __func__); |
| 56 | + |
| 57 | + return FWU_AGENT_SUCCESS; |
| 58 | +} |
| 59 | + |
| 60 | +static enum fwu_agent_error_t metadata_read_without_validation(struct fwu_metadata *p_metadata) |
| 61 | +{ |
| 62 | + int ret; |
| 63 | + |
| 64 | + FWU_LOG_MSG("%s: enter: flash addr = %u, size = %d\n\r", __func__, |
| 65 | + FWU_METADATA_REPLICA_1_OFFSET, sizeof(struct fwu_metadata)); |
| 66 | + |
| 67 | + if (!p_metadata) { |
| 68 | + return FWU_AGENT_ERROR; |
| 69 | + } |
| 70 | + |
| 71 | + ret = FWU_METADATA_FLASH_DEV.ReadData(FWU_METADATA_REPLICA_1_OFFSET, |
| 72 | + p_metadata, sizeof(struct fwu_metadata)); |
| 73 | + if (ret < 0 || ret != sizeof(struct fwu_metadata)) { |
| 74 | + return FWU_AGENT_ERROR; |
| 75 | + } |
| 76 | + |
| 77 | + FWU_LOG_MSG("%s: success: active = %u, previous = %d\n\r", __func__, |
| 78 | + p_metadata->active_index, p_metadata->previous_active_index); |
| 79 | + |
| 80 | + return FWU_AGENT_SUCCESS; |
| 81 | +} |
| 82 | + |
| 83 | static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata) |
| 84 | { |
| 85 | int ret; |
| 86 | @@ -210,6 +257,10 @@ static enum fwu_agent_error_t metadata_read(struct fwu_metadata *p_metadata) |
| 87 | return FWU_AGENT_ERROR; |
| 88 | } |
| 89 | |
| 90 | + if (metadata_validate(p_metadata) != FWU_AGENT_SUCCESS) { |
| 91 | + return FWU_AGENT_ERROR; |
| 92 | + } |
| 93 | + |
| 94 | FWU_LOG_MSG("%s: success: active = %u, previous = %d\n\r", __func__, |
| 95 | p_metadata->active_index, p_metadata->previous_active_index); |
| 96 | |
| 97 | -- |
| 98 | 2.25.1 |
| 99 | |