1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci#ifndef UPGRADE_PKG_FILE_H 16fb299fa2Sopenharmony_ci#define UPGRADE_PKG_FILE_H 17fb299fa2Sopenharmony_ci 18fb299fa2Sopenharmony_ci#include <map> 19fb299fa2Sopenharmony_ci#include "rust/image_hash_check.h" 20fb299fa2Sopenharmony_ci#include "pkg_algorithm.h" 21fb299fa2Sopenharmony_ci#include "pkg_manager.h" 22fb299fa2Sopenharmony_ci#include "pkg_info_utils.h" 23fb299fa2Sopenharmony_ci#include "pkg_pkgfile.h" 24fb299fa2Sopenharmony_ci#include "pkg_utils.h" 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_cinamespace Hpackage { 27fb299fa2Sopenharmony_cistruct __attribute__((packed)) PkgTlv { 28fb299fa2Sopenharmony_ci uint16_t type; 29fb299fa2Sopenharmony_ci uint16_t length; 30fb299fa2Sopenharmony_ci}; 31fb299fa2Sopenharmony_ci 32fb299fa2Sopenharmony_cistruct __attribute__((packed)) UpgradePkgHeader { 33fb299fa2Sopenharmony_ci uint32_t pkgInfoLength; // UpgradePkgTime + UpgradeCompInfo + UPGRADE_RESERVE_LEN 34fb299fa2Sopenharmony_ci uint32_t updateFileVersion; 35fb299fa2Sopenharmony_ci uint8_t productUpdateId[64]; 36fb299fa2Sopenharmony_ci uint8_t softwareVersion[64]; 37fb299fa2Sopenharmony_ci}; 38fb299fa2Sopenharmony_ci 39fb299fa2Sopenharmony_cistruct __attribute__((packed)) UpgradePkgTime { 40fb299fa2Sopenharmony_ci uint8_t date[16]; 41fb299fa2Sopenharmony_ci uint8_t time[16]; 42fb299fa2Sopenharmony_ci}; 43fb299fa2Sopenharmony_ci 44fb299fa2Sopenharmony_cistruct __attribute__((packed)) UpgradeCompInfo { 45fb299fa2Sopenharmony_ci uint8_t address[32]; // L1 16 46fb299fa2Sopenharmony_ci uint16_t id; 47fb299fa2Sopenharmony_ci uint8_t resType; 48fb299fa2Sopenharmony_ci uint8_t flags; 49fb299fa2Sopenharmony_ci uint8_t type; 50fb299fa2Sopenharmony_ci uint8_t version[10]; 51fb299fa2Sopenharmony_ci uint32_t size; 52fb299fa2Sopenharmony_ci uint32_t originalSize; 53fb299fa2Sopenharmony_ci uint8_t digest[DIGEST_MAX_LEN]; 54fb299fa2Sopenharmony_ci}; 55fb299fa2Sopenharmony_ci 56fb299fa2Sopenharmony_cistruct __attribute__((packed)) UpgradeParam { 57fb299fa2Sopenharmony_ci size_t readLen {}; 58fb299fa2Sopenharmony_ci size_t dataOffset {}; 59fb299fa2Sopenharmony_ci size_t srcOffset {}; 60fb299fa2Sopenharmony_ci size_t currLen {}; 61fb299fa2Sopenharmony_ci}; 62fb299fa2Sopenharmony_ci 63fb299fa2Sopenharmony_cienum { 64fb299fa2Sopenharmony_ci UPGRADE_FILE_VERSION_V1 = 1, // bin v1 version 65fb299fa2Sopenharmony_ci UPGRADE_FILE_VERSION_V2, // bin v2 version, add img hash part 66fb299fa2Sopenharmony_ci UPGRADE_FILE_VERSION_V3, // bin v3 version, modify img hash part 67fb299fa2Sopenharmony_ci UPGRADE_FILE_VERSION_V4, // bin v4 version, modify bin file signature 68fb299fa2Sopenharmony_ci}; 69fb299fa2Sopenharmony_ci 70fb299fa2Sopenharmony_ciclass UpgradeFileEntry : public PkgEntry { 71fb299fa2Sopenharmony_cipublic: 72fb299fa2Sopenharmony_ci UpgradeFileEntry(PkgFilePtr pkgFile, uint32_t nodeId) : PkgEntry(pkgFile, nodeId) {} 73fb299fa2Sopenharmony_ci 74fb299fa2Sopenharmony_ci ~UpgradeFileEntry() override {} 75fb299fa2Sopenharmony_ci 76fb299fa2Sopenharmony_ci int32_t Init(const PkgManager::FileInfoPtr fileInfo, PkgStreamPtr inStream) override; 77fb299fa2Sopenharmony_ci 78fb299fa2Sopenharmony_ci int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override; 79fb299fa2Sopenharmony_ci 80fb299fa2Sopenharmony_ci int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override; 81fb299fa2Sopenharmony_ci 82fb299fa2Sopenharmony_ci int32_t DecodeHeader(PkgBuffer &buffer, size_t headerOffset, size_t dataOffset, 83fb299fa2Sopenharmony_ci size_t &decodeLen) override; 84fb299fa2Sopenharmony_ci 85fb299fa2Sopenharmony_ci int32_t Unpack(PkgStreamPtr outStream) override; 86fb299fa2Sopenharmony_ci 87fb299fa2Sopenharmony_ci int32_t Verify(PkgBuffer &buffer, size_t len, size_t offset); 88fb299fa2Sopenharmony_ci 89fb299fa2Sopenharmony_ci const FileInfo *GetFileInfo() const override 90fb299fa2Sopenharmony_ci { 91fb299fa2Sopenharmony_ci return &fileInfo_.fileInfo; 92fb299fa2Sopenharmony_ci } 93fb299fa2Sopenharmony_ci 94fb299fa2Sopenharmony_ciprotected: 95fb299fa2Sopenharmony_ci ComponentInfo fileInfo_ {}; 96fb299fa2Sopenharmony_ci 97fb299fa2Sopenharmony_ciprivate: 98fb299fa2Sopenharmony_ci int32_t GetUpGradeCompInfo(UpgradeCompInfo &comp); 99fb299fa2Sopenharmony_ci}; 100fb299fa2Sopenharmony_ci 101fb299fa2Sopenharmony_ciclass UpgradePkgFile : public PkgFileImpl { 102fb299fa2Sopenharmony_cipublic: 103fb299fa2Sopenharmony_ci UpgradePkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgInfoPtr header) 104fb299fa2Sopenharmony_ci : PkgFileImpl(manager, stream, PkgFile::PKG_TYPE_UPGRADE) 105fb299fa2Sopenharmony_ci { 106fb299fa2Sopenharmony_ci if (header == nullptr || header->entryCount == 0) { 107fb299fa2Sopenharmony_ci return; 108fb299fa2Sopenharmony_ci } 109fb299fa2Sopenharmony_ci UpgradePkgInfo *info = (UpgradePkgInfo *)(header); 110fb299fa2Sopenharmony_ci pkgInfo_ = std::move(*info); 111fb299fa2Sopenharmony_ci } 112fb299fa2Sopenharmony_ci 113fb299fa2Sopenharmony_ci ~UpgradePkgFile() override 114fb299fa2Sopenharmony_ci { 115fb299fa2Sopenharmony_ci#ifndef DIFF_PATCH_SDK 116fb299fa2Sopenharmony_ci if (hashCheck_ != nullptr) { 117fb299fa2Sopenharmony_ci if (pkgInfo_.updateFileVersion >= UPGRADE_FILE_VERSION_V3) { 118fb299fa2Sopenharmony_ci ReleaseImgHashDataNew(hashCheck_); 119fb299fa2Sopenharmony_ci return; 120fb299fa2Sopenharmony_ci } 121fb299fa2Sopenharmony_ci ReleaseImgHashData(hashCheck_); 122fb299fa2Sopenharmony_ci } 123fb299fa2Sopenharmony_ci#endif 124fb299fa2Sopenharmony_ci } 125fb299fa2Sopenharmony_ci 126fb299fa2Sopenharmony_ci int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr inStream) override; 127fb299fa2Sopenharmony_ci 128fb299fa2Sopenharmony_ci int32_t SavePackage(size_t &signOffset) override; 129fb299fa2Sopenharmony_ci 130fb299fa2Sopenharmony_ci int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verify = nullptr) override; 131fb299fa2Sopenharmony_ci 132fb299fa2Sopenharmony_ci size_t GetUpgradeSignatureLen() const; 133fb299fa2Sopenharmony_ci 134fb299fa2Sopenharmony_ci size_t GetDigestLen() const; 135fb299fa2Sopenharmony_ci 136fb299fa2Sopenharmony_ci const PkgInfo *GetPkgInfo() const override 137fb299fa2Sopenharmony_ci { 138fb299fa2Sopenharmony_ci return &pkgInfo_.pkgInfo; 139fb299fa2Sopenharmony_ci } 140fb299fa2Sopenharmony_ci 141fb299fa2Sopenharmony_ci const ImgHashData *GetImgHashData() const 142fb299fa2Sopenharmony_ci { 143fb299fa2Sopenharmony_ci return hashCheck_; 144fb299fa2Sopenharmony_ci } 145fb299fa2Sopenharmony_ci 146fb299fa2Sopenharmony_ci PkgManager::PkgManagerPtr GetPkgMgr() const 147fb299fa2Sopenharmony_ci { 148fb299fa2Sopenharmony_ci return pkgManager_; 149fb299fa2Sopenharmony_ci } 150fb299fa2Sopenharmony_ci 151fb299fa2Sopenharmony_ci int32_t GetUpgradeFileVer() const 152fb299fa2Sopenharmony_ci { 153fb299fa2Sopenharmony_ci return pkgInfo_.updateFileVersion; 154fb299fa2Sopenharmony_ci } 155fb299fa2Sopenharmony_ci 156fb299fa2Sopenharmony_ciprivate: 157fb299fa2Sopenharmony_ci int16_t GetPackageTlvType(); 158fb299fa2Sopenharmony_ci int32_t SaveEntry(const PkgBuffer &buffer, size_t &parsedLen, UpgradeParam &info, 159fb299fa2Sopenharmony_ci DigestAlgorithm::DigestAlgorithmPtr algorithm, std::vector<std::string> &fileNames); 160fb299fa2Sopenharmony_ci int32_t ReadComponents(size_t &parsedLen, 161fb299fa2Sopenharmony_ci DigestAlgorithm::DigestAlgorithmPtr algorithm, std::vector<std::string> &fileNames); 162fb299fa2Sopenharmony_ci 163fb299fa2Sopenharmony_ci void ParsePkgHeaderToTlv(const PkgBuffer &buffer, size_t &currLen, PkgTlv &tlv); 164fb299fa2Sopenharmony_ci int32_t ReadUpgradePkgHeader(size_t &realLen, 165fb299fa2Sopenharmony_ci DigestAlgorithm::DigestAlgorithmPtr &algorithm); 166fb299fa2Sopenharmony_ci 167fb299fa2Sopenharmony_ci int32_t Verify(size_t start, DigestAlgorithm::DigestAlgorithmPtr algorithm, 168fb299fa2Sopenharmony_ci VerifyFunction verifier, const std::vector<uint8_t> &signData); 169fb299fa2Sopenharmony_ci int32_t WriteBuffer(std::vector<uint8_t> &buffer, size_t &offset, size_t &signOffset); 170fb299fa2Sopenharmony_ci int32_t CheckPackageHeader(std::vector<uint8_t> &buffer, size_t &offset); 171fb299fa2Sopenharmony_ci int32_t GetEntryOffset(size_t &dataOffset, const PkgManager::FileInfoPtr file); 172fb299fa2Sopenharmony_ci int32_t ReadPackageInfo(std::vector<uint8_t> &signData, 173fb299fa2Sopenharmony_ci size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm); 174fb299fa2Sopenharmony_ci int32_t ReadReserveData(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr &algorithm); 175fb299fa2Sopenharmony_ci int32_t ReadImgHashTLV(std::vector<uint8_t> &imgHashBuf, size_t &parsedLen, 176fb299fa2Sopenharmony_ci DigestAlgorithm::DigestAlgorithmPtr algorithm, uint32_t needType); 177fb299fa2Sopenharmony_ci int32_t ReadImgHashData(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm); 178fb299fa2Sopenharmony_ci int32_t ReadSignData(std::vector<uint8_t> &signData, 179fb299fa2Sopenharmony_ci size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm); 180fb299fa2Sopenharmony_ci int32_t VerifyHeader(DigestAlgorithm::DigestAlgorithmPtr algorithm, VerifyFunction verifier, 181fb299fa2Sopenharmony_ci const std::vector<uint8_t> &signData); 182fb299fa2Sopenharmony_ci int32_t VerifyFile(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm, 183fb299fa2Sopenharmony_ci VerifyFunction verifier); 184fb299fa2Sopenharmony_ci int32_t VerifyFileV1(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm, 185fb299fa2Sopenharmony_ci VerifyFunction verifier); 186fb299fa2Sopenharmony_ci int32_t VerifyFileV2(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm, 187fb299fa2Sopenharmony_ci VerifyFunction verifier); 188fb299fa2Sopenharmony_ci 189fb299fa2Sopenharmony_ciprivate: 190fb299fa2Sopenharmony_ci UpgradePkgInfo pkgInfo_ {}; 191fb299fa2Sopenharmony_ci size_t packedFileSize_ {0}; 192fb299fa2Sopenharmony_ci const ImgHashData *hashCheck_ = nullptr; 193fb299fa2Sopenharmony_ci}; 194fb299fa2Sopenharmony_ci} // namespace Hpackage 195fb299fa2Sopenharmony_ci#endif 196