Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 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> |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 25 | #include <openssl/crypto.h> |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 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" |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 35 | #include "updater/updater_runtime.h" |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 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 | |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 66 | if (argc != 4 && argc != 5) { |
| 67 | LOG(ERROR) << "unexpected number of arguments: " << argc; |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 68 | return EXIT_FAILURE; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 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]; |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 75 | return EXIT_FAILURE; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int fd; |
| 79 | if (!android::base::ParseInt(argv[2], &fd)) { |
| 80 | LOG(ERROR) << "Failed to parse fd in " << argv[2]; |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 81 | return EXIT_FAILURE; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 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]; |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 92 | return EXIT_FAILURE; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 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 | |
Tianjie Xu | 1536db8 | 2019-05-14 10:54:43 -0700 | [diff] [blame] | 106 | Updater updater(std::make_unique<UpdaterRuntime>(sehandle)); |
| 107 | if (!updater.Init(fd, package_name, is_retry)) { |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 108 | return EXIT_FAILURE; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (!updater.RunUpdate()) { |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 112 | return EXIT_FAILURE; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Tianjie Xu | 9769246 | 2019-10-05 16:59:15 -0700 | [diff] [blame] | 115 | return EXIT_SUCCESS; |
Tianjie Xu | 58d5912 | 2019-05-03 01:05:04 -0700 | [diff] [blame] | 116 | } |