blob: e2a0ead022ce406b74fac0e7c40e65c5bd3736b9 [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>
Colin Crossf8387882012-05-24 17:18:41 -070045
Elliott Hughes2fd45a92015-10-30 11:49:47 -070046#include <base/parseint.h>
Elliott Hughes77c0e662015-11-02 15:51:12 -080047#include <base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070048#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070049#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
Elliott Hughes253c18d2015-03-18 22:47:09 -070051#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070053#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Colin Crossf8387882012-05-24 17:18:41 -070055#ifndef O_BINARY
56#define O_BINARY 0
57#endif
58
Rom Lemarchand622810c2013-06-28 09:54:59 -070059#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
60
Wink Savilleb98762f2011-04-04 17:54:59 -070061char cur_product[FB_RESPONSE_SZ + 1];
62
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063static const char *serial = 0;
64static const char *product = 0;
65static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070067static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070068static int64_t sparse_limit = -1;
69static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Elliott Hughesfc797672015-04-07 20:12:50 -070071static unsigned page_size = 2048;
72static unsigned base_addr = 0x10000000;
73static unsigned kernel_offset = 0x00008000;
74static unsigned ramdisk_offset = 0x01000000;
75static unsigned second_offset = 0x00f00000;
76static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080077
Rom Lemarchand622810c2013-06-28 09:54:59 -070078enum fb_buffer_type {
79 FB_BUFFER,
80 FB_BUFFER_SPARSE,
81};
82
83struct fastboot_buffer {
84 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070085 void* data;
86 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070087};
88
89static struct {
90 char img_name[13];
91 char sig_name[13];
92 char part_name[9];
93 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070094} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070095 {"boot.img", "boot.sig", "boot", false},
96 {"recovery.img", "recovery.sig", "recovery", true},
97 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070098 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070099};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700100
Elliott Hughesfc797672015-04-07 20:12:50 -0700101static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700103 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 char path[PATH_MAX + 128];
105
106 if(!strcmp(item,"boot")) {
107 fn = "boot.img";
108 } else if(!strcmp(item,"recovery")) {
109 fn = "recovery.img";
110 } else if(!strcmp(item,"system")) {
111 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700112 } else if(!strcmp(item,"vendor")) {
113 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 } else if(!strcmp(item,"userdata")) {
115 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700116 } else if(!strcmp(item,"cache")) {
117 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else if(!strcmp(item,"info")) {
119 fn = "android-info.txt";
120 } else {
121 fprintf(stderr,"unknown partition '%s'\n", item);
122 return 0;
123 }
124
125 if(product) {
126 get_my_path(path);
127 sprintf(path + strlen(path),
128 "../../../target/product/%s/%s", product, fn);
129 return strdup(path);
130 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 dir = getenv("ANDROID_PRODUCT_OUT");
133 if((dir == 0) || (dir[0] == 0)) {
134 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
135 return 0;
136 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 sprintf(path, "%s/%s", dir, fn);
139 return strdup(path);
140}
141
Elliott Hughesfc797672015-04-07 20:12:50 -0700142static int64_t get_file_size(int fd) {
143 struct stat sb;
144 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700145}
146
Elliott Hughesfc797672015-04-07 20:12:50 -0700147static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800148 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700149 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150
Elliott Hughesfc797672015-04-07 20:12:50 -0700151 *sz = get_file_size(fd);
152 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700153 goto oops;
154 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
Elliott Hughesfc797672015-04-07 20:12:50 -0700156 data = (char*) malloc(*sz);
157 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 close(fd);
161
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 return data;
163
164oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800165 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 close(fd);
167 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800168 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 return 0;
170}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172static void* load_file(const char* fn, int64_t* sz) {
173 int fd = open(fn, O_RDONLY | O_BINARY);
174 if (fd == -1) return nullptr;
175 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700176}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700179 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700180 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700181 return -1;
182 }
183
184 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
185 return -1;
186 }
187
Scott Anderson13081c62012-04-06 12:39:30 -0700188 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700190 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
191 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 return 0;
193}
194
Elliott Hughesfc797672015-04-07 20:12:50 -0700195static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800196 return match_fastboot_with_serial(info, serial);
197}
198
Elliott Hughesfc797672015-04-07 20:12:50 -0700199static int list_devices_callback(usb_ifc_info* info) {
200 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700201 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700202 if (!info->writable) {
203 serial = "no permissions"; // like "adb devices"
204 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 if (!serial[0]) {
206 serial = "????????????";
207 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700208 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700209 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700210 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800211 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700212 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700213 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700214 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700215 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 }
217
218 return -1;
219}
220
Elliott Hughesfc797672015-04-07 20:12:50 -0700221static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 static usb_handle *usb = 0;
223 int announce = 1;
224
225 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800226
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 for(;;) {
228 usb = usb_open(match_fastboot);
229 if(usb) return usb;
230 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800231 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700232 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700234 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236}
237
Elliott Hughesfc797672015-04-07 20:12:50 -0700238static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 // We don't actually open a USB device here,
240 // just getting our callback called so we can
241 // list all the connected devices.
242 usb_open(list_devices_callback);
243}
244
Elliott Hughesfc797672015-04-07 20:12:50 -0700245static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 fprintf(stderr,
247/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
248 "usage: fastboot [ <option> ] <command>\n"
249 "\n"
250 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700251 " update <filename> Reflash device from update.zip.\n"
252 " flashall Flash boot, system, vendor, and --\n"
253 " if found -- recovery.\n"
254 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
255 " flashing lock Locks the device. Prevents flashing.\n"
256 " flashing unlock Unlocks the device. Allows flashing\n"
257 " any partition except\n"
258 " bootloader-related partitions.\n"
259 " flashing lock_critical Prevents flashing bootloader-related\n"
260 " partitions.\n"
261 " flashing unlock_critical Enables flashing bootloader-related\n"
262 " partitions.\n"
263 " flashing get_unlock_ability Queries bootloader to see if the\n"
264 " device is unlocked.\n"
265 " erase <partition> Erase a flash partition.\n"
266 " format[:[<fs type>][:[<size>]] <partition>\n"
267 " Format a flash partition. Can\n"
268 " override the fs type and/or size\n"
269 " the bootloader reports.\n"
270 " getvar <variable> Display a bootloader variable.\n"
271 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
272 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
273 " Create bootimage and flash it.\n"
274 " devices [-l] List all connected devices [with\n"
275 " device paths].\n"
276 " continue Continue with autoboot.\n"
277 " reboot [bootloader] Reboot device [into bootloader].\n"
278 " reboot-bootloader Reboot device into bootloader.\n"
279 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 "\n"
281 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700282 " -w Erase userdata and cache (and format\n"
283 " if supported by partition type).\n"
284 " -u Do not erase partition before\n"
285 " formatting.\n"
286 " -s <specific device> Specify device serial number\n"
287 " or path to device port.\n"
288 " -p <product> Specify product name.\n"
289 " -c <cmdline> Override kernel commandline.\n"
290 " -i <vendor id> Specify a custom USB vendor id.\n"
291 " -b <base_addr> Specify a custom kernel base\n"
292 " address (default: 0x10000000).\n"
293 " -n <page size> Specify the nand page size\n"
294 " (default: 2048).\n"
295 " -S <size>[K|M|G] Automatically sparse files greater\n"
296 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298}
299
Elliott Hughesfc797672015-04-07 20:12:50 -0700300static void* load_bootable_image(const char* kernel, const char* ramdisk,
301 const char* secondstage, int64_t* sz,
302 const char* cmdline) {
303 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 fprintf(stderr, "no image specified\n");
305 return 0;
306 }
307
Elliott Hughesfc797672015-04-07 20:12:50 -0700308 int64_t ksize;
309 void* kdata = load_file(kernel, &ksize);
310 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800311 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 return 0;
313 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800314
Elliott Hughesfc797672015-04-07 20:12:50 -0700315 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700317 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800318
Elliott Hughesfc797672015-04-07 20:12:50 -0700319 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
321 return 0;
322 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800323
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 *sz = ksize;
325 return kdata;
326 }
327
Elliott Hughesfc797672015-04-07 20:12:50 -0700328 void* rdata = nullptr;
329 int64_t rsize = 0;
330 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700332 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800333 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 return 0;
335 }
336 }
337
Elliott Hughesfc797672015-04-07 20:12:50 -0700338 void* sdata = nullptr;
339 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200340 if (secondstage) {
341 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700342 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200343 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
344 return 0;
345 }
346 }
347
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700349 int64_t bsize = 0;
350 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800351 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200352 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800353 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700354 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 fprintf(stderr,"failed to create boot.img\n");
356 return 0;
357 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700358 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
359 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800361
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 return bdata;
363}
364
Elliott Hughesfc797672015-04-07 20:12:50 -0700365static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366{
Yusuke Sato07447542015-06-25 14:39:19 -0700367 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700368 ZipEntry zip_entry;
369 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700370 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000371 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 }
373
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700374 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700376 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700377 if (data == nullptr) {
378 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000379 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 }
381
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700382 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
383 if (error != 0) {
384 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000386 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 return data;
390}
391
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700392#if defined(_WIN32)
393
394// TODO: move this to somewhere it can be shared.
395
396#include <windows.h>
397
398// Windows' tmpfile(3) requires administrator rights because
399// it creates temporary files in the root directory.
400static FILE* win32_tmpfile() {
401 char temp_path[PATH_MAX];
402 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
403 if (nchars == 0 || nchars >= sizeof(temp_path)) {
404 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
405 return nullptr;
406 }
407
408 char filename[PATH_MAX];
409 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
410 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
411 return nullptr;
412 }
413
414 return fopen(filename, "w+bTD");
415}
416
417#define tmpfile win32_tmpfile
418
419#endif
420
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700421static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
422 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700423 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700424 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
425 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700426 return -1;
427 }
428
Yusuke Sato07447542015-06-25 14:39:19 -0700429 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700430 ZipEntry zip_entry;
431 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
432 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000433 return -1;
434 }
435
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700436 int fd = fileno(fp);
437 int error = ExtractEntryToFile(zip, &zip_entry, fd);
438 if (error != 0) {
439 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
440 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700441 }
442
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000443 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700444 return fd;
445}
446
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447static char *strip(char *s)
448{
449 int n;
450 while(*s && isspace(*s)) s++;
451 n = strlen(s);
452 while(n-- > 0) {
453 if(!isspace(s[n])) break;
454 s[n] = 0;
455 }
456 return s;
457}
458
459#define MAX_OPTIONS 32
460static int setup_requirement_line(char *name)
461{
462 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700463 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 unsigned n, count;
465 char *x;
466 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800467
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 if (!strncmp(name, "reject ", 7)) {
469 name += 7;
470 invert = 1;
471 } else if (!strncmp(name, "require ", 8)) {
472 name += 8;
473 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700474 } else if (!strncmp(name, "require-for-product:", 20)) {
475 // Get the product and point name past it
476 prod = name + 20;
477 name = strchr(name, ' ');
478 if (!name) return -1;
479 *name = 0;
480 name += 1;
481 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 }
483
484 x = strchr(name, '=');
485 if (x == 0) return 0;
486 *x = 0;
487 val[0] = x + 1;
488
489 for(count = 1; count < MAX_OPTIONS; count++) {
490 x = strchr(val[count - 1],'|');
491 if (x == 0) break;
492 *x = 0;
493 val[count] = x + 1;
494 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800495
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 name = strip(name);
497 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 name = strip(name);
500 if (name == 0) return -1;
501
Elliott Hughes253c18d2015-03-18 22:47:09 -0700502 const char* var = name;
503 // Work around an unfortunate name mismatch.
504 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505
Elliott Hughes253c18d2015-03-18 22:47:09 -0700506 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 if (out == 0) return -1;
508
509 for(n = 0; n < count; n++) {
510 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700511 if (out[n] == 0) {
512 for(size_t i = 0; i < n; ++i) {
513 free((char*) out[i]);
514 }
515 free(out);
516 return -1;
517 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518 }
519
Elliott Hughes253c18d2015-03-18 22:47:09 -0700520 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521 return 0;
522}
523
Elliott Hughesfc797672015-04-07 20:12:50 -0700524static void setup_requirements(char* data, int64_t sz) {
525 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700527 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 *s++ = 0;
529 if (setup_requirement_line(data)) {
530 die("out of memory");
531 }
532 data = s;
533 } else {
534 s++;
535 }
536 }
537}
538
Elliott Hughesfc797672015-04-07 20:12:50 -0700539static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 fb_queue_notice("--------------------------------------------");
541 fb_queue_display("version-bootloader", "Bootloader Version...");
542 fb_queue_display("version-baseband", "Baseband Version.....");
543 fb_queue_display("serialno", "Serial Number........");
544 fb_queue_notice("--------------------------------------------");
545}
546
Rom Lemarchand622810c2013-06-28 09:54:59 -0700547static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700548{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700549 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700550 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700551 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700552 }
553
Elliott Hughesfc797672015-04-07 20:12:50 -0700554 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700555 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700556 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700557 }
558
Elliott Hughes253c18d2015-03-18 22:47:09 -0700559 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700560 if (!out_s) {
561 die("Failed to allocate sparse file array\n");
562 }
563
564 files = sparse_file_resparse(s, max_size, out_s, files);
565 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700566 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700567 }
568
569 return out_s;
570}
571
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700572static int64_t get_target_sparse_limit(usb_handle* usb) {
573 std::string max_download_size;
Elliott Hughes2030bac2015-11-03 08:14:43 -0800574 if (!fb_getvar(usb, "max-download-size", &max_download_size) || max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800575 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700576 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700577 }
578
Elliott Hughes77c0e662015-11-02 15:51:12 -0800579 // Some bootloaders (angler, for example) send spurious whitespace too.
580 max_download_size = android::base::Trim(max_download_size);
581
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700582 uint64_t limit;
583 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800584 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700585 return 0;
586 }
587 if (limit > 0) {
588 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
589 }
Colin Crossf8387882012-05-24 17:18:41 -0700590 return limit;
591}
592
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700593static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700594 int64_t limit;
595
596 if (sparse_limit == 0) {
597 return 0;
598 } else if (sparse_limit > 0) {
599 limit = sparse_limit;
600 } else {
601 if (target_sparse_limit == -1) {
602 target_sparse_limit = get_target_sparse_limit(usb);
603 }
604 if (target_sparse_limit > 0) {
605 limit = target_sparse_limit;
606 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700607 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700608 }
609 }
610
611 if (size > limit) {
612 return limit;
613 }
614
615 return 0;
616}
617
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700618// Until we get lazy inode table init working in make_ext4fs, we need to
619// erase partitions of type ext4 before flashing a filesystem so no stale
620// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800621static bool needs_erase(usb_handle* usb, const char* partition) {
622 std::string partition_type;
623 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
624 return false;
625 }
626 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700627}
628
Elliott Hughesfc797672015-04-07 20:12:50 -0700629static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
630 int64_t sz = get_file_size(fd);
631 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000632 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700633 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700634
Elliott Hughesfc797672015-04-07 20:12:50 -0700635 lseek64(fd, 0, SEEK_SET);
636 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700637 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700638 sparse_file** s = load_sparse_files(fd, limit);
639 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700640 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700641 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700642 buf->type = FB_BUFFER_SPARSE;
643 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700644 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700645 void* data = load_fd(fd, &sz);
646 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700647 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700648 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000649 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700650 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700651
652 return 0;
653}
654
655static int load_buf(usb_handle *usb, const char *fname,
656 struct fastboot_buffer *buf)
657{
658 int fd;
659
660 fd = open(fname, O_RDONLY | O_BINARY);
661 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700662 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700663 }
664
665 return load_buf_fd(usb, fd, buf);
666}
667
668static void flash_buf(const char *pname, struct fastboot_buffer *buf)
669{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700670 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700671
672 switch (buf->type) {
673 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700674 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700675 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700676 int64_t sz = sparse_file_len(*s, true, false);
677 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700678 }
679 break;
680 case FB_BUFFER:
681 fb_queue_flash(pname, buf->data, buf->sz);
682 break;
683 default:
684 die("unknown buffer type: %d", buf->type);
685 }
686}
687
Elliott Hughesfc797672015-04-07 20:12:50 -0700688static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700689 struct fastboot_buffer buf;
690
691 if (load_buf(usb, fname, &buf)) {
692 die("cannot load '%s'", fname);
693 }
694 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700695}
696
Elliott Hughesfc797672015-04-07 20:12:50 -0700697static void do_update_signature(ZipArchiveHandle zip, char* fn) {
698 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700699 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700700 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 fb_queue_download("signature", data, sz);
702 fb_queue_command("signature", "installing signature");
703}
704
Elliott Hughesfc797672015-04-07 20:12:50 -0700705static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 queue_info_dump();
707
Wink Savilleb98762f2011-04-04 17:54:59 -0700708 fb_queue_query_save("product", cur_product, sizeof(cur_product));
709
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700710 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700711 int error = OpenArchive(filename, &zip);
712 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100713 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700714 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700715 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716
Elliott Hughesfc797672015-04-07 20:12:50 -0700717 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700718 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700719 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100720 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700721 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 }
723
Elliott Hughes253c18d2015-03-18 22:47:09 -0700724 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725
Elliott Hughesacdbe922015-06-02 21:37:05 -0700726 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700727 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700728 if (fd == -1) {
729 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700730 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700731 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100732 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700733 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700734 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700735 fastboot_buffer buf;
736 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700737 if (rc) die("cannot load %s from flash", images[i].img_name);
738 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700739 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700740 fb_queue_erase(images[i].part_name);
741 }
742 flash_buf(images[i].part_name, &buf);
743 /* not closing the fd here since the sparse code keeps the fd around
744 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
745 * program exits.
746 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700748
749 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750}
751
Elliott Hughesfc797672015-04-07 20:12:50 -0700752static void do_send_signature(char* fn) {
753 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700755
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800757
Elliott Hughesfc797672015-04-07 20:12:50 -0700758 strcpy(xtn, ".sig");
759
760 int64_t sz;
761 void* data = load_file(fn, &sz);
762 strcpy(xtn, ".img");
763 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800764 fb_queue_download("signature", data, sz);
765 fb_queue_command("signature", "installing signature");
766}
767
Elliott Hughesfc797672015-04-07 20:12:50 -0700768static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 queue_info_dump();
770
Wink Savilleb98762f2011-04-04 17:54:59 -0700771 fb_queue_query_save("product", cur_product, sizeof(cur_product));
772
Elliott Hughes253c18d2015-03-18 22:47:09 -0700773 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700774 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775
Elliott Hughesfc797672015-04-07 20:12:50 -0700776 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700777 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700778 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700779
780 setup_requirements(reinterpret_cast<char*>(data), sz);
781
782 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700783 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700784 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700785 if (load_buf(usb, fname, &buf)) {
786 if (images[i].is_optional)
787 continue;
788 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700789 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700790 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700791 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792 fb_queue_erase(images[i].part_name);
793 }
794 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796}
797
798#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800799#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800
Elliott Hughesfc797672015-04-07 20:12:50 -0700801static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 char command[256];
803 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800804
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 command[0] = 0;
806 while(1) {
807 strcat(command,*argv);
808 skip(1);
809 if(argc == 0) break;
810 strcat(command," ");
811 }
812
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800813 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 return 0;
815}
816
Colin Crossf8387882012-05-24 17:18:41 -0700817static int64_t parse_num(const char *arg)
818{
819 char *endptr;
820 unsigned long long num;
821
822 num = strtoull(arg, &endptr, 0);
823 if (endptr == arg) {
824 return -1;
825 }
826
827 if (*endptr == 'k' || *endptr == 'K') {
828 if (num >= (-1ULL) / 1024) {
829 return -1;
830 }
831 num *= 1024LL;
832 endptr++;
833 } else if (*endptr == 'm' || *endptr == 'M') {
834 if (num >= (-1ULL) / (1024 * 1024)) {
835 return -1;
836 }
837 num *= 1024LL * 1024LL;
838 endptr++;
839 } else if (*endptr == 'g' || *endptr == 'G') {
840 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
841 return -1;
842 }
843 num *= 1024LL * 1024LL * 1024LL;
844 endptr++;
845 }
846
847 if (*endptr != '\0') {
848 return -1;
849 }
850
851 if (num > INT64_MAX) {
852 return -1;
853 }
854
855 return num;
856}
857
Elliott Hughesfc797672015-04-07 20:12:50 -0700858static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700859 const char* partition, int skip_if_not_supported,
860 const char* type_override, const char* size_override) {
861 std::string partition_type, partition_size;
862
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700863 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700864 const char* errMsg = nullptr;
865 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700866 int fd;
867
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700868 unsigned int limit = INT_MAX;
869 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700870 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700871 }
872 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700873 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700874 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700875
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700876 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700877 errMsg = "Can't determine partition type.\n";
878 goto failed;
879 }
JP Abgrall7e859742014-05-06 15:14:15 -0700880 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700881 if (partition_type != type_override) {
882 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
883 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700884 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700885 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700886 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700887
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700888 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700889 errMsg = "Unable to get partition size\n";
890 goto failed;
891 }
JP Abgrall7e859742014-05-06 15:14:15 -0700892 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700893 if (partition_size != size_override) {
894 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
895 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700896 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700897 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700898 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700899
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800900 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700901 if (!gen) {
902 if (skip_if_not_supported) {
903 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700904 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700905 return;
906 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700907 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
908 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700909 return;
910 }
911
Elliott Hughes2030bac2015-11-03 08:14:43 -0800912 // Some bootloaders (hammerhead, for example) use implicit hex.
913 // This code used to use strtol with base 16.
914 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
915
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700916 int64_t size;
917 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
918 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
919 return;
920 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700921
922 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700923 if (fs_generator_generate(gen, fd, size)) {
924 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700925 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700926 return;
927 }
928
929 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700930 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700931 close(fd);
932 return;
933 }
934 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700935 return;
936
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700937failed:
938 if (skip_if_not_supported) {
939 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700940 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700941 }
942 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
943}
944
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945int main(int argc, char **argv)
946{
947 int wants_wipe = 0;
948 int wants_reboot = 0;
949 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700950 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700952 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100953 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800954
JP Abgrall7b8970c2013-03-07 17:06:41 -0800955 const struct option longopts[] = {
956 {"base", required_argument, 0, 'b'},
957 {"kernel_offset", required_argument, 0, 'k'},
958 {"page_size", required_argument, 0, 'n'},
959 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800960 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700961 {"help", no_argument, 0, 'h'},
962 {"unbuffered", no_argument, 0, 0},
963 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800964 {0, 0, 0, 0}
965 };
Colin Cross8879f982012-05-22 17:53:34 -0700966
967 serial = getenv("ANDROID_SERIAL");
968
969 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700970 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -0700971 if (c < 0) {
972 break;
973 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800974 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700975 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700976 case 'b':
977 base_addr = strtoul(optarg, 0, 16);
978 break;
Colin Cross8879f982012-05-22 17:53:34 -0700979 case 'c':
980 cmdline = optarg;
981 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800982 case 'h':
983 usage();
984 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700985 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -0700986 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -0700987 unsigned long val;
988
989 val = strtoul(optarg, &endptr, 0);
990 if (!endptr || *endptr != '\0' || (val & ~0xffff))
991 die("invalid vendor id '%s'", optarg);
992 vendor_id = (unsigned short)val;
993 break;
994 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800995 case 'k':
996 kernel_offset = strtoul(optarg, 0, 16);
997 break;
998 case 'l':
999 long_listing = 1;
1000 break;
1001 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001002 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001003 if (!page_size) die("invalid page size");
1004 break;
1005 case 'p':
1006 product = optarg;
1007 break;
1008 case 'r':
1009 ramdisk_offset = strtoul(optarg, 0, 16);
1010 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001011 case 't':
1012 tags_offset = strtoul(optarg, 0, 16);
1013 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001014 case 's':
1015 serial = optarg;
1016 break;
1017 case 'S':
1018 sparse_limit = parse_num(optarg);
1019 if (sparse_limit < 0) {
1020 die("invalid sparse limit");
1021 }
1022 break;
1023 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001024 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001025 break;
1026 case 'w':
1027 wants_wipe = 1;
1028 break;
Colin Cross8879f982012-05-22 17:53:34 -07001029 case '?':
1030 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001031 case 0:
1032 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001033 setvbuf(stdout, nullptr, _IONBF, 0);
1034 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001035 } else if (strcmp("version", longopts[longindex].name) == 0) {
1036 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1037 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001038 }
1039 break;
Colin Cross8879f982012-05-22 17:53:34 -07001040 default:
1041 abort();
1042 }
1043 }
1044
1045 argc -= optind;
1046 argv += optind;
1047
1048 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001049 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001050 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001051 }
1052
Colin Cross8fb6e062012-07-24 16:36:41 -07001053 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001054 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001055 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001056 return 0;
1057 }
1058
Colin Crossc7b75dc2012-08-29 18:17:06 -07001059 if (argc > 0 && !strcmp(*argv, "help")) {
1060 usage();
1061 return 0;
1062 }
1063
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001064 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001065
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001066 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001067 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068 require(2);
1069 fb_queue_display(argv[1], argv[1]);
1070 skip(2);
1071 } else if(!strcmp(*argv, "erase")) {
1072 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001073
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001074 std::string partition_type;
1075 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1076 fs_get_generator(partition_type) != nullptr) {
1077 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1078 partition_type.c_str());
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001079 }
1080
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 fb_queue_erase(argv[1]);
1082 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001083 } else if(!strncmp(*argv, "format", strlen("format"))) {
1084 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001085 char *type_override = nullptr;
1086 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001087 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001088 /*
1089 * Parsing for: "format[:[type][:[size]]]"
1090 * Some valid things:
1091 * - select ontly the size, and leave default fs type:
1092 * format::0x4000000 userdata
1093 * - default fs type and size:
1094 * format userdata
1095 * format:: userdata
1096 */
1097 overrides = strchr(*argv, ':');
1098 if (overrides) {
1099 overrides++;
1100 size_override = strchr(overrides, ':');
1101 if (size_override) {
1102 size_override[0] = '\0';
1103 size_override++;
1104 }
1105 type_override = overrides;
1106 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001107 if (type_override && !type_override[0]) type_override = nullptr;
1108 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001109 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001110 fb_queue_erase(argv[1]);
1111 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001112 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001113 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001114 } else if(!strcmp(*argv, "signature")) {
1115 require(2);
1116 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001117 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001118 if (sz != 256) die("signature must be 256 bytes");
1119 fb_queue_download("signature", data, sz);
1120 fb_queue_command("signature", "installing signature");
1121 skip(2);
1122 } else if(!strcmp(*argv, "reboot")) {
1123 wants_reboot = 1;
1124 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001125 if (argc > 0) {
1126 if (!strcmp(*argv, "bootloader")) {
1127 wants_reboot = 0;
1128 wants_reboot_bootloader = 1;
1129 skip(1);
1130 }
1131 }
1132 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001133 } else if(!strcmp(*argv, "reboot-bootloader")) {
1134 wants_reboot_bootloader = 1;
1135 skip(1);
1136 } else if (!strcmp(*argv, "continue")) {
1137 fb_queue_command("continue", "resuming boot");
1138 skip(1);
1139 } else if(!strcmp(*argv, "boot")) {
1140 char *kname = 0;
1141 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001142 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001143 skip(1);
1144 if (argc > 0) {
1145 kname = argv[0];
1146 skip(1);
1147 }
1148 if (argc > 0) {
1149 rname = argv[0];
1150 skip(1);
1151 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001152 if (argc > 0) {
1153 sname = argv[0];
1154 skip(1);
1155 }
1156 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 if (data == 0) return 1;
1158 fb_queue_download("boot.img", data, sz);
1159 fb_queue_command("boot", "booting");
1160 } else if(!strcmp(*argv, "flash")) {
1161 char *pname = argv[1];
1162 char *fname = 0;
1163 require(2);
1164 if (argc > 2) {
1165 fname = argv[2];
1166 skip(3);
1167 } else {
1168 fname = find_item(pname, product);
1169 skip(2);
1170 }
1171 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001172 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001173 fb_queue_erase(pname);
1174 }
Colin Crossf8387882012-05-24 17:18:41 -07001175 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001176 } else if(!strcmp(*argv, "flash:raw")) {
1177 char *pname = argv[1];
1178 char *kname = argv[2];
1179 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001180 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001181 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001182 skip(3);
1183 if (argc > 0) {
1184 rname = argv[0];
1185 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001187 if (argc > 0) {
1188 sname = argv[0];
1189 skip(1);
1190 }
1191 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 if (data == 0) die("cannot load bootable image");
1193 fb_queue_flash(pname, data, sz);
1194 } else if(!strcmp(*argv, "flashall")) {
1195 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001196 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197 wants_reboot = 1;
1198 } else if(!strcmp(*argv, "update")) {
1199 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001200 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001201 skip(2);
1202 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001203 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001204 skip(1);
1205 }
1206 wants_reboot = 1;
1207 } else if(!strcmp(*argv, "oem")) {
1208 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001209 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1210 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1211 || !strcmp(*(argv+1), "unlock_critical")
1212 || !strcmp(*(argv+1), "lock_critical")
1213 || !strcmp(*(argv+1), "get_unlock_ability")) {
1214 argc = do_oem_command(argc, argv);
1215 } else {
1216 usage();
1217 return 1;
1218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 } else {
1220 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001221 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001222 }
1223 }
1224
1225 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001226 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001228 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001229
1230 std::string cache_type;
1231 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1232 fprintf(stderr, "wiping cache...\n");
1233 fb_queue_erase("cache");
1234 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1235 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001236 }
1237 if (wants_reboot) {
1238 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001239 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001240 } else if (wants_reboot_bootloader) {
1241 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001242 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001243 }
1244
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001245 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001246}