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 UPDATE_PROCESSOR_H 16 #define UPDATE_PROCESSOR_H 17 18 #include <cstdio> 19 #include <string> 20 #include <sys/wait.h> 21 #include <unistd.h> 22 #include "applypatch/data_writer.h" 23 #include "pkg_manager.h" 24 #include "script_instruction.h" 25 #include "script_manager.h" 26 27 using Uscript::UScriptEnv; 28 using Uscript::UScriptInstructionFactory; 29 using Uscript::UScriptInstructionFactoryPtr; 30 using Uscript::UScriptInstructionPtr; 31 32 namespace Updater { 33 enum EXIT_CODES { 34 EXIT_INVALID_ARGS = EXIT_SUCCESS + 1, 35 EXIT_READ_PACKAGE_ERROR = 2, 36 EXIT_FOUND_SCRIPT_ERROR = 3, 37 EXIT_PARSE_SCRIPT_ERROR = 4, 38 EXIT_EXEC_SCRIPT_ERROR = 5, 39 EXIT_VERIFY_SCRIPT_ERROR = 6, 40 }; 41 42 int ProcessUpdater(bool retry, int pipeFd, const std::string &packagePath, const std::string &keyPath); 43 void GetPartitionPathFromName(const std::string &partitionName, std::string &partitionPath); 44 45 class UpdaterInstructionFactory : public UScriptInstructionFactory { 46 public: 47 virtual int32_t CreateInstructionInstance(UScriptInstructionPtr& instr, const std::string& name); UpdaterInstructionFactory()48 UpdaterInstructionFactory() {} ~UpdaterInstructionFactory()49 virtual ~UpdaterInstructionFactory() {} 50 }; 51 52 class UScriptInstructionRawImageWrite : public Uscript::UScriptInstruction { 53 public: UScriptInstructionRawImageWrite()54 UScriptInstructionRawImageWrite() {} ~UScriptInstructionRawImageWrite()55 virtual ~UScriptInstructionRawImageWrite() {} 56 int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override; 57 58 private: 59 static int RawImageWriteProcessor(const Hpackage::PkgBuffer &buffer, size_t size, size_t start, bool isFinish, 60 const void* context); 61 int GetWritePathAndOffset(const std::string &partitionName, std::string &writePath, uint64_t &offset, 62 uint64_t &partitionSize); 63 bool WriteRawImage(const std::string &partitionName, const std::unique_ptr<DataWriter> &writer, 64 uint64_t partitionSize, Uscript::UScriptEnv &env); 65 static size_t totalSize_; 66 static size_t readSize_; 67 }; 68 69 class UScriptInstructionPkgExtract : public Uscript::UScriptInstruction { 70 public: UScriptInstructionPkgExtract()71 UScriptInstructionPkgExtract() {} ~UScriptInstructionPkgExtract()72 virtual ~UScriptInstructionPkgExtract() {} 73 int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override; 74 }; 75 76 class UScriptInstructionUpdateFromBin : public Uscript::UScriptInstruction { 77 public: UScriptInstructionUpdateFromBin()78 UScriptInstructionUpdateFromBin() {} ~UScriptInstructionUpdateFromBin()79 virtual ~UScriptInstructionUpdateFromBin() {} 80 int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override; 81 constexpr static uint32_t STASH_BUFFER_SIZE = 1024 * 1024 * 4; 82 constexpr static uint32_t BUFFER_NUM = 16; 83 static int UnCompressDataProducer(const Hpackage::PkgBuffer &buffer, size_t size, size_t start, bool isFinish, 84 const void* context); 85 private: 86 static size_t stashDataSize_; 87 }; 88 } // Updater 89 #endif /* UPDATE_PROCESSOR_H */ 90