• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef UPDATER_DATA_WRITER_H
17 #define UPDATER_DATA_WRITER_H
18 
19 #include <cstdio>
20 #include <memory>
21 #include <string>
22 #include <unistd.h>
23 #include "updater_env.h"
24 
25 namespace Updater {
26 enum WriteMode : int {
27     WRITE_RAW = 1,
28     WRITE_DECRYPT = 2,
29     WRITE_BLOCK = 3,
30 };
31 
32 class DataWriter {
33 public:
34     using DataWriterPtr = DataWriter *;
35     virtual bool Write(const uint8_t *addr, size_t len, const void *context) = 0;
~DataWriter()36     virtual ~DataWriter() {}
37     static std::unique_ptr<DataWriter> CreateDataWriter(WriteMode mode, const std::string &path, UpdaterEnv *env,
38         uint64_t offset = 0);
39     static std::unique_ptr<DataWriter> CreateDataWriter(WriteMode mode, const std::string &path, uint64_t offset = 0);
40     static UpdaterEnv *GetUpdaterEnv();
41     void SetUpdaterEnv(UpdaterEnv *env);
42     static void ReleaseDataWriter(std::unique_ptr<DataWriter> &writer);
43 
44 protected:
45     virtual int OpenPath(const std::string &path);
46 
47 private:
48     static UpdaterEnv *env_;
49 };
50 
51 // Maybe we should read sector size from flash.
52 #define DEFAULT_SECTOR_SIZE (512)
53 } // Updater
54 #endif // UPDATER_DATA_WRITER_H
55