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 char lunIndexName = 'a' + ptnInfo.lun;
45 const std::string writePath = std::string(PREFIX_UFS_NODE) + lunIndexName;
46 writer_ = DataWriter::CreateDataWriter(WRITE_RAW, writePath, ptnInfo.startAddr);
47 #else
48 writer_ = DataWriter::CreateDataWriter(WRITE_RAW, GetBlockDeviceByMountPoint(partition));
49 #endif
50 if (writer_ == nullptr) {
51 LOG(ERROR) << partition << " FlashdWriterRaw writer error";
52 return false;
53 }
54 return true;
55 }
56
Write(const std::string & partition,const uint8_t * data,size_t len)57 int FlashdWriterRaw::Write(const std::string &partition, const uint8_t *data, size_t len)
58 {
59 if (writer_ == nullptr) {
60 if (!GetDataWriter(partition)) {
61 return -ENOSPC;
62 }
63 }
64
65 if (!writer_->Write(data, len, nullptr)) {
66 LOG(ERROR) << "writer " << partition << " failed ";
67 return -1;
68 }
69
70 return 0;
71 }
72
FlashdImageWriter()73 FlashdImageWriter::FlashdImageWriter()
74 {
75 FlashdWriterGet writerGet;
76 writerGet.checkImage = std::bind(&FlashdImageWriter::IsRawImage, this, _1, _2, _3);
77 writerGet.getWriter = std::bind(&FlashdImageWriter::GetRawWriter, this);
78 writerGet_.emplace_back(std::move(writerGet));
79 }
80
GetInstance()81 FlashdImageWriter &FlashdImageWriter::GetInstance()
82 {
83 static FlashdImageWriter instance;
84 return instance;
85 }
86
RegisterUserWriter(CheckImageProcess checkImage,GetWriterProcess getWriter)87 void FlashdImageWriter::RegisterUserWriter(CheckImageProcess checkImage, GetWriterProcess getWriter)
88 {
89 FlashdWriterGet writerGet = { checkImage, getWriter };
90 writerGet_.insert(writerGet_.begin(), std::move(writerGet));
91 }
92
GetWriter(const std::string & partition,const uint8_t * data,size_t len) const93 std::unique_ptr<FlashdWriter> FlashdImageWriter::GetWriter(const std::string &partition,
94 const uint8_t *data, size_t len) const
95 {
96 // init writer
97 for (auto &iter : writerGet_) {
98 if (iter.checkImage(partition, data, len)) {
99 return iter.getWriter();
100 }
101 }
102 return nullptr;
103 }
104
IsRawImage(const std::string & partition,const uint8_t * data,size_t len) const105 bool FlashdImageWriter::IsRawImage(const std::string &partition, [[maybe_unused]] const uint8_t *data,
106 [[maybe_unused]] size_t len) const
107 {
108 // default raw
109 LOG(INFO) << partition << " is raw instruction";
110 return true;
111 }
112
GetRawWriter() const113 std::unique_ptr<FlashdWriter> FlashdImageWriter::GetRawWriter() const
114 {
115 // init raw writer
116 return std::make_unique<FlashdWriterRaw>();
117 }
118 } // namespace Flashd
119