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
GetSdcardPkgsFromDev(UpdaterParams & upParams)55 __attribute__((weak)) UpdaterStatus GetSdcardPkgsFromDev(UpdaterParams &upParams)
56 {
57 LOG(INFO) << "not implemented get sdcard pkgs from dev";
58 return UPDATE_ERROR;
59 }
60
CheckSdcardPkgs(UpdaterParams & upParams)61 UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams)
62 {
63 #ifndef UPDATER_UT
64 auto sdParam = "updater.data.configs";
65 Utils::SetParameter(sdParam, "1");
66 if (upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV && GetSdcardPkgsFromDev(upParams) == UPDATE_SUCCESS) {
67 LOG(INFO) << "get sd card from dev succeed, skip get package from sd card";
68 return UPDATE_SUCCESS;
69 }
70 std::string mountPoint = std::string(SDCARD_PATH);
71 std::vector<std::string> sdcardStr = GetBlockDevicesByMountPoint(mountPoint);
72 if (sdcardStr.empty()) {
73 UPDATER_UI_INSTANCE.ShowLog(
74 (errno == ENOENT) ? TR(LOG_SDCARD_NOTFIND) : TR(LOG_SDCARD_ABNORMAL), true);
75 return UPDATE_ERROR;
76 }
77
78 bool mountSuccess = false;
79 unsigned int retryTimes = 20;
80 for (unsigned int retryCount = 1; retryCount <= retryTimes; retryCount++) {
81 LOG(INFO) << "the retry time is: " << retryCount;
82 for (auto item : sdcardStr) {
83 if (MountSdcard(item, mountPoint) == 0) {
84 mountSuccess = true;
85 LOG(INFO) << "mount " << item << " sdcard success!";
86 break;
87 }
88 }
89 if (mountSuccess) {
90 break;
91 }
92 sleep(1); // sleep 1 second to wait for sd card recongnition
93 }
94
95 if (!mountSuccess) {
96 LOG(ERROR) << "mount sdcard fail!";
97 return UPDATE_ERROR;
98 }
99 #endif
100 if (GetSdcardPkgsPath(upParams) != UPDATE_SUCCESS) {
101 LOG(ERROR) << "there is no package in sdcard/updater, please check";
102 return UPDATE_ERROR;
103 }
104 return UPDATE_SUCCESS;
105 }
106 } // Updater
107