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