blob: 15dd67d460cb92e5b6f4cc9ec826d048a8d8aad9 [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"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700265 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700266 " unlock nonce.\n"
267 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700268 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700269 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700270 " erase <partition> Erase a flash partition.\n"
271 " format[:[<fs type>][:[<size>]] <partition>\n"
272 " Format a flash partition. Can\n"
273 " override the fs type and/or size\n"
274 " the bootloader reports.\n"
275 " getvar <variable> Display a bootloader variable.\n"
276 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
277 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
278 " Create bootimage and flash it.\n"
279 " devices [-l] List all connected devices [with\n"
280 " device paths].\n"
281 " continue Continue with autoboot.\n"
282 " reboot [bootloader] Reboot device [into bootloader].\n"
283 " reboot-bootloader Reboot device into bootloader.\n"
284 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 "\n"
286 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700287 " -w Erase userdata and cache (and format\n"
288 " if supported by partition type).\n"
289 " -u Do not erase partition before\n"
290 " formatting.\n"
291 " -s <specific device> Specify device serial number\n"
292 " or path to device port.\n"
293 " -p <product> Specify product name.\n"
294 " -c <cmdline> Override kernel commandline.\n"
295 " -i <vendor id> Specify a custom USB vendor id.\n"
296 " -b <base_addr> Specify a custom kernel base\n"
297 " address (default: 0x10000000).\n"
298 " -n <page size> Specify the nand page size\n"
299 " (default: 2048).\n"
300 " -S <size>[K|M|G] Automatically sparse files greater\n"
301 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303}
304
Elliott Hughesfc797672015-04-07 20:12:50 -0700305static void* load_bootable_image(const char* kernel, const char* ramdisk,
306 const char* secondstage, int64_t* sz,
307 const char* cmdline) {
308 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 fprintf(stderr, "no image specified\n");
310 return 0;
311 }
312
Elliott Hughesfc797672015-04-07 20:12:50 -0700313 int64_t ksize;
314 void* kdata = load_file(kernel, &ksize);
315 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800316 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 return 0;
318 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800319
Elliott Hughesfc797672015-04-07 20:12:50 -0700320 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700322 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800323
Elliott Hughesfc797672015-04-07 20:12:50 -0700324 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
326 return 0;
327 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800328
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 *sz = ksize;
330 return kdata;
331 }
332
Elliott Hughesfc797672015-04-07 20:12:50 -0700333 void* rdata = nullptr;
334 int64_t rsize = 0;
335 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700337 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800338 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 return 0;
340 }
341 }
342
Elliott Hughesfc797672015-04-07 20:12:50 -0700343 void* sdata = nullptr;
344 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200345 if (secondstage) {
346 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700347 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200348 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
349 return 0;
350 }
351 }
352
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700354 int64_t bsize = 0;
355 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800356 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200357 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800358 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700359 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 fprintf(stderr,"failed to create boot.img\n");
361 return 0;
362 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700363 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
364 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800366
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 return bdata;
368}
369
Elliott Hughesfc797672015-04-07 20:12:50 -0700370static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371{
Yusuke Sato07447542015-06-25 14:39:19 -0700372 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700373 ZipEntry zip_entry;
374 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700375 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000376 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 }
378
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700379 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700381 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700382 if (data == nullptr) {
383 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000384 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 }
386
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700387 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
388 if (error != 0) {
389 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000391 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000393
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 return data;
395}
396
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700397#if defined(_WIN32)
398
399// TODO: move this to somewhere it can be shared.
400
401#include <windows.h>
402
403// Windows' tmpfile(3) requires administrator rights because
404// it creates temporary files in the root directory.
405static FILE* win32_tmpfile() {
406 char temp_path[PATH_MAX];
407 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
408 if (nchars == 0 || nchars >= sizeof(temp_path)) {
409 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
410 return nullptr;
411 }
412
413 char filename[PATH_MAX];
414 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
415 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
416 return nullptr;
417 }
418
419 return fopen(filename, "w+bTD");
420}
421
422#define tmpfile win32_tmpfile
423
424#endif
425
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700426static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
427 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700429 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
430 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700431 return -1;
432 }
433
Yusuke Sato07447542015-06-25 14:39:19 -0700434 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700435 ZipEntry zip_entry;
436 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
437 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000438 return -1;
439 }
440
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700441 int fd = fileno(fp);
442 int error = ExtractEntryToFile(zip, &zip_entry, fd);
443 if (error != 0) {
444 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
445 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700446 }
447
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000448 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700449 return fd;
450}
451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452static char *strip(char *s)
453{
454 int n;
455 while(*s && isspace(*s)) s++;
456 n = strlen(s);
457 while(n-- > 0) {
458 if(!isspace(s[n])) break;
459 s[n] = 0;
460 }
461 return s;
462}
463
464#define MAX_OPTIONS 32
465static int setup_requirement_line(char *name)
466{
467 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700468 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 unsigned n, count;
470 char *x;
471 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800472
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 if (!strncmp(name, "reject ", 7)) {
474 name += 7;
475 invert = 1;
476 } else if (!strncmp(name, "require ", 8)) {
477 name += 8;
478 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700479 } else if (!strncmp(name, "require-for-product:", 20)) {
480 // Get the product and point name past it
481 prod = name + 20;
482 name = strchr(name, ' ');
483 if (!name) return -1;
484 *name = 0;
485 name += 1;
486 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 }
488
489 x = strchr(name, '=');
490 if (x == 0) return 0;
491 *x = 0;
492 val[0] = x + 1;
493
494 for(count = 1; count < MAX_OPTIONS; count++) {
495 x = strchr(val[count - 1],'|');
496 if (x == 0) break;
497 *x = 0;
498 val[count] = x + 1;
499 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800500
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 name = strip(name);
502 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 name = strip(name);
505 if (name == 0) return -1;
506
Elliott Hughes253c18d2015-03-18 22:47:09 -0700507 const char* var = name;
508 // Work around an unfortunate name mismatch.
509 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510
Elliott Hughes253c18d2015-03-18 22:47:09 -0700511 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 if (out == 0) return -1;
513
514 for(n = 0; n < count; n++) {
515 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700516 if (out[n] == 0) {
517 for(size_t i = 0; i < n; ++i) {
518 free((char*) out[i]);
519 }
520 free(out);
521 return -1;
522 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 }
524
Elliott Hughes253c18d2015-03-18 22:47:09 -0700525 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 return 0;
527}
528
Elliott Hughesfc797672015-04-07 20:12:50 -0700529static void setup_requirements(char* data, int64_t sz) {
530 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700532 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533 *s++ = 0;
534 if (setup_requirement_line(data)) {
535 die("out of memory");
536 }
537 data = s;
538 } else {
539 s++;
540 }
541 }
542}
543
Elliott Hughesfc797672015-04-07 20:12:50 -0700544static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800545 fb_queue_notice("--------------------------------------------");
546 fb_queue_display("version-bootloader", "Bootloader Version...");
547 fb_queue_display("version-baseband", "Baseband Version.....");
548 fb_queue_display("serialno", "Serial Number........");
549 fb_queue_notice("--------------------------------------------");
550}
551
Rom Lemarchand622810c2013-06-28 09:54:59 -0700552static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700553{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700554 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700555 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700556 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700557 }
558
Elliott Hughesfc797672015-04-07 20:12:50 -0700559 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700560 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700561 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700562 }
563
Elliott Hughes253c18d2015-03-18 22:47:09 -0700564 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700565 if (!out_s) {
566 die("Failed to allocate sparse file array\n");
567 }
568
569 files = sparse_file_resparse(s, max_size, out_s, files);
570 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700571 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700572 }
573
574 return out_s;
575}
576
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700577static int64_t get_target_sparse_limit(usb_handle* usb) {
578 std::string max_download_size;
Elliott Hughes2030bac2015-11-03 08:14:43 -0800579 if (!fb_getvar(usb, "max-download-size", &max_download_size) || max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800580 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700581 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700582 }
583
Elliott Hughes77c0e662015-11-02 15:51:12 -0800584 // Some bootloaders (angler, for example) send spurious whitespace too.
585 max_download_size = android::base::Trim(max_download_size);
586
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700587 uint64_t limit;
588 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800589 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700590 return 0;
591 }
592 if (limit > 0) {
593 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
594 }
Colin Crossf8387882012-05-24 17:18:41 -0700595 return limit;
596}
597
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700598static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700599 int64_t limit;
600
601 if (sparse_limit == 0) {
602 return 0;
603 } else if (sparse_limit > 0) {
604 limit = sparse_limit;
605 } else {
606 if (target_sparse_limit == -1) {
607 target_sparse_limit = get_target_sparse_limit(usb);
608 }
609 if (target_sparse_limit > 0) {
610 limit = target_sparse_limit;
611 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700612 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700613 }
614 }
615
616 if (size > limit) {
617 return limit;
618 }
619
620 return 0;
621}
622
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700623// Until we get lazy inode table init working in make_ext4fs, we need to
624// erase partitions of type ext4 before flashing a filesystem so no stale
625// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800626static bool needs_erase(usb_handle* usb, const char* partition) {
627 std::string partition_type;
628 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
629 return false;
630 }
631 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700632}
633
Elliott Hughesfc797672015-04-07 20:12:50 -0700634static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
635 int64_t sz = get_file_size(fd);
636 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000637 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700639
Elliott Hughesfc797672015-04-07 20:12:50 -0700640 lseek64(fd, 0, SEEK_SET);
641 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700642 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700643 sparse_file** s = load_sparse_files(fd, limit);
644 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700646 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700647 buf->type = FB_BUFFER_SPARSE;
648 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700649 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700650 void* data = load_fd(fd, &sz);
651 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700652 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700653 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000654 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700655 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700656
657 return 0;
658}
659
660static int load_buf(usb_handle *usb, const char *fname,
661 struct fastboot_buffer *buf)
662{
663 int fd;
664
665 fd = open(fname, O_RDONLY | O_BINARY);
666 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700667 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700668 }
669
670 return load_buf_fd(usb, fd, buf);
671}
672
673static void flash_buf(const char *pname, struct fastboot_buffer *buf)
674{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700675 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700676
677 switch (buf->type) {
678 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700679 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700680 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700681 int64_t sz = sparse_file_len(*s, true, false);
682 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700683 }
684 break;
685 case FB_BUFFER:
686 fb_queue_flash(pname, buf->data, buf->sz);
687 break;
688 default:
689 die("unknown buffer type: %d", buf->type);
690 }
691}
692
Elliott Hughesfc797672015-04-07 20:12:50 -0700693static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700694 struct fastboot_buffer buf;
695
696 if (load_buf(usb, fname, &buf)) {
697 die("cannot load '%s'", fname);
698 }
699 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700700}
701
Elliott Hughesfc797672015-04-07 20:12:50 -0700702static void do_update_signature(ZipArchiveHandle zip, char* fn) {
703 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700704 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700705 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 fb_queue_download("signature", data, sz);
707 fb_queue_command("signature", "installing signature");
708}
709
Elliott Hughesfc797672015-04-07 20:12:50 -0700710static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 queue_info_dump();
712
Wink Savilleb98762f2011-04-04 17:54:59 -0700713 fb_queue_query_save("product", cur_product, sizeof(cur_product));
714
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700715 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700716 int error = OpenArchive(filename, &zip);
717 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100718 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700719 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700720 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721
Elliott Hughesfc797672015-04-07 20:12:50 -0700722 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700723 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700724 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100725 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700726 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727 }
728
Elliott Hughes253c18d2015-03-18 22:47:09 -0700729 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730
Elliott Hughesacdbe922015-06-02 21:37:05 -0700731 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700732 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700733 if (fd == -1) {
734 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700735 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700736 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100737 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700738 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700739 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700740 fastboot_buffer buf;
741 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742 if (rc) die("cannot load %s from flash", images[i].img_name);
743 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700744 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 fb_queue_erase(images[i].part_name);
746 }
747 flash_buf(images[i].part_name, &buf);
748 /* not closing the fd here since the sparse code keeps the fd around
749 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
750 * program exits.
751 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700753
754 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755}
756
Elliott Hughesfc797672015-04-07 20:12:50 -0700757static void do_send_signature(char* fn) {
758 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700760
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800762
Elliott Hughesfc797672015-04-07 20:12:50 -0700763 strcpy(xtn, ".sig");
764
765 int64_t sz;
766 void* data = load_file(fn, &sz);
767 strcpy(xtn, ".img");
768 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 fb_queue_download("signature", data, sz);
770 fb_queue_command("signature", "installing signature");
771}
772
Elliott Hughesfc797672015-04-07 20:12:50 -0700773static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774 queue_info_dump();
775
Wink Savilleb98762f2011-04-04 17:54:59 -0700776 fb_queue_query_save("product", cur_product, sizeof(cur_product));
777
Elliott Hughes253c18d2015-03-18 22:47:09 -0700778 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700779 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780
Elliott Hughesfc797672015-04-07 20:12:50 -0700781 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700782 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700783 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700784
785 setup_requirements(reinterpret_cast<char*>(data), sz);
786
787 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700788 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700789 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700790 if (load_buf(usb, fname, &buf)) {
791 if (images[i].is_optional)
792 continue;
793 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700794 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700795 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700796 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700797 fb_queue_erase(images[i].part_name);
798 }
799 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801}
802
803#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800804#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805
Elliott Hughes45be42e2015-09-02 20:15:48 -0700806static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700807{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700808 if (argc <= 2) return 0;
809 skip(2);
810
811 /*
812 * Process unlock_bootloader, we have to load the message file
813 * and send that to the remote device.
814 */
815 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700816
817 int64_t sz;
818 void* data = load_file(*argv, &sz);
819 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700820 fb_queue_download("unlock_message", data, sz);
821 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
822 skip(1);
823 return 0;
824}
825
Elliott Hughes45be42e2015-09-02 20:15:48 -0700826static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 char command[256];
829 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800830
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 command[0] = 0;
832 while(1) {
833 strcat(command,*argv);
834 skip(1);
835 if(argc == 0) break;
836 strcat(command," ");
837 }
838
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800839 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 return 0;
841}
842
Colin Crossf8387882012-05-24 17:18:41 -0700843static int64_t parse_num(const char *arg)
844{
845 char *endptr;
846 unsigned long long num;
847
848 num = strtoull(arg, &endptr, 0);
849 if (endptr == arg) {
850 return -1;
851 }
852
853 if (*endptr == 'k' || *endptr == 'K') {
854 if (num >= (-1ULL) / 1024) {
855 return -1;
856 }
857 num *= 1024LL;
858 endptr++;
859 } else if (*endptr == 'm' || *endptr == 'M') {
860 if (num >= (-1ULL) / (1024 * 1024)) {
861 return -1;
862 }
863 num *= 1024LL * 1024LL;
864 endptr++;
865 } else if (*endptr == 'g' || *endptr == 'G') {
866 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
867 return -1;
868 }
869 num *= 1024LL * 1024LL * 1024LL;
870 endptr++;
871 }
872
873 if (*endptr != '\0') {
874 return -1;
875 }
876
877 if (num > INT64_MAX) {
878 return -1;
879 }
880
881 return num;
882}
883
Elliott Hughesfc797672015-04-07 20:12:50 -0700884static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700885 const char* partition, int skip_if_not_supported,
886 const char* type_override, const char* size_override) {
887 std::string partition_type, partition_size;
888
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700889 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700890 const char* errMsg = nullptr;
891 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700892 int fd;
893
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700894 unsigned int limit = INT_MAX;
895 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700896 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700897 }
898 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700899 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700900 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700901
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700902 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700903 errMsg = "Can't determine partition type.\n";
904 goto failed;
905 }
JP Abgrall7e859742014-05-06 15:14:15 -0700906 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700907 if (partition_type != type_override) {
908 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
909 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700910 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700911 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700912 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700913
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700914 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700915 errMsg = "Unable to get partition size\n";
916 goto failed;
917 }
JP Abgrall7e859742014-05-06 15:14:15 -0700918 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700919 if (partition_size != size_override) {
920 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
921 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700922 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700923 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700924 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700925
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800926 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700927 if (!gen) {
928 if (skip_if_not_supported) {
929 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700930 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700931 return;
932 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700933 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
934 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700935 return;
936 }
937
Elliott Hughes2030bac2015-11-03 08:14:43 -0800938 // Some bootloaders (hammerhead, for example) use implicit hex.
939 // This code used to use strtol with base 16.
940 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
941
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700942 int64_t size;
943 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
944 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
945 return;
946 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700947
948 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700949 if (fs_generator_generate(gen, fd, size)) {
950 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700951 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700952 return;
953 }
954
955 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700956 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700957 close(fd);
958 return;
959 }
960 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700961 return;
962
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700963failed:
964 if (skip_if_not_supported) {
965 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700966 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700967 }
968 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
969}
970
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971int main(int argc, char **argv)
972{
973 int wants_wipe = 0;
974 int wants_reboot = 0;
975 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700976 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700978 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100979 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980
JP Abgrall7b8970c2013-03-07 17:06:41 -0800981 const struct option longopts[] = {
982 {"base", required_argument, 0, 'b'},
983 {"kernel_offset", required_argument, 0, 'k'},
984 {"page_size", required_argument, 0, 'n'},
985 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800986 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700987 {"help", no_argument, 0, 'h'},
988 {"unbuffered", no_argument, 0, 0},
989 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800990 {0, 0, 0, 0}
991 };
Colin Cross8879f982012-05-22 17:53:34 -0700992
993 serial = getenv("ANDROID_SERIAL");
994
995 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700996 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 -0700997 if (c < 0) {
998 break;
999 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001000 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001001 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001002 case 'b':
1003 base_addr = strtoul(optarg, 0, 16);
1004 break;
Colin Cross8879f982012-05-22 17:53:34 -07001005 case 'c':
1006 cmdline = optarg;
1007 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001008 case 'h':
1009 usage();
1010 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001011 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001012 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001013 unsigned long val;
1014
1015 val = strtoul(optarg, &endptr, 0);
1016 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1017 die("invalid vendor id '%s'", optarg);
1018 vendor_id = (unsigned short)val;
1019 break;
1020 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001021 case 'k':
1022 kernel_offset = strtoul(optarg, 0, 16);
1023 break;
1024 case 'l':
1025 long_listing = 1;
1026 break;
1027 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001028 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001029 if (!page_size) die("invalid page size");
1030 break;
1031 case 'p':
1032 product = optarg;
1033 break;
1034 case 'r':
1035 ramdisk_offset = strtoul(optarg, 0, 16);
1036 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001037 case 't':
1038 tags_offset = strtoul(optarg, 0, 16);
1039 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001040 case 's':
1041 serial = optarg;
1042 break;
1043 case 'S':
1044 sparse_limit = parse_num(optarg);
1045 if (sparse_limit < 0) {
1046 die("invalid sparse limit");
1047 }
1048 break;
1049 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001050 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001051 break;
1052 case 'w':
1053 wants_wipe = 1;
1054 break;
Colin Cross8879f982012-05-22 17:53:34 -07001055 case '?':
1056 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001057 case 0:
1058 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001059 setvbuf(stdout, nullptr, _IONBF, 0);
1060 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001061 } else if (strcmp("version", longopts[longindex].name) == 0) {
1062 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1063 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001064 }
1065 break;
Colin Cross8879f982012-05-22 17:53:34 -07001066 default:
1067 abort();
1068 }
1069 }
1070
1071 argc -= optind;
1072 argv += optind;
1073
1074 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001076 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001077 }
1078
Colin Cross8fb6e062012-07-24 16:36:41 -07001079 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001080 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001082 return 0;
1083 }
1084
Colin Crossc7b75dc2012-08-29 18:17:06 -07001085 if (argc > 0 && !strcmp(*argv, "help")) {
1086 usage();
1087 return 0;
1088 }
1089
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001090 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001091
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001092 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001093 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 require(2);
1095 fb_queue_display(argv[1], argv[1]);
1096 skip(2);
1097 } else if(!strcmp(*argv, "erase")) {
1098 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001099
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001100 std::string partition_type;
1101 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1102 fs_get_generator(partition_type) != nullptr) {
1103 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1104 partition_type.c_str());
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001105 }
1106
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001107 fb_queue_erase(argv[1]);
1108 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001109 } else if(!strncmp(*argv, "format", strlen("format"))) {
1110 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001111 char *type_override = nullptr;
1112 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001113 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001114 /*
1115 * Parsing for: "format[:[type][:[size]]]"
1116 * Some valid things:
1117 * - select ontly the size, and leave default fs type:
1118 * format::0x4000000 userdata
1119 * - default fs type and size:
1120 * format userdata
1121 * format:: userdata
1122 */
1123 overrides = strchr(*argv, ':');
1124 if (overrides) {
1125 overrides++;
1126 size_override = strchr(overrides, ':');
1127 if (size_override) {
1128 size_override[0] = '\0';
1129 size_override++;
1130 }
1131 type_override = overrides;
1132 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001133 if (type_override && !type_override[0]) type_override = nullptr;
1134 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001135 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001136 fb_queue_erase(argv[1]);
1137 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001138 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001139 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001140 } else if(!strcmp(*argv, "signature")) {
1141 require(2);
1142 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001143 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144 if (sz != 256) die("signature must be 256 bytes");
1145 fb_queue_download("signature", data, sz);
1146 fb_queue_command("signature", "installing signature");
1147 skip(2);
1148 } else if(!strcmp(*argv, "reboot")) {
1149 wants_reboot = 1;
1150 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001151 if (argc > 0) {
1152 if (!strcmp(*argv, "bootloader")) {
1153 wants_reboot = 0;
1154 wants_reboot_bootloader = 1;
1155 skip(1);
1156 }
1157 }
1158 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001159 } else if(!strcmp(*argv, "reboot-bootloader")) {
1160 wants_reboot_bootloader = 1;
1161 skip(1);
1162 } else if (!strcmp(*argv, "continue")) {
1163 fb_queue_command("continue", "resuming boot");
1164 skip(1);
1165 } else if(!strcmp(*argv, "boot")) {
1166 char *kname = 0;
1167 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001168 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169 skip(1);
1170 if (argc > 0) {
1171 kname = argv[0];
1172 skip(1);
1173 }
1174 if (argc > 0) {
1175 rname = argv[0];
1176 skip(1);
1177 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001178 if (argc > 0) {
1179 sname = argv[0];
1180 skip(1);
1181 }
1182 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 if (data == 0) return 1;
1184 fb_queue_download("boot.img", data, sz);
1185 fb_queue_command("boot", "booting");
1186 } else if(!strcmp(*argv, "flash")) {
1187 char *pname = argv[1];
1188 char *fname = 0;
1189 require(2);
1190 if (argc > 2) {
1191 fname = argv[2];
1192 skip(3);
1193 } else {
1194 fname = find_item(pname, product);
1195 skip(2);
1196 }
1197 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001198 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001199 fb_queue_erase(pname);
1200 }
Colin Crossf8387882012-05-24 17:18:41 -07001201 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 } else if(!strcmp(*argv, "flash:raw")) {
1203 char *pname = argv[1];
1204 char *kname = argv[2];
1205 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001206 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001208 skip(3);
1209 if (argc > 0) {
1210 rname = argv[0];
1211 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001212 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001213 if (argc > 0) {
1214 sname = argv[0];
1215 skip(1);
1216 }
1217 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001218 if (data == 0) die("cannot load bootable image");
1219 fb_queue_flash(pname, data, sz);
1220 } else if(!strcmp(*argv, "flashall")) {
1221 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001222 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223 wants_reboot = 1;
1224 } else if(!strcmp(*argv, "update")) {
1225 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001226 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227 skip(2);
1228 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001229 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230 skip(1);
1231 }
1232 wants_reboot = 1;
1233 } else if(!strcmp(*argv, "oem")) {
1234 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001235 } else if(!strcmp(*argv, "flashing")) {
1236 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1237 !strcmp(*(argv+1), "lock") ||
1238 !strcmp(*(argv+1), "unlock_critical") ||
1239 !strcmp(*(argv+1), "lock_critical") ||
1240 !strcmp(*(argv+1), "get_unlock_ability") ||
1241 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1242 !strcmp(*(argv+1), "lock_bootloader"))) {
1243 argc = do_oem_command(argc, argv);
1244 } else
1245 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1246 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001247 } else {
1248 usage();
1249 return 1;
1250 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001251 } else {
1252 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001253 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254 }
1255 }
1256
1257 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001258 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001259 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001260 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001261
1262 std::string cache_type;
1263 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1264 fprintf(stderr, "wiping cache...\n");
1265 fb_queue_erase("cache");
1266 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1267 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001268 }
1269 if (wants_reboot) {
1270 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001271 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001272 } else if (wants_reboot_bootloader) {
1273 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001274 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001275 }
1276
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001277 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001278}