blob: 053d6544788a64f05c3620d1faab04e02e6f163c [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 "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>
David Anderson12211d12018-07-24 15:21:20 -070029#include <ext4_utils/wipe.h>
David Anderson0d4277d2018-07-31 13:27:37 -070030#include <liblp/builder.h>
31#include <liblp/liblp.h>
32#include <uuid/uuid.h>
Hridya Valsarajudea91b42018-07-17 11:14:01 -070033
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070034#include "constants.h"
Hridya Valsarajudea91b42018-07-17 11:14:01 -070035#include "fastboot_device.h"
David Anderson12211d12018-07-24 15:21:20 -070036#include "flashing.h"
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070037#include "utility.h"
38
39using ::android::hardware::hidl_string;
40using ::android::hardware::boot::V1_0::BoolResult;
41using ::android::hardware::boot::V1_0::CommandResult;
42using ::android::hardware::boot::V1_0::Slot;
David Anderson0d4277d2018-07-31 13:27:37 -070043using namespace android::fs_mgr;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070044
45bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson856b7ec2018-08-08 17:58:56 -070046 using VariableHandler = std::function<bool(FastbootDevice*, const std::vector<std::string>&)>;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070047 const std::unordered_map<std::string, VariableHandler> kVariableMap = {
48 {FB_VAR_VERSION, GetVersion},
49 {FB_VAR_VERSION_BOOTLOADER, GetBootloaderVersion},
50 {FB_VAR_VERSION_BASEBAND, GetBasebandVersion},
51 {FB_VAR_PRODUCT, GetProduct},
52 {FB_VAR_SERIALNO, GetSerial},
53 {FB_VAR_SECURE, GetSecure},
54 {FB_VAR_UNLOCKED, GetUnlocked},
55 {FB_VAR_MAX_DOWNLOAD_SIZE, GetMaxDownloadSize},
56 {FB_VAR_CURRENT_SLOT, ::GetCurrentSlot},
57 {FB_VAR_SLOT_COUNT, GetSlotCount},
58 {FB_VAR_HAS_SLOT, GetHasSlot},
59 {FB_VAR_SLOT_SUCCESSFUL, GetSlotSuccessful},
David Anderson12211d12018-07-24 15:21:20 -070060 {FB_VAR_SLOT_UNBOOTABLE, GetSlotUnbootable},
David Anderson0d4277d2018-07-31 13:27:37 -070061 {FB_VAR_PARTITION_SIZE, GetPartitionSize},
62 {FB_VAR_IS_LOGICAL, GetPartitionIsLogical}};
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070063
64 // args[0] is command name, args[1] is variable.
65 auto found_variable = kVariableMap.find(args[1]);
66 if (found_variable == kVariableMap.end()) {
67 return device->WriteStatus(FastbootResult::FAIL, "Unknown variable");
68 }
69
70 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson856b7ec2018-08-08 17:58:56 -070071 return found_variable->second(device, getvar_args);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070072}
Hridya Valsarajudea91b42018-07-17 11:14:01 -070073
David Anderson12211d12018-07-24 15:21:20 -070074bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
75 if (args.size() < 2) {
76 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
77 }
78 PartitionHandle handle;
79 if (!OpenPartition(device, args[1], &handle)) {
80 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
81 }
82 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
83 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
84 }
85 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
86}
87
Hridya Valsarajudea91b42018-07-17 11:14:01 -070088bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
89 if (args.size() < 2) {
90 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
91 }
92 // arg[0] is the command name, arg[1] contains size of data to be downloaded
93 unsigned int size;
94 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
95 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
96 }
David Anderson12211d12018-07-24 15:21:20 -070097 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -070098 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
99 return false;
100 }
101
David Anderson12211d12018-07-24 15:21:20 -0700102 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700103 return device->WriteStatus(FastbootResult::OKAY, "");
104 }
105
106 PLOG(ERROR) << "Couldn't download data";
107 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
108}
109
David Anderson12211d12018-07-24 15:21:20 -0700110bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
111 if (args.size() < 2) {
112 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
113 }
114 int ret = Flash(device, args[1]);
115 if (ret < 0) {
116 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
117 }
118 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
119}
120
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700121bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
122 if (args.size() < 2) {
123 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
124 }
125
126 // Slot suffix needs to be between 'a' and 'z'.
127 Slot slot;
128 if (!GetSlotNumber(args[1], &slot)) {
129 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
130 }
131
132 // Non-A/B devices will not have a boot control HAL.
133 auto boot_control_hal = device->boot_control_hal();
134 if (!boot_control_hal) {
135 return device->WriteStatus(FastbootResult::FAIL,
136 "Cannot set slot: boot control HAL absent");
137 }
138 if (slot >= boot_control_hal->getNumberSlots()) {
139 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
140 }
141 CommandResult ret;
142 auto cb = [&ret](CommandResult result) { ret = result; };
143 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
144 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
145 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700146}
147
148bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
149 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
150 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
151 device->CloseDevice();
152 TEMP_FAILURE_RETRY(pause());
153 return result;
154}
155
156bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
157 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
158 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
159 device->CloseDevice();
160 TEMP_FAILURE_RETRY(pause());
161 return result;
162}
163
164bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
165 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
166 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
167 device->CloseDevice();
168 TEMP_FAILURE_RETRY(pause());
169 return result;
170}
171
172bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
173 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
174 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
175 device->CloseDevice();
176 TEMP_FAILURE_RETRY(pause());
177 return result;
178}
179
180static bool EnterRecovery() {
181 const char msg_switch_to_recovery = 'r';
182
183 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
184 if (sock < 0) {
185 PLOG(ERROR) << "Couldn't create sock";
186 return false;
187 }
188
189 struct sockaddr_un addr = {.sun_family = AF_UNIX};
190 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
191 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
192 PLOG(ERROR) << "Couldn't connect to recovery";
193 return false;
194 }
195 // Switch to recovery will not update the boot reason since it does not
196 // require a reboot.
197 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
198 if (ret != sizeof(msg_switch_to_recovery)) {
199 PLOG(ERROR) << "Couldn't write message to switch to recovery";
200 return false;
201 }
202
203 return true;
204}
205
206bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
207 auto status = true;
208 if (EnterRecovery()) {
209 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
210 } else {
211 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
212 }
213 device->CloseDevice();
214 TEMP_FAILURE_RETRY(pause());
215 return status;
216}
David Anderson0d4277d2018-07-31 13:27:37 -0700217
218// Helper class for opening a handle to a MetadataBuilder and writing the new
219// partition table to the same place it was read.
220class PartitionBuilder {
221 public:
222 explicit PartitionBuilder(FastbootDevice* device);
223
224 bool Write();
225 bool Valid() const { return !!builder_; }
226 MetadataBuilder* operator->() const { return builder_.get(); }
227
228 private:
229 std::string super_device_;
230 uint32_t slot_number_;
231 std::unique_ptr<MetadataBuilder> builder_;
232};
233
234PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
235 auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
236 if (!super_device) {
237 return;
238 }
239 super_device_ = *super_device;
240
241 std::string slot = device->GetCurrentSlot();
242 slot_number_ = SlotNumberForSlotSuffix(slot);
243 builder_ = MetadataBuilder::New(super_device_, slot_number_);
244}
245
246bool PartitionBuilder::Write() {
247 std::unique_ptr<LpMetadata> metadata = builder_->Export();
248 if (!metadata) {
249 return false;
250 }
251 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
252}
253
254bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
255 if (args.size() < 3) {
256 return device->WriteFail("Invalid partition name and size");
257 }
258
259 uint64_t partition_size;
260 std::string partition_name = args[1];
261 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
262 return device->WriteFail("Invalid partition size");
263 }
264
265 PartitionBuilder builder(device);
266 if (!builder.Valid()) {
267 return device->WriteFail("Could not open super partition");
268 }
269 // TODO(112433293) Disallow if the name is in the physical table as well.
270 if (builder->FindPartition(partition_name)) {
271 return device->WriteFail("Partition already exists");
272 }
273
274 // Make a random UUID, since they're not currently used.
275 uuid_t uuid;
276 char uuid_str[37];
277 uuid_generate_random(uuid);
278 uuid_unparse(uuid, uuid_str);
279
280 Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
281 if (!partition) {
282 return device->WriteFail("Failed to add partition");
283 }
284 if (!builder->ResizePartition(partition, partition_size)) {
285 builder->RemovePartition(partition_name);
286 return device->WriteFail("Not enough space for partition");
287 }
288 if (!builder.Write()) {
289 return device->WriteFail("Failed to write partition table");
290 }
291 return device->WriteOkay("Partition created");
292}
293
294bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
295 if (args.size() < 2) {
296 return device->WriteFail("Invalid partition name and size");
297 }
298
299 PartitionBuilder builder(device);
300 if (!builder.Valid()) {
301 return device->WriteFail("Could not open super partition");
302 }
303 builder->RemovePartition(args[1]);
304 if (!builder.Write()) {
305 return device->WriteFail("Failed to write partition table");
306 }
307 return device->WriteOkay("Partition deleted");
308}
309
310bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
311 if (args.size() < 3) {
312 return device->WriteFail("Invalid partition name and size");
313 }
314
315 uint64_t partition_size;
316 std::string partition_name = args[1];
317 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
318 return device->WriteFail("Invalid partition size");
319 }
320
321 PartitionBuilder builder(device);
322 if (!builder.Valid()) {
323 return device->WriteFail("Could not open super partition");
324 }
325
326 Partition* partition = builder->FindPartition(partition_name);
327 if (!partition) {
328 return device->WriteFail("Partition does not exist");
329 }
330 if (!builder->ResizePartition(partition, partition_size)) {
331 return device->WriteFail("Not enough space to resize partition");
332 }
333 if (!builder.Write()) {
334 return device->WriteFail("Failed to write partition table");
335 }
336 return device->WriteOkay("Partition resized");
337}