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