|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (c) 2023 Project CHIP Authors |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <platform/internal/CHIPDeviceLayerInternal.h> |
| 20 | +#include <platform/silabs/multi-ota/OTAMultiImageProcessorImpl.h> |
| 21 | +#include <platform/silabs/multi-ota/efr32/OTAFirmwareProcessor.h> |
| 22 | + |
| 23 | +#include <app/clusters/ota-requestor/OTARequestorInterface.h> |
| 24 | +#include "wfx_host_events.h" |
| 25 | +#include <platform/silabs/SilabsConfig.h> |
| 26 | +#ifdef __cplusplus |
| 27 | +extern "C" { |
| 28 | +#endif |
| 29 | +#include "sl_si91x_driver.h" |
| 30 | +#ifdef SLI_SI91X_MCU_INTERFACE |
| 31 | +#include "sl_si91x_hal_soc_soft_reset.h" |
| 32 | +#endif |
| 33 | +#ifdef __cplusplus |
| 34 | +} |
| 35 | +#endif |
| 36 | + |
| 37 | +#define RPS_HEADER 1 |
| 38 | +#define RPS_DATA 2 |
| 39 | + |
| 40 | +#define SL_STATUS_FW_UPDATE_DONE SL_STATUS_SI91X_NO_AP_FOUND |
| 41 | +uint8_t flag = RPS_HEADER; |
| 42 | + |
| 43 | +// TODO: more descriptive error codes |
| 44 | +#define SL_OTA_ERROR 1L |
| 45 | + |
| 46 | +namespace chip { |
| 47 | + |
| 48 | +// Define static memebers |
| 49 | +uint8_t OTAFirmwareProcessor::mReset = false; |
| 50 | +uint32_t OTAFirmwareProcessor::mWriteOffset = 0; |
| 51 | +uint16_t OTAFirmwareProcessor::writeBufOffset = 0; |
| 52 | +uint8_t OTAFirmwareProcessor::writeBuffer[kAlignmentBytes] __attribute__((aligned(4))) = { 0 }; |
| 53 | + |
| 54 | +CHIP_ERROR OTAFirmwareProcessor::Init() |
| 55 | +{ |
| 56 | + ReturnErrorCodeIf(mCallbackProcessDescriptor == nullptr, CHIP_OTA_PROCESSOR_CB_NOT_REGISTERED); |
| 57 | + mAccumulator.Init(sizeof(Descriptor)); |
| 58 | +#if OTA_ENCRYPTION_ENABLE |
| 59 | + mUnalignmentNum = 0; |
| 60 | +#endif |
| 61 | + |
| 62 | + return CHIP_NO_ERROR; |
| 63 | +} |
| 64 | + |
| 65 | +CHIP_ERROR OTAFirmwareProcessor::Clear() |
| 66 | +{ |
| 67 | + OTATlvProcessor::ClearInternal(); |
| 68 | + mAccumulator.Clear(); |
| 69 | + mDescriptorProcessed = false; |
| 70 | +#if OTA_ENCRYPTION_ENABLE |
| 71 | + mUnalignmentNum = 0; |
| 72 | +#endif |
| 73 | + |
| 74 | + return CHIP_NO_ERROR; |
| 75 | +} |
| 76 | + |
| 77 | +CHIP_ERROR OTAFirmwareProcessor::ProcessInternal(ByteSpan & block) |
| 78 | +{ |
| 79 | + int32_t status = SL_STATUS_OK; |
| 80 | + if (!mDescriptorProcessed) |
| 81 | + { |
| 82 | + ReturnErrorOnFailure(ProcessDescriptor(block)); |
| 83 | + } |
| 84 | + |
| 85 | + uint32_t blockReadOffset = 0; |
| 86 | + while (blockReadOffset < block.size()) |
| 87 | + { |
| 88 | + writeBuffer[writeBufOffset] = *((block.data()) + blockReadOffset); |
| 89 | + writeBufOffset++; |
| 90 | + blockReadOffset++; |
| 91 | + if (writeBufOffset == kAlignmentBytes) |
| 92 | + { |
| 93 | + writeBufOffset = 0; |
| 94 | + |
| 95 | + if (flag == RPS_HEADER) |
| 96 | + { |
| 97 | + // Send RPS header which is received as first chunk |
| 98 | + status = sl_si91x_fwup_start(writeBuffer); |
| 99 | + status = sl_si91x_fwup_load(writeBuffer, kAlignmentBytes); |
| 100 | + flag = RPS_DATA; |
| 101 | + } |
| 102 | + else if (flag == RPS_DATA) |
| 103 | + { |
| 104 | + // Send RPS content |
| 105 | + status = sl_si91x_fwup_load(writeBuffer, kAlignmentBytes); |
| 106 | + if (status != SL_STATUS_OK) |
| 107 | + { |
| 108 | + // If the last chunk of last block-writeBufOffset length is exactly kAlignmentBytes(64) bytes then mReset value |
| 109 | + // should be set to true in HandleProcessBlock |
| 110 | + if (status == SL_STATUS_FW_UPDATE_DONE) |
| 111 | + { |
| 112 | + mReset = true; |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + ChipLogError(SoftwareUpdate, "ERROR: In HandleProcessBlock sl_si91x_fwup_load() error %ld", status); |
| 117 | + imageProcessor->mDownloader->EndDownload(CHIP_ERROR_WRITE_FAILED); |
| 118 | + return; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + if (err) |
| 123 | + { |
| 124 | + ChipLogError(SoftwareUpdate, "bootloader_eraseWriteStorage() error: %ld", err); |
| 125 | + // TODO: add this somewhere |
| 126 | + // imageProcessor->mDownloader->EndDownload(CHIP_ERROR_WRITE_FAILED); |
| 127 | + // TODO: Replace CHIP_ERROR_CANCELLED with new error statement |
| 128 | + return CHIP_ERROR_CANCELLED; |
| 129 | + } |
| 130 | + mWriteOffset += kAlignmentBytes; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return CHIP_NO_ERROR; |
| 135 | +} |
| 136 | + |
| 137 | +CHIP_ERROR OTAFirmwareProcessor::ProcessDescriptor(ByteSpan & block) |
| 138 | +{ |
| 139 | + ReturnErrorOnFailure(mAccumulator.Accumulate(block)); |
| 140 | + ReturnErrorOnFailure(mCallbackProcessDescriptor(static_cast<void *>(mAccumulator.data()))); |
| 141 | + |
| 142 | + mDescriptorProcessed = true; |
| 143 | + mAccumulator.Clear(); |
| 144 | + |
| 145 | + return CHIP_NO_ERROR; |
| 146 | +} |
| 147 | + |
| 148 | +CHIP_ERROR OTAFirmwareProcessor::ApplyAction() |
| 149 | +{ |
| 150 | + uint32_t err = SL_BOOTLOADER_OK; |
| 151 | + if (err != SL_BOOTLOADER_OK) |
| 152 | + { |
| 153 | + ChipLogError(SoftwareUpdate, "bootloader_verifyImage() error: %ld", err); |
| 154 | + // Call the OTARequestor API to reset the state |
| 155 | + GetRequestorInstance()->CancelImageUpdate(); |
| 156 | + |
| 157 | + return SL_GENERIC_OTA_ERROR; |
| 158 | + } |
| 159 | + |
| 160 | + CORE_CRITICAL_SECTION(err = bootloader_setImageToBootload(mSlotId);) |
| 161 | + if (err != SL_BOOTLOADER_OK) |
| 162 | + { |
| 163 | + ChipLogError(SoftwareUpdate, "bootloader_setImageToBootload() error: %ld", err); |
| 164 | + // Call the OTARequestor API to reset the state |
| 165 | + GetRequestorInstance()->CancelImageUpdate(); |
| 166 | + return SL_GENERIC_OTA_ERROR; |
| 167 | + } |
| 168 | + |
| 169 | + return CHIP_NO_ERROR; |
| 170 | +} |
| 171 | + |
| 172 | +CHIP_ERROR OTAFirmwareProcessor::FinalizeAction() |
| 173 | +{ |
| 174 | + int32_t status = SL_STATUS_OK; |
| 175 | + |
| 176 | + // Pad the remainder of the write buffer with zeros and write it to bootloader storage |
| 177 | + if (writeBufOffset != 0) |
| 178 | + { |
| 179 | + |
| 180 | + while (writeBufOffset != kAlignmentBytes) |
| 181 | + { |
| 182 | + writeBuffer[writeBufOffset] = 0; |
| 183 | + writeBufOffset++; |
| 184 | + } |
| 185 | + status = sl_si91x_fwup_load(writeBuffer, writeBufOffset); |
| 186 | + ChipLogProgress(SoftwareUpdate, "status: 0x%lX", status); |
| 187 | + |
| 188 | + if (status != SL_STATUS_OK) |
| 189 | + { |
| 190 | + if (status == SL_STATUS_FW_UPDATE_DONE) |
| 191 | + { |
| 192 | + mReset = true; |
| 193 | + } |
| 194 | + else |
| 195 | + { |
| 196 | + ChipLogError(SoftwareUpdate, "ERROR: In HandleFinalize for last chunk sl_si91x_fwup_load() error %ld", status); |
| 197 | + imageProcessor->mDownloader->EndDownload(CHIP_ERROR_WRITE_FAILED); |
| 198 | + return; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + } |
| 203 | + |
| 204 | + return err ? CHIP_ERROR_WRITE_FAILED : CHIP_NO_ERROR; |
| 205 | +} |
| 206 | + |
| 207 | +} // namespace chip |
0 commit comments