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 <algorithm>
20
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 #include <android-base/strings.h>
24 #include <android/hardware/boot/1.0/IBootControl.h>
25 #include <android/hardware/fastboot/1.0/IFastboot.h>
26 #include <fs_mgr.h>
27 #include <fs_mgr/roots.h>
28 #include <healthhalutils/HealthHalUtils.h>
29
30 #include "constants.h"
31 #include "flashing.h"
32 #include "tcp_client.h"
33 #include "usb_client.h"
34
35 using android::fs_mgr::EnsurePathUnmounted;
36 using android::fs_mgr::Fstab;
37 using ::android::hardware::hidl_string;
38 using ::android::hardware::boot::V1_0::IBootControl;
39 using ::android::hardware::boot::V1_0::Slot;
40 using ::android::hardware::fastboot::V1_0::IFastboot;
41 using ::android::hardware::health::V2_0::get_health_service;
42
43 namespace sph = std::placeholders;
44
FastbootDevice()45 FastbootDevice::FastbootDevice()
46 : kCommandMap({
47 {FB_CMD_SET_ACTIVE, SetActiveHandler},
48 {FB_CMD_DOWNLOAD, DownloadHandler},
49 {FB_CMD_GETVAR, GetVarHandler},
50 {FB_CMD_SHUTDOWN, ShutDownHandler},
51 {FB_CMD_REBOOT, RebootHandler},
52 {FB_CMD_REBOOT_BOOTLOADER, RebootBootloaderHandler},
53 {FB_CMD_REBOOT_FASTBOOT, RebootFastbootHandler},
54 {FB_CMD_REBOOT_RECOVERY, RebootRecoveryHandler},
55 {FB_CMD_ERASE, EraseHandler},
56 {FB_CMD_FLASH, FlashHandler},
57 {FB_CMD_CREATE_PARTITION, CreatePartitionHandler},
58 {FB_CMD_DELETE_PARTITION, DeletePartitionHandler},
59 {FB_CMD_RESIZE_PARTITION, ResizePartitionHandler},
60 {FB_CMD_UPDATE_SUPER, UpdateSuperHandler},
61 {FB_CMD_OEM, OemCmdHandler},
62 {FB_CMD_GSI, GsiHandler},
63 {FB_CMD_SNAPSHOT_UPDATE, SnapshotUpdateHandler},
64 }),
65 boot_control_hal_(IBootControl::getService()),
66 health_hal_(get_health_service()),
67 fastboot_hal_(IFastboot::getService()),
68 active_slot_("") {
69 if (android::base::GetProperty("fastbootd.protocol", "usb") == "tcp") {
70 transport_ = std::make_unique<ClientTcpTransport>();
71 } else {
72 transport_ = std::make_unique<ClientUsbTransport>();
73 }
74
75 if (boot_control_hal_) {
76 boot1_1_ = android::hardware::boot::V1_1::IBootControl::castFrom(boot_control_hal_);
77 }
78
79 // Make sure cache is unmounted, since recovery will have mounted it for
80 // logging.
81 Fstab fstab;
82 if (ReadDefaultFstab(&fstab)) {
83 EnsurePathUnmounted(&fstab, "/cache");
84 }
85 }
86
~FastbootDevice()87 FastbootDevice::~FastbootDevice() {
88 CloseDevice();
89 }
90
CloseDevice()91 void FastbootDevice::CloseDevice() {
92 transport_->Close();
93 }
94
GetCurrentSlot()95 std::string FastbootDevice::GetCurrentSlot() {
96 // Check if a set_active ccommand was issued earlier since the boot control HAL
97 // returns the slot that is currently booted into.
98 if (!active_slot_.empty()) {
99 return active_slot_;
100 }
101 // Non-A/B devices must not have boot control HALs.
102 if (!boot_control_hal_) {
103 return "";
104 }
105 std::string suffix;
106 auto cb = [&suffix](hidl_string s) { suffix = s; };
107 boot_control_hal_->getSuffix(boot_control_hal_->getCurrentSlot(), cb);
108 return suffix;
109 }
110
WriteStatus(FastbootResult result,const std::string & message)111 bool FastbootDevice::WriteStatus(FastbootResult result, const std::string& message) {
112 constexpr size_t kResponseReasonSize = 4;
113 constexpr size_t kNumResponseTypes = 4; // "FAIL", "OKAY", "INFO", "DATA"
114
115 char buf[FB_RESPONSE_SZ];
116 constexpr size_t kMaxMessageSize = sizeof(buf) - kResponseReasonSize;
117 size_t msg_len = std::min(kMaxMessageSize, message.size());
118
119 constexpr const char* kResultStrings[kNumResponseTypes] = {RESPONSE_OKAY, RESPONSE_FAIL,
120 RESPONSE_INFO, RESPONSE_DATA};
121
122 if (static_cast<size_t>(result) >= kNumResponseTypes) {
123 return false;
124 }
125
126 memcpy(buf, kResultStrings[static_cast<size_t>(result)], kResponseReasonSize);
127 memcpy(buf + kResponseReasonSize, message.c_str(), msg_len);
128
129 size_t response_len = kResponseReasonSize + msg_len;
130 auto write_ret = this->get_transport()->Write(buf, response_len);
131 if (write_ret != static_cast<ssize_t>(response_len)) {
132 PLOG(ERROR) << "Failed to write " << message;
133 return false;
134 }
135
136 return true;
137 }
138
HandleData(bool read,std::vector<char> * data)139 bool FastbootDevice::HandleData(bool read, std::vector<char>* data) {
140 auto read_write_data_size = read ? this->get_transport()->Read(data->data(), data->size())
141 : this->get_transport()->Write(data->data(), data->size());
142 if (read_write_data_size == -1 || static_cast<size_t>(read_write_data_size) != data->size()) {
143 return false;
144 }
145 return true;
146 }
147
ExecuteCommands()148 void FastbootDevice::ExecuteCommands() {
149 char command[FB_RESPONSE_SZ + 1];
150 for (;;) {
151 auto bytes_read = transport_->Read(command, FB_RESPONSE_SZ);
152 if (bytes_read == -1) {
153 PLOG(ERROR) << "Couldn't read command";
154 return;
155 }
156 command[bytes_read] = '\0';
157
158 LOG(INFO) << "Fastboot command: " << command;
159
160 std::vector<std::string> args;
161 std::string cmd_name;
162 if (android::base::StartsWith(command, "oem ")) {
163 args = {command};
164 cmd_name = FB_CMD_OEM;
165 } else {
166 args = android::base::Split(command, ":");
167 cmd_name = args[0];
168 }
169
170 auto found_command = kCommandMap.find(cmd_name);
171 if (found_command == kCommandMap.end()) {
172 WriteStatus(FastbootResult::FAIL, "Unrecognized command " + args[0]);
173 continue;
174 }
175 if (!found_command->second(this, args)) {
176 return;
177 }
178 }
179 }
180
WriteOkay(const std::string & message)181 bool FastbootDevice::WriteOkay(const std::string& message) {
182 return WriteStatus(FastbootResult::OKAY, message);
183 }
184
WriteFail(const std::string & message)185 bool FastbootDevice::WriteFail(const std::string& message) {
186 return WriteStatus(FastbootResult::FAIL, message);
187 }
188
WriteInfo(const std::string & message)189 bool FastbootDevice::WriteInfo(const std::string& message) {
190 return WriteStatus(FastbootResult::INFO, message);
191 }
192