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