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 <iostream>
16 #include "fs_manager/mount.h"
17 #include "misc_info/misc_info.h"
18 #include "securec.h"
19 #include "updater/updater_const.h"
20
21 using namespace std;
22 using namespace Updater;
23
PrintPrompts()24 static void PrintPrompts()
25 {
26 cout << "Please input correct command, examples :" << endl;
27 cout << "updater : write_updater updater /data/updater/updater.zip" << endl;
28 cout << "factory_reset : write_updater user_factory_reset" << endl;
29 cout << "sdcard_update : write_updater sdcard_update" << endl;
30 cout << "clear command : write_updater clear" << endl;
31 }
32
main(int argc,char ** argv)33 int main(int argc, char **argv)
34 {
35 if (argc == 1) {
36 PrintPrompts();
37 return -1;
38 }
39
40 const std::string miscFile = "/dev/block/by-name/misc";
41 struct UpdateMessage boot {};
42 if (strcmp(argv[1], "updater") == 0) {
43 if (argc < BINARY_MAX_ARGS) {
44 cout << "Please input correct updater command!" << endl;
45 return -1;
46 }
47 if (argv[WRITE_SECOND_CMD] != nullptr) {
48 if (snprintf_s(boot.update, sizeof(boot.update), sizeof(boot.update) - 1, "--update_package=%s",
49 argv[WRITE_SECOND_CMD]) == -1) {
50 cout << "WriteUpdaterMessage snprintf_s failed!" << endl;
51 return -1;
52 }
53 }
54 } else if (strcmp(argv[1], "user_factory_reset") == 0) {
55 if (strncpy_s(boot.update, sizeof(boot.update), "--user_wipe_data", sizeof(boot.update) - 1) != 0) {
56 cout << "strncpy_s failed!" << endl;
57 return -1;
58 }
59 } else if (strcmp(argv[1], "boot_flash") == 0) {
60 if (strncpy_s(boot.update, sizeof(boot.update), "boot_flash", sizeof(boot.update) - 1) != 0) {
61 cout << "strncpy_s failed!" << endl;
62 return -1;
63 }
64 } else if (strcmp(argv[1], "clear") == 0) {
65 cout << "clear misc" << endl;
66 } else if (strcmp(argv[1], "sdcard_update") == 0) {
67 if (strncpy_s(boot.update, sizeof(boot.update), "--sdcard_update", sizeof(boot.update) - 1) != 0) {
68 cout << "strncpy_s failed!" << endl;
69 return -1;
70 }
71 } else {
72 cout << "Please input correct command!" << endl;
73 return -1;
74 }
75 bool ret = WriteUpdaterMessage(miscFile, boot);
76 if (!ret) {
77 cout << "WriteUpdaterMessage failed!" << endl;
78 return -1;
79 }
80 return 0;
81 }
82