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 "pkg_algorithm.h" 20 #include "pkg_manager.h" 21 #include "pkg_pkgfile.h" 22 #include "pkg_utils.h" 23 24 namespace Hpackage { 25 struct __attribute__((packed)) PkgTlv { 26 uint16_t type; 27 uint16_t length; 28 }; 29 30 struct __attribute__((packed)) UpgradePkgHeader { 31 uint32_t pkgInfoLength; // UpgradePkgTime + UpgradeCompInfo + UPGRADE_RESERVE_LEN 32 uint32_t updateFileVersion; 33 uint8_t productUpdateId[64]; 34 uint8_t softwareVersion[64]; 35 }; 36 37 struct __attribute__((packed)) UpgradePkgTime { 38 uint8_t date[16]; 39 uint8_t time[16]; 40 }; 41 42 struct __attribute__((packed)) UpgradeCompInfo { 43 uint8_t address[32]; // L1 16 44 uint16_t id; 45 uint8_t resType; 46 uint8_t flags; 47 uint8_t type; 48 uint8_t version[10]; 49 uint32_t size; 50 uint32_t originalSize; 51 uint8_t digest[DIGEST_MAX_LEN]; 52 }; 53 54 class UpgradeFileEntry : public PkgEntry { 55 public: UpgradeFileEntry(PkgFilePtr pkgFile,uint32_t nodeId)56 UpgradeFileEntry(PkgFilePtr pkgFile, uint32_t nodeId) : PkgEntry(pkgFile, nodeId) {} 57 ~UpgradeFileEntry()58 ~UpgradeFileEntry() override {} 59 60 int32_t Init(const PkgManager::FileInfoPtr fileInfo, PkgStreamPtr inStream) override; 61 62 int32_t EncodeHeader(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override; 63 64 int32_t Pack(PkgStreamPtr inStream, size_t startOffset, size_t &encodeLen) override; 65 66 int32_t DecodeHeader(const PkgBuffer &buffer, size_t offset, size_t dataOffset, 67 size_t &decodeLen) override; 68 69 int32_t Unpack(PkgStreamPtr outStream) override; 70 GetFileInfo()71 const FileInfo *GetFileInfo() const override 72 { 73 return &fileInfo_.fileInfo; 74 } 75 76 private: 77 ComponentInfo fileInfo_ {}; 78 }; 79 80 class UpgradePkgFile : public PkgFile { 81 public: UpgradePkgFile(PkgManager::PkgManagerPtr manager,PkgStreamPtr stream,PkgManager::PkgInfoPtr header)82 UpgradePkgFile(PkgManager::PkgManagerPtr manager, PkgStreamPtr stream, PkgManager::PkgInfoPtr header) 83 : PkgFile(manager, stream, PkgFile::PKG_TYPE_UPGRADE) 84 { 85 if (header == nullptr || header->entryCount == 0) { 86 return; 87 } 88 UpgradePkgInfo *info = reinterpret_cast<UpgradePkgInfo *>(header); 89 pkgInfo_ = std::move(*info); 90 } 91 ~UpgradePkgFile()92 ~UpgradePkgFile() override {} 93 94 int32_t AddEntry(const PkgManager::FileInfoPtr file, const PkgStreamPtr inStream) override; 95 96 int32_t SavePackage(size_t &signOffset) override; 97 98 int32_t LoadPackage(std::vector<std::string> &fileNames, VerifyFunction verify = nullptr) override; 99 100 size_t GetUpgradeSignatureLen() const; 101 102 size_t GetDigestLen() const; 103 GetPkgInfo()104 const PkgInfo *GetPkgInfo() const override 105 { 106 return &pkgInfo_.pkgInfo; 107 } 108 109 private: 110 int16_t GetPackageTlvType(); 111 int32_t ReadComponents(const PkgBuffer &buffer, size_t &parsedLen, 112 DigestAlgorithm::DigestAlgorithmPtr algorithm, std::vector<std::string> &fileNames); 113 114 int32_t ReadUpgradePkgHeader(const PkgBuffer &buffer, size_t &realLen, 115 DigestAlgorithm::DigestAlgorithmPtr &algorithm); 116 117 int32_t Verify(size_t start, DigestAlgorithm::DigestAlgorithmPtr algorithm, 118 VerifyFunction verifier, const std::vector<uint8_t> &signData); 119 120 private: 121 UpgradePkgInfo pkgInfo_ {}; 122 size_t packedFileSize_ {0}; 123 }; 124 } // namespace Hpackage 125 #endif 126