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 #include "fs_manager/mount.h"
17 #include <cerrno>
18 #include <fcntl.h>
19 #include <string>
20 #include <sys/mount.h>
21 #include <sys/stat.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24 #include <vector>
25 #include <linux/fs.h>
26 #include "log/dump.h"
27 #include "log/log.h"
28 #include "utils.h"
29
30 namespace Updater {
31 using Updater::Utils::SplitString;
32 static std::string g_defaultUpdaterFstab = "";
33 static Fstab *g_fstab = nullptr;
34 static const std::string PARTITION_PATH = "/dev/block/by-name";
35
GetFstabFile()36 static std::string GetFstabFile()
37 {
38 /* check vendor fstab files from specific directory */
39 std::vector<const std::string> specificFstabFiles = {"/vendor/etc/fstab.updater"};
40 for (auto& fstabFile : specificFstabFiles) {
41 if (access(fstabFile.c_str(), F_OK) == 0) {
42 return fstabFile;
43 }
44 }
45 return "";
46 }
47
GetMountStatusForPath(const std::string & path)48 MountStatus GetMountStatusForPath(const std::string &path)
49 {
50 FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
51 if (item == nullptr) {
52 return MountStatus::MOUNT_ERROR;
53 }
54 return GetMountStatusForMountPoint(item->mountPoint);
55 }
56
LoadFstab()57 void LoadFstab()
58 {
59 std::string fstabFile = g_defaultUpdaterFstab;
60 if (fstabFile.empty()) {
61 fstabFile = GetFstabFile();
62 if (fstabFile.empty()) {
63 fstabFile = "/etc/fstab.updater";
64 }
65 }
66 if (g_fstab != nullptr) {
67 ReleaseFstab(g_fstab);
68 g_fstab = nullptr;
69 }
70 // Clear fstab before read fstab file.
71 if ((g_fstab = ReadFstabFromFile(fstabFile.c_str(), false)) == nullptr) {
72 LOG(WARNING) << "Read " << fstabFile << " failed";
73 return;
74 }
75
76 LOG(DEBUG) << "Updater filesystem config info:";
77 for (FstabItem *item = g_fstab->head; item != nullptr; item = item->next) {
78 LOG(DEBUG) << "\tDevice: " << item->deviceName;
79 LOG(DEBUG) << "\tMount point : " << item->mountPoint;
80 LOG(DEBUG) << "\tFs type : " << item->fsType;
81 LOG(DEBUG) << "\tMount options: " << item->mountOptions;
82 }
83 }
84
LoadSpecificFstab(const std::string & fstabName)85 void LoadSpecificFstab(const std::string &fstabName)
86 {
87 g_defaultUpdaterFstab = fstabName;
88 LoadFstab();
89 g_defaultUpdaterFstab = "";
90 }
91
UmountForPath(const std::string & path)92 int UmountForPath(const std::string& path)
93 {
94 if (g_fstab == nullptr) {
95 LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
96 return -1;
97 }
98
99 FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
100 if (item == nullptr) {
101 LOG(ERROR) << "Cannot find fstab item for " << path << " to umount.";
102 return -1;
103 }
104
105 LOG(DEBUG) << "Umount for path " << path;
106 MountStatus rc = GetMountStatusForMountPoint(item->mountPoint);
107 if (rc == MOUNT_ERROR) {
108 return -1;
109 } else if (rc == MOUNT_UMOUNTED) {
110 return 0;
111 } else {
112 int ret = umount(item->mountPoint);
113 if (ret == -1) {
114 LOG(ERROR) << "Umount " << item->mountPoint << "failed: " << errno;
115 return -1;
116 }
117 }
118 return 0;
119 }
120
MountNtfsWithRetry(std::string source,std::string target)121 static int MountNtfsWithRetry(std::string source, std::string target)
122 {
123 char *argv[] = {const_cast<char *>("system/bin/mount.ntfs"),
124 const_cast<char *>(source.c_str()), const_cast<char *>(target.c_str()), nullptr};
125 int num = 0;
126 do {
127 pid_t child = fork();
128 if (child == 0) {
129 if (execv(argv[0], argv)) {
130 _exit(-1);
131 }
132 }
133 int status = -1;
134 if (waitpid(child, &status, 0) < 0) {
135 LOG(ERROR) << "waitpid failed, " << child;
136 }
137 if (WIFEXITED(status)) {
138 LOG(ERROR) << "child terminated by exit " << WEXITSTATUS(status);
139 } else if (WIFSIGNALED(status)) {
140 LOG(ERROR) << "child terminated by signal " << WTERMSIG(status);
141 } else if (WIFSTOPPED(status)) {
142 LOG(ERROR) << "child stopped by signal " << WSTOPSIG(status);
143 }
144
145 if (status == 0) {
146 Utils::UsSleep(100); // 100 : Wait interval
147 LOG(INFO) << "success to mount " << source << " on " << target;
148 return 0;
149 } else {
150 if ((errno == ENOENT) || (errno == ENODEV) || (errno == ENOMEDIUM)) {
151 LOG(ERROR) << "SD card never insert, dont try again, failed to mount " << source << " on " << target;
152 return -1;
153 }
154 }
155 num++;
156 LOG(ERROR) << "failed to mount " << source << " on " << target << ", errno is " << errno;
157 } while (num < 3); // 3 : retry three times
158 return -1;
159 }
160
MountSdcard(std::string & path,std::string & mountPoint)161 int MountSdcard(std::string &path, std::string &mountPoint)
162 {
163 if (path.empty() || mountPoint.empty()) {
164 LOG(ERROR) << "path or mountPoint is null, mount fail";
165 return -1;
166 }
167 MountStatus rc = GetMountStatusForMountPoint(mountPoint.c_str());
168 if (rc == MountStatus::MOUNT_ERROR) {
169 return -1;
170 } else if (rc == MountStatus::MOUNT_MOUNTED) {
171 LOG(INFO) << path << " already mounted";
172 return 0;
173 }
174 const std::vector<const char *> fileSystemType = {"ext4", "vfat", "exfat"};
175 for (auto type : fileSystemType) {
176 if (mount(path.c_str(), mountPoint.c_str(), type, 0, nullptr) == 0) {
177 LOG(INFO) << "mount success, sdcard type is " << type;
178 return 0;
179 }
180 }
181 if (MountNtfsWithRetry(path, mountPoint) == 0) {
182 LOG(INFO) << "mount success, sdcard type is ntfs";
183 return 0;
184 }
185 return -1;
186 }
187
MountForPath(const std::string & path)188 int MountForPath(const std::string &path)
189 {
190 if (g_fstab == nullptr) {
191 LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
192 return -1;
193 }
194
195 FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
196 int ret = -1;
197 if (item == nullptr) {
198 LOG(ERROR) << "Cannot find fstab item for " << path << " to mount.";
199 return -1;
200 }
201
202 LOG(DEBUG) << "Mount for path " << path;
203 MountStatus rc = GetMountStatusForMountPoint(item->mountPoint);
204 if (rc == MountStatus::MOUNT_ERROR) {
205 ret = -1;
206 } else if (rc == MountStatus::MOUNT_MOUNTED) {
207 LOG(INFO) << path << " already mounted";
208 ret = 0;
209 } else {
210 ret = MountOneItem(item);
211 }
212 return ret;
213 }
214
ErasePartition(const std::string & devPath)215 void ErasePartition(const std::string &devPath)
216 {
217 std::string realPath {};
218 if (!Utils::PathToRealPath(devPath, realPath)) {
219 LOG(ERROR) << "realpath failed:" << devPath;
220 return;
221 }
222 int fd = open(realPath.c_str(), O_RDWR | O_LARGEFILE);
223 if (fd == -1) {
224 LOG(ERROR) << "open failed:" << realPath;
225 return;
226 }
227
228 uint64_t size = 0;
229 int ret = ioctl(fd, BLKGETSIZE64, &size);
230 if (ret < 0) {
231 LOG(ERROR) << "get partition size failed:" << size;
232 close(fd);
233 return;
234 }
235
236 LOG(INFO) << "erase partition size:" << size;
237
238 uint64_t range[] { 0, size };
239 ret = ioctl(fd, BLKDISCARD, &range);
240 if (ret < 0) {
241 LOG(ERROR) << "erase partition failed";
242 }
243 close(fd);
244
245 return;
246 }
247
FormatPartition(const std::string & path,bool isZeroErase)248 int FormatPartition(const std::string &path, bool isZeroErase)
249 {
250 if (g_fstab == nullptr) {
251 LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
252 return -1;
253 }
254
255 FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
256 if (item == nullptr) {
257 LOG(ERROR) << "Cannot find fstab item for " << path << " to format.";
258 return -1;
259 }
260
261 if (strcmp(item->mountPoint, "/") == 0) {
262 /* Can not format root */
263 return 0;
264 }
265
266 if (!IsSupportedFilesystem(item->fsType)) {
267 LOG(ERROR) << "Try to format " << item->mountPoint << " with unsupported file system type: " << item->fsType;
268 return -1;
269 }
270
271 // Umount first
272 if (UmountForPath(path) != 0) {
273 return -1;
274 }
275
276 if (isZeroErase) {
277 ErasePartition(item->deviceName);
278 }
279
280 int ret = DoFormat(item->deviceName, item->fsType);
281 if (ret != 0) {
282 LOG(ERROR) << "Format " << path << " failed";
283 }
284 return ((ret != 0) ? -1 : 0);
285 }
286
SetupPartitions(bool isMountData)287 int SetupPartitions(bool isMountData)
288 {
289 UPDATER_INIT_RECORD;
290 if (!Utils::IsUpdaterMode()) {
291 LOG(ERROR) << "live update mode";
292 return 0;
293 }
294
295 if (g_fstab == NULL || g_fstab->head == NULL) {
296 LOG(ERROR) << "Fstab is invalid";
297 UPDATER_LAST_WORD(-1);
298 return -1;
299 }
300 for (const FstabItem *item = g_fstab->head; item != nullptr; item = item->next) {
301 std::string mountPoint(item->mountPoint);
302 std::string fsType(item->fsType);
303 if (mountPoint == "/" || mountPoint == "/tmp" || fsType == "none" ||
304 mountPoint == "/sdcard") {
305 continue;
306 }
307
308 if (mountPoint == "/data" && isMountData) {
309 if (MountForPath(mountPoint) != 0) {
310 LOG(ERROR) << "Expected partition " << mountPoint << " is not mounted.";
311 UPDATER_LAST_WORD(-1);
312 return -1;
313 }
314 continue;
315 }
316 if (UmountForPath(mountPoint) != 0) {
317 LOG(ERROR) << "Umount " << mountPoint << " failed";
318 UPDATER_LAST_WORD(-1);
319 return -1;
320 }
321 }
322 return 0;
323 }
324
GetBlockDeviceByMountPoint(const std::string & mountPoint)325 const std::string GetBlockDeviceByMountPoint(const std::string &mountPoint)
326 {
327 if (mountPoint.empty()) {
328 LOG(ERROR) << "mountPoint empty error.";
329 return "";
330 }
331 std::string blockDevice = PARTITION_PATH + mountPoint;
332 if (mountPoint[0] != '/') {
333 blockDevice = PARTITION_PATH + "/" + mountPoint;
334 }
335 if (g_fstab != nullptr) {
336 FstabItem *item = FindFstabItemForMountPoint(*g_fstab, mountPoint.c_str());
337 if (item != NULL) {
338 blockDevice = item->deviceName;
339 }
340 }
341 return blockDevice;
342 }
343
GetBlockDevicesByMountPoint(const std::string & mountPoint)344 const std::vector<std::string> GetBlockDevicesByMountPoint(const std::string &mountPoint)
345 {
346 std::vector<std::string> blockDevices;
347 if (mountPoint.empty() || g_fstab == nullptr) {
348 LOG(ERROR) << "mountPoint or g_fstab empty error.";
349 return blockDevices;
350 }
351 for (FstabItem *item = g_fstab->head; item != NULL; item = item->next) {
352 if ((item->mountPoint != NULL) && item->mountPoint == mountPoint) {
353 blockDevices.push_back(item->deviceName);
354 }
355 }
356
357 if (blockDevices.empty()) {
358 std::string blockDevice = PARTITION_PATH + mountPoint;
359 if (mountPoint[0] != '/') {
360 blockDevice = PARTITION_PATH + "/" + mountPoint;
361 }
362 blockDevices.push_back(blockDevice);
363 }
364 return blockDevices;
365 }
366 } // updater
367