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 <dlfcn.h>
16 #include <iostream>
17 #include "fs_manager/mount.h"
18 #include "misc_info/misc_info.h"
19 #include "securec.h"
20 #include "updater/updater_const.h"
21 #include "parameter.h"
22 #include "utils.h"
23 #include "utils_fs.h"
24
25 using namespace std;
26 using namespace Updater;
27
28 constexpr const char *HANDLE_MISC_LIB = "libupdater_handle_misc.z.so";
29 constexpr const char *NOTIFY_MISC_INFO = "NotifyWriteMiscInfo";
30 constexpr const char *HANDLE_MISC_LIB_PATH = "/system/lib64/libupdater_handle_misc.z.so";
31
PrintPrompts()32 static void PrintPrompts()
33 {
34 cout << "Please input correct command, examples :" << endl;
35 cout << "updater : write_updater updater /data/updater/updater.zip" << endl;
36 cout << "factory_reset : write_updater user_factory_reset" << endl;
37 cout << "sdcard_update : write_updater sdcard_update" << endl;
38 cout << "clear command : write_updater clear" << endl;
39 cout << "updater_para : write_updater updater_para" << endl;
40 }
41
ExceptionUpdater(int argc,char ** argv,UpdateMessage & boot)42 static int ExceptionUpdater(int argc, char **argv, UpdateMessage &boot)
43 {
44 if (argc < BINARY_MAX_ARGS) {
45 cout << "Please input correct updater command!" << endl;
46 return -1;
47 }
48 if (argv[WRITE_SECOND_CMD] != nullptr) {
49 if (snprintf_s(boot.update, sizeof(boot.update), sizeof(boot.update) - 1, "--update_package=%s",
50 argv[WRITE_SECOND_CMD]) == -1) {
51 cout << "WriteUpdaterMessage snprintf_s failed!" << endl;
52 return -1;
53 }
54 }
55 return 0;
56 }
57
WriteUpdaterPara(int argc,UpdaterPara & para)58 static int WriteUpdaterPara(int argc, UpdaterPara ¶)
59 {
60 if (argc != 2) { // 2 : Not 2 is an invalid input
61 cout << "please input correct updater command!" << endl;
62 return -1;
63 }
64 int res = GetParameter("persist.global.locale", "", para.language, MAX_PARA_SIZE);
65 if (res <= 0) {
66 cout << "Get persist.global.locale parameter failed" << endl;
67 res = GetParameter("const.global.locale", "", para.language, MAX_PARA_SIZE);
68 if (res <= 0) {
69 cout << "Get const.global.locale parameter failed" << endl;
70 return -1;
71 }
72 }
73 if (!WriteUpdaterParaMisc(para)) {
74 cout << "WriteUpdaterParaMisc failed!" << endl;
75 return -1;
76 }
77 return 0;
78 }
79
HandleMiscInfo(int argc,char ** argv)80 static void HandleMiscInfo(int argc, char **argv)
81 {
82 if (!Utils::IsFileExist(HANDLE_MISC_LIB_PATH)) {
83 return;
84 }
85 auto handle = Utils::LoadLibrary(HANDLE_MISC_LIB);
86 if (handle == nullptr) {
87 cout << "load libupdater_handle_misc fail";
88 return;
89 }
90 auto getFunc = (void(*)(int, char **))Utils::GetFunction(handle, NOTIFY_MISC_INFO);
91 if (getFunc == nullptr) {
92 cout << "getFunc is nullptr";
93 Utils::CloseLibrary(handle);
94 return;
95 }
96 getFunc(argc, argv);
97 Utils::CloseLibrary(handle);
98 }
99
main(int argc,char ** argv)100 int main(int argc, char **argv)
101 {
102 if (argc == 1) {
103 PrintPrompts();
104 return -1;
105 }
106
107 const std::string miscFile = "/dev/block/by-name/misc";
108 struct UpdateMessage boot {};
109 struct UpdaterPara para {};
110 if (strcmp(argv[1], "updater") == 0) {
111 if (ExceptionUpdater(argc, argv, boot) == -1) {
112 return -1;
113 }
114 } else if (strcmp(argv[1], "user_factory_reset") == 0) {
115 if (strncpy_s(boot.update, sizeof(boot.update), "--user_wipe_data", sizeof(boot.update) - 1) != 0) {
116 cout << "strncpy_s failed!" << endl;
117 return -1;
118 }
119 } else if (strcmp(argv[1], "boot_flash") == 0) {
120 if (strncpy_s(boot.update, sizeof(boot.update), "boot_flash", sizeof(boot.update) - 1) != 0) {
121 cout << "strncpy_s failed!" << endl;
122 return -1;
123 }
124 } else if (strcmp(argv[1], "clear") == 0) {
125 cout << "clear misc" << endl;
126 } else if (strcmp(argv[1], "sdcard_update") == 0) {
127 if (strncpy_s(boot.update, sizeof(boot.update), "--sdcard_update", sizeof(boot.update) - 1) != 0) {
128 cout << "strncpy_s failed!" << endl;
129 return -1;
130 }
131 } else if (strcmp(argv[1], "updater_para") == 0) {
132 if (WriteUpdaterPara(argc, para) != 0) {
133 return -1;
134 }
135 return 0;
136 } else {
137 cout << "Please input correct command!" << endl;
138 return -1;
139 }
140 bool ret = WriteUpdaterMessage(miscFile, boot);
141 if (!ret) {
142 cout << "WriteUpdaterMessage failed!" << endl;
143 return -1;
144 }
145 HandleMiscInfo(argc, argv);
146 return 0;
147 }
148