blob: 771c28892cad61e58cf5160124b639716a3b6a01 [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
David Anderson0f626632018-08-31 16:44:25 -070045struct VariableHandlers {
46 // Callback to retrieve the value of a single variable.
47 std::function<bool(FastbootDevice*, const std::vector<std::string>&, std::string*)> get;
48 // Callback to retrieve all possible argument combinations, for getvar all.
49 std::function<std::vector<std::vector<std::string>>(FastbootDevice*)> get_all_args;
50};
51
52static void GetAllVars(FastbootDevice* device, const std::string& name,
53 const VariableHandlers& handlers) {
54 if (!handlers.get_all_args) {
55 std::string message;
56 if (!handlers.get(device, std::vector<std::string>(), &message)) {
57 return;
58 }
59 device->WriteInfo(android::base::StringPrintf("%s:%s", name.c_str(), message.c_str()));
60 return;
61 }
62
63 auto all_args = handlers.get_all_args(device);
64 for (const auto& args : all_args) {
65 std::string message;
66 if (!handlers.get(device, args, &message)) {
67 continue;
68 }
69 std::string arg_string = android::base::Join(args, ":");
70 device->WriteInfo(android::base::StringPrintf("%s:%s:%s", name.c_str(), arg_string.c_str(),
71 message.c_str()));
72 }
73}
74
Hridya Valsaraju31d2c262018-07-20 13:35:50 -070075bool GetVarHandler(FastbootDevice* device, const std::vector<std::string>& args) {
David Anderson0f626632018-08-31 16:44:25 -070076 const std::unordered_map<std::string, VariableHandlers> kVariableMap = {
77 {FB_VAR_VERSION, {GetVersion, nullptr}},
78 {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}},
79 {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}},
80 {FB_VAR_PRODUCT, {GetProduct, nullptr}},
81 {FB_VAR_SERIALNO, {GetSerial, nullptr}},
82 {FB_VAR_SECURE, {GetSecure, nullptr}},
83 {FB_VAR_UNLOCKED, {GetUnlocked, nullptr}},
84 {FB_VAR_MAX_DOWNLOAD_SIZE, {GetMaxDownloadSize, nullptr}},
85 {FB_VAR_CURRENT_SLOT, {::GetCurrentSlot, nullptr}},
86 {FB_VAR_SLOT_COUNT, {GetSlotCount, nullptr}},
87 {FB_VAR_HAS_SLOT, {GetHasSlot, GetAllPartitionArgsNoSlot}},
88 {FB_VAR_SLOT_SUCCESSFUL, {GetSlotSuccessful, nullptr}},
89 {FB_VAR_SLOT_UNBOOTABLE, {GetSlotUnbootable, nullptr}},
90 {FB_VAR_PARTITION_SIZE, {GetPartitionSize, GetAllPartitionArgsWithSlot}},
91 {FB_VAR_IS_LOGICAL, {GetPartitionIsLogical, GetAllPartitionArgsWithSlot}},
David Andersonc091c172018-09-04 18:11:03 -070092 {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}},
93 {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}}};
David Anderson0f626632018-08-31 16:44:25 -070094
95 if (args.size() < 2) {
96 return device->WriteFail("Missing argument");
97 }
98
99 // Special case: return all variables that we can.
100 if (args[1] == "all") {
101 for (const auto& [name, handlers] : kVariableMap) {
102 GetAllVars(device, name, handlers);
103 }
104 return device->WriteOkay("");
105 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700106
107 // args[0] is command name, args[1] is variable.
108 auto found_variable = kVariableMap.find(args[1]);
109 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700110 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700111 }
112
David Anderson1fb3fd72018-08-31 14:40:22 -0700113 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700114 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700115 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700116 return device->WriteFail(message);
117 }
118 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700119}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700120
David Anderson12211d12018-07-24 15:21:20 -0700121bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
122 if (args.size() < 2) {
123 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
124 }
125 PartitionHandle handle;
126 if (!OpenPartition(device, args[1], &handle)) {
127 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
128 }
129 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
130 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
131 }
132 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
133}
134
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700135bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
136 if (args.size() < 2) {
137 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
138 }
139 // arg[0] is the command name, arg[1] contains size of data to be downloaded
140 unsigned int size;
141 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
142 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
143 }
David Anderson12211d12018-07-24 15:21:20 -0700144 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700145 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
146 return false;
147 }
148
David Anderson12211d12018-07-24 15:21:20 -0700149 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700150 return device->WriteStatus(FastbootResult::OKAY, "");
151 }
152
153 PLOG(ERROR) << "Couldn't download data";
154 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
155}
156
David Anderson12211d12018-07-24 15:21:20 -0700157bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
158 if (args.size() < 2) {
159 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
160 }
161 int ret = Flash(device, args[1]);
162 if (ret < 0) {
163 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
164 }
165 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
166}
167
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700168bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
169 if (args.size() < 2) {
170 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
171 }
172
173 // Slot suffix needs to be between 'a' and 'z'.
174 Slot slot;
175 if (!GetSlotNumber(args[1], &slot)) {
176 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
177 }
178
179 // Non-A/B devices will not have a boot control HAL.
180 auto boot_control_hal = device->boot_control_hal();
181 if (!boot_control_hal) {
182 return device->WriteStatus(FastbootResult::FAIL,
183 "Cannot set slot: boot control HAL absent");
184 }
185 if (slot >= boot_control_hal->getNumberSlots()) {
186 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
187 }
188 CommandResult ret;
189 auto cb = [&ret](CommandResult result) { ret = result; };
190 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
191 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
192 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700193}
194
195bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
196 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
197 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
198 device->CloseDevice();
199 TEMP_FAILURE_RETRY(pause());
200 return result;
201}
202
203bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
204 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
205 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
206 device->CloseDevice();
207 TEMP_FAILURE_RETRY(pause());
208 return result;
209}
210
211bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
212 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
213 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
214 device->CloseDevice();
215 TEMP_FAILURE_RETRY(pause());
216 return result;
217}
218
219bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
220 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
221 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
222 device->CloseDevice();
223 TEMP_FAILURE_RETRY(pause());
224 return result;
225}
226
227static bool EnterRecovery() {
228 const char msg_switch_to_recovery = 'r';
229
230 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
231 if (sock < 0) {
232 PLOG(ERROR) << "Couldn't create sock";
233 return false;
234 }
235
236 struct sockaddr_un addr = {.sun_family = AF_UNIX};
237 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
238 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
239 PLOG(ERROR) << "Couldn't connect to recovery";
240 return false;
241 }
242 // Switch to recovery will not update the boot reason since it does not
243 // require a reboot.
244 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
245 if (ret != sizeof(msg_switch_to_recovery)) {
246 PLOG(ERROR) << "Couldn't write message to switch to recovery";
247 return false;
248 }
249
250 return true;
251}
252
253bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
254 auto status = true;
255 if (EnterRecovery()) {
256 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
257 } else {
258 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
259 }
260 device->CloseDevice();
261 TEMP_FAILURE_RETRY(pause());
262 return status;
263}
David Anderson0d4277d2018-07-31 13:27:37 -0700264
265// Helper class for opening a handle to a MetadataBuilder and writing the new
266// partition table to the same place it was read.
267class PartitionBuilder {
268 public:
269 explicit PartitionBuilder(FastbootDevice* device);
270
271 bool Write();
272 bool Valid() const { return !!builder_; }
273 MetadataBuilder* operator->() const { return builder_.get(); }
274
275 private:
276 std::string super_device_;
277 uint32_t slot_number_;
278 std::unique_ptr<MetadataBuilder> builder_;
279};
280
281PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
282 auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
283 if (!super_device) {
284 return;
285 }
286 super_device_ = *super_device;
287
288 std::string slot = device->GetCurrentSlot();
289 slot_number_ = SlotNumberForSlotSuffix(slot);
290 builder_ = MetadataBuilder::New(super_device_, slot_number_);
291}
292
293bool PartitionBuilder::Write() {
294 std::unique_ptr<LpMetadata> metadata = builder_->Export();
295 if (!metadata) {
296 return false;
297 }
298 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
299}
300
301bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
302 if (args.size() < 3) {
303 return device->WriteFail("Invalid partition name and size");
304 }
305
306 uint64_t partition_size;
307 std::string partition_name = args[1];
308 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
309 return device->WriteFail("Invalid partition size");
310 }
311
312 PartitionBuilder builder(device);
313 if (!builder.Valid()) {
314 return device->WriteFail("Could not open super partition");
315 }
316 // TODO(112433293) Disallow if the name is in the physical table as well.
317 if (builder->FindPartition(partition_name)) {
318 return device->WriteFail("Partition already exists");
319 }
320
321 // Make a random UUID, since they're not currently used.
322 uuid_t uuid;
323 char uuid_str[37];
324 uuid_generate_random(uuid);
325 uuid_unparse(uuid, uuid_str);
326
327 Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
328 if (!partition) {
329 return device->WriteFail("Failed to add partition");
330 }
331 if (!builder->ResizePartition(partition, partition_size)) {
332 builder->RemovePartition(partition_name);
333 return device->WriteFail("Not enough space for partition");
334 }
335 if (!builder.Write()) {
336 return device->WriteFail("Failed to write partition table");
337 }
338 return device->WriteOkay("Partition created");
339}
340
341bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
342 if (args.size() < 2) {
343 return device->WriteFail("Invalid partition name and size");
344 }
345
346 PartitionBuilder builder(device);
347 if (!builder.Valid()) {
348 return device->WriteFail("Could not open super partition");
349 }
350 builder->RemovePartition(args[1]);
351 if (!builder.Write()) {
352 return device->WriteFail("Failed to write partition table");
353 }
354 return device->WriteOkay("Partition deleted");
355}
356
357bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
358 if (args.size() < 3) {
359 return device->WriteFail("Invalid partition name and size");
360 }
361
362 uint64_t partition_size;
363 std::string partition_name = args[1];
364 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
365 return device->WriteFail("Invalid partition size");
366 }
367
368 PartitionBuilder builder(device);
369 if (!builder.Valid()) {
370 return device->WriteFail("Could not open super partition");
371 }
372
373 Partition* partition = builder->FindPartition(partition_name);
374 if (!partition) {
375 return device->WriteFail("Partition does not exist");
376 }
377 if (!builder->ResizePartition(partition, partition_size)) {
378 return device->WriteFail("Not enough space to resize partition");
379 }
380 if (!builder.Write()) {
381 return device->WriteFail("Failed to write partition table");
382 }
383 return device->WriteOkay("Partition resized");
384}
David Anderson38b3c7a2018-08-15 16:27:42 -0700385
386bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
387 if (args.size() < 2) {
388 return device->WriteFail("Invalid arguments");
389 }
390 bool wipe = (args.size() >= 3 && args[2] == "wipe");
391 return UpdateSuper(device, args[1], wipe);
392}