blob: c097d045e2761b9b14e1f28b65910b825a80d165 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070046#include <functional>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes749ae2d2016-06-28 14:48:45 -070050#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080052#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070053#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070055#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070056#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Elliott Hughes253c18d2015-03-18 22:47:09 -070058#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080059#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070061#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080062#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080064#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070065#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Colin Crossf8387882012-05-24 17:18:41 -070067#ifndef O_BINARY
68#define O_BINARY 0
69#endif
70
Wink Savilleb98762f2011-04-04 17:54:59 -070071char cur_product[FB_RESPONSE_SZ + 1];
72
David Pursell2ec418a2016-01-20 08:32:08 -080073static const char* serial = nullptr;
74static const char* product = nullptr;
75static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughesfc797672015-04-07 20:12:50 -070081static unsigned page_size = 2048;
82static unsigned base_addr = 0x10000000;
83static unsigned kernel_offset = 0x00008000;
84static unsigned ramdisk_offset = 0x01000000;
85static unsigned second_offset = 0x00f00000;
86static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080087
Rom Lemarchand622810c2013-06-28 09:54:59 -070088enum fb_buffer_type {
89 FB_BUFFER,
90 FB_BUFFER_SPARSE,
91};
92
93struct fastboot_buffer {
94 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070095 void* data;
96 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070097};
98
99static struct {
100 char img_name[13];
101 char sig_name[13];
102 char part_name[9];
103 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700104} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 {"boot.img", "boot.sig", "boot", false},
106 {"recovery.img", "recovery.sig", "recovery", true},
107 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700108 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700109};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700110
Elliott Hughes2810d002016-04-25 14:31:18 -0700111static std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700112 const char *fn;
Elliott Hughes2810d002016-04-25 14:31:18 -0700113 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 fn = "boot.img";
115 } else if(!strcmp(item,"recovery")) {
116 fn = "recovery.img";
117 } else if(!strcmp(item,"system")) {
118 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700119 } else if(!strcmp(item,"vendor")) {
120 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 } else if(!strcmp(item,"userdata")) {
122 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700123 } else if(!strcmp(item,"cache")) {
124 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 } else if(!strcmp(item,"info")) {
126 fn = "android-info.txt";
127 } else {
128 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700129 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 }
131
Elliott Hughes2810d002016-04-25 14:31:18 -0700132 if (product) {
133 std::string path = get_my_path();
134 path.erase(path.find_last_of('/'));
135 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
136 path.c_str(), product, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800138
Elliott Hughes2810d002016-04-25 14:31:18 -0700139 char* dir = getenv("ANDROID_PRODUCT_OUT");
140 if (dir == nullptr || dir[0] == '\0') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800143
Elliott Hughes2810d002016-04-25 14:31:18 -0700144 return android::base::StringPrintf("%s/%s", dir, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145}
146
Elliott Hughesfc797672015-04-07 20:12:50 -0700147static int64_t get_file_size(int fd) {
148 struct stat sb;
149 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700150}
151
Elliott Hughesfc797672015-04-07 20:12:50 -0700152static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800153 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700154 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
Elliott Hughesfc797672015-04-07 20:12:50 -0700156 *sz = get_file_size(fd);
157 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700158 goto oops;
159 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
Elliott Hughesfc797672015-04-07 20:12:50 -0700161 data = (char*) malloc(*sz);
162 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 close(fd);
166
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 return data;
168
169oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800170 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 close(fd);
172 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800173 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 return 0;
175}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughes2810d002016-04-25 14:31:18 -0700177static void* load_file(const std::string& path, int64_t* sz) {
178 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700179 if (fd == -1) return nullptr;
180 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700181}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
Elliott Hughesfc797672015-04-07 20:12:50 -0700183static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700184 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700185 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700186 return -1;
187 }
188
189 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
190 return -1;
191 }
192
Scott Anderson13081c62012-04-06 12:39:30 -0700193 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700195 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
196 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 return 0;
198}
199
Elliott Hughesfc797672015-04-07 20:12:50 -0700200static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800201 return match_fastboot_with_serial(info, serial);
202}
203
Elliott Hughesfc797672015-04-07 20:12:50 -0700204static int list_devices_callback(usb_ifc_info* info) {
205 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800206 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700207 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800208 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700209 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 if (!serial[0]) {
211 serial = "????????????";
212 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700213 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700214 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700216 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 printf("%-22s fastboot", serial.c_str());
218 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700219 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 }
222
223 return -1;
224}
225
David Pursell2ec418a2016-01-20 08:32:08 -0800226// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
227// a specific device, otherwise the first USB device found will be used.
228//
229// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
230// Otherwise it blocks until the target is available.
231//
232// The returned Transport is a singleton, so multiple calls to this function will return the same
233// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700234static Transport* open_device() {
235 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800236 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237
David Pursell2ec418a2016-01-20 08:32:08 -0800238 if (transport != nullptr) {
239 return transport;
240 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800241
David Pursell4601c972016-02-05 15:35:09 -0800242 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800243 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800244 int port = 0;
245 if (serial != nullptr) {
246 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800247
David Pursell4601c972016-02-05 15:35:09 -0800248 if (android::base::StartsWith(serial, "tcp:")) {
249 protocol = Socket::Protocol::kTcp;
250 port = tcp::kDefaultPort;
251 net_address = serial + strlen("tcp:");
252 } else if (android::base::StartsWith(serial, "udp:")) {
253 protocol = Socket::Protocol::kUdp;
254 port = udp::kDefaultPort;
255 net_address = serial + strlen("udp:");
256 }
257
258 if (net_address != nullptr) {
259 std::string error;
260 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
261 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
262 error.c_str());
263 return nullptr;
264 }
David Pursell2ec418a2016-01-20 08:32:08 -0800265 }
266 }
267
268 while (true) {
269 if (!host.empty()) {
270 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800271 if (protocol == Socket::Protocol::kTcp) {
272 transport = tcp::Connect(host, port, &error).release();
273 } else if (protocol == Socket::Protocol::kUdp) {
274 transport = udp::Connect(host, port, &error).release();
275 }
276
David Pursell2ec418a2016-01-20 08:32:08 -0800277 if (transport == nullptr && announce) {
278 fprintf(stderr, "error: %s\n", error.c_str());
279 }
280 } else {
281 transport = usb_open(match_fastboot);
282 }
283
284 if (transport != nullptr) {
285 return transport;
286 }
287
David Pursell0b156632015-10-30 11:22:01 -0700288 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800289 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700290 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700292 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 }
294}
295
Elliott Hughesfc797672015-04-07 20:12:50 -0700296static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 // We don't actually open a USB device here,
298 // just getting our callback called so we can
299 // list all the connected devices.
300 usb_open(list_devices_callback);
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 fprintf(stderr,
305/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
306 "usage: fastboot [ <option> ] <command>\n"
307 "\n"
308 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700309 " update <filename> Reflash device from update.zip.\n"
310 " flashall Flash boot, system, vendor, and --\n"
311 " if found -- recovery.\n"
312 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
313 " flashing lock Locks the device. Prevents flashing.\n"
314 " flashing unlock Unlocks the device. Allows flashing\n"
315 " any partition except\n"
316 " bootloader-related partitions.\n"
317 " flashing lock_critical Prevents flashing bootloader-related\n"
318 " partitions.\n"
319 " flashing unlock_critical Enables flashing bootloader-related\n"
320 " partitions.\n"
321 " flashing get_unlock_ability Queries bootloader to see if the\n"
322 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700323 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700324 " unlock nonce.\n"
325 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700326 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700327 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700328 " erase <partition> Erase a flash partition.\n"
329 " format[:[<fs type>][:[<size>]] <partition>\n"
330 " Format a flash partition. Can\n"
331 " override the fs type and/or size\n"
332 " the bootloader reports.\n"
333 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800334 " set_active <suffix> Sets the active slot. If slots are\n"
335 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700336 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
337 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
338 " Create bootimage and flash it.\n"
339 " devices [-l] List all connected devices [with\n"
340 " device paths].\n"
341 " continue Continue with autoboot.\n"
342 " reboot [bootloader] Reboot device [into bootloader].\n"
343 " reboot-bootloader Reboot device into bootloader.\n"
344 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 "\n"
346 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700347 " -w Erase userdata and cache (and format\n"
348 " if supported by partition type).\n"
349 " -u Do not erase partition before\n"
350 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800351 " -s <specific device> Specify a device. For USB, provide either\n"
352 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700353 " For ethernet, provide an address in the\n"
354 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800355 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700356 " -p <product> Specify product name.\n"
357 " -c <cmdline> Override kernel commandline.\n"
358 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800359 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700360 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800361 " --kernel-offset Specify a custom kernel offset.\n"
362 " (default: 0x00008000)\n"
363 " --ramdisk-offset Specify a custom ramdisk offset.\n"
364 " (default: 0x01000000)\n"
365 " --tags-offset Specify a custom tags offset.\n"
366 " (default: 0x00000100)\n"
367 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700368 " (default: 2048).\n"
369 " -S <size>[K|M|G] Automatically sparse files greater\n"
370 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700371 " --slot <suffix> Specify slot suffix to be used if the\n"
372 " device supports slots. This will be\n"
373 " added to all partition names that use\n"
374 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800375 " to all slots. 'other' can be given to\n"
376 " refer to a non-current slot. If this\n"
377 " flag is not used, slotted partitions\n"
378 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800379 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800380 " provided, this will default to the value\n"
381 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800382 " supported, this does nothing. This will\n"
383 " run after all non-reboot commands.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800384 " --unbuffered Do not buffer input or output.\n"
385 " --version Display version.\n"
386 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388}
Elliott Hughesfc797672015-04-07 20:12:50 -0700389static void* load_bootable_image(const char* kernel, const char* ramdisk,
390 const char* secondstage, int64_t* sz,
391 const char* cmdline) {
392 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 fprintf(stderr, "no image specified\n");
394 return 0;
395 }
396
Elliott Hughesfc797672015-04-07 20:12:50 -0700397 int64_t ksize;
398 void* kdata = load_file(kernel, &ksize);
399 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800400 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 return 0;
402 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800403
Elliott Hughesfc797672015-04-07 20:12:50 -0700404 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700406 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800407
Elliott Hughesfc797672015-04-07 20:12:50 -0700408 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
410 return 0;
411 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 *sz = ksize;
414 return kdata;
415 }
416
Elliott Hughesfc797672015-04-07 20:12:50 -0700417 void* rdata = nullptr;
418 int64_t rsize = 0;
419 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700421 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800422 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 return 0;
424 }
425 }
426
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 void* sdata = nullptr;
428 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200429 if (secondstage) {
430 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700431 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200432 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
433 return 0;
434 }
435 }
436
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700438 int64_t bsize = 0;
439 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800440 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200441 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800442 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 fprintf(stderr,"failed to create boot.img\n");
445 return 0;
446 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
448 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800450
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 return bdata;
452}
453
Elliott Hughesfc797672015-04-07 20:12:50 -0700454static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455{
Yusuke Sato07447542015-06-25 14:39:19 -0700456 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700457 ZipEntry zip_entry;
458 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700459 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000460 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 }
462
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700463 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700465 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 if (data == nullptr) {
467 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000468 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 }
470
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700471 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
472 if (error != 0) {
473 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000475 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000477
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 return data;
479}
480
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700481#if defined(_WIN32)
482
483// TODO: move this to somewhere it can be shared.
484
485#include <windows.h>
486
487// Windows' tmpfile(3) requires administrator rights because
488// it creates temporary files in the root directory.
489static FILE* win32_tmpfile() {
490 char temp_path[PATH_MAX];
491 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
492 if (nchars == 0 || nchars >= sizeof(temp_path)) {
493 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
494 return nullptr;
495 }
496
497 char filename[PATH_MAX];
498 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
499 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
500 return nullptr;
501 }
502
503 return fopen(filename, "w+bTD");
504}
505
506#define tmpfile win32_tmpfile
507
508#endif
509
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700510static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
511 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700512 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700513 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
514 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700515 return -1;
516 }
517
Yusuke Sato07447542015-06-25 14:39:19 -0700518 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700519 ZipEntry zip_entry;
520 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
521 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700522 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000523 return -1;
524 }
525
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700526 int fd = fileno(fp);
527 int error = ExtractEntryToFile(zip, &zip_entry, fd);
528 if (error != 0) {
529 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700530 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700531 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700532 }
533
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000534 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700535 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700536 return fd;
537}
538
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539static char *strip(char *s)
540{
541 int n;
542 while(*s && isspace(*s)) s++;
543 n = strlen(s);
544 while(n-- > 0) {
545 if(!isspace(s[n])) break;
546 s[n] = 0;
547 }
548 return s;
549}
550
551#define MAX_OPTIONS 32
552static int setup_requirement_line(char *name)
553{
554 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700555 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 unsigned n, count;
557 char *x;
558 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800559
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560 if (!strncmp(name, "reject ", 7)) {
561 name += 7;
562 invert = 1;
563 } else if (!strncmp(name, "require ", 8)) {
564 name += 8;
565 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700566 } else if (!strncmp(name, "require-for-product:", 20)) {
567 // Get the product and point name past it
568 prod = name + 20;
569 name = strchr(name, ' ');
570 if (!name) return -1;
571 *name = 0;
572 name += 1;
573 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574 }
575
576 x = strchr(name, '=');
577 if (x == 0) return 0;
578 *x = 0;
579 val[0] = x + 1;
580
581 for(count = 1; count < MAX_OPTIONS; count++) {
582 x = strchr(val[count - 1],'|');
583 if (x == 0) break;
584 *x = 0;
585 val[count] = x + 1;
586 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800587
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588 name = strip(name);
589 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800590
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800591 name = strip(name);
592 if (name == 0) return -1;
593
Elliott Hughes253c18d2015-03-18 22:47:09 -0700594 const char* var = name;
595 // Work around an unfortunate name mismatch.
596 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597
Elliott Hughes253c18d2015-03-18 22:47:09 -0700598 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 if (out == 0) return -1;
600
601 for(n = 0; n < count; n++) {
602 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700603 if (out[n] == 0) {
604 for(size_t i = 0; i < n; ++i) {
605 free((char*) out[i]);
606 }
607 free(out);
608 return -1;
609 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 }
611
Elliott Hughes253c18d2015-03-18 22:47:09 -0700612 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 return 0;
614}
615
Elliott Hughesfc797672015-04-07 20:12:50 -0700616static void setup_requirements(char* data, int64_t sz) {
617 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700619 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 *s++ = 0;
621 if (setup_requirement_line(data)) {
622 die("out of memory");
623 }
624 data = s;
625 } else {
626 s++;
627 }
628 }
629}
630
Elliott Hughesfc797672015-04-07 20:12:50 -0700631static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632 fb_queue_notice("--------------------------------------------");
633 fb_queue_display("version-bootloader", "Bootloader Version...");
634 fb_queue_display("version-baseband", "Baseband Version.....");
635 fb_queue_display("serialno", "Serial Number........");
636 fb_queue_notice("--------------------------------------------");
637}
638
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700640{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700641 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700642 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700644 }
645
Elliott Hughesfc797672015-04-07 20:12:50 -0700646 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700647 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700648 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700649 }
650
Elliott Hughes253c18d2015-03-18 22:47:09 -0700651 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700652 if (!out_s) {
653 die("Failed to allocate sparse file array\n");
654 }
655
656 files = sparse_file_resparse(s, max_size, out_s, files);
657 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700658 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700659 }
660
661 return out_s;
662}
663
David Pursell0b156632015-10-30 11:22:01 -0700664static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700665 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700666 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
667 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800668 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700669 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700670 }
671
Elliott Hughes77c0e662015-11-02 15:51:12 -0800672 // Some bootloaders (angler, for example) send spurious whitespace too.
673 max_download_size = android::base::Trim(max_download_size);
674
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700675 uint64_t limit;
676 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800677 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700678 return 0;
679 }
680 if (limit > 0) {
681 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
682 }
Colin Crossf8387882012-05-24 17:18:41 -0700683 return limit;
684}
685
David Pursell0b156632015-10-30 11:22:01 -0700686static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700687 int64_t limit;
688
689 if (sparse_limit == 0) {
690 return 0;
691 } else if (sparse_limit > 0) {
692 limit = sparse_limit;
693 } else {
694 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700695 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700696 }
697 if (target_sparse_limit > 0) {
698 limit = target_sparse_limit;
699 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700700 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700701 }
702 }
703
704 if (size > limit) {
705 return limit;
706 }
707
708 return 0;
709}
710
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700711// Until we get lazy inode table init working in make_ext4fs, we need to
712// erase partitions of type ext4 before flashing a filesystem so no stale
713// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700714static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800715 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700716 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800717 return false;
718 }
719 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700720}
721
Elliott Hughes53ec4952016-05-11 12:39:27 -0700722static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700723 int64_t sz = get_file_size(fd);
724 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700725 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700726 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700727
Elliott Hughesfc797672015-04-07 20:12:50 -0700728 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700729 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700730 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700731 sparse_file** s = load_sparse_files(fd, limit);
732 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700733 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700734 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700735 buf->type = FB_BUFFER_SPARSE;
736 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700737 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700738 void* data = load_fd(fd, &sz);
739 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700740 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700741 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000742 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700743 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700744
Elliott Hughes53ec4952016-05-11 12:39:27 -0700745 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700746}
747
Elliott Hughes53ec4952016-05-11 12:39:27 -0700748static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
749 int fd = open(fname, O_RDONLY | O_BINARY);
750 if (fd == -1) {
751 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700752 }
David Pursell0b156632015-10-30 11:22:01 -0700753 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700754}
755
756static void flash_buf(const char *pname, struct fastboot_buffer *buf)
757{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700758 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700759
760 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800761 case FB_BUFFER_SPARSE: {
762 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700763 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700764 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700765 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800766 sparse_files.emplace_back(*s, sz);
767 ++s;
768 }
769
770 for (size_t i = 0; i < sparse_files.size(); ++i) {
771 const auto& pair = sparse_files[i];
772 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700773 }
774 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800775 }
776
Rom Lemarchand622810c2013-06-28 09:54:59 -0700777 case FB_BUFFER:
778 fb_queue_flash(pname, buf->data, buf->sz);
779 break;
780 default:
781 die("unknown buffer type: %d", buf->type);
782 }
783}
784
David Pursell0b156632015-10-30 11:22:01 -0700785static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700786 std::vector<std::string> suffixes;
787 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700788 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700789 die("Could not get suffixes.\n");
790 }
791 return android::base::Split(suffix_list, ",");
792}
793
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800794static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700795 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800796 if (allow_all) {
797 return "all";
798 } else {
799 std::vector<std::string> suffixes = get_suffixes(transport);
800 if (!suffixes.empty()) {
801 return suffixes[0];
802 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800803 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800804 }
805 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700806 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800807
David Pursell0b156632015-10-30 11:22:01 -0700808 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800809
810 if (strcmp(slot, "other") == 0) {
811 std::string current_slot;
812 if (!fb_getvar(transport, "current-slot", &current_slot)) {
813 die("Failed to identify current slot.");
814 }
815 if (!suffixes.empty()) {
816 for (size_t i = 0; i < suffixes.size(); i++) {
817 if (current_slot == suffixes[i])
818 return suffixes[(i+1)%suffixes.size()];
819 }
820 } else {
821 die("No known slots.");
822 }
823 }
824
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700825 for (const std::string &suffix : suffixes) {
826 if (suffix == slot)
827 return slot;
828 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800829 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700830 for (const std::string &suffix : suffixes) {
831 fprintf(stderr, "%s\n", suffix.c_str());
832 }
833 exit(1);
834}
835
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800836static std::string verify_slot(Transport* transport, const char *slot) {
837 return verify_slot(transport, slot, true);
838}
839
David Pursell0b156632015-10-30 11:22:01 -0700840static void do_for_partition(Transport* transport, const char *part, const char *slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700841 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800842 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700843 std::string current_slot;
844
David Pursell0b156632015-10-30 11:22:01 -0700845 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800846 /* If has-slot is not supported, the answer is no. */
847 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700848 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800849 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700850 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700851 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700852 die("Failed to identify current slot.\n");
853 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800854 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700855 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800856 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700857 }
858 } else {
859 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700860 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
861 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700862 }
863 func(part);
864 }
865}
866
David Pursell0b156632015-10-30 11:22:01 -0700867/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
868 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
869 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
870 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700871 */
David Pursell0b156632015-10-30 11:22:01 -0700872static void do_for_partitions(Transport* transport, const char *part, const char *slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700873 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800874 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700875
876 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700877 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700878 die("Could not check if partition %s has slot.", part);
879 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800880 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700881 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700882 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700883 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700884 }
885 } else {
David Pursell0b156632015-10-30 11:22:01 -0700886 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700887 }
888 } else {
David Pursell0b156632015-10-30 11:22:01 -0700889 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700890 }
891}
892
David Pursell0b156632015-10-30 11:22:01 -0700893static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700894 struct fastboot_buffer buf;
895
Elliott Hughes53ec4952016-05-11 12:39:27 -0700896 if (!load_buf(transport, fname, &buf)) {
897 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700898 }
899 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700900}
901
Elliott Hughesfc797672015-04-07 20:12:50 -0700902static void do_update_signature(ZipArchiveHandle zip, char* fn) {
903 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700904 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700905 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906 fb_queue_download("signature", data, sz);
907 fb_queue_command("signature", "installing signature");
908}
909
David Pursell0b156632015-10-30 11:22:01 -0700910static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 queue_info_dump();
912
Wink Savilleb98762f2011-04-04 17:54:59 -0700913 fb_queue_query_save("product", cur_product, sizeof(cur_product));
914
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700915 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700916 int error = OpenArchive(filename, &zip);
917 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100918 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700919 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700920 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800921
Elliott Hughesfc797672015-04-07 20:12:50 -0700922 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700923 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700924 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100925 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700926 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 }
928
Elliott Hughes253c18d2015-03-18 22:47:09 -0700929 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700931 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700932 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700933 if (fd == -1) {
934 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700935 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700936 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100937 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700938 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700939 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700940 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -0700941 if (!load_buf_fd(transport, fd, &buf)) {
942 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
943 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944
945 auto update = [&](const std::string &partition) {
946 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700947 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700948 fb_queue_erase(partition.c_str());
949 }
950 flash_buf(partition.c_str(), &buf);
951 /* not closing the fd here since the sparse code keeps the fd around
952 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
953 * program exits.
954 */
955 };
David Pursell0b156632015-10-30 11:22:01 -0700956 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700958
959 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960}
961
Elliott Hughes2810d002016-04-25 14:31:18 -0700962static void do_send_signature(const char* filename) {
963 if (android::base::EndsWith(filename, ".img") == false) {
964 return;
965 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700966
Elliott Hughes2810d002016-04-25 14:31:18 -0700967 std::string sig_path = filename;
968 sig_path.erase(sig_path.size() - 4);
969 sig_path += ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -0700970
971 int64_t sz;
Elliott Hughes2810d002016-04-25 14:31:18 -0700972 void* data = load_file(sig_path, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700973 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -0700974
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800975 fb_queue_download("signature", data, sz);
976 fb_queue_command("signature", "installing signature");
977}
978
David Pursell0b156632015-10-30 11:22:01 -0700979static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980 queue_info_dump();
981
Wink Savilleb98762f2011-04-04 17:54:59 -0700982 fb_queue_query_save("product", cur_product, sizeof(cur_product));
983
Elliott Hughes2810d002016-04-25 14:31:18 -0700984 std::string fname = find_item("info", product);
985 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800986
Elliott Hughesfc797672015-04-07 20:12:50 -0700987 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700988 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700989 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700990
991 setup_requirements(reinterpret_cast<char*>(data), sz);
992
Elliott Hughes749ae2d2016-06-28 14:48:45 -0700993 for (size_t i = 0; i < arraysize(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700994 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700995 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -0700996 if (!load_buf(transport, fname.c_str(), &buf)) {
997 if (images[i].is_optional) continue;
998 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700999 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000
1001 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001002 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001003 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001004 fb_queue_erase(partition.c_str());
1005 }
1006 flash_buf(partition.c_str(), &buf);
1007 };
David Pursell0b156632015-10-30 11:22:01 -07001008 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001009 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001010}
1011
1012#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001013#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001014
Elliott Hughes45be42e2015-09-02 20:15:48 -07001015static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001016{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001017 if (argc <= 2) return 0;
1018 skip(2);
1019
1020 /*
1021 * Process unlock_bootloader, we have to load the message file
1022 * and send that to the remote device.
1023 */
1024 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001025
1026 int64_t sz;
1027 void* data = load_file(*argv, &sz);
1028 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001029 fb_queue_download("unlock_message", data, sz);
1030 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1031 skip(1);
1032 return 0;
1033}
1034
Elliott Hughes45be42e2015-09-02 20:15:48 -07001035static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001036{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001037 char command[256];
1038 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001039
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001040 command[0] = 0;
1041 while(1) {
1042 strcat(command,*argv);
1043 skip(1);
1044 if(argc == 0) break;
1045 strcat(command," ");
1046 }
1047
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001048 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001049 return 0;
1050}
1051
Colin Crossf8387882012-05-24 17:18:41 -07001052static int64_t parse_num(const char *arg)
1053{
1054 char *endptr;
1055 unsigned long long num;
1056
1057 num = strtoull(arg, &endptr, 0);
1058 if (endptr == arg) {
1059 return -1;
1060 }
1061
1062 if (*endptr == 'k' || *endptr == 'K') {
1063 if (num >= (-1ULL) / 1024) {
1064 return -1;
1065 }
1066 num *= 1024LL;
1067 endptr++;
1068 } else if (*endptr == 'm' || *endptr == 'M') {
1069 if (num >= (-1ULL) / (1024 * 1024)) {
1070 return -1;
1071 }
1072 num *= 1024LL * 1024LL;
1073 endptr++;
1074 } else if (*endptr == 'g' || *endptr == 'G') {
1075 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1076 return -1;
1077 }
1078 num *= 1024LL * 1024LL * 1024LL;
1079 endptr++;
1080 }
1081
1082 if (*endptr != '\0') {
1083 return -1;
1084 }
1085
1086 if (num > INT64_MAX) {
1087 return -1;
1088 }
1089
1090 return num;
1091}
1092
David Pursell0b156632015-10-30 11:22:01 -07001093static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001094 const char* partition, int skip_if_not_supported,
1095 const char* type_override, const char* size_override) {
1096 std::string partition_type, partition_size;
1097
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001098 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001099 const char* errMsg = nullptr;
1100 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001101 int fd;
1102
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001103 unsigned int limit = INT_MAX;
1104 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001105 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001106 }
1107 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001108 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001109 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001110
David Pursell0b156632015-10-30 11:22:01 -07001111 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001112 errMsg = "Can't determine partition type.\n";
1113 goto failed;
1114 }
JP Abgrall7e859742014-05-06 15:14:15 -07001115 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001116 if (partition_type != type_override) {
1117 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1118 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001119 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001120 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001121 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001122
David Pursell0b156632015-10-30 11:22:01 -07001123 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001124 errMsg = "Unable to get partition size\n";
1125 goto failed;
1126 }
JP Abgrall7e859742014-05-06 15:14:15 -07001127 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001128 if (partition_size != size_override) {
1129 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1130 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001131 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001132 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001133 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001134 // Some bootloaders (angler, for example), send spurious leading whitespace.
1135 partition_size = android::base::Trim(partition_size);
1136 // Some bootloaders (hammerhead, for example) use implicit hex.
1137 // This code used to use strtol with base 16.
1138 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001139
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001140 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001141 if (!gen) {
1142 if (skip_if_not_supported) {
1143 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001144 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001145 return;
1146 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001147 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1148 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001149 return;
1150 }
1151
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001152 int64_t size;
1153 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1154 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1155 return;
1156 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001157
1158 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001159 if (fs_generator_generate(gen, fd, size)) {
1160 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001161 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001162 return;
1163 }
1164
Elliott Hughes53ec4952016-05-11 12:39:27 -07001165 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001166 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001167 close(fd);
1168 return;
1169 }
1170 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001171 return;
1172
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001173failed:
1174 if (skip_if_not_supported) {
1175 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001176 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001177 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001178 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001179}
1180
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001181int main(int argc, char **argv)
1182{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001183 bool wants_wipe = false;
1184 bool wants_reboot = false;
1185 bool wants_reboot_bootloader = false;
1186 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001187 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001188 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001189 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001190 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001191 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001192 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193
JP Abgrall7b8970c2013-03-07 17:06:41 -08001194 const struct option longopts[] = {
1195 {"base", required_argument, 0, 'b'},
1196 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001197 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001198 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001199 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001200 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001201 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001202 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001203 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001204 {"help", no_argument, 0, 'h'},
1205 {"unbuffered", no_argument, 0, 0},
1206 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001207 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001208 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001209 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001210 {0, 0, 0, 0}
1211 };
Colin Cross8879f982012-05-22 17:53:34 -07001212
1213 serial = getenv("ANDROID_SERIAL");
1214
1215 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001216 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001217 if (c < 0) {
1218 break;
1219 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001220 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001221 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001222 case 'a':
1223 wants_set_active = true;
1224 if (optarg)
1225 next_active = optarg;
1226 break;
Colin Cross8879f982012-05-22 17:53:34 -07001227 case 'b':
1228 base_addr = strtoul(optarg, 0, 16);
1229 break;
Colin Cross8879f982012-05-22 17:53:34 -07001230 case 'c':
1231 cmdline = optarg;
1232 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001233 case 'h':
1234 usage();
1235 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001236 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001237 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001238 unsigned long val;
1239
1240 val = strtoul(optarg, &endptr, 0);
1241 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1242 die("invalid vendor id '%s'", optarg);
1243 vendor_id = (unsigned short)val;
1244 break;
1245 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001246 case 'k':
1247 kernel_offset = strtoul(optarg, 0, 16);
1248 break;
1249 case 'l':
1250 long_listing = 1;
1251 break;
1252 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001253 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001254 if (!page_size) die("invalid page size");
1255 break;
1256 case 'p':
1257 product = optarg;
1258 break;
1259 case 'r':
1260 ramdisk_offset = strtoul(optarg, 0, 16);
1261 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001262 case 't':
1263 tags_offset = strtoul(optarg, 0, 16);
1264 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001265 case 's':
1266 serial = optarg;
1267 break;
1268 case 'S':
1269 sparse_limit = parse_num(optarg);
1270 if (sparse_limit < 0) {
1271 die("invalid sparse limit");
1272 }
1273 break;
1274 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001275 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001276 break;
1277 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001278 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001279 break;
Colin Cross8879f982012-05-22 17:53:34 -07001280 case '?':
1281 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001282 case 0:
1283 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001284 setvbuf(stdout, nullptr, _IONBF, 0);
1285 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001286 } else if (strcmp("version", longopts[longindex].name) == 0) {
1287 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1288 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001289 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1290 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001291 }
1292 break;
Colin Cross8879f982012-05-22 17:53:34 -07001293 default:
1294 abort();
1295 }
1296 }
1297
1298 argc -= optind;
1299 argv += optind;
1300
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001301 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001303 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001304 }
1305
Colin Cross8fb6e062012-07-24 16:36:41 -07001306 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001307 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001308 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001309 return 0;
1310 }
1311
Colin Crossc7b75dc2012-08-29 18:17:06 -07001312 if (argc > 0 && !strcmp(*argv, "help")) {
1313 usage();
1314 return 0;
1315 }
1316
David Pursell0b156632015-10-30 11:22:01 -07001317 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001318 if (transport == nullptr) {
1319 return 1;
1320 }
1321
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001322 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001323 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001324 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001325 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001326
1327 if (wants_set_active) {
1328 if (next_active == "") {
1329 if (slot_override == "") {
1330 wants_set_active = false;
1331 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001332 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001333 }
1334 }
1335 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001336
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001337 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001338 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001339 require(2);
1340 fb_queue_display(argv[1], argv[1]);
1341 skip(2);
1342 } else if(!strcmp(*argv, "erase")) {
1343 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001344
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001345 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001346 std::string partition_type;
1347 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001348 fs_get_generator(partition_type) != nullptr) {
1349 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1350 partition_type.c_str());
1351 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001352
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001353 fb_queue_erase(partition.c_str());
1354 };
David Pursell0b156632015-10-30 11:22:01 -07001355 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001356 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001357 } else if(!strncmp(*argv, "format", strlen("format"))) {
1358 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001359 char *type_override = nullptr;
1360 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001361 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001362 /*
1363 * Parsing for: "format[:[type][:[size]]]"
1364 * Some valid things:
1365 * - select ontly the size, and leave default fs type:
1366 * format::0x4000000 userdata
1367 * - default fs type and size:
1368 * format userdata
1369 * format:: userdata
1370 */
1371 overrides = strchr(*argv, ':');
1372 if (overrides) {
1373 overrides++;
1374 size_override = strchr(overrides, ':');
1375 if (size_override) {
1376 size_override[0] = '\0';
1377 size_override++;
1378 }
1379 type_override = overrides;
1380 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001381 if (type_override && !type_override[0]) type_override = nullptr;
1382 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001383
1384 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001385 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001386 fb_queue_erase(partition.c_str());
1387 }
David Pursell0b156632015-10-30 11:22:01 -07001388 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001389 };
David Pursell0b156632015-10-30 11:22:01 -07001390 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001391 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001392 } else if(!strcmp(*argv, "signature")) {
1393 require(2);
1394 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001395 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001396 if (sz != 256) die("signature must be 256 bytes");
1397 fb_queue_download("signature", data, sz);
1398 fb_queue_command("signature", "installing signature");
1399 skip(2);
1400 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001401 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001402 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001403 if (argc > 0) {
1404 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001405 wants_reboot = false;
1406 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001407 skip(1);
1408 }
1409 }
1410 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001411 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001412 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001413 skip(1);
1414 } else if (!strcmp(*argv, "continue")) {
1415 fb_queue_command("continue", "resuming boot");
1416 skip(1);
1417 } else if(!strcmp(*argv, "boot")) {
1418 char *kname = 0;
1419 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001420 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001421 skip(1);
1422 if (argc > 0) {
1423 kname = argv[0];
1424 skip(1);
1425 }
1426 if (argc > 0) {
1427 rname = argv[0];
1428 skip(1);
1429 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001430 if (argc > 0) {
1431 sname = argv[0];
1432 skip(1);
1433 }
1434 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001435 if (data == 0) return 1;
1436 fb_queue_download("boot.img", data, sz);
1437 fb_queue_command("boot", "booting");
1438 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001439 char* pname = argv[1];
1440 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001441 require(2);
1442 if (argc > 2) {
1443 fname = argv[2];
1444 skip(3);
1445 } else {
1446 fname = find_item(pname, product);
1447 skip(2);
1448 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001449 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001450
1451 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001452 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001453 fb_queue_erase(partition.c_str());
1454 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001455 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001456 };
David Pursell0b156632015-10-30 11:22:01 -07001457 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001458 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001459 char *kname = argv[2];
1460 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001461 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001462 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001463 skip(3);
1464 if (argc > 0) {
1465 rname = argv[0];
1466 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001467 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001468 if (argc > 0) {
1469 sname = argv[0];
1470 skip(1);
1471 }
1472 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001473 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001474 auto flashraw = [&](const std::string &partition) {
1475 fb_queue_flash(partition.c_str(), data, sz);
1476 };
David Pursell0b156632015-10-30 11:22:01 -07001477 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001478 } else if(!strcmp(*argv, "flashall")) {
1479 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001480 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001481 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001482 } else if(!strcmp(*argv, "update")) {
1483 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001484 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001485 skip(2);
1486 } else {
David Pursell0b156632015-10-30 11:22:01 -07001487 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001488 skip(1);
1489 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001490 wants_reboot = 1;
1491 } else if(!strcmp(*argv, "set_active")) {
1492 require(2);
1493 std::string slot = verify_slot(transport, argv[1], false);
1494 fb_set_active(slot.c_str());
1495 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001496 } else if(!strcmp(*argv, "oem")) {
1497 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001498 } else if(!strcmp(*argv, "flashing")) {
1499 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1500 !strcmp(*(argv+1), "lock") ||
1501 !strcmp(*(argv+1), "unlock_critical") ||
1502 !strcmp(*(argv+1), "lock_critical") ||
1503 !strcmp(*(argv+1), "get_unlock_ability") ||
1504 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1505 !strcmp(*(argv+1), "lock_bootloader"))) {
1506 argc = do_oem_command(argc, argv);
1507 } else
1508 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1509 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001510 } else {
1511 usage();
1512 return 1;
1513 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001514 } else {
1515 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001516 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001517 }
1518 }
1519
1520 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001521 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001523 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001524
1525 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001526 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001527 fprintf(stderr, "wiping cache...\n");
1528 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001529 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001530 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001532 if (wants_set_active) {
1533 fb_set_active(next_active.c_str());
1534 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001535 if (wants_reboot) {
1536 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001537 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001538 } else if (wants_reboot_bootloader) {
1539 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001540 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001541 }
1542
David Pursell0b156632015-10-30 11:22:01 -07001543 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001544}