Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 "commands.h" |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/un.h> |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/parseint.h> |
| 24 | #include <android-base/properties.h> |
| 25 | #include <android-base/stringprintf.h> |
| 26 | #include <android-base/strings.h> |
| 27 | #include <android-base/unique_fd.h> |
| 28 | #include <cutils/android_reboot.h> |
| 29 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame^] | 30 | #include "constants.h" |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 31 | #include "fastboot_device.h" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame^] | 32 | #include "utility.h" |
| 33 | |
| 34 | using ::android::hardware::hidl_string; |
| 35 | using ::android::hardware::boot::V1_0::BoolResult; |
| 36 | using ::android::hardware::boot::V1_0::CommandResult; |
| 37 | using ::android::hardware::boot::V1_0::Slot; |
| 38 | |
| 39 | bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 40 | using VariableHandler = |
| 41 | std::function<std::string(FastbootDevice*, const std::vector<std::string>&)>; |
| 42 | const std::unordered_map<std::string, VariableHandler> kVariableMap = { |
| 43 | {FB_VAR_VERSION, GetVersion}, |
| 44 | {FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion}, |
| 45 | {FB_VAR_VERSION_BASEBAND, GetBasebandVersion}, |
| 46 | {FB_VAR_PRODUCT, GetProduct}, |
| 47 | {FB_VAR_SERIALNO, GetSerial}, |
| 48 | {FB_VAR_SECURE, GetSecure}, |
| 49 | {FB_VAR_UNLOCKED, GetUnlocked}, |
| 50 | {FB_VAR_MAX_DOWNLOAD_SIZE, GetMaxDownloadSize}, |
| 51 | {FB_VAR_CURRENT_SLOT, ::GetCurrentSlot}, |
| 52 | {FB_VAR_SLOT_COUNT, GetSlotCount}, |
| 53 | {FB_VAR_HAS_SLOT, GetHasSlot}, |
| 54 | {FB_VAR_SLOT_SUCCESSFUL, GetSlotSuccessful}, |
| 55 | {FB_VAR_SLOT_UNBOOTABLE, GetSlotUnbootable}}; |
| 56 | |
| 57 | // args[0] is command name, args[1] is variable. |
| 58 | auto found_variable = kVariableMap.find(args[1]); |
| 59 | if (found_variable == kVariableMap.end()) { |
| 60 | return device->WriteStatus(FastbootResult::FAIL, "Unknown variable"); |
| 61 | } |
| 62 | |
| 63 | std::vector<std::string> getvar_args(args.begin() + 2, args.end()); |
| 64 | auto result = found_variable->second(device, getvar_args); |
| 65 | return device->WriteStatus(FastbootResult::OKAY, result); |
| 66 | } |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 67 | |
| 68 | bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 69 | if (args.size() < 2) { |
| 70 | return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified"); |
| 71 | } |
| 72 | // arg[0] is the command name, arg[1] contains size of data to be downloaded |
| 73 | unsigned int size; |
| 74 | if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) { |
| 75 | return device->WriteStatus(FastbootResult::FAIL, "Invalid size"); |
| 76 | } |
| 77 | device->get_download_data().resize(size); |
| 78 | if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (device->HandleData(true, &device->get_download_data())) { |
| 83 | return device->WriteStatus(FastbootResult::OKAY, ""); |
| 84 | } |
| 85 | |
| 86 | PLOG(ERROR) << "Couldn't download data"; |
| 87 | return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data"); |
| 88 | } |
| 89 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame^] | 90 | bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) { |
| 91 | if (args.size() < 2) { |
| 92 | return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument"); |
| 93 | } |
| 94 | |
| 95 | // Slot suffix needs to be between 'a' and 'z'. |
| 96 | Slot slot; |
| 97 | if (!GetSlotNumber(args[1], &slot)) { |
| 98 | return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix"); |
| 99 | } |
| 100 | |
| 101 | // Non-A/B devices will not have a boot control HAL. |
| 102 | auto boot_control_hal = device->boot_control_hal(); |
| 103 | if (!boot_control_hal) { |
| 104 | return device->WriteStatus(FastbootResult::FAIL, |
| 105 | "Cannot set slot: boot control HAL absent"); |
| 106 | } |
| 107 | if (slot >= boot_control_hal->getNumberSlots()) { |
| 108 | return device->WriteStatus(FastbootResult::FAIL, "Slot out of range"); |
| 109 | } |
| 110 | CommandResult ret; |
| 111 | auto cb = [&ret](CommandResult result) { ret = result; }; |
| 112 | auto result = boot_control_hal->setActiveBootSlot(slot, cb); |
| 113 | if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, ""); |
| 114 | return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot"); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 118 | auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down"); |
| 119 | android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot"); |
| 120 | device->CloseDevice(); |
| 121 | TEMP_FAILURE_RETRY(pause()); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 126 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting"); |
| 127 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot"); |
| 128 | device->CloseDevice(); |
| 129 | TEMP_FAILURE_RETRY(pause()); |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 134 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader"); |
| 135 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader"); |
| 136 | device->CloseDevice(); |
| 137 | TEMP_FAILURE_RETRY(pause()); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 142 | auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot"); |
| 143 | android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot"); |
| 144 | device->CloseDevice(); |
| 145 | TEMP_FAILURE_RETRY(pause()); |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | static bool EnterRecovery() { |
| 150 | const char msg_switch_to_recovery = 'r'; |
| 151 | |
| 152 | android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0)); |
| 153 | if (sock < 0) { |
| 154 | PLOG(ERROR) << "Couldn't create sock"; |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | struct sockaddr_un addr = {.sun_family = AF_UNIX}; |
| 159 | strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1); |
| 160 | if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { |
| 161 | PLOG(ERROR) << "Couldn't connect to recovery"; |
| 162 | return false; |
| 163 | } |
| 164 | // Switch to recovery will not update the boot reason since it does not |
| 165 | // require a reboot. |
| 166 | auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery)); |
| 167 | if (ret != sizeof(msg_switch_to_recovery)) { |
| 168 | PLOG(ERROR) << "Couldn't write message to switch to recovery"; |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) { |
| 176 | auto status = true; |
| 177 | if (EnterRecovery()) { |
| 178 | status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery"); |
| 179 | } else { |
| 180 | status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery"); |
| 181 | } |
| 182 | device->CloseDevice(); |
| 183 | TEMP_FAILURE_RETRY(pause()); |
| 184 | return status; |
| 185 | } |