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 #include "updaterkits/updaterkits.h"
16 #include <string>
17 #include <sys/reboot.h>
18 #include <sys/syscall.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "misc_info/misc_info.h"
22 #include "parameters.h"
23 #include "securec.h"
24 #include "utils.h"
25
26 using namespace updater;
27
WriteToMiscAndRebootToUpdater(const std::string & miscFile,const struct UpdateMessage & updateMsg)28 static bool WriteToMiscAndRebootToUpdater(const std::string &miscFile,
29 const struct UpdateMessage &updateMsg)
30 {
31 // Write package name to misc, then trigger reboot.
32 const char *bootCmd = "boot_updater";
33 int ret = strncpy_s(const_cast<char*>(updateMsg.command), sizeof(updateMsg.command), bootCmd,
34 sizeof(updateMsg.command) - 1);
35 if (ret != 0) {
36 return false;
37 }
38 #ifndef UPDATER_UT
39 int32_t propertyMaxSize = 92;
40 char updateCmd[propertyMaxSize];
41 void(snprintf_s(updateCmd, propertyMaxSize, propertyMaxSize - 1, "reboot,updater:%s", updateMsg.update));
42 bool bRet = OHOS::system::SetParameter("ohos.startup.powerctrl", updateCmd);
43 if (!bRet) {
44 std::cout << "WriteToMiscAndRebootToUpdater SetParameter failed, errno: " << errno << std::endl;
45 return false;
46 }
47 while (true) {
48 pause();
49 }
50 #else
51 return true;
52 #endif
53 }
54
RebootAndInstallUpgradePackage(const std::string & miscFile,const std::string & packageName)55 bool RebootAndInstallUpgradePackage(const std::string &miscFile, const std::string &packageName)
56 {
57 if (packageName.empty() || miscFile.empty()) {
58 std::cout << "updaterkits: invalid argument. one of arugments is empty\n";
59 return false;
60 }
61
62 // Check if package readalbe.
63 if (access(packageName.c_str(), R_OK) < 0) {
64 std::cout << "updaterkits: " << packageName << " is not readable\n";
65 return false;
66 }
67
68 struct UpdateMessage updateMsg {};
69 if (snprintf_s(updateMsg.update, sizeof(updateMsg.update), sizeof(updateMsg.update) - 1, "--update_package=%s",
70 packageName.c_str()) < 0) {
71 std::cout << "updaterkits: copy updater message failed\n";
72 return false;
73 }
74
75 WriteToMiscAndRebootToUpdater(miscFile, updateMsg);
76
77 // Never get here.
78 return true;
79 }
80
RebootAndCleanUserData(const std::string & miscFile,const std::string & cmd)81 bool RebootAndCleanUserData(const std::string &miscFile, const std::string &cmd)
82 {
83 if (miscFile.empty() || cmd.empty()) {
84 std::cout << "updaterkits: invalid argument. one of arugments is empty\n";
85 return false;
86 }
87
88 // Write package name to misc, then trigger reboot.
89 struct UpdateMessage updateMsg {};
90 if (strncpy_s(updateMsg.update, sizeof(updateMsg.update), cmd.c_str(), cmd.size()) != EOK) {
91 std::cout << "updaterkits: copy updater message failed\n";
92 return false;
93 }
94
95 WriteToMiscAndRebootToUpdater(miscFile, updateMsg);
96
97 // Never get here.
98 return true;
99 }
100
101