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