blob: 07889d46c76653e8faa73a965ada7a031a470b9c [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>
Colin Crossf8387882012-05-24 17:18:41 -070047#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070048#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Elliott Hughes253c18d2015-03-18 22:47:09 -070050#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070052#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Colin Crossf8387882012-05-24 17:18:41 -070054#ifndef O_BINARY
55#define O_BINARY 0
56#endif
57
Rom Lemarchand622810c2013-06-28 09:54:59 -070058#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
59
Wink Savilleb98762f2011-04-04 17:54:59 -070060char cur_product[FB_RESPONSE_SZ + 1];
61
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062static const char *serial = 0;
63static const char *product = 0;
64static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070066static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070067static int64_t sparse_limit = -1;
68static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Elliott Hughesfc797672015-04-07 20:12:50 -070070static unsigned page_size = 2048;
71static unsigned base_addr = 0x10000000;
72static unsigned kernel_offset = 0x00008000;
73static unsigned ramdisk_offset = 0x01000000;
74static unsigned second_offset = 0x00f00000;
75static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080076
Rom Lemarchand622810c2013-06-28 09:54:59 -070077enum fb_buffer_type {
78 FB_BUFFER,
79 FB_BUFFER_SPARSE,
80};
81
82struct fastboot_buffer {
83 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070084 void* data;
85 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070086};
87
88static struct {
89 char img_name[13];
90 char sig_name[13];
91 char part_name[9];
92 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070093} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070094 {"boot.img", "boot.sig", "boot", false},
95 {"recovery.img", "recovery.sig", "recovery", true},
96 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070097 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070098};
Brian Swetland2a63bb72009-04-28 16:05:07 -070099
Elliott Hughesfc797672015-04-07 20:12:50 -0700100static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700102 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 char path[PATH_MAX + 128];
104
105 if(!strcmp(item,"boot")) {
106 fn = "boot.img";
107 } else if(!strcmp(item,"recovery")) {
108 fn = "recovery.img";
109 } else if(!strcmp(item,"system")) {
110 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700111 } else if(!strcmp(item,"vendor")) {
112 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 } else if(!strcmp(item,"userdata")) {
114 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700115 } else if(!strcmp(item,"cache")) {
116 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 } else if(!strcmp(item,"info")) {
118 fn = "android-info.txt";
119 } else {
120 fprintf(stderr,"unknown partition '%s'\n", item);
121 return 0;
122 }
123
124 if(product) {
125 get_my_path(path);
126 sprintf(path + strlen(path),
127 "../../../target/product/%s/%s", product, fn);
128 return strdup(path);
129 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800130
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 dir = getenv("ANDROID_PRODUCT_OUT");
132 if((dir == 0) || (dir[0] == 0)) {
133 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
134 return 0;
135 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 sprintf(path, "%s/%s", dir, fn);
138 return strdup(path);
139}
140
Elliott Hughesfc797672015-04-07 20:12:50 -0700141static int64_t get_file_size(int fd) {
142 struct stat sb;
143 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700144}
145
Elliott Hughesfc797672015-04-07 20:12:50 -0700146static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800147 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700148 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Elliott Hughesfc797672015-04-07 20:12:50 -0700150 *sz = get_file_size(fd);
151 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700152 goto oops;
153 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155 data = (char*) malloc(*sz);
156 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 close(fd);
160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 return data;
162
163oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800164 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 close(fd);
166 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800167 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return 0;
169}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171static void* load_file(const char* fn, int64_t* sz) {
172 int fd = open(fn, O_RDONLY | O_BINARY);
173 if (fd == -1) return nullptr;
174 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700175}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700178 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700179 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700180 return -1;
181 }
182
183 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
184 return -1;
185 }
186
Scott Anderson13081c62012-04-06 12:39:30 -0700187 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700189 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
190 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return 0;
192}
193
Elliott Hughesfc797672015-04-07 20:12:50 -0700194static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800195 return match_fastboot_with_serial(info, serial);
196}
197
Elliott Hughesfc797672015-04-07 20:12:50 -0700198static int list_devices_callback(usb_ifc_info* info) {
199 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700200 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700201 if (!info->writable) {
202 serial = "no permissions"; // like "adb devices"
203 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 if (!serial[0]) {
205 serial = "????????????";
206 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700207 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700208 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700209 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800210 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700211 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700212 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700213 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 }
216
217 return -1;
218}
219
Elliott Hughesfc797672015-04-07 20:12:50 -0700220static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 static usb_handle *usb = 0;
222 int announce = 1;
223
224 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800225
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 for(;;) {
227 usb = usb_open(match_fastboot);
228 if(usb) return usb;
229 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800230 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700231 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700233 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235}
236
Elliott Hughesfc797672015-04-07 20:12:50 -0700237static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 // We don't actually open a USB device here,
239 // just getting our callback called so we can
240 // list all the connected devices.
241 usb_open(list_devices_callback);
242}
243
Elliott Hughesfc797672015-04-07 20:12:50 -0700244static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 fprintf(stderr,
246/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
247 "usage: fastboot [ <option> ] <command>\n"
248 "\n"
249 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700250 " update <filename> Reflash device from update.zip.\n"
251 " flashall Flash boot, system, vendor, and --\n"
252 " if found -- recovery.\n"
253 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
254 " flashing lock Locks the device. Prevents flashing.\n"
255 " flashing unlock Unlocks the device. Allows flashing\n"
256 " any partition except\n"
257 " bootloader-related partitions.\n"
258 " flashing lock_critical Prevents flashing bootloader-related\n"
259 " partitions.\n"
260 " flashing unlock_critical Enables flashing bootloader-related\n"
261 " partitions.\n"
262 " flashing get_unlock_ability Queries bootloader to see if the\n"
263 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700264 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700265 " unlock nonce.\n"
266 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700267 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700268 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700269 " erase <partition> Erase a flash partition.\n"
270 " format[:[<fs type>][:[<size>]] <partition>\n"
271 " Format a flash partition. Can\n"
272 " override the fs type and/or size\n"
273 " the bootloader reports.\n"
274 " getvar <variable> Display a bootloader variable.\n"
275 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
276 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
277 " Create bootimage and flash it.\n"
278 " devices [-l] List all connected devices [with\n"
279 " device paths].\n"
280 " continue Continue with autoboot.\n"
281 " reboot [bootloader] Reboot device [into bootloader].\n"
282 " reboot-bootloader Reboot device into bootloader.\n"
283 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 "\n"
285 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700286 " -w Erase userdata and cache (and format\n"
287 " if supported by partition type).\n"
288 " -u Do not erase partition before\n"
289 " formatting.\n"
290 " -s <specific device> Specify device serial number\n"
291 " or path to device port.\n"
292 " -p <product> Specify product name.\n"
293 " -c <cmdline> Override kernel commandline.\n"
294 " -i <vendor id> Specify a custom USB vendor id.\n"
295 " -b <base_addr> Specify a custom kernel base\n"
296 " address (default: 0x10000000).\n"
297 " -n <page size> Specify the nand page size\n"
298 " (default: 2048).\n"
299 " -S <size>[K|M|G] Automatically sparse files greater\n"
300 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302}
303
Elliott Hughesfc797672015-04-07 20:12:50 -0700304static void* load_bootable_image(const char* kernel, const char* ramdisk,
305 const char* secondstage, int64_t* sz,
306 const char* cmdline) {
307 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 fprintf(stderr, "no image specified\n");
309 return 0;
310 }
311
Elliott Hughesfc797672015-04-07 20:12:50 -0700312 int64_t ksize;
313 void* kdata = load_file(kernel, &ksize);
314 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800315 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 return 0;
317 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800318
Elliott Hughesfc797672015-04-07 20:12:50 -0700319 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700321 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800322
Elliott Hughesfc797672015-04-07 20:12:50 -0700323 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
325 return 0;
326 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800327
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 *sz = ksize;
329 return kdata;
330 }
331
Elliott Hughesfc797672015-04-07 20:12:50 -0700332 void* rdata = nullptr;
333 int64_t rsize = 0;
334 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700336 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800337 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 return 0;
339 }
340 }
341
Elliott Hughesfc797672015-04-07 20:12:50 -0700342 void* sdata = nullptr;
343 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200344 if (secondstage) {
345 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700346 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200347 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
348 return 0;
349 }
350 }
351
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700353 int64_t bsize = 0;
354 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800355 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200356 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800357 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700358 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 fprintf(stderr,"failed to create boot.img\n");
360 return 0;
361 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700362 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
363 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 return bdata;
367}
368
Elliott Hughesfc797672015-04-07 20:12:50 -0700369static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370{
Yusuke Sato07447542015-06-25 14:39:19 -0700371 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700372 ZipEntry zip_entry;
373 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700374 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000375 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 }
377
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700378 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700380 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700381 if (data == nullptr) {
382 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000383 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 }
385
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700386 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
387 if (error != 0) {
388 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000390 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000392
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 return data;
394}
395
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700396#if defined(_WIN32)
397
398// TODO: move this to somewhere it can be shared.
399
400#include <windows.h>
401
402// Windows' tmpfile(3) requires administrator rights because
403// it creates temporary files in the root directory.
404static FILE* win32_tmpfile() {
405 char temp_path[PATH_MAX];
406 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
407 if (nchars == 0 || nchars >= sizeof(temp_path)) {
408 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
409 return nullptr;
410 }
411
412 char filename[PATH_MAX];
413 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
414 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
415 return nullptr;
416 }
417
418 return fopen(filename, "w+bTD");
419}
420
421#define tmpfile win32_tmpfile
422
423#endif
424
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700425static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
426 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700428 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
429 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700430 return -1;
431 }
432
Yusuke Sato07447542015-06-25 14:39:19 -0700433 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700434 ZipEntry zip_entry;
435 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
436 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000437 return -1;
438 }
439
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700440 int fd = fileno(fp);
441 int error = ExtractEntryToFile(zip, &zip_entry, fd);
442 if (error != 0) {
443 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
444 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700445 }
446
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000447 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700448 return fd;
449}
450
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451static char *strip(char *s)
452{
453 int n;
454 while(*s && isspace(*s)) s++;
455 n = strlen(s);
456 while(n-- > 0) {
457 if(!isspace(s[n])) break;
458 s[n] = 0;
459 }
460 return s;
461}
462
463#define MAX_OPTIONS 32
464static int setup_requirement_line(char *name)
465{
466 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700467 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 unsigned n, count;
469 char *x;
470 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800471
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 if (!strncmp(name, "reject ", 7)) {
473 name += 7;
474 invert = 1;
475 } else if (!strncmp(name, "require ", 8)) {
476 name += 8;
477 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700478 } else if (!strncmp(name, "require-for-product:", 20)) {
479 // Get the product and point name past it
480 prod = name + 20;
481 name = strchr(name, ' ');
482 if (!name) return -1;
483 *name = 0;
484 name += 1;
485 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 }
487
488 x = strchr(name, '=');
489 if (x == 0) return 0;
490 *x = 0;
491 val[0] = x + 1;
492
493 for(count = 1; count < MAX_OPTIONS; count++) {
494 x = strchr(val[count - 1],'|');
495 if (x == 0) break;
496 *x = 0;
497 val[count] = x + 1;
498 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800499
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 name = strip(name);
501 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 name = strip(name);
504 if (name == 0) return -1;
505
Elliott Hughes253c18d2015-03-18 22:47:09 -0700506 const char* var = name;
507 // Work around an unfortunate name mismatch.
508 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509
Elliott Hughes253c18d2015-03-18 22:47:09 -0700510 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 if (out == 0) return -1;
512
513 for(n = 0; n < count; n++) {
514 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700515 if (out[n] == 0) {
516 for(size_t i = 0; i < n; ++i) {
517 free((char*) out[i]);
518 }
519 free(out);
520 return -1;
521 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 }
523
Elliott Hughes253c18d2015-03-18 22:47:09 -0700524 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 return 0;
526}
527
Elliott Hughesfc797672015-04-07 20:12:50 -0700528static void setup_requirements(char* data, int64_t sz) {
529 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800530 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700531 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 *s++ = 0;
533 if (setup_requirement_line(data)) {
534 die("out of memory");
535 }
536 data = s;
537 } else {
538 s++;
539 }
540 }
541}
542
Elliott Hughesfc797672015-04-07 20:12:50 -0700543static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 fb_queue_notice("--------------------------------------------");
545 fb_queue_display("version-bootloader", "Bootloader Version...");
546 fb_queue_display("version-baseband", "Baseband Version.....");
547 fb_queue_display("serialno", "Serial Number........");
548 fb_queue_notice("--------------------------------------------");
549}
550
Rom Lemarchand622810c2013-06-28 09:54:59 -0700551static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700552{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700553 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700554 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700555 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700556 }
557
Elliott Hughesfc797672015-04-07 20:12:50 -0700558 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700559 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700560 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700561 }
562
Elliott Hughes253c18d2015-03-18 22:47:09 -0700563 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700564 if (!out_s) {
565 die("Failed to allocate sparse file array\n");
566 }
567
568 files = sparse_file_resparse(s, max_size, out_s, files);
569 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700570 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700571 }
572
573 return out_s;
574}
575
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700576static int64_t get_target_sparse_limit(usb_handle* usb) {
577 std::string max_download_size;
578 if (!fb_getvar(usb, "max-download-size", &max_download_size)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800579 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700580 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700581 }
582
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700583 uint64_t limit;
584 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800585 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700586 return 0;
587 }
588 if (limit > 0) {
589 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
590 }
Colin Crossf8387882012-05-24 17:18:41 -0700591 return limit;
592}
593
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700594static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700595 int64_t limit;
596
597 if (sparse_limit == 0) {
598 return 0;
599 } else if (sparse_limit > 0) {
600 limit = sparse_limit;
601 } else {
602 if (target_sparse_limit == -1) {
603 target_sparse_limit = get_target_sparse_limit(usb);
604 }
605 if (target_sparse_limit > 0) {
606 limit = target_sparse_limit;
607 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700608 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700609 }
610 }
611
612 if (size > limit) {
613 return limit;
614 }
615
616 return 0;
617}
618
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700619// Until we get lazy inode table init working in make_ext4fs, we need to
620// erase partitions of type ext4 before flashing a filesystem so no stale
621// inodes are left lying around. Otherwise, e2fsck gets very upset.
622static bool needs_erase(usb_handle* usb, const char* part) {
623 return !fb_format_supported(usb, part, nullptr);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700624}
625
Elliott Hughesfc797672015-04-07 20:12:50 -0700626static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
627 int64_t sz = get_file_size(fd);
628 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000629 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700630 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700631
Elliott Hughesfc797672015-04-07 20:12:50 -0700632 lseek64(fd, 0, SEEK_SET);
633 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700634 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700635 sparse_file** s = load_sparse_files(fd, limit);
636 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700638 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639 buf->type = FB_BUFFER_SPARSE;
640 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700641 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700642 void* data = load_fd(fd, &sz);
643 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700644 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700645 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000646 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700647 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700648
649 return 0;
650}
651
652static int load_buf(usb_handle *usb, const char *fname,
653 struct fastboot_buffer *buf)
654{
655 int fd;
656
657 fd = open(fname, O_RDONLY | O_BINARY);
658 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700659 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660 }
661
662 return load_buf_fd(usb, fd, buf);
663}
664
665static void flash_buf(const char *pname, struct fastboot_buffer *buf)
666{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700667 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700668
669 switch (buf->type) {
670 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700671 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700672 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700673 int64_t sz = sparse_file_len(*s, true, false);
674 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700675 }
676 break;
677 case FB_BUFFER:
678 fb_queue_flash(pname, buf->data, buf->sz);
679 break;
680 default:
681 die("unknown buffer type: %d", buf->type);
682 }
683}
684
Elliott Hughesfc797672015-04-07 20:12:50 -0700685static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700686 struct fastboot_buffer buf;
687
688 if (load_buf(usb, fname, &buf)) {
689 die("cannot load '%s'", fname);
690 }
691 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700692}
693
Elliott Hughesfc797672015-04-07 20:12:50 -0700694static void do_update_signature(ZipArchiveHandle zip, char* fn) {
695 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700696 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700697 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 fb_queue_download("signature", data, sz);
699 fb_queue_command("signature", "installing signature");
700}
701
Elliott Hughesfc797672015-04-07 20:12:50 -0700702static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 queue_info_dump();
704
Wink Savilleb98762f2011-04-04 17:54:59 -0700705 fb_queue_query_save("product", cur_product, sizeof(cur_product));
706
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700707 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700708 int error = OpenArchive(filename, &zip);
709 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100710 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700711 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700712 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713
Elliott Hughesfc797672015-04-07 20:12:50 -0700714 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700715 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700716 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100717 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700718 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 }
720
Elliott Hughes253c18d2015-03-18 22:47:09 -0700721 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722
Elliott Hughesacdbe922015-06-02 21:37:05 -0700723 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700724 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700725 if (fd == -1) {
726 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700727 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700728 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100729 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700730 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700731 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700732 fastboot_buffer buf;
733 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700734 if (rc) die("cannot load %s from flash", images[i].img_name);
735 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700736 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700737 fb_queue_erase(images[i].part_name);
738 }
739 flash_buf(images[i].part_name, &buf);
740 /* not closing the fd here since the sparse code keeps the fd around
741 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
742 * program exits.
743 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700745
746 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747}
748
Elliott Hughesfc797672015-04-07 20:12:50 -0700749static void do_send_signature(char* fn) {
750 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700752
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800754
Elliott Hughesfc797672015-04-07 20:12:50 -0700755 strcpy(xtn, ".sig");
756
757 int64_t sz;
758 void* data = load_file(fn, &sz);
759 strcpy(xtn, ".img");
760 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 fb_queue_download("signature", data, sz);
762 fb_queue_command("signature", "installing signature");
763}
764
Elliott Hughesfc797672015-04-07 20:12:50 -0700765static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 queue_info_dump();
767
Wink Savilleb98762f2011-04-04 17:54:59 -0700768 fb_queue_query_save("product", cur_product, sizeof(cur_product));
769
Elliott Hughes253c18d2015-03-18 22:47:09 -0700770 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700771 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772
Elliott Hughesfc797672015-04-07 20:12:50 -0700773 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700774 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700775 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700776
777 setup_requirements(reinterpret_cast<char*>(data), sz);
778
779 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700780 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700781 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700782 if (load_buf(usb, fname, &buf)) {
783 if (images[i].is_optional)
784 continue;
785 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700786 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700787 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700788 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700789 fb_queue_erase(images[i].part_name);
790 }
791 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800793}
794
795#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800796#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797
Elliott Hughes45be42e2015-09-02 20:15:48 -0700798static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700799{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700800 if (argc <= 2) return 0;
801 skip(2);
802
803 /*
804 * Process unlock_bootloader, we have to load the message file
805 * and send that to the remote device.
806 */
807 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700808
809 int64_t sz;
810 void* data = load_file(*argv, &sz);
811 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700812 fb_queue_download("unlock_message", data, sz);
813 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
814 skip(1);
815 return 0;
816}
817
Elliott Hughes45be42e2015-09-02 20:15:48 -0700818static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 char command[256];
821 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800822
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823 command[0] = 0;
824 while(1) {
825 strcat(command,*argv);
826 skip(1);
827 if(argc == 0) break;
828 strcat(command," ");
829 }
830
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800831 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 return 0;
833}
834
Colin Crossf8387882012-05-24 17:18:41 -0700835static int64_t parse_num(const char *arg)
836{
837 char *endptr;
838 unsigned long long num;
839
840 num = strtoull(arg, &endptr, 0);
841 if (endptr == arg) {
842 return -1;
843 }
844
845 if (*endptr == 'k' || *endptr == 'K') {
846 if (num >= (-1ULL) / 1024) {
847 return -1;
848 }
849 num *= 1024LL;
850 endptr++;
851 } else if (*endptr == 'm' || *endptr == 'M') {
852 if (num >= (-1ULL) / (1024 * 1024)) {
853 return -1;
854 }
855 num *= 1024LL * 1024LL;
856 endptr++;
857 } else if (*endptr == 'g' || *endptr == 'G') {
858 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
859 return -1;
860 }
861 num *= 1024LL * 1024LL * 1024LL;
862 endptr++;
863 }
864
865 if (*endptr != '\0') {
866 return -1;
867 }
868
869 if (num > INT64_MAX) {
870 return -1;
871 }
872
873 return num;
874}
875
Elliott Hughesfc797672015-04-07 20:12:50 -0700876static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700877 const char* partition, int skip_if_not_supported,
878 const char* type_override, const char* size_override) {
879 std::string partition_type, partition_size;
880
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700881 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700882 const char* errMsg = nullptr;
883 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700884 int fd;
885
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700886 unsigned int limit = INT_MAX;
887 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700888 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700889 }
890 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700891 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700892 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700893
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700894 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700895 errMsg = "Can't determine partition type.\n";
896 goto failed;
897 }
JP Abgrall7e859742014-05-06 15:14:15 -0700898 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700899 if (partition_type != type_override) {
900 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
901 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700902 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700903 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700904 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700905
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700906 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700907 errMsg = "Unable to get partition size\n";
908 goto failed;
909 }
JP Abgrall7e859742014-05-06 15:14:15 -0700910 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700911 if (partition_size != size_override) {
912 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
913 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700914 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700915 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700916 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700917
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700918 gen = fs_get_generator(partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700919 if (!gen) {
920 if (skip_if_not_supported) {
921 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700922 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700923 return;
924 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700925 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
926 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700927 return;
928 }
929
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700930 int64_t size;
931 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
932 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
933 return;
934 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700935
936 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700937 if (fs_generator_generate(gen, fd, size)) {
938 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700939 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700940 return;
941 }
942
943 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700944 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700945 close(fd);
946 return;
947 }
948 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700949 return;
950
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700951failed:
952 if (skip_if_not_supported) {
953 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700954 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700955 }
956 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
957}
958
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800959int main(int argc, char **argv)
960{
961 int wants_wipe = 0;
962 int wants_reboot = 0;
963 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700964 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800965 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700966 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100967 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800968
JP Abgrall7b8970c2013-03-07 17:06:41 -0800969 const struct option longopts[] = {
970 {"base", required_argument, 0, 'b'},
971 {"kernel_offset", required_argument, 0, 'k'},
972 {"page_size", required_argument, 0, 'n'},
973 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800974 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700975 {"help", no_argument, 0, 'h'},
976 {"unbuffered", no_argument, 0, 0},
977 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800978 {0, 0, 0, 0}
979 };
Colin Cross8879f982012-05-22 17:53:34 -0700980
981 serial = getenv("ANDROID_SERIAL");
982
983 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700984 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 -0700985 if (c < 0) {
986 break;
987 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800988 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700989 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700990 case 'b':
991 base_addr = strtoul(optarg, 0, 16);
992 break;
Colin Cross8879f982012-05-22 17:53:34 -0700993 case 'c':
994 cmdline = optarg;
995 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800996 case 'h':
997 usage();
998 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700999 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001000 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001001 unsigned long val;
1002
1003 val = strtoul(optarg, &endptr, 0);
1004 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1005 die("invalid vendor id '%s'", optarg);
1006 vendor_id = (unsigned short)val;
1007 break;
1008 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001009 case 'k':
1010 kernel_offset = strtoul(optarg, 0, 16);
1011 break;
1012 case 'l':
1013 long_listing = 1;
1014 break;
1015 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001016 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001017 if (!page_size) die("invalid page size");
1018 break;
1019 case 'p':
1020 product = optarg;
1021 break;
1022 case 'r':
1023 ramdisk_offset = strtoul(optarg, 0, 16);
1024 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001025 case 't':
1026 tags_offset = strtoul(optarg, 0, 16);
1027 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001028 case 's':
1029 serial = optarg;
1030 break;
1031 case 'S':
1032 sparse_limit = parse_num(optarg);
1033 if (sparse_limit < 0) {
1034 die("invalid sparse limit");
1035 }
1036 break;
1037 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001038 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001039 break;
1040 case 'w':
1041 wants_wipe = 1;
1042 break;
Colin Cross8879f982012-05-22 17:53:34 -07001043 case '?':
1044 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001045 case 0:
1046 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001047 setvbuf(stdout, nullptr, _IONBF, 0);
1048 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001049 } else if (strcmp("version", longopts[longindex].name) == 0) {
1050 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1051 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001052 }
1053 break;
Colin Cross8879f982012-05-22 17:53:34 -07001054 default:
1055 abort();
1056 }
1057 }
1058
1059 argc -= optind;
1060 argv += optind;
1061
1062 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001063 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001064 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001065 }
1066
Colin Cross8fb6e062012-07-24 16:36:41 -07001067 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001068 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001069 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001070 return 0;
1071 }
1072
Colin Crossc7b75dc2012-08-29 18:17:06 -07001073 if (argc > 0 && !strcmp(*argv, "help")) {
1074 usage();
1075 return 0;
1076 }
1077
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001078 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001079
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001081 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082 require(2);
1083 fb_queue_display(argv[1], argv[1]);
1084 skip(2);
1085 } else if(!strcmp(*argv, "erase")) {
1086 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001087
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001088 if (!fb_format_supported(usb, argv[1], nullptr)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001089 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1090 }
1091
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001092 fb_queue_erase(argv[1]);
1093 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001094 } else if(!strncmp(*argv, "format", strlen("format"))) {
1095 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001096 char *type_override = nullptr;
1097 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001098 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001099 /*
1100 * Parsing for: "format[:[type][:[size]]]"
1101 * Some valid things:
1102 * - select ontly the size, and leave default fs type:
1103 * format::0x4000000 userdata
1104 * - default fs type and size:
1105 * format userdata
1106 * format:: userdata
1107 */
1108 overrides = strchr(*argv, ':');
1109 if (overrides) {
1110 overrides++;
1111 size_override = strchr(overrides, ':');
1112 if (size_override) {
1113 size_override[0] = '\0';
1114 size_override++;
1115 }
1116 type_override = overrides;
1117 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001118 if (type_override && !type_override[0]) type_override = nullptr;
1119 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001120 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001121 fb_queue_erase(argv[1]);
1122 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001123 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001124 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001125 } else if(!strcmp(*argv, "signature")) {
1126 require(2);
1127 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001128 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129 if (sz != 256) die("signature must be 256 bytes");
1130 fb_queue_download("signature", data, sz);
1131 fb_queue_command("signature", "installing signature");
1132 skip(2);
1133 } else if(!strcmp(*argv, "reboot")) {
1134 wants_reboot = 1;
1135 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001136 if (argc > 0) {
1137 if (!strcmp(*argv, "bootloader")) {
1138 wants_reboot = 0;
1139 wants_reboot_bootloader = 1;
1140 skip(1);
1141 }
1142 }
1143 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144 } else if(!strcmp(*argv, "reboot-bootloader")) {
1145 wants_reboot_bootloader = 1;
1146 skip(1);
1147 } else if (!strcmp(*argv, "continue")) {
1148 fb_queue_command("continue", "resuming boot");
1149 skip(1);
1150 } else if(!strcmp(*argv, "boot")) {
1151 char *kname = 0;
1152 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001153 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001154 skip(1);
1155 if (argc > 0) {
1156 kname = argv[0];
1157 skip(1);
1158 }
1159 if (argc > 0) {
1160 rname = argv[0];
1161 skip(1);
1162 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001163 if (argc > 0) {
1164 sname = argv[0];
1165 skip(1);
1166 }
1167 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 if (data == 0) return 1;
1169 fb_queue_download("boot.img", data, sz);
1170 fb_queue_command("boot", "booting");
1171 } else if(!strcmp(*argv, "flash")) {
1172 char *pname = argv[1];
1173 char *fname = 0;
1174 require(2);
1175 if (argc > 2) {
1176 fname = argv[2];
1177 skip(3);
1178 } else {
1179 fname = find_item(pname, product);
1180 skip(2);
1181 }
1182 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001183 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001184 fb_queue_erase(pname);
1185 }
Colin Crossf8387882012-05-24 17:18:41 -07001186 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187 } else if(!strcmp(*argv, "flash:raw")) {
1188 char *pname = argv[1];
1189 char *kname = argv[2];
1190 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001191 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001193 skip(3);
1194 if (argc > 0) {
1195 rname = argv[0];
1196 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001198 if (argc > 0) {
1199 sname = argv[0];
1200 skip(1);
1201 }
1202 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001203 if (data == 0) die("cannot load bootable image");
1204 fb_queue_flash(pname, data, sz);
1205 } else if(!strcmp(*argv, "flashall")) {
1206 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001207 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001208 wants_reboot = 1;
1209 } else if(!strcmp(*argv, "update")) {
1210 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001211 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 skip(2);
1213 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001214 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215 skip(1);
1216 }
1217 wants_reboot = 1;
1218 } else if(!strcmp(*argv, "oem")) {
1219 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001220 } else if(!strcmp(*argv, "flashing")) {
1221 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1222 !strcmp(*(argv+1), "lock") ||
1223 !strcmp(*(argv+1), "unlock_critical") ||
1224 !strcmp(*(argv+1), "lock_critical") ||
1225 !strcmp(*(argv+1), "get_unlock_ability") ||
1226 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1227 !strcmp(*(argv+1), "lock_bootloader"))) {
1228 argc = do_oem_command(argc, argv);
1229 } else
1230 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1231 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001232 } else {
1233 usage();
1234 return 1;
1235 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001236 } else {
1237 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001238 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001239 }
1240 }
1241
1242 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001243 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001244 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001245 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001246
1247 std::string cache_type;
1248 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1249 fprintf(stderr, "wiping cache...\n");
1250 fb_queue_erase("cache");
1251 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1252 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001253 }
1254 if (wants_reboot) {
1255 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001256 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001257 } else if (wants_reboot_bootloader) {
1258 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001259 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260 }
1261
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001262 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001263}