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 "fastboot_device.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/strings.h> |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 21 | #include <android/hardware/boot/1.0/IBootControl.h> |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 22 | #include <android/hardware/fastboot/1.0/IFastboot.h> |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 23 | #include <healthhalutils/HealthHalUtils.h> |
| 24 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | |
| 27 | #include "constants.h" |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 28 | #include "flashing.h" |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 29 | #include "usb_client.h" |
| 30 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 31 | using ::android::hardware::hidl_string; |
| 32 | using ::android::hardware::boot::V1_0::IBootControl; |
| 33 | using ::android::hardware::boot::V1_0::Slot; |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 34 | using ::android::hardware::fastboot::V1_0::IFastboot; |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 35 | using ::android::hardware::health::V2_0::get_health_service; |
| 36 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 37 | namespace sph = std::placeholders; |
| 38 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 39 | FastbootDevice::FastbootDevice() |
| 40 | : kCommandMap({ |
| 41 | {FB_CMD_SET_ACTIVE, SetActiveHandler}, |
| 42 | {FB_CMD_DOWNLOAD, DownloadHandler}, |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 43 | {FB_CMD_GETVAR, GetVarHandler}, |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 44 | {FB_CMD_SHUTDOWN, ShutDownHandler}, |
| 45 | {FB_CMD_REBOOT, RebootHandler}, |
| 46 | {FB_CMD_REBOOT_BOOTLOADER, RebootBootloaderHandler}, |
| 47 | {FB_CMD_REBOOT_FASTBOOT, RebootFastbootHandler}, |
| 48 | {FB_CMD_REBOOT_RECOVERY, RebootRecoveryHandler}, |
David Anderson | 12211d1 | 2018-07-24 15:21:20 -0700 | [diff] [blame] | 49 | {FB_CMD_ERASE, EraseHandler}, |
| 50 | {FB_CMD_FLASH, FlashHandler}, |
David Anderson | 0d4277d | 2018-07-31 13:27:37 -0700 | [diff] [blame] | 51 | {FB_CMD_CREATE_PARTITION, CreatePartitionHandler}, |
| 52 | {FB_CMD_DELETE_PARTITION, DeletePartitionHandler}, |
| 53 | {FB_CMD_RESIZE_PARTITION, ResizePartitionHandler}, |
David Anderson | 38b3c7a | 2018-08-15 16:27:42 -0700 | [diff] [blame] | 54 | {FB_CMD_UPDATE_SUPER, UpdateSuperHandler}, |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 55 | {FB_CMD_OEM, OemCmdHandler}, |
David Anderson | 1d504e3 | 2019-01-15 14:38:20 -0800 | [diff] [blame] | 56 | {FB_CMD_GSI, GsiHandler}, |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 57 | }), |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 58 | transport_(std::make_unique<ClientUsbTransport>()), |
Hridya Valsaraju | bf9f8d1 | 2018-09-05 16:57:24 -0700 | [diff] [blame] | 59 | boot_control_hal_(IBootControl::getService()), |
Hridya Valsaraju | 47658ca | 2018-09-28 11:41:22 -0700 | [diff] [blame] | 60 | health_hal_(get_health_service()), |
Hridya Valsaraju | 20bdf89 | 2018-10-10 11:02:19 -0700 | [diff] [blame] | 61 | fastboot_hal_(IFastboot::getService()), |
| 62 | active_slot_("") {} |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 63 | |
| 64 | FastbootDevice::~FastbootDevice() { |
| 65 | CloseDevice(); |
| 66 | } |
| 67 | |
| 68 | void FastbootDevice::CloseDevice() { |
| 69 | transport_->Close(); |
| 70 | } |
| 71 | |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 72 | std::string FastbootDevice::GetCurrentSlot() { |
Hridya Valsaraju | 20bdf89 | 2018-10-10 11:02:19 -0700 | [diff] [blame] | 73 | // Check if a set_active ccommand was issued earlier since the boot control HAL |
| 74 | // returns the slot that is currently booted into. |
| 75 | if (!active_slot_.empty()) { |
| 76 | return active_slot_; |
| 77 | } |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 78 | // Non-A/B devices must not have boot control HALs. |
| 79 | if (!boot_control_hal_) { |
| 80 | return ""; |
| 81 | } |
| 82 | std::string suffix; |
| 83 | auto cb = [&suffix](hidl_string s) { suffix = s; }; |
| 84 | boot_control_hal_->getSuffix(boot_control_hal_->getCurrentSlot(), cb); |
| 85 | return suffix; |
| 86 | } |
| 87 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 88 | bool FastbootDevice::WriteStatus(FastbootResult result, const std::string& message) { |
| 89 | constexpr size_t kResponseReasonSize = 4; |
| 90 | constexpr size_t kNumResponseTypes = 4; // "FAIL", "OKAY", "INFO", "DATA" |
Hridya Valsaraju | 31d2c26 | 2018-07-20 13:35:50 -0700 | [diff] [blame] | 91 | |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 92 | char buf[FB_RESPONSE_SZ]; |
| 93 | constexpr size_t kMaxMessageSize = sizeof(buf) - kResponseReasonSize; |
| 94 | size_t msg_len = std::min(kMaxMessageSize, message.size()); |
| 95 | |
| 96 | constexpr const char* kResultStrings[kNumResponseTypes] = {RESPONSE_OKAY, RESPONSE_FAIL, |
| 97 | RESPONSE_INFO, RESPONSE_DATA}; |
| 98 | |
| 99 | if (static_cast<size_t>(result) >= kNumResponseTypes) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | memcpy(buf, kResultStrings[static_cast<size_t>(result)], kResponseReasonSize); |
| 104 | memcpy(buf + kResponseReasonSize, message.c_str(), msg_len); |
| 105 | |
| 106 | size_t response_len = kResponseReasonSize + msg_len; |
| 107 | auto write_ret = this->get_transport()->Write(buf, response_len); |
| 108 | if (write_ret != static_cast<ssize_t>(response_len)) { |
| 109 | PLOG(ERROR) << "Failed to write " << message; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool FastbootDevice::HandleData(bool read, std::vector<char>* data) { |
| 117 | auto read_write_data_size = read ? this->get_transport()->Read(data->data(), data->size()) |
| 118 | : this->get_transport()->Write(data->data(), data->size()); |
| 119 | if (read_write_data_size == -1 || static_cast<size_t>(read_write_data_size) != data->size()) { |
| 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | void FastbootDevice::ExecuteCommands() { |
| 126 | char command[FB_RESPONSE_SZ + 1]; |
| 127 | for (;;) { |
| 128 | auto bytes_read = transport_->Read(command, FB_RESPONSE_SZ); |
| 129 | if (bytes_read == -1) { |
| 130 | PLOG(ERROR) << "Couldn't read command"; |
| 131 | return; |
| 132 | } |
| 133 | command[bytes_read] = '\0'; |
| 134 | |
| 135 | LOG(INFO) << "Fastboot command: " << command; |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 136 | |
| 137 | std::vector<std::string> args; |
| 138 | std::string cmd_name; |
| 139 | if (android::base::StartsWith(command, "oem ")) { |
| 140 | args = {command}; |
Mark Salyzyn | 8e7e9cb | 2018-08-29 10:44:33 -0700 | [diff] [blame] | 141 | cmd_name = FB_CMD_OEM; |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 142 | } else { |
| 143 | args = android::base::Split(command, ":"); |
| 144 | cmd_name = args[0]; |
| 145 | } |
| 146 | |
| 147 | auto found_command = kCommandMap.find(cmd_name); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 148 | if (found_command == kCommandMap.end()) { |
Hridya Valsaraju | a15fe31 | 2018-09-14 13:58:21 -0700 | [diff] [blame] | 149 | WriteStatus(FastbootResult::FAIL, "Unrecognized command " + args[0]); |
Hridya Valsaraju | dea91b4 | 2018-07-17 11:14:01 -0700 | [diff] [blame] | 150 | continue; |
| 151 | } |
| 152 | if (!found_command->second(this, args)) { |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | } |
David Anderson | 856b7ec | 2018-08-08 17:58:56 -0700 | [diff] [blame] | 157 | |
| 158 | bool FastbootDevice::WriteOkay(const std::string& message) { |
| 159 | return WriteStatus(FastbootResult::OKAY, message); |
| 160 | } |
| 161 | |
| 162 | bool FastbootDevice::WriteFail(const std::string& message) { |
| 163 | return WriteStatus(FastbootResult::FAIL, message); |
| 164 | } |
David Anderson | 0f62663 | 2018-08-31 16:44:25 -0700 | [diff] [blame] | 165 | |
| 166 | bool FastbootDevice::WriteInfo(const std::string& message) { |
| 167 | return WriteStatus(FastbootResult::INFO, message); |
| 168 | } |