blob: 56fafab047cec92128d4b905401f3dcac2f18437 [file] [log] [blame]
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001/*
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 Valsaraju31d2c262018-07-20 13:35:50 -070021#include <android/hardware/boot/1.0/IBootControl.h>
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070022#include <android/hardware/fastboot/1.0/IFastboot.h>
Hridya Valsaraju47658ca2018-09-28 11:41:22 -070023#include <healthhalutils/HealthHalUtils.h>
24
Hridya Valsarajudea91b42018-07-17 11:14:01 -070025#include <algorithm>
26
27#include "constants.h"
David Anderson12211d12018-07-24 15:21:20 -070028#include "flashing.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070029#include "usb_client.h"
30
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070031using ::android::hardware::hidl_string;
32using ::android::hardware::boot::V1_0::IBootControl;
33using ::android::hardware::boot::V1_0::Slot;
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070034using ::android::hardware::fastboot::V1_0::IFastboot;
Hridya Valsaraju47658ca2018-09-28 11:41:22 -070035using ::android::hardware::health::V2_0::get_health_service;
36
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070037namespace sph = std::placeholders;
38
Hridya Valsarajudea91b42018-07-17 11:14:01 -070039FastbootDevice::FastbootDevice()
40 : kCommandMap({
41 {FB_CMD_SET_ACTIVE, SetActiveHandler},
42 {FB_CMD_DOWNLOAD, DownloadHandler},
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070043 {FB_CMD_GETVAR, GetVarHandler},
Hridya Valsarajudea91b42018-07-17 11:14:01 -070044 {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 Anderson12211d12018-07-24 15:21:20 -070049 {FB_CMD_ERASE, EraseHandler},
50 {FB_CMD_FLASH, FlashHandler},
David Anderson0d4277d2018-07-31 13:27:37 -070051 {FB_CMD_CREATE_PARTITION, CreatePartitionHandler},
52 {FB_CMD_DELETE_PARTITION, DeletePartitionHandler},
53 {FB_CMD_RESIZE_PARTITION, ResizePartitionHandler},
David Anderson38b3c7a2018-08-15 16:27:42 -070054 {FB_CMD_UPDATE_SUPER, UpdateSuperHandler},
Hridya Valsarajua15fe312018-09-14 13:58:21 -070055 {FB_CMD_OEM, OemCmdHandler},
David Anderson1d504e32019-01-15 14:38:20 -080056 {FB_CMD_GSI, GsiHandler},
Hridya Valsarajudea91b42018-07-17 11:14:01 -070057 }),
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070058 transport_(std::make_unique<ClientUsbTransport>()),
Hridya Valsarajubf9f8d12018-09-05 16:57:24 -070059 boot_control_hal_(IBootControl::getService()),
Hridya Valsaraju47658ca2018-09-28 11:41:22 -070060 health_hal_(get_health_service()),
Hridya Valsaraju20bdf892018-10-10 11:02:19 -070061 fastboot_hal_(IFastboot::getService()),
62 active_slot_("") {}
Hridya Valsarajudea91b42018-07-17 11:14:01 -070063
64FastbootDevice::~FastbootDevice() {
65 CloseDevice();
66}
67
68void FastbootDevice::CloseDevice() {
69 transport_->Close();
70}
71
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070072std::string FastbootDevice::GetCurrentSlot() {
Hridya Valsaraju20bdf892018-10-10 11:02:19 -070073 // 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 Valsaraju31d2c262018-07-20 13:35:50 -070078 // 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 Valsarajudea91b42018-07-17 11:14:01 -070088bool 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 Valsaraju31d2c262018-07-20 13:35:50 -070091
Hridya Valsarajudea91b42018-07-17 11:14:01 -070092 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
116bool 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
125void 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 Valsarajua15fe312018-09-14 13:58:21 -0700136
137 std::vector<std::string> args;
138 std::string cmd_name;
139 if (android::base::StartsWith(command, "oem ")) {
140 args = {command};
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -0700141 cmd_name = FB_CMD_OEM;
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700142 } else {
143 args = android::base::Split(command, ":");
144 cmd_name = args[0];
145 }
146
147 auto found_command = kCommandMap.find(cmd_name);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700148 if (found_command == kCommandMap.end()) {
Hridya Valsarajua15fe312018-09-14 13:58:21 -0700149 WriteStatus(FastbootResult::FAIL, "Unrecognized command " + args[0]);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700150 continue;
151 }
152 if (!found_command->second(this, args)) {
153 return;
154 }
155 }
156}
David Anderson856b7ec2018-08-08 17:58:56 -0700157
158bool FastbootDevice::WriteOkay(const std::string& message) {
159 return WriteStatus(FastbootResult::OKAY, message);
160}
161
162bool FastbootDevice::WriteFail(const std::string& message) {
163 return WriteStatus(FastbootResult::FAIL, message);
164}
David Anderson0f626632018-08-31 16:44:25 -0700165
166bool FastbootDevice::WriteInfo(const std::string& message) {
167 return WriteStatus(FastbootResult::INFO, message);
168}