1 /*
2  * Copyright (c) 2023 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 BIN_FLOW_UPDATE
16 #define BIN_FLOW_UPDATE
17 
18 #include <cstdio>
19 #include <functional>
20 #include <string>
21 #include <sys/wait.h>
22 #include <vector>
23 #include <map>
24 
25 #include "applypatch/data_writer.h"
26 #include "package/pkg_manager.h"
27 
28 namespace Updater {
29 
30 using BinFlowUpdateStep = enum {
31     BIN_UPDATE_STEP_PRE = 0,
32     BIN_UPDATE_STEP_DO,
33     BIN_UPDATE_STEP_POST
34 };
35 
36 struct BinFlowUpdateInfo {
37     std::vector<std::string> componentNames;
38     uint32_t curIndex = 0;
39     size_t imageWriteLen = 0;
40     bool needNewData = false;
41     std::map<BinFlowUpdateStep, std::function<int32_t (uint8_t *, uint32_t &)>> updateBinProcess_;
42     BinFlowUpdateStep updateStep = BIN_UPDATE_STEP_PRE;
43     std::unique_ptr<DataWriter> writer;
44     const Hpackage::FileInfo *info = nullptr;
45 };
46 
47 class BinFlowUpdate {
48 public:
49     explicit BinFlowUpdate(uint32_t maxBufSize);
50     virtual ~BinFlowUpdate();
51     int32_t StartBinFlowUpdate(uint8_t *data, uint32_t len);
52 private:
53     int32_t BinUpdatePreWrite(uint8_t *data, uint32_t &len);
54     int32_t BinUpdateDoWrite(uint8_t *data, uint32_t &len);
55     int32_t BinUpdatePostWrite(uint8_t *data, uint32_t &len);
56     int32_t UpdateBinData(uint8_t *data, uint32_t &len);
57     std::unique_ptr<DataWriter> GetDataWriter(const std::string &partition);
58 
59     uint32_t UpdateBinHead(uint8_t *data, uint32_t &len);
60     bool AddRemainData(uint8_t *data, uint32_t &len);
61 
62     Hpackage::PkgManager::PkgManagerPtr pkgManager_;
63     uint8_t *buffer_ = nullptr;
64     uint32_t maxBufSize_ = 0;
65     uint32_t curlen_ = 0;
66 
67     // bin images
68     bool headInit_ = false;
69     std::vector<std::string> componentNames_;
70     uint32_t procCompIndex_ = 0;
71     std::map<BinFlowUpdateStep, std::function<int32_t (uint8_t *, uint32_t &)>> updateBinProcess_;
72     BinFlowUpdateInfo updateInfo_ {};
73 };
74 } // Updater
75 #endif /* BIN_FLOW_UPDATE */
76