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 #ifndef UPDATER_UT
26 #include "language/language_ui.h"
27 #endif
28 #include "log/dump.h"
29 #include "log/log.h"
30 #include "fs_manager/mount.h"
31 #include "securec.h"
32 #ifndef UPDATER_UT
33 #include "ui/updater_ui_stub.h"
34 #endif
35 #include "updater/updater_const.h"
36 #include "utils.h"
37
38 namespace Updater {
GetSdcardPkgsPath(UpdaterParams & upParams)39 __attribute__((weak)) UpdaterStatus GetSdcardPkgsPath(UpdaterParams &upParams)
40 {
41 if (upParams.updatePackage.size() != 0) {
42 LOG(INFO) << "get sdcard packages from misc";
43 return UPDATE_SUCCESS;
44 }
45 LOG(INFO) << "get sdcard packages from sdcard path";
46 std::vector<std::string> sdcardPkgs = Utils::SplitString(SDCARD_CARD_PKG_PATH, ", ");
47 for (auto pkgPath : sdcardPkgs) {
48 if (access(pkgPath.c_str(), 0) == 0) {
49 LOG(INFO) << "find sdcard package : " << pkgPath;
50 upParams.updatePackage.push_back(pkgPath);
51 }
52 }
53 if (upParams.updatePackage.size() == 0) {
54 return UPDATE_ERROR;
55 }
56 return UPDATE_SUCCESS;
57 }
58
GetSdcardPkgsFromDev(UpdaterParams & upParams)59 __attribute__((weak)) UpdaterStatus GetSdcardPkgsFromDev(UpdaterParams &upParams)
60 {
61 LOG(INFO) << "not implemented get sdcard pkgs from dev";
62 return UPDATE_ERROR;
63 }
64
65
DoMountSdCard(std::vector<std::string> & sdCardStr,std::string & mountPoint,UpdaterParams & upParams)66 bool DoMountSdCard(std::vector<std::string> &sdCardStr, std::string &mountPoint, UpdaterParams &upParams)
67 {
68 #ifndef UPDATER_UT
69 bool mountSuccess = false;
70 unsigned int retryTimes = 20; // Wait 20s
71 if (upParams.sdExtMode == SDCARD_MAINIMG || upParams.sdExtMode == SDCARD_NORMAL_UPDATE) {
72 retryTimes = 60; // Wait 60s
73 }
74 for (unsigned int retryCount = 1; retryCount <= retryTimes; retryCount++) {
75 LOG(INFO) << "the retry time is: " << retryCount;
76 for (auto item : sdCardStr) {
77 if (MountSdcard(item, mountPoint) == 0) {
78 mountSuccess = true;
79 LOG(INFO) << "mount " << item << " sdcard success!";
80 break;
81 }
82 }
83 if (mountSuccess) {
84 break;
85 }
86 sleep(1); // sleep 1 second to wait for sd card recognition
87 }
88 return mountSuccess;
89 #else
90 return true;
91 #endif
92 }
93
FindAndMountSdcard(UpdaterParams & upParams)94 UpdaterStatus FindAndMountSdcard(UpdaterParams &upParams)
95 {
96 #ifndef UPDATER_UT
97 std::string mountPoint = std::string(SDCARD_PATH);
98 std::vector<std::string> sdcardStr = GetBlockDevicesByMountPoint(mountPoint);
99 if (sdcardStr.empty()) {
100 UPDATER_UI_INSTANCE.ShowLog(
101 (errno == ENOENT) ? TR(LOG_SDCARD_NOTFIND) : TR(LOG_SDCARD_ABNORMAL), true);
102 return UPDATE_ERROR;
103 }
104 if (!DoMountSdCard(sdcardStr, mountPoint, upParams)) {
105 LOG(ERROR) << "mount sdcard fail!";
106 return UPDATE_ERROR;
107 }
108 #endif
109 return UPDATE_SUCCESS;
110 }
111
GetPkgsFromSdcard(UpdaterParams & upParams)112 UpdaterStatus GetPkgsFromSdcard(UpdaterParams &upParams)
113 {
114 if (FindAndMountSdcard(upParams) != UPDATE_SUCCESS) {
115 LOG(ERROR) << "mount sdcard fail!";
116 return UPDATE_ERROR;
117 }
118 if (GetSdcardPkgsPath(upParams) != UPDATE_SUCCESS) {
119 LOG(ERROR) << "there is no package in sdcard/updater, please check";
120 return UPDATE_ERROR;
121 }
122 return UPDATE_SUCCESS;
123 }
124
MountAndGetPkgs(UpdaterParams & upParams)125 __attribute__((weak)) UpdaterStatus MountAndGetPkgs(UpdaterParams &upParams)
126 {
127 return GetPkgsFromSdcard(upParams);
128 }
129
CheckSdcardPkgs(UpdaterParams & upParams)130 UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams)
131 {
132 #ifndef UPDATER_UT
133 auto sdParam = "updater.data.configs";
134 Utils::SetParameter(sdParam, "1");
135 #endif
136 return MountAndGetPkgs(upParams);
137 }
138 } // Updater
139