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 #include "image_writer.h"
16
17 #include <cerrno>
18 #include <cstdio>
19 #include <dlfcn.h>
20 #include <fcntl.h>
21 #include <memory>
22 #include <string>
23 #include <unistd.h>
24
25 #include "applypatch/raw_writer.h"
26 #include "fs_manager/mount.h"
27 #include "log/log.h"
28 #include "ptable_manager.h"
29 #include "updater/updater_const.h"
30
31 namespace Flashd {
32 using namespace Updater;
33 using namespace std::placeholders;
34
GetDataWriter(const std::string & partition)35 bool FlashdWriterRaw::GetDataWriter(const std::string &partition)
36 {
37 #ifdef UPDATER_USE_PTABLE
38 DevicePtable &devicePtb = DevicePtable::GetInstance();
39 Ptable::PtnInfo ptnInfo;
40 if (!devicePtb.GetPartionInfoByName(partition, ptnInfo)) {
41 LOG(ERROR) << "FlashdWriterRaw: cannot find the lun index of partition: " << partition;
42 return false;
43 }
44 writer_ = DataWriter::CreateDataWriter(ptnInfo.writeMode, ptnInfo.writePath, partition, ptnInfo.startAddr);
45 #else
46 writer_ = DataWriter::CreateDataWriter(WRITE_RAW, GetBlockDeviceByMountPoint(partition));
47 #endif
48 if (writer_ == nullptr) {
49 LOG(ERROR) << partition << " FlashdWriterRaw writer error";
50 return false;
51 }
52 return true;
53 }
54
Write(const std::string & partition,const uint8_t * data,size_t len)55 int FlashdWriterRaw::Write(const std::string &partition, const uint8_t *data, size_t len)
56 {
57 if (writer_ == nullptr) {
58 if (!GetDataWriter(partition)) {
59 return -ENOSPC;
60 }
61 }
62
63 if (!writer_->Write(data, len, nullptr)) {
64 LOG(ERROR) << "writer " << partition << " failed ";
65 return -1;
66 }
67
68 return 0;
69 }
70
FlashdImageWriter()71 FlashdImageWriter::FlashdImageWriter()
72 {
73 FlashdWriterGet writerGet;
74 writerGet.checkImage = [this](const std::string &partition, const uint8_t *data, size_t len) {
75 return this->IsRawImage(partition, data, len);
76 };
77 writerGet.getWriter = [this] {
78 return this->GetRawWriter();
79 };
80 writerGet_.emplace_back(std::move(writerGet));
81 }
82
GetInstance()83 FlashdImageWriter &FlashdImageWriter::GetInstance()
84 {
85 static FlashdImageWriter instance;
86 return instance;
87 }
88
RegisterUserWriter(CheckImageProcess checkImage,GetWriterProcess getWriter)89 void FlashdImageWriter::RegisterUserWriter(CheckImageProcess checkImage, GetWriterProcess getWriter)
90 {
91 FlashdWriterGet writerGet = { checkImage, getWriter };
92 writerGet_.insert(writerGet_.begin(), std::move(writerGet));
93 }
94
GetWriter(const std::string & partition,const uint8_t * data,size_t len) const95 std::unique_ptr<FlashdWriter> FlashdImageWriter::GetWriter(const std::string &partition,
96 const uint8_t *data, size_t len) const
97 {
98 // init writer
99 for (auto &iter : writerGet_) {
100 if (iter.checkImage(partition, data, len)) {
101 return iter.getWriter();
102 }
103 }
104 return nullptr;
105 }
106
107 bool FlashdImageWriter::IsRawImage(const std::string &partition, [[maybe_unused]] const uint8_t *data,
108 [[maybe_unused]] size_t len) const
109 {
110 // default raw
111 LOG(INFO) << partition << " is raw instruction";
112 return true;
113 }
114
GetRawWriter() const115 std::unique_ptr<FlashdWriter> FlashdImageWriter::GetRawWriter() const
116 {
117 // init raw writer
118 return std::make_unique<FlashdWriterRaw>();
119 }
120 } // namespace Flashd
121