1 /* 2 * Copyright (c) 2022 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 FLASHD_IMAGE_WRITER_H 16 #define FLASHD_IMAGE_WRITER_H 17 18 #include <cstdio> 19 #include <memory> 20 #include <string> 21 #include <unistd.h> 22 23 #include "applypatch/data_writer.h" 24 #include "macros.h" 25 26 namespace Flashd { 27 using namespace Updater; 28 class FlashdWriter { 29 public: ~FlashdWriter()30 virtual ~FlashdWriter() {} 31 virtual int Write(const std::string &partition, const uint8_t *data, size_t len) = 0; 32 }; 33 34 class FlashdWriterRaw : public FlashdWriter { 35 public: 36 int Write(const std::string &partition, const uint8_t *data, size_t len) override; 37 38 private: 39 bool GetDataWriter(const std::string &partition); 40 std::unique_ptr<DataWriter> writer_ = nullptr; 41 }; 42 43 class FlashdImageWriter { 44 DISALLOW_COPY_MOVE(FlashdImageWriter); 45 public: 46 using CheckImageProcess = std::function<bool (const std::string &, const uint8_t *, size_t)>; 47 using GetWriterProcess = std::function<std::unique_ptr<FlashdWriter>()>; 48 struct FlashdWriterGet { 49 CheckImageProcess checkImage {}; 50 GetWriterProcess getWriter {}; 51 }; 52 static FlashdImageWriter &GetInstance(); 53 std::unique_ptr<FlashdWriter> GetWriter(const std::string &partition, const uint8_t *data, size_t len) const; 54 void RegisterUserWriter(CheckImageProcess checkImage, GetWriterProcess getWriter); 55 56 private: 57 FlashdImageWriter(); ~FlashdImageWriter()58 ~FlashdImageWriter() {} 59 bool IsRawImage(const std::string &partition, const uint8_t *data, size_t len) const; 60 std::unique_ptr<FlashdWriter> GetRawWriter() const; 61 std::vector<FlashdWriterGet> writerGet_ = {}; 62 }; 63 } // Flashd 64 #endif // FLASHD_IMAGE_WRITER_H 65