1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci#ifndef UPDATE_PROCESSOR_H
16fb299fa2Sopenharmony_ci#define UPDATE_PROCESSOR_H
17fb299fa2Sopenharmony_ci
18fb299fa2Sopenharmony_ci#include <cstdio>
19fb299fa2Sopenharmony_ci#include <string>
20fb299fa2Sopenharmony_ci#include <sys/wait.h>
21fb299fa2Sopenharmony_ci#include <unistd.h>
22fb299fa2Sopenharmony_ci#include "applypatch/data_writer.h"
23fb299fa2Sopenharmony_ci#include "pkg_manager.h"
24fb299fa2Sopenharmony_ci#include "script_instruction.h"
25fb299fa2Sopenharmony_ci#include "script_manager.h"
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_ciusing Uscript::UScriptEnv;
28fb299fa2Sopenharmony_ciusing Uscript::UScriptInstructionFactory;
29fb299fa2Sopenharmony_ciusing Uscript::UScriptInstructionFactoryPtr;
30fb299fa2Sopenharmony_ciusing Uscript::UScriptInstructionPtr;
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_cinamespace Updater {
33fb299fa2Sopenharmony_cienum EXIT_CODES {
34fb299fa2Sopenharmony_ci    EXIT_INVALID_ARGS = EXIT_SUCCESS + 1,
35fb299fa2Sopenharmony_ci    EXIT_READ_PACKAGE_ERROR = 2,
36fb299fa2Sopenharmony_ci    EXIT_FOUND_SCRIPT_ERROR = 3,
37fb299fa2Sopenharmony_ci    EXIT_PARSE_SCRIPT_ERROR = 4,
38fb299fa2Sopenharmony_ci    EXIT_EXEC_SCRIPT_ERROR = 5,
39fb299fa2Sopenharmony_ci    EXIT_VERIFY_SCRIPT_ERROR = 6,
40fb299fa2Sopenharmony_ci};
41fb299fa2Sopenharmony_ci
42fb299fa2Sopenharmony_ciint ProcessUpdater(bool retry, int pipeFd, const std::string &packagePath, const std::string &keyPath);
43fb299fa2Sopenharmony_civoid GetPartitionPathFromName(const std::string &partitionName, std::string &partitionPath);
44fb299fa2Sopenharmony_ci
45fb299fa2Sopenharmony_ci#ifdef __cplusplus
46fb299fa2Sopenharmony_ci#if __cplusplus
47fb299fa2Sopenharmony_ciextern "C" {
48fb299fa2Sopenharmony_ci#endif
49fb299fa2Sopenharmony_ci#endif /* __cplusplus */
50fb299fa2Sopenharmony_ciint32_t GetFinalBinaryResult(int32_t result);
51fb299fa2Sopenharmony_ci#ifdef __cplusplus
52fb299fa2Sopenharmony_ci#if __cplusplus
53fb299fa2Sopenharmony_ci}
54fb299fa2Sopenharmony_ci#endif
55fb299fa2Sopenharmony_ci#endif /* __cplusplus */
56fb299fa2Sopenharmony_ci
57fb299fa2Sopenharmony_ciclass UpdaterInstructionFactory : public UScriptInstructionFactory {
58fb299fa2Sopenharmony_cipublic:
59fb299fa2Sopenharmony_ci    virtual int32_t CreateInstructionInstance(UScriptInstructionPtr& instr, const std::string& name);
60fb299fa2Sopenharmony_ci    UpdaterInstructionFactory() {}
61fb299fa2Sopenharmony_ci    virtual ~UpdaterInstructionFactory() {}
62fb299fa2Sopenharmony_ci};
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_ciclass UScriptInstructionRawImageWrite : public Uscript::UScriptInstruction {
65fb299fa2Sopenharmony_cipublic:
66fb299fa2Sopenharmony_ci    UScriptInstructionRawImageWrite() {}
67fb299fa2Sopenharmony_ci    virtual ~UScriptInstructionRawImageWrite() {}
68fb299fa2Sopenharmony_ci    int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override;
69fb299fa2Sopenharmony_ci
70fb299fa2Sopenharmony_ciprivate:
71fb299fa2Sopenharmony_ci    static int RawImageWriteProcessor(const Hpackage::PkgBuffer &buffer, size_t size, size_t start, bool isFinish,
72fb299fa2Sopenharmony_ci        const void* context);
73fb299fa2Sopenharmony_ci    int GetWritePathAndOffset(const std::string &partitionName, std::string &writePath, uint64_t &offset,
74fb299fa2Sopenharmony_ci        uint64_t &partitionSize);
75fb299fa2Sopenharmony_ci    bool WriteRawImage(const std::string &partitionName, const std::unique_ptr<DataWriter> &writer,
76fb299fa2Sopenharmony_ci        uint64_t partitionSize, Uscript::UScriptEnv &env);
77fb299fa2Sopenharmony_ci    static size_t totalSize_;
78fb299fa2Sopenharmony_ci    static size_t readSize_;
79fb299fa2Sopenharmony_ci};
80fb299fa2Sopenharmony_ci
81fb299fa2Sopenharmony_ciclass UScriptInstructionPkgExtract : public Uscript::UScriptInstruction {
82fb299fa2Sopenharmony_cipublic:
83fb299fa2Sopenharmony_ci    UScriptInstructionPkgExtract() {}
84fb299fa2Sopenharmony_ci    virtual ~UScriptInstructionPkgExtract() {}
85fb299fa2Sopenharmony_ci    int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override;
86fb299fa2Sopenharmony_ci};
87fb299fa2Sopenharmony_ci
88fb299fa2Sopenharmony_ciclass UScriptInstructionPkgExtractRetSuc : public UScriptInstructionPkgExtract {
89fb299fa2Sopenharmony_cipublic:
90fb299fa2Sopenharmony_ci    UScriptInstructionPkgExtractRetSuc() {}
91fb299fa2Sopenharmony_ci    ~UScriptInstructionPkgExtractRetSuc() override {}
92fb299fa2Sopenharmony_ci    int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override;
93fb299fa2Sopenharmony_ci};
94fb299fa2Sopenharmony_ci
95fb299fa2Sopenharmony_ciclass UScriptInstructionUpdateFromBin : public Uscript::UScriptInstruction {
96fb299fa2Sopenharmony_cipublic:
97fb299fa2Sopenharmony_ci    UScriptInstructionUpdateFromBin() {}
98fb299fa2Sopenharmony_ci    virtual ~UScriptInstructionUpdateFromBin() {}
99fb299fa2Sopenharmony_ci    int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override;
100fb299fa2Sopenharmony_ci    constexpr static uint32_t STASH_BUFFER_SIZE = 1024 * 1024 * 4;
101fb299fa2Sopenharmony_ci    constexpr static uint32_t BUFFER_NUM = 16;
102fb299fa2Sopenharmony_ci    static int UnCompressDataProducer(const Hpackage::PkgBuffer &buffer, size_t size, size_t start, bool isFinish,
103fb299fa2Sopenharmony_ci        const void* context);
104fb299fa2Sopenharmony_ciprivate:
105fb299fa2Sopenharmony_ci    static size_t stashDataSize_;
106fb299fa2Sopenharmony_ci};
107fb299fa2Sopenharmony_ci} // Updater
108fb299fa2Sopenharmony_ci#endif /* UPDATE_PROCESSOR_H */
109