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