nijel8 | 2c821a8 | 2018-12-29 04:56:41 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <signal.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <poll.h> |
| 26 | |
| 27 | #include <cutils/properties.h> |
| 28 | #include <sys/select.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/un.h> |
| 32 | |
| 33 | #include "android/os/IVold.h" |
| 34 | |
| 35 | #include <binder/IServiceManager.h> |
| 36 | #include <binder/Status.h> |
| 37 | |
| 38 | #include <private/android_filesystem_config.h> |
| 39 | |
| 40 | static void usage(); |
| 41 | |
| 42 | static android::sp<android::IBinder> getServiceAggressive() { |
| 43 | static char prop_value[PROPERTY_VALUE_MAX]; |
| 44 | property_get("init.svc.sys_vold", prop_value, "error"); |
| 45 | if (strncmp(prop_value, "running", strlen("running")) != 0) { |
| 46 | printf("vdc_pie: vold is not running, init.svc.sys_vold=%s\n", prop_value); |
| 47 | exit(EINVAL); |
| 48 | } |
| 49 | |
| 50 | android::sp<android::IBinder> res; |
| 51 | auto sm = android::defaultServiceManager(); |
| 52 | auto name = android::String16("vold"); |
| 53 | for (int i = 0; i < 5000; i++) { |
| 54 | res = sm->checkService(name); |
| 55 | if (res) { |
| 56 | printf("vdc_pie: Got vold, waited %d ms\n", (i * 10)); |
| 57 | break; |
| 58 | } |
| 59 | usleep(10000); // 10ms |
| 60 | } |
| 61 | return res; |
| 62 | } |
| 63 | |
| 64 | static int checkStatus(android::binder::Status status) { |
| 65 | if (status.isOk()) return 0; |
| 66 | std::string ret = status.toString8().string(); |
| 67 | #ifdef TWRP_INCLUDE_LOGCAT |
| 68 | printf("vdc_pie: Decryption failed, vold service returned: %s," |
| 69 | " see logcat for details\n", ret.c_str()); |
| 70 | #else |
| 71 | printf("vdc_pie: Decryption failed, vold service returned: %s\n", ret.c_str()); |
| 72 | #endif |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | int main(int argc, char** argv) { |
| 77 | std::vector<std::string> args(argv + 1, argv + argc); |
| 78 | |
| 79 | if (args.size() > 0 && args[0] == "--wait") { |
| 80 | // Just ignore the --wait flag |
| 81 | args.erase(args.begin()); |
| 82 | } |
| 83 | |
| 84 | if (args.size() < 2) { |
| 85 | usage(); |
| 86 | exit(5); |
| 87 | } |
| 88 | android::sp<android::IBinder> binder = getServiceAggressive(); |
| 89 | if (!binder) { |
| 90 | printf("vdc_pie: Failed to obtain vold Binder\n"); |
| 91 | exit(EINVAL); |
| 92 | } |
| 93 | auto vold = android::interface_cast<android::os::IVold>(binder); |
| 94 | |
| 95 | if (args[0] == "cryptfs" && args[1] == "checkpw" && args.size() == 3) { |
| 96 | return checkStatus(vold->fdeCheckPassword(args[2])); |
| 97 | } else { |
| 98 | usage(); |
| 99 | exit(EINVAL); |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static void usage() { |
| 105 | printf("vdc_pie: Usage: vold_pie cryptfs checkpw <password>\n"); |
| 106 | } |