blob: 1839d259829157a3bbd1244a166683b8fbfabbd7 [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 Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080051#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070052#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080053#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070054#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070055#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Elliott Hughes253c18d2015-03-18 22:47:09 -070057#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080058#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070060#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080061#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070062#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080063#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070064#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Colin Crossf8387882012-05-24 17:18:41 -070066#ifndef O_BINARY
67#define O_BINARY 0
68#endif
69
Rom Lemarchand622810c2013-06-28 09:54:59 -070070#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
71
Wink Savilleb98762f2011-04-04 17:54:59 -070072char cur_product[FB_RESPONSE_SZ + 1];
73
David Pursell2ec418a2016-01-20 08:32:08 -080074static const char* serial = nullptr;
75static const char* product = nullptr;
76static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070078static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070079static int64_t sparse_limit = -1;
80static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Elliott Hughesfc797672015-04-07 20:12:50 -070082static unsigned page_size = 2048;
83static unsigned base_addr = 0x10000000;
84static unsigned kernel_offset = 0x00008000;
85static unsigned ramdisk_offset = 0x01000000;
86static unsigned second_offset = 0x00f00000;
87static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080088
Rom Lemarchand622810c2013-06-28 09:54:59 -070089enum fb_buffer_type {
90 FB_BUFFER,
91 FB_BUFFER_SPARSE,
92};
93
94struct fastboot_buffer {
95 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070096 void* data;
97 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070098};
99
100static struct {
101 char img_name[13];
102 char sig_name[13];
103 char part_name[9];
104 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700105} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106 {"boot.img", "boot.sig", "boot", false},
107 {"recovery.img", "recovery.sig", "recovery", true},
108 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700109 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700111
Elliott Hughes2810d002016-04-25 14:31:18 -0700112static std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700113 const char *fn;
Elliott Hughes2810d002016-04-25 14:31:18 -0700114 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 fn = "boot.img";
116 } else if(!strcmp(item,"recovery")) {
117 fn = "recovery.img";
118 } else if(!strcmp(item,"system")) {
119 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700120 } else if(!strcmp(item,"vendor")) {
121 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 } else if(!strcmp(item,"userdata")) {
123 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700124 } else if(!strcmp(item,"cache")) {
125 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"info")) {
127 fn = "android-info.txt";
128 } else {
129 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700130 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 }
132
Elliott Hughes2810d002016-04-25 14:31:18 -0700133 if (product) {
134 std::string path = get_my_path();
135 path.erase(path.find_last_of('/'));
136 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
137 path.c_str(), product, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800139
Elliott Hughes2810d002016-04-25 14:31:18 -0700140 char* dir = getenv("ANDROID_PRODUCT_OUT");
141 if (dir == nullptr || dir[0] == '\0') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800144
Elliott Hughes2810d002016-04-25 14:31:18 -0700145 return android::base::StringPrintf("%s/%s", dir, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146}
147
Elliott Hughesfc797672015-04-07 20:12:50 -0700148static int64_t get_file_size(int fd) {
149 struct stat sb;
150 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700151}
152
Elliott Hughesfc797672015-04-07 20:12:50 -0700153static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800154 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700155 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157 *sz = get_file_size(fd);
158 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700159 goto oops;
160 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162 data = (char*) malloc(*sz);
163 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 close(fd);
167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return data;
169
170oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800171 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 close(fd);
173 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800174 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 return 0;
176}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughes2810d002016-04-25 14:31:18 -0700178static void* load_file(const std::string& path, int64_t* sz) {
179 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700180 if (fd == -1) return nullptr;
181 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700182}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700185 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700186 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700187 return -1;
188 }
189
190 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
191 return -1;
192 }
193
Scott Anderson13081c62012-04-06 12:39:30 -0700194 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700196 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
197 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 return 0;
199}
200
Elliott Hughesfc797672015-04-07 20:12:50 -0700201static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800202 return match_fastboot_with_serial(info, serial);
203}
204
Elliott Hughesfc797672015-04-07 20:12:50 -0700205static int list_devices_callback(usb_ifc_info* info) {
206 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800207 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700208 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800209 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700210 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 if (!serial[0]) {
212 serial = "????????????";
213 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700214 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700215 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800216 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700217 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 printf("%-22s fastboot", serial.c_str());
219 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700220 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 }
223
224 return -1;
225}
226
David Pursell2ec418a2016-01-20 08:32:08 -0800227// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
228// a specific device, otherwise the first USB device found will be used.
229//
230// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
231// Otherwise it blocks until the target is available.
232//
233// The returned Transport is a singleton, so multiple calls to this function will return the same
234// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700235static Transport* open_device() {
236 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800237 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238
David Pursell2ec418a2016-01-20 08:32:08 -0800239 if (transport != nullptr) {
240 return transport;
241 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800242
David Pursell4601c972016-02-05 15:35:09 -0800243 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800244 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800245 int port = 0;
246 if (serial != nullptr) {
247 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800248
David Pursell4601c972016-02-05 15:35:09 -0800249 if (android::base::StartsWith(serial, "tcp:")) {
250 protocol = Socket::Protocol::kTcp;
251 port = tcp::kDefaultPort;
252 net_address = serial + strlen("tcp:");
253 } else if (android::base::StartsWith(serial, "udp:")) {
254 protocol = Socket::Protocol::kUdp;
255 port = udp::kDefaultPort;
256 net_address = serial + strlen("udp:");
257 }
258
259 if (net_address != nullptr) {
260 std::string error;
261 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
262 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
263 error.c_str());
264 return nullptr;
265 }
David Pursell2ec418a2016-01-20 08:32:08 -0800266 }
267 }
268
269 while (true) {
270 if (!host.empty()) {
271 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800272 if (protocol == Socket::Protocol::kTcp) {
273 transport = tcp::Connect(host, port, &error).release();
274 } else if (protocol == Socket::Protocol::kUdp) {
275 transport = udp::Connect(host, port, &error).release();
276 }
277
David Pursell2ec418a2016-01-20 08:32:08 -0800278 if (transport == nullptr && announce) {
279 fprintf(stderr, "error: %s\n", error.c_str());
280 }
281 } else {
282 transport = usb_open(match_fastboot);
283 }
284
285 if (transport != nullptr) {
286 return transport;
287 }
288
David Pursell0b156632015-10-30 11:22:01 -0700289 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800290 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700291 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700293 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 }
295}
296
Elliott Hughesfc797672015-04-07 20:12:50 -0700297static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 // We don't actually open a USB device here,
299 // just getting our callback called so we can
300 // list all the connected devices.
301 usb_open(list_devices_callback);
302}
303
Elliott Hughesfc797672015-04-07 20:12:50 -0700304static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 fprintf(stderr,
306/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
307 "usage: fastboot [ <option> ] <command>\n"
308 "\n"
309 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700310 " update <filename> Reflash device from update.zip.\n"
311 " flashall Flash boot, system, vendor, and --\n"
312 " if found -- recovery.\n"
313 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
314 " flashing lock Locks the device. Prevents flashing.\n"
315 " flashing unlock Unlocks the device. Allows flashing\n"
316 " any partition except\n"
317 " bootloader-related partitions.\n"
318 " flashing lock_critical Prevents flashing bootloader-related\n"
319 " partitions.\n"
320 " flashing unlock_critical Enables flashing bootloader-related\n"
321 " partitions.\n"
322 " flashing get_unlock_ability Queries bootloader to see if the\n"
323 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700324 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700325 " unlock nonce.\n"
326 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700327 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700328 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700329 " erase <partition> Erase a flash partition.\n"
330 " format[:[<fs type>][:[<size>]] <partition>\n"
331 " Format a flash partition. Can\n"
332 " override the fs type and/or size\n"
333 " the bootloader reports.\n"
334 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800335 " set_active <suffix> Sets the active slot. If slots are\n"
336 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700337 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
338 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
339 " Create bootimage and flash it.\n"
340 " devices [-l] List all connected devices [with\n"
341 " device paths].\n"
342 " continue Continue with autoboot.\n"
343 " reboot [bootloader] Reboot device [into bootloader].\n"
344 " reboot-bootloader Reboot device into bootloader.\n"
345 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 "\n"
347 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700348 " -w Erase userdata and cache (and format\n"
349 " if supported by partition type).\n"
350 " -u Do not erase partition before\n"
351 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800352 " -s <specific device> Specify a device. For USB, provide either\n"
353 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700354 " For ethernet, provide an address in the\n"
355 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800356 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700357 " -p <product> Specify product name.\n"
358 " -c <cmdline> Override kernel commandline.\n"
359 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800360 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700361 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800362 " --kernel-offset Specify a custom kernel offset.\n"
363 " (default: 0x00008000)\n"
364 " --ramdisk-offset Specify a custom ramdisk offset.\n"
365 " (default: 0x01000000)\n"
366 " --tags-offset Specify a custom tags offset.\n"
367 " (default: 0x00000100)\n"
368 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700369 " (default: 2048).\n"
370 " -S <size>[K|M|G] Automatically sparse files greater\n"
371 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700372 " --slot <suffix> Specify slot suffix to be used if the\n"
373 " device supports slots. This will be\n"
374 " added to all partition names that use\n"
375 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800376 " to all slots. 'other' can be given to\n"
377 " refer to a non-current slot. If this\n"
378 " flag is not used, slotted partitions\n"
379 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800380 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800381 " provided, this will default to the value\n"
382 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800383 " supported, this does nothing. This will\n"
384 " run after all non-reboot commands.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800385 " --unbuffered Do not buffer input or output.\n"
386 " --version Display version.\n"
387 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389}
Elliott Hughesfc797672015-04-07 20:12:50 -0700390static void* load_bootable_image(const char* kernel, const char* ramdisk,
391 const char* secondstage, int64_t* sz,
392 const char* cmdline) {
393 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 fprintf(stderr, "no image specified\n");
395 return 0;
396 }
397
Elliott Hughesfc797672015-04-07 20:12:50 -0700398 int64_t ksize;
399 void* kdata = load_file(kernel, &ksize);
400 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800401 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return 0;
403 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800404
Elliott Hughesfc797672015-04-07 20:12:50 -0700405 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700407 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800408
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
411 return 0;
412 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800413
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800414 *sz = ksize;
415 return kdata;
416 }
417
Elliott Hughesfc797672015-04-07 20:12:50 -0700418 void* rdata = nullptr;
419 int64_t rsize = 0;
420 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800423 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 return 0;
425 }
426 }
427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 void* sdata = nullptr;
429 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200430 if (secondstage) {
431 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700432 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200433 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
434 return 0;
435 }
436 }
437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700439 int64_t bsize = 0;
440 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800441 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200442 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800443 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700444 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 fprintf(stderr,"failed to create boot.img\n");
446 return 0;
447 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700448 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
449 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 return bdata;
453}
454
Elliott Hughesfc797672015-04-07 20:12:50 -0700455static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456{
Yusuke Sato07447542015-06-25 14:39:19 -0700457 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700458 ZipEntry zip_entry;
459 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700460 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000461 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 }
463
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700464 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700466 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700467 if (data == nullptr) {
468 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 }
471
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700472 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
473 if (error != 0) {
474 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000476 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000478
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 return data;
480}
481
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700482#if defined(_WIN32)
483
484// TODO: move this to somewhere it can be shared.
485
486#include <windows.h>
487
488// Windows' tmpfile(3) requires administrator rights because
489// it creates temporary files in the root directory.
490static FILE* win32_tmpfile() {
491 char temp_path[PATH_MAX];
492 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
493 if (nchars == 0 || nchars >= sizeof(temp_path)) {
494 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
495 return nullptr;
496 }
497
498 char filename[PATH_MAX];
499 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
500 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
501 return nullptr;
502 }
503
504 return fopen(filename, "w+bTD");
505}
506
507#define tmpfile win32_tmpfile
508
509#endif
510
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700511static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
512 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700513 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700514 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
515 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700516 return -1;
517 }
518
Yusuke Sato07447542015-06-25 14:39:19 -0700519 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700520 ZipEntry zip_entry;
521 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
522 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
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));
530 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700531 }
532
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000533 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700534 return fd;
535}
536
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537static char *strip(char *s)
538{
539 int n;
540 while(*s && isspace(*s)) s++;
541 n = strlen(s);
542 while(n-- > 0) {
543 if(!isspace(s[n])) break;
544 s[n] = 0;
545 }
546 return s;
547}
548
549#define MAX_OPTIONS 32
550static int setup_requirement_line(char *name)
551{
552 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700553 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 unsigned n, count;
555 char *x;
556 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 if (!strncmp(name, "reject ", 7)) {
559 name += 7;
560 invert = 1;
561 } else if (!strncmp(name, "require ", 8)) {
562 name += 8;
563 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700564 } else if (!strncmp(name, "require-for-product:", 20)) {
565 // Get the product and point name past it
566 prod = name + 20;
567 name = strchr(name, ' ');
568 if (!name) return -1;
569 *name = 0;
570 name += 1;
571 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 }
573
574 x = strchr(name, '=');
575 if (x == 0) return 0;
576 *x = 0;
577 val[0] = x + 1;
578
579 for(count = 1; count < MAX_OPTIONS; count++) {
580 x = strchr(val[count - 1],'|');
581 if (x == 0) break;
582 *x = 0;
583 val[count] = x + 1;
584 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800585
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586 name = strip(name);
587 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800588
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 name = strip(name);
590 if (name == 0) return -1;
591
Elliott Hughes253c18d2015-03-18 22:47:09 -0700592 const char* var = name;
593 // Work around an unfortunate name mismatch.
594 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595
Elliott Hughes253c18d2015-03-18 22:47:09 -0700596 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 if (out == 0) return -1;
598
599 for(n = 0; n < count; n++) {
600 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700601 if (out[n] == 0) {
602 for(size_t i = 0; i < n; ++i) {
603 free((char*) out[i]);
604 }
605 free(out);
606 return -1;
607 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 }
609
Elliott Hughes253c18d2015-03-18 22:47:09 -0700610 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 return 0;
612}
613
Elliott Hughesfc797672015-04-07 20:12:50 -0700614static void setup_requirements(char* data, int64_t sz) {
615 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700617 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 *s++ = 0;
619 if (setup_requirement_line(data)) {
620 die("out of memory");
621 }
622 data = s;
623 } else {
624 s++;
625 }
626 }
627}
628
Elliott Hughesfc797672015-04-07 20:12:50 -0700629static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 fb_queue_notice("--------------------------------------------");
631 fb_queue_display("version-bootloader", "Bootloader Version...");
632 fb_queue_display("version-baseband", "Baseband Version.....");
633 fb_queue_display("serialno", "Serial Number........");
634 fb_queue_notice("--------------------------------------------");
635}
636
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700638{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700639 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700640 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700642 }
643
Elliott Hughesfc797672015-04-07 20:12:50 -0700644 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700645 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700647 }
648
Elliott Hughes253c18d2015-03-18 22:47:09 -0700649 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700650 if (!out_s) {
651 die("Failed to allocate sparse file array\n");
652 }
653
654 files = sparse_file_resparse(s, max_size, out_s, files);
655 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700656 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700657 }
658
659 return out_s;
660}
661
David Pursell0b156632015-10-30 11:22:01 -0700662static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700663 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700664 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
665 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800666 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700667 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700668 }
669
Elliott Hughes77c0e662015-11-02 15:51:12 -0800670 // Some bootloaders (angler, for example) send spurious whitespace too.
671 max_download_size = android::base::Trim(max_download_size);
672
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700673 uint64_t limit;
674 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800675 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700676 return 0;
677 }
678 if (limit > 0) {
679 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
680 }
Colin Crossf8387882012-05-24 17:18:41 -0700681 return limit;
682}
683
David Pursell0b156632015-10-30 11:22:01 -0700684static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700685 int64_t limit;
686
687 if (sparse_limit == 0) {
688 return 0;
689 } else if (sparse_limit > 0) {
690 limit = sparse_limit;
691 } else {
692 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700693 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700694 }
695 if (target_sparse_limit > 0) {
696 limit = target_sparse_limit;
697 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700698 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700699 }
700 }
701
702 if (size > limit) {
703 return limit;
704 }
705
706 return 0;
707}
708
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700709// Until we get lazy inode table init working in make_ext4fs, we need to
710// erase partitions of type ext4 before flashing a filesystem so no stale
711// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700712static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800713 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700714 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800715 return false;
716 }
717 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700718}
719
Elliott Hughes53ec4952016-05-11 12:39:27 -0700720static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700721 int64_t sz = get_file_size(fd);
722 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700723 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700724 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700725
Elliott Hughesfc797672015-04-07 20:12:50 -0700726 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700727 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700728 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700729 sparse_file** s = load_sparse_files(fd, limit);
730 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700731 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700732 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700733 buf->type = FB_BUFFER_SPARSE;
734 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700735 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700736 void* data = load_fd(fd, &sz);
737 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700738 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700739 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000740 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700741 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742
Elliott Hughes53ec4952016-05-11 12:39:27 -0700743 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700744}
745
Elliott Hughes53ec4952016-05-11 12:39:27 -0700746static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
747 int fd = open(fname, O_RDONLY | O_BINARY);
748 if (fd == -1) {
749 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700750 }
David Pursell0b156632015-10-30 11:22:01 -0700751 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700752}
753
754static void flash_buf(const char *pname, struct fastboot_buffer *buf)
755{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700756 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700757
758 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800759 case FB_BUFFER_SPARSE: {
760 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700761 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700762 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700763 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800764 sparse_files.emplace_back(*s, sz);
765 ++s;
766 }
767
768 for (size_t i = 0; i < sparse_files.size(); ++i) {
769 const auto& pair = sparse_files[i];
770 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700771 }
772 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800773 }
774
Rom Lemarchand622810c2013-06-28 09:54:59 -0700775 case FB_BUFFER:
776 fb_queue_flash(pname, buf->data, buf->sz);
777 break;
778 default:
779 die("unknown buffer type: %d", buf->type);
780 }
781}
782
David Pursell0b156632015-10-30 11:22:01 -0700783static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700784 std::vector<std::string> suffixes;
785 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700786 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700787 die("Could not get suffixes.\n");
788 }
789 return android::base::Split(suffix_list, ",");
790}
791
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800792static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700793 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800794 if (allow_all) {
795 return "all";
796 } else {
797 std::vector<std::string> suffixes = get_suffixes(transport);
798 if (!suffixes.empty()) {
799 return suffixes[0];
800 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800801 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800802 }
803 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700804 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800805
David Pursell0b156632015-10-30 11:22:01 -0700806 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800807
808 if (strcmp(slot, "other") == 0) {
809 std::string current_slot;
810 if (!fb_getvar(transport, "current-slot", &current_slot)) {
811 die("Failed to identify current slot.");
812 }
813 if (!suffixes.empty()) {
814 for (size_t i = 0; i < suffixes.size(); i++) {
815 if (current_slot == suffixes[i])
816 return suffixes[(i+1)%suffixes.size()];
817 }
818 } else {
819 die("No known slots.");
820 }
821 }
822
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700823 for (const std::string &suffix : suffixes) {
824 if (suffix == slot)
825 return slot;
826 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800827 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700828 for (const std::string &suffix : suffixes) {
829 fprintf(stderr, "%s\n", suffix.c_str());
830 }
831 exit(1);
832}
833
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800834static std::string verify_slot(Transport* transport, const char *slot) {
835 return verify_slot(transport, slot, true);
836}
837
David Pursell0b156632015-10-30 11:22:01 -0700838static void do_for_partition(Transport* transport, const char *part, const char *slot,
839 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800840 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700841 std::string current_slot;
842
David Pursell0b156632015-10-30 11:22:01 -0700843 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800844 /* If has-slot is not supported, the answer is no. */
845 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700846 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800847 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700848 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700849 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700850 die("Failed to identify current slot.\n");
851 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800852 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700853 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800854 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700855 }
856 } else {
857 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700858 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
859 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700860 }
861 func(part);
862 }
863}
864
David Pursell0b156632015-10-30 11:22:01 -0700865/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
866 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
867 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
868 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700869 */
David Pursell0b156632015-10-30 11:22:01 -0700870static void do_for_partitions(Transport* transport, const char *part, const char *slot,
871 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800872 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700873
874 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700875 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700876 die("Could not check if partition %s has slot.", part);
877 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800878 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700879 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700880 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700881 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700882 }
883 } else {
David Pursell0b156632015-10-30 11:22:01 -0700884 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700885 }
886 } else {
David Pursell0b156632015-10-30 11:22:01 -0700887 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700888 }
889}
890
David Pursell0b156632015-10-30 11:22:01 -0700891static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700892 struct fastboot_buffer buf;
893
Elliott Hughes53ec4952016-05-11 12:39:27 -0700894 if (!load_buf(transport, fname, &buf)) {
895 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700896 }
897 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700898}
899
Elliott Hughesfc797672015-04-07 20:12:50 -0700900static void do_update_signature(ZipArchiveHandle zip, char* fn) {
901 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700902 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700903 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 fb_queue_download("signature", data, sz);
905 fb_queue_command("signature", "installing signature");
906}
907
David Pursell0b156632015-10-30 11:22:01 -0700908static 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 -0800909 queue_info_dump();
910
Wink Savilleb98762f2011-04-04 17:54:59 -0700911 fb_queue_query_save("product", cur_product, sizeof(cur_product));
912
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700913 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700914 int error = OpenArchive(filename, &zip);
915 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100916 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700917 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700918 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919
Elliott Hughesfc797672015-04-07 20:12:50 -0700920 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700921 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700922 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100923 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700924 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925 }
926
Elliott Hughes253c18d2015-03-18 22:47:09 -0700927 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800928
Elliott Hughesacdbe922015-06-02 21:37:05 -0700929 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700930 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700931 if (fd == -1) {
932 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700933 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700934 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100935 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700936 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700937 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700938 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -0700939 if (!load_buf_fd(transport, fd, &buf)) {
940 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
941 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700942
943 auto update = [&](const std::string &partition) {
944 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700945 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700946 fb_queue_erase(partition.c_str());
947 }
948 flash_buf(partition.c_str(), &buf);
949 /* not closing the fd here since the sparse code keeps the fd around
950 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
951 * program exits.
952 */
953 };
David Pursell0b156632015-10-30 11:22:01 -0700954 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800955 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700956
957 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958}
959
Elliott Hughes2810d002016-04-25 14:31:18 -0700960static void do_send_signature(const char* filename) {
961 if (android::base::EndsWith(filename, ".img") == false) {
962 return;
963 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700964
Elliott Hughes2810d002016-04-25 14:31:18 -0700965 std::string sig_path = filename;
966 sig_path.erase(sig_path.size() - 4);
967 sig_path += ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -0700968
969 int64_t sz;
Elliott Hughes2810d002016-04-25 14:31:18 -0700970 void* data = load_file(sig_path, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700971 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -0700972
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800973 fb_queue_download("signature", data, sz);
974 fb_queue_command("signature", "installing signature");
975}
976
David Pursell0b156632015-10-30 11:22:01 -0700977static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800978 queue_info_dump();
979
Wink Savilleb98762f2011-04-04 17:54:59 -0700980 fb_queue_query_save("product", cur_product, sizeof(cur_product));
981
Elliott Hughes2810d002016-04-25 14:31:18 -0700982 std::string fname = find_item("info", product);
983 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800984
Elliott Hughesfc797672015-04-07 20:12:50 -0700985 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700986 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700987 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700988
989 setup_requirements(reinterpret_cast<char*>(data), sz);
990
991 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700992 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700993 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -0700994 if (!load_buf(transport, fname.c_str(), &buf)) {
995 if (images[i].is_optional) continue;
996 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700997 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998
999 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001000 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001001 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001002 fb_queue_erase(partition.c_str());
1003 }
1004 flash_buf(partition.c_str(), &buf);
1005 };
David Pursell0b156632015-10-30 11:22:01 -07001006 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001008}
1009
1010#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001011#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001012
Elliott Hughes45be42e2015-09-02 20:15:48 -07001013static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001014{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001015 if (argc <= 2) return 0;
1016 skip(2);
1017
1018 /*
1019 * Process unlock_bootloader, we have to load the message file
1020 * and send that to the remote device.
1021 */
1022 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001023
1024 int64_t sz;
1025 void* data = load_file(*argv, &sz);
1026 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001027 fb_queue_download("unlock_message", data, sz);
1028 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1029 skip(1);
1030 return 0;
1031}
1032
Elliott Hughes45be42e2015-09-02 20:15:48 -07001033static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001034{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 char command[256];
1036 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001037
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001038 command[0] = 0;
1039 while(1) {
1040 strcat(command,*argv);
1041 skip(1);
1042 if(argc == 0) break;
1043 strcat(command," ");
1044 }
1045
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001046 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047 return 0;
1048}
1049
Colin Crossf8387882012-05-24 17:18:41 -07001050static int64_t parse_num(const char *arg)
1051{
1052 char *endptr;
1053 unsigned long long num;
1054
1055 num = strtoull(arg, &endptr, 0);
1056 if (endptr == arg) {
1057 return -1;
1058 }
1059
1060 if (*endptr == 'k' || *endptr == 'K') {
1061 if (num >= (-1ULL) / 1024) {
1062 return -1;
1063 }
1064 num *= 1024LL;
1065 endptr++;
1066 } else if (*endptr == 'm' || *endptr == 'M') {
1067 if (num >= (-1ULL) / (1024 * 1024)) {
1068 return -1;
1069 }
1070 num *= 1024LL * 1024LL;
1071 endptr++;
1072 } else if (*endptr == 'g' || *endptr == 'G') {
1073 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1074 return -1;
1075 }
1076 num *= 1024LL * 1024LL * 1024LL;
1077 endptr++;
1078 }
1079
1080 if (*endptr != '\0') {
1081 return -1;
1082 }
1083
1084 if (num > INT64_MAX) {
1085 return -1;
1086 }
1087
1088 return num;
1089}
1090
David Pursell0b156632015-10-30 11:22:01 -07001091static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001092 const char* partition, int skip_if_not_supported,
1093 const char* type_override, const char* size_override) {
1094 std::string partition_type, partition_size;
1095
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001096 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001097 const char* errMsg = nullptr;
1098 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001099 int fd;
1100
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001101 unsigned int limit = INT_MAX;
1102 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001103 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001104 }
1105 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001106 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001107 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001108
David Pursell0b156632015-10-30 11:22:01 -07001109 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001110 errMsg = "Can't determine partition type.\n";
1111 goto failed;
1112 }
JP Abgrall7e859742014-05-06 15:14:15 -07001113 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001114 if (partition_type != type_override) {
1115 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1116 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001117 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001118 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001119 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001120
David Pursell0b156632015-10-30 11:22:01 -07001121 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001122 errMsg = "Unable to get partition size\n";
1123 goto failed;
1124 }
JP Abgrall7e859742014-05-06 15:14:15 -07001125 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001126 if (partition_size != size_override) {
1127 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1128 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001129 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001130 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001131 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001132 // Some bootloaders (angler, for example), send spurious leading whitespace.
1133 partition_size = android::base::Trim(partition_size);
1134 // Some bootloaders (hammerhead, for example) use implicit hex.
1135 // This code used to use strtol with base 16.
1136 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001137
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001138 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001139 if (!gen) {
1140 if (skip_if_not_supported) {
1141 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001142 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001143 return;
1144 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001145 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1146 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001147 return;
1148 }
1149
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001150 int64_t size;
1151 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1152 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1153 return;
1154 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001155
1156 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001157 if (fs_generator_generate(gen, fd, size)) {
1158 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001159 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001160 return;
1161 }
1162
Elliott Hughes53ec4952016-05-11 12:39:27 -07001163 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001164 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001165 close(fd);
1166 return;
1167 }
1168 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001169 return;
1170
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001171failed:
1172 if (skip_if_not_supported) {
1173 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001174 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001175 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001176 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001177}
1178
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001179int main(int argc, char **argv)
1180{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001181 bool wants_wipe = false;
1182 bool wants_reboot = false;
1183 bool wants_reboot_bootloader = false;
1184 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001185 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001187 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001188 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001189 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001190 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191
JP Abgrall7b8970c2013-03-07 17:06:41 -08001192 const struct option longopts[] = {
1193 {"base", required_argument, 0, 'b'},
1194 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001195 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001196 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001197 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001198 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001199 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001200 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001201 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001202 {"help", no_argument, 0, 'h'},
1203 {"unbuffered", no_argument, 0, 0},
1204 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001205 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001206 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001207 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001208 {0, 0, 0, 0}
1209 };
Colin Cross8879f982012-05-22 17:53:34 -07001210
1211 serial = getenv("ANDROID_SERIAL");
1212
1213 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001214 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 -07001215 if (c < 0) {
1216 break;
1217 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001218 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001219 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001220 case 'a':
1221 wants_set_active = true;
1222 if (optarg)
1223 next_active = optarg;
1224 break;
Colin Cross8879f982012-05-22 17:53:34 -07001225 case 'b':
1226 base_addr = strtoul(optarg, 0, 16);
1227 break;
Colin Cross8879f982012-05-22 17:53:34 -07001228 case 'c':
1229 cmdline = optarg;
1230 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001231 case 'h':
1232 usage();
1233 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001234 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001235 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001236 unsigned long val;
1237
1238 val = strtoul(optarg, &endptr, 0);
1239 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1240 die("invalid vendor id '%s'", optarg);
1241 vendor_id = (unsigned short)val;
1242 break;
1243 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001244 case 'k':
1245 kernel_offset = strtoul(optarg, 0, 16);
1246 break;
1247 case 'l':
1248 long_listing = 1;
1249 break;
1250 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001251 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001252 if (!page_size) die("invalid page size");
1253 break;
1254 case 'p':
1255 product = optarg;
1256 break;
1257 case 'r':
1258 ramdisk_offset = strtoul(optarg, 0, 16);
1259 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001260 case 't':
1261 tags_offset = strtoul(optarg, 0, 16);
1262 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001263 case 's':
1264 serial = optarg;
1265 break;
1266 case 'S':
1267 sparse_limit = parse_num(optarg);
1268 if (sparse_limit < 0) {
1269 die("invalid sparse limit");
1270 }
1271 break;
1272 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001273 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001274 break;
1275 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001276 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001277 break;
Colin Cross8879f982012-05-22 17:53:34 -07001278 case '?':
1279 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001280 case 0:
1281 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001282 setvbuf(stdout, nullptr, _IONBF, 0);
1283 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001284 } else if (strcmp("version", longopts[longindex].name) == 0) {
1285 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1286 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001287 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1288 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001289 }
1290 break;
Colin Cross8879f982012-05-22 17:53:34 -07001291 default:
1292 abort();
1293 }
1294 }
1295
1296 argc -= optind;
1297 argv += optind;
1298
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001299 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001300 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001301 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 }
1303
Colin Cross8fb6e062012-07-24 16:36:41 -07001304 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001305 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001306 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001307 return 0;
1308 }
1309
Colin Crossc7b75dc2012-08-29 18:17:06 -07001310 if (argc > 0 && !strcmp(*argv, "help")) {
1311 usage();
1312 return 0;
1313 }
1314
David Pursell0b156632015-10-30 11:22:01 -07001315 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001316 if (transport == nullptr) {
1317 return 1;
1318 }
1319
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001320 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001321 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001322 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001323 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001324
1325 if (wants_set_active) {
1326 if (next_active == "") {
1327 if (slot_override == "") {
1328 wants_set_active = false;
1329 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001330 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001331 }
1332 }
1333 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001334
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001335 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001336 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001337 require(2);
1338 fb_queue_display(argv[1], argv[1]);
1339 skip(2);
1340 } else if(!strcmp(*argv, "erase")) {
1341 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001342
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001343 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001344 std::string partition_type;
1345 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001346 fs_get_generator(partition_type) != nullptr) {
1347 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1348 partition_type.c_str());
1349 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001350
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001351 fb_queue_erase(partition.c_str());
1352 };
David Pursell0b156632015-10-30 11:22:01 -07001353 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001354 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001355 } else if(!strncmp(*argv, "format", strlen("format"))) {
1356 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001357 char *type_override = nullptr;
1358 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001359 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001360 /*
1361 * Parsing for: "format[:[type][:[size]]]"
1362 * Some valid things:
1363 * - select ontly the size, and leave default fs type:
1364 * format::0x4000000 userdata
1365 * - default fs type and size:
1366 * format userdata
1367 * format:: userdata
1368 */
1369 overrides = strchr(*argv, ':');
1370 if (overrides) {
1371 overrides++;
1372 size_override = strchr(overrides, ':');
1373 if (size_override) {
1374 size_override[0] = '\0';
1375 size_override++;
1376 }
1377 type_override = overrides;
1378 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001379 if (type_override && !type_override[0]) type_override = nullptr;
1380 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001381
1382 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001383 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001384 fb_queue_erase(partition.c_str());
1385 }
David Pursell0b156632015-10-30 11:22:01 -07001386 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001387 };
David Pursell0b156632015-10-30 11:22:01 -07001388 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001389 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001390 } else if(!strcmp(*argv, "signature")) {
1391 require(2);
1392 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001393 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001394 if (sz != 256) die("signature must be 256 bytes");
1395 fb_queue_download("signature", data, sz);
1396 fb_queue_command("signature", "installing signature");
1397 skip(2);
1398 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001399 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001400 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001401 if (argc > 0) {
1402 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001403 wants_reboot = false;
1404 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001405 skip(1);
1406 }
1407 }
1408 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001409 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001410 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001411 skip(1);
1412 } else if (!strcmp(*argv, "continue")) {
1413 fb_queue_command("continue", "resuming boot");
1414 skip(1);
1415 } else if(!strcmp(*argv, "boot")) {
1416 char *kname = 0;
1417 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001418 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001419 skip(1);
1420 if (argc > 0) {
1421 kname = argv[0];
1422 skip(1);
1423 }
1424 if (argc > 0) {
1425 rname = argv[0];
1426 skip(1);
1427 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001428 if (argc > 0) {
1429 sname = argv[0];
1430 skip(1);
1431 }
1432 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001433 if (data == 0) return 1;
1434 fb_queue_download("boot.img", data, sz);
1435 fb_queue_command("boot", "booting");
1436 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001437 char* pname = argv[1];
1438 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001439 require(2);
1440 if (argc > 2) {
1441 fname = argv[2];
1442 skip(3);
1443 } else {
1444 fname = find_item(pname, product);
1445 skip(2);
1446 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001447 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001448
1449 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001450 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001451 fb_queue_erase(partition.c_str());
1452 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001453 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001454 };
David Pursell0b156632015-10-30 11:22:01 -07001455 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001456 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001457 char *kname = argv[2];
1458 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001459 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001460 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001461 skip(3);
1462 if (argc > 0) {
1463 rname = argv[0];
1464 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001465 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001466 if (argc > 0) {
1467 sname = argv[0];
1468 skip(1);
1469 }
1470 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001471 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001472 auto flashraw = [&](const std::string &partition) {
1473 fb_queue_flash(partition.c_str(), data, sz);
1474 };
David Pursell0b156632015-10-30 11:22:01 -07001475 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476 } else if(!strcmp(*argv, "flashall")) {
1477 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001478 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001479 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001480 } else if(!strcmp(*argv, "update")) {
1481 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001482 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001483 skip(2);
1484 } else {
David Pursell0b156632015-10-30 11:22:01 -07001485 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001486 skip(1);
1487 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001488 wants_reboot = 1;
1489 } else if(!strcmp(*argv, "set_active")) {
1490 require(2);
1491 std::string slot = verify_slot(transport, argv[1], false);
1492 fb_set_active(slot.c_str());
1493 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001494 } else if(!strcmp(*argv, "oem")) {
1495 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001496 } else if(!strcmp(*argv, "flashing")) {
1497 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1498 !strcmp(*(argv+1), "lock") ||
1499 !strcmp(*(argv+1), "unlock_critical") ||
1500 !strcmp(*(argv+1), "lock_critical") ||
1501 !strcmp(*(argv+1), "get_unlock_ability") ||
1502 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1503 !strcmp(*(argv+1), "lock_bootloader"))) {
1504 argc = do_oem_command(argc, argv);
1505 } else
1506 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1507 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001508 } else {
1509 usage();
1510 return 1;
1511 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001512 } else {
1513 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001514 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001515 }
1516 }
1517
1518 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001519 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001520 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001521 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001522
1523 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001524 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001525 fprintf(stderr, "wiping cache...\n");
1526 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001527 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001528 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001529 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001530 if (wants_set_active) {
1531 fb_set_active(next_active.c_str());
1532 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001533 if (wants_reboot) {
1534 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001535 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001536 } else if (wants_reboot_bootloader) {
1537 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001538 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001539 }
1540
David Pursell0b156632015-10-30 11:22:01 -07001541 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001542}