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 
16 #include "partition.h"
17 
18 #include <linux/fs.h>
19 #include <sys/ioctl.h>
20 #include <sys/mount.h>
21 #include <vector>
22 #include "flashd_define.h"
23 #include "flashd_utils.h"
24 #include "fs_manager/mount.h"
25 #include "utils.h"
26 #include "ptable_manager.h"
27 #include "applypatch/data_writer.h"
28 
29 namespace Flashd {
DoFlash(const uint8_t *buffer, int bufferSize) const30 int Partition::DoFlash(const uint8_t *buffer, int bufferSize) const
31 {
32     if (buffer == nullptr || bufferSize <= 0 || writer_ == nullptr) {
33         FLASHD_LOGE("buffer is null or writer_ is null or bufferSize is invaild");
34         return FLASHING_ARG_INVALID;
35     }
36 
37     if (auto ret = writer_->Write(devName_, buffer, bufferSize); ret != 0) {
38         FLASHD_LOGE("Write fail, ret = %d", ret);
39         return ret;
40     }
41     return 0;
42 }
43 
DoErase() const44 int Partition::DoErase() const
45 {
46 #ifdef UPDATER_USE_PTABLE
47     DevicePtable::GetInstance().LoadPartitionInfo();
48     DevicePtable &devicePtb = DevicePtable::GetInstance();
49     Ptable::PtnInfo ptnInfo;
50 
51     if (!devicePtb.GetPartionInfoByName(devName_, ptnInfo)) {
52         FLASHD_LOGE("DoErase: cannot find the lun index of partition: %s", devName_.c_str());
53         return FLASHING_ARG_INVALID;
54     }
55     if (ptnInfo.writeMode == "WRITE_IOCTL") {
56         FLASHD_LOGI("erase ext partition");
57         return DoEraseExt(ptnInfo);
58     }
59 #endif
60     return DoErasePartition();
61 }
62 
DoErasePartition() const63 int Partition::DoErasePartition() const
64 {
65     auto fd = open(Updater::Utils::GetPartitionRealPath(devName_).c_str(), O_RDWR);
66     if (fd < 0) {
67         FLASHD_LOGE("open partition %s fail, error = %d", devName_.c_str(), errno);
68         return FLASHING_OPEN_PART_ERROR;
69     }
70 
71 #ifndef UPDATER_UT
72     uint64_t size = GetBlockDeviceSize(fd);
73     uint64_t range[2] = { 0, size };
74     if (ioctl(fd, BLKSECDISCARD, &range) >= 0) {
75         FLASHD_LOGI("BLKDISCARD success");
76     }
77 
78     range[0] = 0;
79     range[1] = size;
80     if (ioctl(fd, BLKDISCARD, &range) < 0) {
81         close(fd);
82         FLASHD_LOGE("BLKDISCARD fail");
83         return FLASHING_NOPERMISSION;
84     }
85     std::vector<uint8_t> buffer(BLOCK_SIZE, 0);
86     if (!Updater::Utils::WriteFully(fd, buffer.data(), buffer.size())) {
87         close(fd);
88         FLASHD_LOGE("WriteFully fail");
89         return FLASHING_PART_WRITE_ERROR;
90     }
91     fsync(fd);
92 #endif
93     close(fd);
94     return 0;
95 }
96 
97 #ifdef UPDATER_USE_PTABLE
DoEraseExt(const Ptable::PtnInfo &ptnInfo) const98 int Partition::DoEraseExt(const Ptable::PtnInfo &ptnInfo) const
99 {
100     uint64_t partitionSize = ptnInfo.partitionSize;
101     std::unique_ptr<DataWriter> dataWriter = DataWriter::CreateDataWriter(ptnInfo.writeMode,
102         ptnInfo.writePath, devName_, ptnInfo.startAddr);
103 
104     if (!dataWriter) {
105         FLASHD_LOGE("DataWriter creation failed");
106         return FLASHING_ARG_INVALID;
107     }
108     std::vector<uint8_t> zeroBuffer(partitionSize, 0);
109     if (!dataWriter->Write(zeroBuffer.data(), partitionSize, nullptr)) {
110         FLASHD_LOGE("erase %s failed", devName_.c_str());
111         return FLASHING_PART_WRITE_ERROR;
112     }
113 
114     return 0;
115 }
116 #endif
117 
DoFormat() const118 int Partition::DoFormat() const
119 {
120     auto name = "/" + devName_;
121     if (auto ret = Updater::FormatPartition(name); ret != 0) {
122         FLASHD_LOGE("FormatPartition fail, ret = %d", ret);
123         return ret;
124     }
125 
126     if (auto ret = Updater::MountForPath(name); ret != 0) {
127         FLASHD_LOGE("MountForPath fail, ret = %d", ret);
128         return ret;
129     }
130     Updater::Utils::RestoreconPath(name);
131     return 0;
132 }
133 
GetBlockDeviceSize(int fd) const134 uint64_t Partition::GetBlockDeviceSize(int fd) const
135 {
136     uint64_t size = 0;
137     return (ioctl(fd, BLKGETSIZE64, &size) == 0) ? size : 0;
138 }
139 }
140