1 /*
2 * Copyright (c) 2021-2023 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 "sdcard_update.h"
16 #include <chrono>
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <string>
20 #include <sys/mount.h>
21 #include <sys/stat.h>
22 #include <thread>
23 #include <unistd.h>
24 #include <vector>
25 #include "language/language_ui.h"
26 #include "log/dump.h"
27 #include "log/log.h"
28 #include "fs_manager/mount.h"
29 #include "securec.h"
30 #include "ui/updater_ui_stub.h"
31 #include "updater/updater_const.h"
32 #include "utils.h"
33
34 namespace Updater {
GetSdcardPkgsPath(UpdaterParams & upParams)35 __attribute__((weak)) UpdaterStatus GetSdcardPkgsPath(UpdaterParams &upParams)
36 {
37 if (upParams.updatePackage.size() != 0) {
38 LOG(INFO) << "get sdcard packages from misc";
39 return UPDATE_SUCCESS;
40 }
41 LOG(INFO) << "get sdcard packages from sdcard path";
42 std::vector<std::string> sdcardPkgs = Utils::SplitString(SDCARD_CARD_PKG_PATH, ", ");
43 for (auto pkgPath : sdcardPkgs) {
44 if (access(pkgPath.c_str(), 0) == 0) {
45 LOG(INFO) << "find sdcard package : " << pkgPath;
46 upParams.updatePackage.push_back(pkgPath);
47 }
48 }
49 if (upParams.updatePackage.size() == 0) {
50 return UPDATE_ERROR;
51 }
52 return UPDATE_SUCCESS;
53 }
54
CheckSdcardPkgs(UpdaterParams & upParams)55 UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams)
56 {
57 #ifndef UPDATER_UT
58 auto sdParam = "updater.data.configs";
59 Utils::SetParameter(sdParam, "1");
60 std::string mountPoint = std::string(SDCARD_PATH);
61 std::vector<std::string> sdcardStr = GetBlockDevicesByMountPoint(mountPoint);
62 if (sdcardStr.empty()) {
63 UPDATER_UI_INSTANCE.ShowLog(
64 (errno == ENOENT) ? TR(LOG_SDCARD_NOTFIND) : TR(LOG_SDCARD_ABNORMAL), true);
65 return UPDATE_ERROR;
66 }
67
68 bool mountSuccess = false;
69 unsigned int retryTimes = 20;
70 for (unsigned int retryCount = 1; retryCount <= retryTimes; retryCount++) {
71 LOG(INFO) << "the retry time is: " << retryCount;
72 for (auto item : sdcardStr) {
73 if (MountSdcard(item, mountPoint) == 0) {
74 mountSuccess = true;
75 LOG(INFO) << "mount " << item << " sdcard success!";
76 break;
77 }
78 }
79 if (mountSuccess) {
80 break;
81 }
82 sleep(1); // sleep 1 second to wait for sd card recongnition
83 }
84
85 if (!mountSuccess) {
86 LOG(ERROR) << "mount sdcard fail!";
87 return UPDATE_ERROR;
88 }
89 #endif
90 if (GetSdcardPkgsPath(upParams) != UPDATE_SUCCESS) {
91 LOG(ERROR) << "there is no package in sdcard/updater, please check";
92 return UPDATE_ERROR;
93 }
94 return UPDATE_SUCCESS;
95 }
96 } // Updater
97