/* * Copyright (c) 2023 Fancy Code. * 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 "UpgradeBase.h" #include "ILog.h" #include "LinuxApi.h" #include #include #include StatusCode UpgradeBase::CheckUpgradeFile(const char *fileName, UpgradeFileHeader &header) { FILE *file = nullptr; LogInfo("Checking file:%s\n", fileName); file = fopen(fileName, "rb"); if (file == nullptr) { LogError("Error opening file:%s\n", fileName); return CreateStatusCode(STATUS_CODE_NOT_OK); } size_t bytesRead = fread(&header, sizeof(UpgradeFileHeader), 1, file); if (bytesRead != 1) { LogError("Error reading file header\n"); fclose(file); return CreateStatusCode(STATUS_CODE_NOT_OK); } PrintfHeader(header); return CheckFileHeader(header); } StatusCode UpgradeBase::MoveUpgradeFile(const char *sourceFile, const char *targetFile) { FILE *inputFile = nullptr; FILE *outputFile = nullptr; constexpr int BUFF_SIZE = 1024 * 200; UpgradeFileHeader fileHeader; const size_t headerSize = sizeof(UpgradeFileHeader); unsigned char buffer[BUFF_SIZE]; size_t bytesRead; inputFile = fopen(sourceFile, "rb"); if (inputFile == nullptr) { LogError("Error opening input file\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } outputFile = fopen(targetFile, "wb"); if (outputFile == nullptr) { LogError("Error opening output file\n"); fclose(inputFile); return CreateStatusCode(STATUS_CODE_NOT_OK); } bytesRead = fread(&fileHeader, 1, headerSize, inputFile); if (bytesRead != headerSize) { LogError("Error reading file header\n"); fclose(inputFile); fclose(outputFile); return CreateStatusCode(STATUS_CODE_NOT_OK); } while ((bytesRead = fread(buffer, 1, sizeof(buffer), inputFile)) > 0) { fwrite(buffer, 1, bytesRead, outputFile); if (ferror(inputFile) || ferror(outputFile)) { LogError("Error reading or writing file\n"); fclose(inputFile); fclose(outputFile); return CreateStatusCode(STATUS_CODE_NOT_OK); } } fclose(inputFile); fclose(outputFile); LogInfo("File processed successfully.\n"); return CreateStatusCode(STATUS_CODE_OK); } void UpgradeBase::PrintfHeader(const UpgradeFileHeader &header) { printf("=====================================\n"); printf("packTime:"); for (long unsigned int i = 0; i < sizeof(header.packTime); ++i) { printf("0x%02x ", header.packTime[i]); } printf("\n"); printf("version:"); for (long unsigned int i = 0; i < sizeof(header.version); ++i) { printf("0x%02x ", header.version[i]); } printf("\n"); printf("product:"); for (long unsigned int i = 0; i < sizeof(header.product); ++i) { printf("0x%02x ", header.product[i]); } printf("\n"); printf("upgradeType:"); for (long unsigned int i = 0; i < sizeof(header.upgradeType); ++i) { printf("0x%02x ", header.upgradeType[i]); } printf("\n"); printf("=====================================\n"); }