/* * * Copyright (c) 2021 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "OTAImageProcessorImpl.h" #include "mw320_ota.h" #include namespace chip { static fw_update_id_t fwupid; CHIP_ERROR OTAImageProcessorImpl::PrepareDownload() { // mw320++ /* if (mImageFile == nullptr) { ChipLogError(SoftwareUpdate, "Invalid output image file supplied"); return CHIP_ERROR_INTERNAL; } */ // mw320-- DeviceLayer::PlatformMgr().ScheduleWork(HandlePrepareDownload, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::Finalize() { DeviceLayer::PlatformMgr().ScheduleWork(HandleFinalize, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::Apply() { // DeviceLayer::PlatformMgr().ScheduleWork(HandleApply, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::Abort() { // mw320++ /* if (mImageFile == nullptr) { ChipLogError(SoftwareUpdate, "Invalid output image file supplied"); return CHIP_ERROR_INTERNAL; } */ // mw320-- DeviceLayer::PlatformMgr().ScheduleWork(HandleAbort, reinterpret_cast(this)); return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & block) { // mw320++ /* if (!mOfs.is_open() || !mOfs.good()) { return CHIP_ERROR_INTERNAL; } */ ChipLogProgress(SoftwareUpdate, "==> %s()", __FUNCTION__); // mw320-- if ((block.data() == nullptr) || block.empty()) { return CHIP_ERROR_INVALID_ARGUMENT; } // Store block data for HandleProcessBlock to access CHIP_ERROR err = SetBlock(block); if (err != CHIP_NO_ERROR) { ChipLogError(SoftwareUpdate, "Cannot set block data: %" CHIP_ERROR_FORMAT, err.Format()); } DeviceLayer::PlatformMgr().ScheduleWork(HandleProcessBlock, reinterpret_cast(this)); return CHIP_NO_ERROR; } bool OTAImageProcessorImpl::IsFirstImageRun() { OTARequestorInterface * requestor = chip::GetRequestorInstance(); if (requestor == nullptr) { return false; } return requestor->GetCurrentUpdateState() == OTARequestorInterface::OTAUpdateStateEnum::kApplying; } CHIP_ERROR OTAImageProcessorImpl::ConfirmCurrentImage() { OTARequestorInterface * requestor = chip::GetRequestorInstance(); if (requestor == nullptr) { return CHIP_ERROR_INTERNAL; } uint32_t currentVersion; uint32_t targetVersion = requestor->GetTargetVersion(); ReturnErrorOnFailure(DeviceLayer::ConfigurationMgr().GetSoftwareVersion(currentVersion)); if (currentVersion != targetVersion) { ChipLogError(SoftwareUpdate, "Current software version = %" PRIu32 ", expected software version = %" PRIu32, currentVersion, targetVersion); return CHIP_ERROR_INCORRECT_STATE; } return CHIP_NO_ERROR; } void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); if (imageProcessor == nullptr) { ChipLogError(SoftwareUpdate, "ImageProcessor context is null"); return; } else if (imageProcessor->mDownloader == nullptr) { ChipLogError(SoftwareUpdate, "mDownloader is null"); return; } // mw320++ // No need to open file for mw320 imageProcessor->mHeaderParser.Init(); /* unlink(imageProcessor->mImageFile); imageProcessor->mHeaderParser.Init(); imageProcessor->mOfs.open(imageProcessor->mImageFile, std::ofstream::out | std::ofstream::ate | std::ofstream::app); if (!imageProcessor->mOfs.good()) { imageProcessor->mDownloader->OnPreparedForDownload(CHIP_ERROR_OPEN_FAILED); return; } */ ChipLogProgress(SoftwareUpdate, "==> %s(), [open the file]", __FUNCTION__); fwupid = mw320_fw_update_begin(); // mw320-- // TODO: if file already exists and is not empty, erase previous contents imageProcessor->mDownloader->OnPreparedForDownload(CHIP_NO_ERROR); } void OTAImageProcessorImpl::HandleFinalize(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); if (imageProcessor == nullptr) { return; } // mw320++ // imageProcessor->mOfs.close(); ChipLogProgress(SoftwareUpdate, "==> %s(), [close the file]", __FUNCTION__); mw320_fw_update_end(fwupid, -1); // mw320-- imageProcessor->ReleaseBlock(); ChipLogProgress(SoftwareUpdate, "OTA image downloaded to %s", imageProcessor->mImageFile); } #if 0 void OTAImageProcessorImpl::HandleApply(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); VerifyOrReturn(imageProcessor != nullptr); OTARequestorInterface * requestor = chip::GetRequestorInstance(); VerifyOrReturn(requestor != nullptr); // Move the downloaded image to the location where the new image is to be executed from unlink(kImageExecPath); rename(imageProcessor->mImageFile, kImageExecPath); chmod(kImageExecPath, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Shutdown the stack and expect to boot into the new image once the event loop is stopped DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { DeviceLayer::PlatformMgr().HandleServerShuttingDown(); }); DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { DeviceLayer::PlatformMgr().StopEventLoopTask(); }); } #endif // 0 void OTAImageProcessorImpl::HandleAbort(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); if (imageProcessor == nullptr) { return; } // mw320++ // imageProcessor->mOfs.close(); ChipLogProgress(SoftwareUpdate, "==> %s(), [close the file]", __FUNCTION__); mw320_fw_update_end(fwupid, -1); // mw320-- // unlink(imageProcessor->mImageFile); imageProcessor->ReleaseBlock(); } void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context) { auto * imageProcessor = reinterpret_cast(context); if (imageProcessor == nullptr) { ChipLogError(SoftwareUpdate, "ImageProcessor context is null"); return; } else if (imageProcessor->mDownloader == nullptr) { ChipLogError(SoftwareUpdate, "mDownloader is null"); return; } // TODO: Process block header if any // mw320++ // /* ByteSpan block = imageProcessor->mBlock; CHIP_ERROR error = imageProcessor->ProcessHeader(block); if (error != CHIP_NO_ERROR) { ChipLogError(SoftwareUpdate, "Image does not contain a valid header"); imageProcessor->mDownloader->EndDownload(CHIP_ERROR_INVALID_FILE_IDENTIFIER); return; } /* if (!imageProcessor->mOfs.write(reinterpret_cast(block.data()), static_cast(block.size()))) { imageProcessor->mDownloader->EndDownload(CHIP_ERROR_WRITE_FAILED); return; } */ { int error_wtblk; // uint32_t blksize = static_cast(imageProcessor->mBlock.size()); uint32_t blksize = static_cast(block.size()); ChipLogProgress(SoftwareUpdate, "==> %s(), [write the block, %lu]", __FUNCTION__, blksize); error_wtblk = mw320_fw_update_wrblock(fwupid, const_cast(reinterpret_cast(block.data())), static_cast(block.size())); if (error_wtblk != 0) { ChipLogProgress(SoftwareUpdate, "==> %s(), failed to write", __FUNCTION__); } imageProcessor->mParams.downloadedBytes += block.size(); // imageProcessor->mParams.downloadedBytes += blksize; } // mw320-- imageProcessor->mDownloader->FetchNextData(); } CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & block) { if (mHeaderParser.IsInitialized()) { OTAImageHeader header; CHIP_ERROR error = mHeaderParser.AccumulateAndDecode(block, header); // Needs more data to decode the header VerifyOrReturnError(error != CHIP_ERROR_BUFFER_TOO_SMALL, CHIP_NO_ERROR); ReturnErrorOnFailure(error); // mParams.totalFileBytes = header.mPayloadSize; mHeaderParser.Clear(); } return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::SetBlock(ByteSpan & block) { if (block.empty()) { ReleaseBlock(); return CHIP_NO_ERROR; } if (mBlock.size() < block.size()) { if (!mBlock.empty()) { ReleaseBlock(); } uint8_t * mBlock_ptr = static_cast(chip::Platform::MemoryAlloc(block.size())); if (mBlock_ptr == nullptr) { return CHIP_ERROR_NO_MEMORY; } mBlock = MutableByteSpan(mBlock_ptr, block.size()); } CHIP_ERROR err = CopySpanToMutableSpan(block, mBlock); if (err != CHIP_NO_ERROR) { ChipLogError(SoftwareUpdate, "Cannot copy block data: %" CHIP_ERROR_FORMAT, err.Format()); return err; } return CHIP_NO_ERROR; } CHIP_ERROR OTAImageProcessorImpl::ReleaseBlock() { if (mBlock.data() != nullptr) { chip::Platform::MemoryFree(mBlock.data()); } mBlock = MutableByteSpan(); return CHIP_NO_ERROR; } } // namespace chip