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 v3 version, modify img hash part 67 UPGRADE_FILE_VERSION_V4, // bin v4 version, modify bin file signature 68 }; 69 70 class UpgradeFileEntry : public PkgEntry { 71 public: UpgradeFileEntry(PkgFilePtr pkgFile,uint32_t nodeId)72 UpgradeFileEntry(PkgFilePtr pkgFile, uint32_t nodeId) : PkgEntry(pkgFile, nodeId) {} 73 ~UpgradeFileEntry()74 ~UpgradeFileEntry() override {} 75 76 int32_t Init(const PkgManager::FileInfoPtr fileInfo, PkgStreamPtr inStream) override; 77 78 int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override; 79 80 int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override; 81 82 int32_t DecodeHeader(PkgBuffer &buffer, size_t headerOffset, size_t dataOffset, 83 size_t &decodeLen) override; 84 85 int32_t Unpack(PkgStreamPtr outStream) override; 86 87 int32_t Verify(PkgBuffer &buffer, size_t len, size_t offset); 88 GetFileInfo()89 const FileInfo *GetFileInfo() const override 90 { 91 return &fileInfo_.fileInfo; 92 } 93 94 protected: 95 ComponentInfo fileInfo_ {}; 96 97 private: 98 int32_t GetUpGradeCompInfo(UpgradeCompInfo &comp); 99 }; 100 101 class UpgradePkgFile : public PkgFileImpl { 102 public: UpgradePkgFile(PkgManager::PkgManagerPtr manager,PkgStreamPtr stream,PkgInfoPtr header)103 UpgradePkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgInfoPtr header) 104 : PkgFileImpl(manager, stream, PkgFile::PKG_TYPE_UPGRADE) 105 { 106 if (header == nullptr || header->entryCount == 0) { 107 return; 108 } 109 UpgradePkgInfo *info = (UpgradePkgInfo *)(header); 110 pkgInfo_ = std::move(*info); 111 } 112 ~UpgradePkgFile()113 ~UpgradePkgFile() override 114 { 115 #ifndef DIFF_PATCH_SDK 116 if (hashCheck_ != nullptr) { 117 if (pkgInfo_.updateFileVersion >= UPGRADE_FILE_VERSION_V3) { 118 ReleaseImgHashDataNew(hashCheck_); 119 return; 120 } 121 ReleaseImgHashData(hashCheck_); 122 } 123 #endif 124 } 125 126 int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr inStream) override; 127 128 int32_t SavePackage(size_t &signOffset) override; 129 130 int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verify = nullptr) override; 131 132 size_t GetUpgradeSignatureLen() const; 133 134 size_t GetDigestLen() const; 135 GetPkgInfo()136 const PkgInfo *GetPkgInfo() const override 137 { 138 return &pkgInfo_.pkgInfo; 139 } 140 GetImgHashData()141 const ImgHashData *GetImgHashData() const 142 { 143 return hashCheck_; 144 } 145 GetPkgMgr()146 PkgManager::PkgManagerPtr GetPkgMgr() const 147 { 148 return pkgManager_; 149 } 150 GetUpgradeFileVer()151 int32_t GetUpgradeFileVer() const 152 { 153 return pkgInfo_.updateFileVersion; 154 } 155 156 private: 157 int16_t GetPackageTlvType(); 158 int32_t SaveEntry(const PkgBuffer &buffer, size_t &parsedLen, UpgradeParam &info, 159 DigestAlgorithm::DigestAlgorithmPtr algorithm, std::vector<std::string> &fileNames); 160 int32_t ReadComponents(size_t &parsedLen, 161 DigestAlgorithm::DigestAlgorithmPtr algorithm, std::vector<std::string> &fileNames); 162 163 void ParsePkgHeaderToTlv(const PkgBuffer &buffer, size_t &currLen, PkgTlv &tlv); 164 int32_t ReadUpgradePkgHeader(size_t &realLen, 165 DigestAlgorithm::DigestAlgorithmPtr &algorithm); 166 167 int32_t Verify(size_t start, DigestAlgorithm::DigestAlgorithmPtr algorithm, 168 VerifyFunction verifier, const std::vector<uint8_t> &signData); 169 int32_t WriteBuffer(std::vector<uint8_t> &buffer, size_t &offset, size_t &signOffset); 170 int32_t CheckPackageHeader(std::vector<uint8_t> &buffer, size_t &offset); 171 int32_t GetEntryOffset(size_t &dataOffset, const PkgManager::FileInfoPtr file); 172 int32_t ReadPackageInfo(std::vector<uint8_t> &signData, 173 size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm); 174 int32_t ReadReserveData(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr &algorithm); 175 int32_t ReadImgHashTLV(std::vector<uint8_t> &imgHashBuf, size_t &parsedLen, 176 DigestAlgorithm::DigestAlgorithmPtr algorithm, uint32_t needType); 177 int32_t ReadImgHashData(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm); 178 int32_t ReadSignData(std::vector<uint8_t> &signData, 179 size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm); 180 int32_t VerifyHeader(DigestAlgorithm::DigestAlgorithmPtr algorithm, VerifyFunction verifier, 181 const std::vector<uint8_t> &signData); 182 int32_t VerifyFile(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm, 183 VerifyFunction verifier); 184 int32_t VerifyFileV1(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm, 185 VerifyFunction verifier); 186 int32_t VerifyFileV2(size_t &parsedLen, DigestAlgorithm::DigestAlgorithmPtr algorithm, 187 VerifyFunction verifier); 188 189 private: 190 UpgradePkgInfo pkgInfo_ {}; 191 size_t packedFileSize_ {0}; 192 const ImgHashData *hashCheck_ = nullptr; 193 }; 194 } // namespace Hpackage 195 #endif 196