blob: 055a8ac76c94ac56d1bfd44a7b87d66b4bf6a05d [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>
25#include <selinux/android.h>
26#include <selinux/label.h>
27#include <selinux/selinux.h>
28
29#include "edify/expr.h"
30#include "updater/blockimg.h"
31#include "updater/dynamic_partitions.h"
32#include "updater/install.h"
33#include "updater/updater.h"
Tianjie Xu1536db82019-05-14 10:54:43 -070034#include "updater/updater_runtime.h"
Tianjie Xu58d59122019-05-03 01:05:04 -070035
36// Generated by the makefile, this function defines the
37// RegisterDeviceExtensions() function, which calls all the
38// registration functions for device-specific extensions.
39#include "register.inc"
40
41static void UpdaterLogger(android::base::LogId /* id */, android::base::LogSeverity /* severity */,
42 const char* /* tag */, const char* /* file */, unsigned int /* line */,
43 const char* message) {
44 fprintf(stdout, "%s\n", message);
45}
46
47int main(int argc, char** argv) {
48 // Various things log information to stdout or stderr more or less
49 // at random (though we've tried to standardize on stdout). The
50 // log file makes more sense if buffering is turned off so things
51 // appear in the right order.
52 setbuf(stdout, nullptr);
53 setbuf(stderr, nullptr);
54
55 // We don't have logcat yet under recovery. Update logs will always be written to stdout
56 // (which is redirected to recovery.log).
57 android::base::InitLogging(argv, &UpdaterLogger);
58
59 if (argc != 4 && argc != 5) {
60 LOG(ERROR) << "unexpected number of arguments: " << argc;
61 return 1;
62 }
63
64 char* version = argv[1];
65 if ((version[0] != '1' && version[0] != '2' && version[0] != '3') || version[1] != '\0') {
66 // We support version 1, 2, or 3.
67 LOG(ERROR) << "wrong updater binary API; expected 1, 2, or 3; got " << argv[1];
68 return 1;
69 }
70
71 int fd;
72 if (!android::base::ParseInt(argv[2], &fd)) {
73 LOG(ERROR) << "Failed to parse fd in " << argv[2];
74 return 1;
75 }
76
77 std::string package_name = argv[3];
78
79 bool is_retry = false;
80 if (argc == 5) {
81 if (strcmp(argv[4], "retry") == 0) {
82 is_retry = true;
83 } else {
84 LOG(ERROR) << "unexpected argument: " << argv[4];
85 return 1;
86 }
87 }
88
89 // Configure edify's functions.
90 RegisterBuiltins();
91 RegisterInstallFunctions();
92 RegisterBlockImageFunctions();
93 RegisterDynamicPartitionsFunctions();
94 RegisterDeviceExtensions();
95
96 auto sehandle = selinux_android_file_context_handle();
97 selinux_android_set_sehandle(sehandle);
98
Tianjie Xu1536db82019-05-14 10:54:43 -070099 Updater updater(std::make_unique<UpdaterRuntime>(sehandle));
100 if (!updater.Init(fd, package_name, is_retry)) {
Tianjie Xu58d59122019-05-03 01:05:04 -0700101 return 1;
102 }
103
104 if (!updater.RunUpdate()) {
105 return 1;
106 }
107
108 return 0;
109}