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
CheckPathNeedMountSD(UpdaterParams & upParams)61 bool CheckPathNeedMountSD(UpdaterParams &upParams)
62 {
63 for (auto pkgPath : upParams.updatePackage) {
64 if (pkgPath.find("/sdcard") != 0) {
65 return false;
66 }
67 }
68 return true;
69 }
70
DoMountSdcard(std::vector<std::string> & sdcardStr,std::string & mountPoint)71 bool DoMountSdcard(std::vector<std::string> &sdcardStr, std::string &mountPoint)
72 {
73 bool mountSuccess = false;
74 unsigned int retryTimes = 20;
75 for (unsigned int retryCount = 1; retryCount <= retryTimes; retryCount++) {
76 LOG(INFO) << "the retry time is: " << retryCount;
77 for (auto item : sdcardStr) {
78 if (MountSdcard(item, mountPoint) == 0) {
79 mountSuccess = true;
80 LOG(INFO) << "mount " << item << " sdcard success!";
81 break;
82 }
83 }
84 if (mountSuccess) {
85 break;
86 }
87 sleep(1); // sleep 1 second to wait for sd card recognition
88 }
89 return mountSuccess;
90 }
91
CheckSdcardPkgs(UpdaterParams & upParams)92 UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams)
93 {
94 #ifndef UPDATER_UT
95 auto sdParam = "updater.data.configs";
96 Utils::SetParameter(sdParam, "1");
97 if (upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV && GetSdcardPkgsFromDev(upParams) == UPDATE_SUCCESS) {
98 LOG(INFO) << "get sd card from dev succeed, skip get package from sd card";
99 return UPDATE_SUCCESS;
100 }
101
102 if (GetSdcardInternalPkgs(upParams) == UPDATE_SUCCESS) {
103 LOG(INFO) << "get sdcard internal pkgs succeed";
104 return UPDATE_SUCCESS;
105 }
106
107 std::string mountPoint = std::string(SDCARD_PATH);
108 std::vector<std::string> sdcardStr = GetBlockDevicesByMountPoint(mountPoint);
109 if (sdcardStr.empty()) {
110 UPDATER_UI_INSTANCE.ShowLog(
111 (errno == ENOENT) ? TR(LOG_SDCARD_NOTFIND) : TR(LOG_SDCARD_ABNORMAL), true);
112 return UPDATE_ERROR;
113 }
114 if (Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) {
115 if (MountForPath("/data") != 0) {
116 LOG(ERROR) << "data partition mount fail";
117 return UPDATE_ERROR;
118 }
119 }
120 if ((Utils::CheckUpdateMode(Updater::SDCARD_MODE) && !Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) ||
121 (Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE) && CheckPathNeedMountSD(upParams))) {
122 if (!DoMountSdcard(sdcardStr, mountPoint)) {
123 LOG(ERROR) << "mount sdcard fail!";
124 return UPDATE_ERROR;
125 }
126 }
127 #endif
128 if (GetSdcardPkgsPath(upParams) != UPDATE_SUCCESS) {
129 LOG(ERROR) << "there is no package in sdcard/updater, please check";
130 return UPDATE_ERROR;
131 }
132 return UPDATE_SUCCESS;
133 }
134
GetSdcardInternalPkgs(UpdaterParams & upParams)135 __attribute__((weak)) UpdaterStatus GetSdcardInternalPkgs(UpdaterParams &upParams)
136 {
137 LOG(INFO) << "not implemented get normal update sdcard pkgs";
138 return UPDATE_ERROR;
139 }
140 } // Updater
141