blob: 33d5b5b475e85496f6c319fe93e581bba4f0c424 [file] [log] [blame]
Tianjie Xu58d59122019-05-03 01:05:04 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <string.h>
19#include <unistd.h>
20
21#include <string>
22
23#include <android-base/logging.h>
24#include <android-base/parseint.h>
Tianjie Xu97692462019-10-05 16:59:15 -070025#include <openssl/crypto.h>
Tianjie Xu58d59122019-05-03 01:05:04 -070026#include <selinux/android.h>
27#include <selinux/label.h>
28#include <selinux/selinux.h>
29
30#include "edify/expr.h"
31#include "updater/blockimg.h"
32#include "updater/dynamic_partitions.h"
33#include "updater/install.h"
34#include "updater/updater.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070035#include "updater/updater_runtime.h"
Tianjie Xu58d59122019-05-03 01:05:04 -070036
37// Generated by the makefile, this function defines the
38// RegisterDeviceExtensions() function, which calls all the
39// registration functions for device-specific extensions.
40#include "register.inc"
41
42static void UpdaterLogger(android::base::LogId /* id */, android::base::LogSeverity /* severity */,
43 const char* /* tag */, const char* /* file */, unsigned int /* line */,
44 const char* message) {
45 fprintf(stdout, "%s\n", message);
46}
47
48int main(int argc, char** argv) {
49 // Various things log information to stdout or stderr more or less
50 // at random (though we've tried to standardize on stdout). The
51 // log file makes more sense if buffering is turned off so things
52 // appear in the right order.
53 setbuf(stdout, nullptr);
54 setbuf(stderr, nullptr);
55
56 // We don't have logcat yet under recovery. Update logs will always be written to stdout
57 // (which is redirected to recovery.log).
58 android::base::InitLogging(argv, &UpdaterLogger);
59
Tianjie Xu97692462019-10-05 16:59:15 -070060 // Run the libcrypto KAT(known answer tests) based self tests.
61 if (BORINGSSL_self_test() != 1) {
62 LOG(ERROR) << "Failed to run the boringssl self tests";
63 return EXIT_FAILURE;
64 }
65
Tianjie Xu58d59122019-05-03 01:05:04 -070066 if (argc != 4 && argc != 5) {
67 LOG(ERROR) << "unexpected number of arguments: " << argc;
Tianjie Xu97692462019-10-05 16:59:15 -070068 return EXIT_FAILURE;
Tianjie Xu58d59122019-05-03 01:05:04 -070069 }
70
71 char* version = argv[1];
72 if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || version[1] != '\0') {
73 // We support version 1, 2, or 3.
74 LOG(ERROR) << "wrong updater binary API; expected 1, 2, or 3; got " << argv[1];
Tianjie Xu97692462019-10-05 16:59:15 -070075 return EXIT_FAILURE;
Tianjie Xu58d59122019-05-03 01:05:04 -070076 }
77
78 int fd;
79 if (!android::base::ParseInt(argv[2], &fd)) {
80 LOG(ERROR) << "Failed to parse fd in " << argv[2];
Tianjie Xu97692462019-10-05 16:59:15 -070081 return EXIT_FAILURE;
Tianjie Xu58d59122019-05-03 01:05:04 -070082 }
83
84 std::string package_name = argv[3];
85
86 bool is_retry = false;
87 if (argc == 5) {
88 if (strcmp(argv[4], "retry") == 0) {
89 is_retry = true;
90 } else {
91 LOG(ERROR) << "unexpected argument: " << argv[4];
Tianjie Xu97692462019-10-05 16:59:15 -070092 return EXIT_FAILURE;
Tianjie Xu58d59122019-05-03 01:05:04 -070093 }
94 }
95
96 // Configure edify's functions.
97 RegisterBuiltins();
98 RegisterInstallFunctions();
99 RegisterBlockImageFunctions();
100 RegisterDynamicPartitionsFunctions();
101 RegisterDeviceExtensions();
102
103 auto sehandle = selinux_android_file_context_handle();
104 selinux_android_set_sehandle(sehandle);
105
Tianjie Xu1536db82019-05-14 10:54:43 -0700106 Updater updater(std::make_unique<UpdaterRuntime>(sehandle));
107 if (!updater.Init(fd, package_name, is_retry)) {
Tianjie Xu97692462019-10-05 16:59:15 -0700108 return EXIT_FAILURE;
Tianjie Xu58d59122019-05-03 01:05:04 -0700109 }
110
111 if (!updater.RunUpdate()) {
Tianjie Xu97692462019-10-05 16:59:15 -0700112 return EXIT_FAILURE;
Tianjie Xu58d59122019-05-03 01:05:04 -0700113 }
114
Tianjie Xu97692462019-10-05 16:59:15 -0700115 return EXIT_SUCCESS;
Tianjie Xu58d59122019-05-03 01:05:04 -0700116}