blob: b1c2958fef5c1a425a3234cd5f8d41938a24c773 [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}},
92 {FB_VAR_IS_USERSPACE, {GetIsUserspace, nullptr}}};
93
94 if (args.size() < 2) {
95 return device->WriteFail("Missing argument");
96 }
97
98 // Special case: return all variables that we can.
99 if (args[1] == "all") {
100 for (const auto& [name, handlers] : kVariableMap) {
101 GetAllVars(device, name, handlers);
102 }
103 return device->WriteOkay("");
104 }
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700105
106 // args[0] is command name, args[1] is variable.
107 auto found_variable = kVariableMap.find(args[1]);
108 if (found_variable == kVariableMap.end()) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700109 return device->WriteFail("Unknown variable");
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700110 }
111
David Anderson1fb3fd72018-08-31 14:40:22 -0700112 std::string message;
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700113 std::vector<std::string> getvar_args(args.begin() + 2, args.end());
David Anderson0f626632018-08-31 16:44:25 -0700114 if (!found_variable->second.get(device, getvar_args, &message)) {
David Anderson1fb3fd72018-08-31 14:40:22 -0700115 return device->WriteFail(message);
116 }
117 return device->WriteOkay(message);
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700118}
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700119
David Anderson12211d12018-07-24 15:21:20 -0700120bool EraseHandler(FastbootDevice* device, const std::vector<std::string>& args) {
121 if (args.size() < 2) {
122 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
123 }
124 PartitionHandle handle;
125 if (!OpenPartition(device, args[1], &handle)) {
126 return device->WriteStatus(FastbootResult::FAIL, "Partition doesn't exist");
127 }
128 if (wipe_block_device(handle.fd(), get_block_device_size(handle.fd())) == 0) {
129 return device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded");
130 }
131 return device->WriteStatus(FastbootResult::FAIL, "Erasing failed");
132}
133
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700134bool DownloadHandler(FastbootDevice* device, const std::vector<std::string>& args) {
135 if (args.size() < 2) {
136 return device->WriteStatus(FastbootResult::FAIL, "size argument unspecified");
137 }
138 // arg[0] is the command name, arg[1] contains size of data to be downloaded
139 unsigned int size;
140 if (!android::base::ParseUint("0x" + args[1], &size, UINT_MAX)) {
141 return device->WriteStatus(FastbootResult::FAIL, "Invalid size");
142 }
David Anderson12211d12018-07-24 15:21:20 -0700143 device->download_data().resize(size);
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700144 if (!device->WriteStatus(FastbootResult::DATA, android::base::StringPrintf("%08x", size))) {
145 return false;
146 }
147
David Anderson12211d12018-07-24 15:21:20 -0700148 if (device->HandleData(true, &device->download_data())) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700149 return device->WriteStatus(FastbootResult::OKAY, "");
150 }
151
152 PLOG(ERROR) << "Couldn't download data";
153 return device->WriteStatus(FastbootResult::FAIL, "Couldn't download data");
154}
155
David Anderson12211d12018-07-24 15:21:20 -0700156bool FlashHandler(FastbootDevice* device, const std::vector<std::string>& args) {
157 if (args.size() < 2) {
158 return device->WriteStatus(FastbootResult::FAIL, "Invalid arguments");
159 }
160 int ret = Flash(device, args[1]);
161 if (ret < 0) {
162 return device->WriteStatus(FastbootResult::FAIL, strerror(-ret));
163 }
164 return device->WriteStatus(FastbootResult::OKAY, "Flashing succeeded");
165}
166
Hridya Valsaraju31d2c262018-07-20 13:35:50 -0700167bool SetActiveHandler(FastbootDevice* device, const std::vector<std::string>& args) {
168 if (args.size() < 2) {
169 return device->WriteStatus(FastbootResult::FAIL, "Missing slot argument");
170 }
171
172 // Slot suffix needs to be between 'a' and 'z'.
173 Slot slot;
174 if (!GetSlotNumber(args[1], &slot)) {
175 return device->WriteStatus(FastbootResult::FAIL, "Bad slot suffix");
176 }
177
178 // Non-A/B devices will not have a boot control HAL.
179 auto boot_control_hal = device->boot_control_hal();
180 if (!boot_control_hal) {
181 return device->WriteStatus(FastbootResult::FAIL,
182 "Cannot set slot: boot control HAL absent");
183 }
184 if (slot >= boot_control_hal->getNumberSlots()) {
185 return device->WriteStatus(FastbootResult::FAIL, "Slot out of range");
186 }
187 CommandResult ret;
188 auto cb = [&ret](CommandResult result) { ret = result; };
189 auto result = boot_control_hal->setActiveBootSlot(slot, cb);
190 if (result.isOk() && ret.success) return device->WriteStatus(FastbootResult::OKAY, "");
191 return device->WriteStatus(FastbootResult::FAIL, "Unable to set slot");
Hridya Valsarajudea91b42018-07-17 11:14:01 -0700192}
193
194bool ShutDownHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
195 auto result = device->WriteStatus(FastbootResult::OKAY, "Shutting down");
196 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,fastboot");
197 device->CloseDevice();
198 TEMP_FAILURE_RETRY(pause());
199 return result;
200}
201
202bool RebootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
203 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting");
204 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,from_fastboot");
205 device->CloseDevice();
206 TEMP_FAILURE_RETRY(pause());
207 return result;
208}
209
210bool RebootBootloaderHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
211 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting bootloader");
212 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
213 device->CloseDevice();
214 TEMP_FAILURE_RETRY(pause());
215 return result;
216}
217
218bool RebootFastbootHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
219 auto result = device->WriteStatus(FastbootResult::OKAY, "Rebooting fastboot");
220 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
221 device->CloseDevice();
222 TEMP_FAILURE_RETRY(pause());
223 return result;
224}
225
226static bool EnterRecovery() {
227 const char msg_switch_to_recovery = 'r';
228
229 android::base::unique_fd sock(socket(AF_UNIX, SOCK_STREAM, 0));
230 if (sock < 0) {
231 PLOG(ERROR) << "Couldn't create sock";
232 return false;
233 }
234
235 struct sockaddr_un addr = {.sun_family = AF_UNIX};
236 strncpy(addr.sun_path, "/dev/socket/recovery", sizeof(addr.sun_path) - 1);
237 if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
238 PLOG(ERROR) << "Couldn't connect to recovery";
239 return false;
240 }
241 // Switch to recovery will not update the boot reason since it does not
242 // require a reboot.
243 auto ret = write(sock, &msg_switch_to_recovery, sizeof(msg_switch_to_recovery));
244 if (ret != sizeof(msg_switch_to_recovery)) {
245 PLOG(ERROR) << "Couldn't write message to switch to recovery";
246 return false;
247 }
248
249 return true;
250}
251
252bool RebootRecoveryHandler(FastbootDevice* device, const std::vector<std::string>& /* args */) {
253 auto status = true;
254 if (EnterRecovery()) {
255 status = device->WriteStatus(FastbootResult::OKAY, "Rebooting to recovery");
256 } else {
257 status = device->WriteStatus(FastbootResult::FAIL, "Unable to reboot to recovery");
258 }
259 device->CloseDevice();
260 TEMP_FAILURE_RETRY(pause());
261 return status;
262}
David Anderson0d4277d2018-07-31 13:27:37 -0700263
264// Helper class for opening a handle to a MetadataBuilder and writing the new
265// partition table to the same place it was read.
266class PartitionBuilder {
267 public:
268 explicit PartitionBuilder(FastbootDevice* device);
269
270 bool Write();
271 bool Valid() const { return !!builder_; }
272 MetadataBuilder* operator->() const { return builder_.get(); }
273
274 private:
275 std::string super_device_;
276 uint32_t slot_number_;
277 std::unique_ptr<MetadataBuilder> builder_;
278};
279
280PartitionBuilder::PartitionBuilder(FastbootDevice* device) {
281 auto super_device = FindPhysicalPartition(LP_METADATA_PARTITION_NAME);
282 if (!super_device) {
283 return;
284 }
285 super_device_ = *super_device;
286
287 std::string slot = device->GetCurrentSlot();
288 slot_number_ = SlotNumberForSlotSuffix(slot);
289 builder_ = MetadataBuilder::New(super_device_, slot_number_);
290}
291
292bool PartitionBuilder::Write() {
293 std::unique_ptr<LpMetadata> metadata = builder_->Export();
294 if (!metadata) {
295 return false;
296 }
297 return UpdatePartitionTable(super_device_, *metadata.get(), slot_number_);
298}
299
300bool CreatePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
301 if (args.size() < 3) {
302 return device->WriteFail("Invalid partition name and size");
303 }
304
305 uint64_t partition_size;
306 std::string partition_name = args[1];
307 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
308 return device->WriteFail("Invalid partition size");
309 }
310
311 PartitionBuilder builder(device);
312 if (!builder.Valid()) {
313 return device->WriteFail("Could not open super partition");
314 }
315 // TODO(112433293) Disallow if the name is in the physical table as well.
316 if (builder->FindPartition(partition_name)) {
317 return device->WriteFail("Partition already exists");
318 }
319
320 // Make a random UUID, since they're not currently used.
321 uuid_t uuid;
322 char uuid_str[37];
323 uuid_generate_random(uuid);
324 uuid_unparse(uuid, uuid_str);
325
326 Partition* partition = builder->AddPartition(partition_name, uuid_str, 0);
327 if (!partition) {
328 return device->WriteFail("Failed to add partition");
329 }
330 if (!builder->ResizePartition(partition, partition_size)) {
331 builder->RemovePartition(partition_name);
332 return device->WriteFail("Not enough space for partition");
333 }
334 if (!builder.Write()) {
335 return device->WriteFail("Failed to write partition table");
336 }
337 return device->WriteOkay("Partition created");
338}
339
340bool DeletePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
341 if (args.size() < 2) {
342 return device->WriteFail("Invalid partition name and size");
343 }
344
345 PartitionBuilder builder(device);
346 if (!builder.Valid()) {
347 return device->WriteFail("Could not open super partition");
348 }
349 builder->RemovePartition(args[1]);
350 if (!builder.Write()) {
351 return device->WriteFail("Failed to write partition table");
352 }
353 return device->WriteOkay("Partition deleted");
354}
355
356bool ResizePartitionHandler(FastbootDevice* device, const std::vector<std::string>& args) {
357 if (args.size() < 3) {
358 return device->WriteFail("Invalid partition name and size");
359 }
360
361 uint64_t partition_size;
362 std::string partition_name = args[1];
363 if (!android::base::ParseUint(args[2].c_str(), &partition_size)) {
364 return device->WriteFail("Invalid partition size");
365 }
366
367 PartitionBuilder builder(device);
368 if (!builder.Valid()) {
369 return device->WriteFail("Could not open super partition");
370 }
371
372 Partition* partition = builder->FindPartition(partition_name);
373 if (!partition) {
374 return device->WriteFail("Partition does not exist");
375 }
376 if (!builder->ResizePartition(partition, partition_size)) {
377 return device->WriteFail("Not enough space to resize partition");
378 }
379 if (!builder.Write()) {
380 return device->WriteFail("Failed to write partition table");
381 }
382 return device->WriteOkay("Partition resized");
383}
David Anderson38b3c7a2018-08-15 16:27:42 -0700384
385bool UpdateSuperHandler(FastbootDevice* device, const std::vector<std::string>& args) {
386 if (args.size() < 2) {
387 return device->WriteFail("Invalid arguments");
388 }
389 bool wipe = (args.size() >= 3 && args[2] == "wipe");
390 return UpdateSuper(device, args[1], wipe);
391}