1 /*
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>
25 #include <openssl/crypto.h>
26 #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"
35 #include "updater/updater_runtime.h"
36
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
UpdaterLogger(android::base::LogId,android::base::LogSeverity,const char *,const char *,unsigned int,const char * message)42 static 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
main(int argc,char ** argv)48 int 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
60 // 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
66 if (argc != 4 && argc != 5) {
67 LOG(ERROR) << "unexpected number of arguments: " << argc;
68 return EXIT_FAILURE;
69 }
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];
75 return EXIT_FAILURE;
76 }
77
78 int fd;
79 if (!android::base::ParseInt(argv[2], &fd)) {
80 LOG(ERROR) << "Failed to parse fd in " << argv[2];
81 return EXIT_FAILURE;
82 }
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];
92 return EXIT_FAILURE;
93 }
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
106 Updater updater(std::make_unique<UpdaterRuntime>(sehandle));
107 if (!updater.Init(fd, package_name, is_retry)) {
108 return EXIT_FAILURE;
109 }
110
111 if (!updater.RunUpdate()) {
112 return EXIT_FAILURE;
113 }
114
115 return EXIT_SUCCESS;
116 }