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 if (path == "/data") {
113 Utils::SetParameter("updater.data.ready", "0");
114 }
115 int ret = umount(item->mountPoint);
116 if (ret == -1) {
117 LOG(ERROR) << "Umount " << item->mountPoint << "failed: " << errno;
118 return -1;
119 }
120 }
121 return 0;
122 }
123
MountNtfsWithRetry(std::string source,std::string target)124 static int MountNtfsWithRetry(std::string source, std::string target)
125 {
126 char *argv[] = {const_cast<char *>("system/bin/mount.ntfs"),
127 const_cast<char *>(source.c_str()), const_cast<char *>(target.c_str()), nullptr};
128 int num = 0;
129 do {
130 pid_t child = fork();
131 if (child == 0) {
132 if (execv(argv[0], argv)) {
133 _exit(-1);
134 }
135 }
136 int status = -1;
137 if (waitpid(child, &status, 0) < 0) {
138 LOG(ERROR) << "waitpid failed, " << child;
139 }
140 if (WIFEXITED(status)) {
141 LOG(ERROR) << "child terminated by exit " << WEXITSTATUS(status);
142 } else if (WIFSIGNALED(status)) {
143 LOG(ERROR) << "child terminated by signal " << WTERMSIG(status);
144 } else if (WIFSTOPPED(status)) {
145 LOG(ERROR) << "child stopped by signal " << WSTOPSIG(status);
146 }
147
148 if (status == 0) {
149 Utils::UsSleep(100); // 100 : Wait interval
150 LOG(INFO) << "success to mount " << source << " on " << target;
151 return 0;
152 } else {
153 if ((errno == ENOENT) || (errno == ENODEV) || (errno == ENOMEDIUM)) {
154 LOG(ERROR) << "SD card never insert, dont try again, failed to mount " << source << " on " << target;
155 return -1;
156 }
157 }
158 num++;
159 LOG(ERROR) << "failed to mount " << source << " on " << target << ", errno is " << errno;
160 } while (num < 3); // 3 : retry three times
161 return -1;
162 }
163
MountSdcard(std::string & path,std::string & mountPoint)164 int MountSdcard(std::string &path, std::string &mountPoint)
165 {
166 if (path.empty() || mountPoint.empty()) {
167 LOG(ERROR) << "path or mountPoint is null, mount fail";
168 return -1;
169 }
170 MountStatus rc = GetMountStatusForMountPoint(mountPoint.c_str());
171 if (rc == MountStatus::MOUNT_ERROR) {
172 return -1;
173 } else if (rc == MountStatus::MOUNT_MOUNTED) {
174 LOG(INFO) << path << " already mounted";
175 return 0;
176 }
177 const std::vector<const char *> fileSystemType = {"ext4", "vfat", "exfat"};
178 for (auto type : fileSystemType) {
179 if (mount(path.c_str(), mountPoint.c_str(), type, 0, nullptr) == 0) {
180 LOG(INFO) << "mount success, sdcard type is " << type;
181 return 0;
182 }
183 }
184 if (MountNtfsWithRetry(path, mountPoint) == 0) {
185 LOG(INFO) << "mount success, sdcard type is ntfs";
186 return 0;
187 }
188 return -1;
189 }
190
MountForPath(const std::string & path)191 int MountForPath(const std::string &path)
192 {
193 if (g_fstab == nullptr) {
194 LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
195 return -1;
196 }
197
198 FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
199 int ret = -1;
200 if (item == nullptr) {
201 LOG(ERROR) << "Cannot find fstab item for " << path << " to mount.";
202 return -1;
203 }
204
205 LOG(DEBUG) << "Mount for path " << path;
206 MountStatus rc = GetMountStatusForMountPoint(item->mountPoint);
207 if (rc == MountStatus::MOUNT_ERROR) {
208 LOG(ERROR) << "GetMountStatusForMountPoint ret is MOUNT_ERROR";
209 ret = -1;
210 } else if (rc == MountStatus::MOUNT_MOUNTED) {
211 LOG(INFO) << path << " already mounted";
212 ret = 0;
213 } else {
214 ret = MountOneItem(item);
215 }
216 return ret;
217 }
218
ErasePartition(const std::string & devPath)219 void ErasePartition(const std::string &devPath)
220 {
221 std::string realPath {};
222 if (!Utils::PathToRealPath(devPath, realPath)) {
223 LOG(ERROR) << "realpath failed:" << devPath;
224 return;
225 }
226 int fd = open(realPath.c_str(), O_RDWR | O_LARGEFILE);
227 if (fd == -1) {
228 LOG(ERROR) << "open failed:" << realPath;
229 return;
230 }
231
232 uint64_t size = 0;
233 int ret = ioctl(fd, BLKGETSIZE64, &size);
234 if (ret < 0) {
235 LOG(ERROR) << "get partition size failed:" << size;
236 close(fd);
237 return;
238 }
239
240 LOG(INFO) << "erase partition size:" << size;
241
242 uint64_t range[] { 0, size };
243 ret = ioctl(fd, BLKDISCARD, &range);
244 if (ret < 0) {
245 LOG(ERROR) << "erase partition failed";
246 }
247 close(fd);
248
249 return;
250 }
251
FormatPartition(const std::string & path,bool isZeroErase)252 int FormatPartition(const std::string &path, bool isZeroErase)
253 {
254 if (g_fstab == nullptr) {
255 LOG(ERROR) << "fstab is not loaded, g_fstab is null.";
256 return -1;
257 }
258
259 FstabItem *item = FindFstabItemForPath(*g_fstab, path.c_str());
260 if (item == nullptr) {
261 LOG(ERROR) << "Cannot find fstab item for " << path << " to format.";
262 return -1;
263 }
264
265 if (strcmp(item->mountPoint, "/") == 0) {
266 /* Can not format root */
267 return 0;
268 }
269
270 if (!IsSupportedFilesystem(item->fsType)) {
271 LOG(ERROR) << "Try to format " << item->mountPoint << " with unsupported file system type: " << item->fsType;
272 return -1;
273 }
274
275 // Umount first
276 if (UmountForPath(path) != 0) {
277 return -1;
278 }
279
280 if (isZeroErase) {
281 ErasePartition(item->deviceName);
282 }
283
284 int ret = DoFormat(item->deviceName, item->fsType);
285 if (ret != 0) {
286 LOG(ERROR) << "Format " << path << " failed";
287 }
288 return ((ret != 0) ? -1 : 0);
289 }
290
SetupPartitions(bool isMountData)291 int SetupPartitions(bool isMountData)
292 {
293 UPDATER_INIT_RECORD;
294 if (!Utils::IsUpdaterMode()) {
295 LOG(ERROR) << "live update mode";
296 return 0;
297 }
298
299 if (g_fstab == NULL || g_fstab->head == NULL) {
300 LOG(ERROR) << "Fstab is invalid";
301 UPDATER_LAST_WORD(-1);
302 return -1;
303 }
304 for (const FstabItem *item = g_fstab->head; item != nullptr; item = item->next) {
305 std::string mountPoint(item->mountPoint);
306 std::string fsType(item->fsType);
307 if (mountPoint == "/" || mountPoint == "/tmp" || fsType == "none" ||
308 mountPoint == "/sdcard") {
309 continue;
310 }
311
312 if (mountPoint == "/data" && isMountData) {
313 if (MountForPath(mountPoint) != 0) {
314 LOG(ERROR) << "Expected partition " << mountPoint << " is not mounted.";
315 UPDATER_LAST_WORD(-1);
316 return -1;
317 }
318 Utils::SetParameter("updater.data.ready", "1");
319 LOG(INFO) << "mount data not fail";
320 continue;
321 }
322 if (UmountForPath(mountPoint) != 0) {
323 LOG(ERROR) << "Umount " << mountPoint << " failed";
324 UPDATER_LAST_WORD(-1);
325 return -1;
326 }
327 }
328 return 0;
329 }
330
GetBlockDeviceByMountPoint(const std::string & mountPoint)331 const std::string GetBlockDeviceByMountPoint(const std::string &mountPoint)
332 {
333 if (mountPoint.empty()) {
334 LOG(ERROR) << "mountPoint empty error.";
335 return "";
336 }
337 std::string blockDevice = PARTITION_PATH + mountPoint;
338 if (mountPoint[0] != '/') {
339 blockDevice = PARTITION_PATH + "/" + mountPoint;
340 }
341 if (g_fstab != nullptr) {
342 FstabItem *item = FindFstabItemForMountPoint(*g_fstab, mountPoint.c_str());
343 if (item != NULL) {
344 blockDevice = item->deviceName;
345 }
346 }
347 return blockDevice;
348 }
349
GetBlockDevicesByMountPoint(const std::string & mountPoint)350 const std::vector<std::string> GetBlockDevicesByMountPoint(const std::string &mountPoint)
351 {
352 std::vector<std::string> blockDevices;
353 if (mountPoint.empty() || g_fstab == nullptr) {
354 LOG(ERROR) << "mountPoint or g_fstab empty error.";
355 return blockDevices;
356 }
357 for (FstabItem *item = g_fstab->head; item != NULL; item = item->next) {
358 if ((item->mountPoint != NULL) && item->mountPoint == mountPoint) {
359 blockDevices.push_back(item->deviceName);
360 }
361 }
362
363 if (blockDevices.empty()) {
364 std::string blockDevice = PARTITION_PATH + mountPoint;
365 if (mountPoint[0] != '/') {
366 blockDevice = PARTITION_PATH + "/" + mountPoint;
367 }
368 blockDevices.push_back(blockDevice);
369 }
370 return blockDevices;
371 }
372 } // updater
373