xref: /base/update/updater/utils/write_updater.cpp (revision fb299fa2)
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#include "parameter.h"
21
22using namespace std;
23using namespace Updater;
24
25static void PrintPrompts()
26{
27    cout << "Please input correct command, examples :" << endl;
28    cout << "updater       :  write_updater updater /data/updater/updater.zip" << endl;
29    cout << "factory_reset :  write_updater user_factory_reset" << endl;
30    cout << "sdcard_update :  write_updater sdcard_update" << endl;
31    cout << "clear command :  write_updater clear" << endl;
32    cout << "updater_para  : write_updater updater_para" << endl;
33}
34
35static int ExceptionUpdater(int argc, char **argv, UpdateMessage &boot)
36{
37    if (argc < BINARY_MAX_ARGS) {
38        cout << "Please input correct updater command!" << endl;
39        return -1;
40    }
41    if (argv[WRITE_SECOND_CMD] != nullptr) {
42        if (snprintf_s(boot.update, sizeof(boot.update), sizeof(boot.update) - 1, "--update_package=%s",
43            argv[WRITE_SECOND_CMD]) == -1) {
44            cout << "WriteUpdaterMessage snprintf_s failed!" << endl;
45            return -1;
46        }
47    }
48    return 0;
49}
50
51static int WriteUpdaterPara(int argc, UpdaterPara &para)
52{
53    if (argc != 2) { // 2 : Not 2 is an invalid input
54        cout << "please input correct updater command!" << endl;
55        return -1;
56    }
57    int res = GetParameter("persist.global.language", "", para.language, MAX_PARA_SIZE);
58    if (res <= 0) {
59        cout << "Get language parameter failed" << endl;
60        return -1;
61    }
62    if (!WriteUpdaterParaMisc(para)) {
63        cout << "WriteUpdaterParaMisc failed!" << endl;
64        return -1;
65    }
66    return 0;
67}
68
69int main(int argc, char **argv)
70{
71    if (argc == 1) {
72        PrintPrompts();
73        return -1;
74    }
75
76    const std::string miscFile = "/dev/block/by-name/misc";
77    struct UpdateMessage boot {};
78    struct UpdaterPara para {};
79    if (strcmp(argv[1], "updater") == 0) {
80        if (ExceptionUpdater(argc, argv, boot) == -1) {
81            return -1;
82        }
83    } else if (strcmp(argv[1], "user_factory_reset") == 0) {
84        if (strncpy_s(boot.update, sizeof(boot.update), "--user_wipe_data", sizeof(boot.update) - 1) != 0) {
85            cout << "strncpy_s failed!" << endl;
86            return -1;
87        }
88    } else if (strcmp(argv[1], "boot_flash") == 0) {
89        if (strncpy_s(boot.update, sizeof(boot.update), "boot_flash", sizeof(boot.update) - 1) != 0) {
90            cout << "strncpy_s failed!" << endl;
91            return -1;
92        }
93    } else if (strcmp(argv[1], "clear") == 0) {
94        cout << "clear misc" << endl;
95    } else if (strcmp(argv[1], "sdcard_update") == 0) {
96        if (strncpy_s(boot.update, sizeof(boot.update), "--sdcard_update", sizeof(boot.update) - 1) != 0) {
97            cout << "strncpy_s failed!" << endl;
98            return -1;
99        }
100    } else if (strcmp(argv[1], "updater_para") == 0) {
101        if (WriteUpdaterPara(argc, para) != 0) {
102            return -1;
103        }
104        return 0;
105    } else {
106        cout << "Please input correct command!" << endl;
107        return -1;
108    }
109    bool ret = WriteUpdaterMessage(miscFile, boot);
110    if (!ret) {
111        cout << "WriteUpdaterMessage failed!" << endl;
112        return -1;
113    }
114    return 0;
115}
116