blob: 5745fb0a4251f94e28daf9a6c8ad77044d17195a [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"
264 " erase <partition> Erase a flash partition.\n"
265 " format[:[<fs type>][:[<size>]] <partition>\n"
266 " Format a flash partition. Can\n"
267 " override the fs type and/or size\n"
268 " the bootloader reports.\n"
269 " getvar <variable> Display a bootloader variable.\n"
270 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
271 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
272 " Create bootimage and flash it.\n"
273 " devices [-l] List all connected devices [with\n"
274 " device paths].\n"
275 " continue Continue with autoboot.\n"
276 " reboot [bootloader] Reboot device [into bootloader].\n"
277 " reboot-bootloader Reboot device into bootloader.\n"
278 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 "\n"
280 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700281 " -w Erase userdata and cache (and format\n"
282 " if supported by partition type).\n"
283 " -u Do not erase partition before\n"
284 " formatting.\n"
285 " -s <specific device> Specify device serial number\n"
286 " or path to device port.\n"
287 " -p <product> Specify product name.\n"
288 " -c <cmdline> Override kernel commandline.\n"
289 " -i <vendor id> Specify a custom USB vendor id.\n"
290 " -b <base_addr> Specify a custom kernel base\n"
291 " address (default: 0x10000000).\n"
292 " -n <page size> Specify the nand page size\n"
293 " (default: 2048).\n"
294 " -S <size>[K|M|G] Automatically sparse files greater\n"
295 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297}
298
Elliott Hughesfc797672015-04-07 20:12:50 -0700299static void* load_bootable_image(const char* kernel, const char* ramdisk,
300 const char* secondstage, int64_t* sz,
301 const char* cmdline) {
302 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 fprintf(stderr, "no image specified\n");
304 return 0;
305 }
306
Elliott Hughesfc797672015-04-07 20:12:50 -0700307 int64_t ksize;
308 void* kdata = load_file(kernel, &ksize);
309 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800310 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 return 0;
312 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800313
Elliott Hughesfc797672015-04-07 20:12:50 -0700314 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700316 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800317
Elliott Hughesfc797672015-04-07 20:12:50 -0700318 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
320 return 0;
321 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800322
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 *sz = ksize;
324 return kdata;
325 }
326
Elliott Hughesfc797672015-04-07 20:12:50 -0700327 void* rdata = nullptr;
328 int64_t rsize = 0;
329 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700331 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800332 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 return 0;
334 }
335 }
336
Elliott Hughesfc797672015-04-07 20:12:50 -0700337 void* sdata = nullptr;
338 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200339 if (secondstage) {
340 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700341 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200342 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
343 return 0;
344 }
345 }
346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700348 int64_t bsize = 0;
349 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800350 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200351 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800352 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700353 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 fprintf(stderr,"failed to create boot.img\n");
355 return 0;
356 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700357 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
358 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 return bdata;
362}
363
Elliott Hughesfc797672015-04-07 20:12:50 -0700364static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365{
Yusuke Sato07447542015-06-25 14:39:19 -0700366 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700367 ZipEntry zip_entry;
368 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700369 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000370 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 }
372
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700373 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700375 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700376 if (data == nullptr) {
377 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000378 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 }
380
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700381 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
382 if (error != 0) {
383 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000385 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000387
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 return data;
389}
390
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700391#if defined(_WIN32)
392
393// TODO: move this to somewhere it can be shared.
394
395#include <windows.h>
396
397// Windows' tmpfile(3) requires administrator rights because
398// it creates temporary files in the root directory.
399static FILE* win32_tmpfile() {
400 char temp_path[PATH_MAX];
401 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
402 if (nchars == 0 || nchars >= sizeof(temp_path)) {
403 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
404 return nullptr;
405 }
406
407 char filename[PATH_MAX];
408 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
409 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
410 return nullptr;
411 }
412
413 return fopen(filename, "w+bTD");
414}
415
416#define tmpfile win32_tmpfile
417
418#endif
419
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700420static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
421 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700423 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
424 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700425 return -1;
426 }
427
Yusuke Sato07447542015-06-25 14:39:19 -0700428 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700429 ZipEntry zip_entry;
430 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
431 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000432 return -1;
433 }
434
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700435 int fd = fileno(fp);
436 int error = ExtractEntryToFile(zip, &zip_entry, fd);
437 if (error != 0) {
438 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
439 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700440 }
441
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000442 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700443 return fd;
444}
445
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446static char *strip(char *s)
447{
448 int n;
449 while(*s && isspace(*s)) s++;
450 n = strlen(s);
451 while(n-- > 0) {
452 if(!isspace(s[n])) break;
453 s[n] = 0;
454 }
455 return s;
456}
457
458#define MAX_OPTIONS 32
459static int setup_requirement_line(char *name)
460{
461 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700462 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 unsigned n, count;
464 char *x;
465 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 if (!strncmp(name, "reject ", 7)) {
468 name += 7;
469 invert = 1;
470 } else if (!strncmp(name, "require ", 8)) {
471 name += 8;
472 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700473 } else if (!strncmp(name, "require-for-product:", 20)) {
474 // Get the product and point name past it
475 prod = name + 20;
476 name = strchr(name, ' ');
477 if (!name) return -1;
478 *name = 0;
479 name += 1;
480 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 }
482
483 x = strchr(name, '=');
484 if (x == 0) return 0;
485 *x = 0;
486 val[0] = x + 1;
487
488 for(count = 1; count < MAX_OPTIONS; count++) {
489 x = strchr(val[count - 1],'|');
490 if (x == 0) break;
491 *x = 0;
492 val[count] = x + 1;
493 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800494
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 name = strip(name);
496 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800497
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 name = strip(name);
499 if (name == 0) return -1;
500
Elliott Hughes253c18d2015-03-18 22:47:09 -0700501 const char* var = name;
502 // Work around an unfortunate name mismatch.
503 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504
Elliott Hughes253c18d2015-03-18 22:47:09 -0700505 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 if (out == 0) return -1;
507
508 for(n = 0; n < count; n++) {
509 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700510 if (out[n] == 0) {
511 for(size_t i = 0; i < n; ++i) {
512 free((char*) out[i]);
513 }
514 free(out);
515 return -1;
516 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 }
518
Elliott Hughes253c18d2015-03-18 22:47:09 -0700519 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 return 0;
521}
522
Elliott Hughesfc797672015-04-07 20:12:50 -0700523static void setup_requirements(char* data, int64_t sz) {
524 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700526 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 *s++ = 0;
528 if (setup_requirement_line(data)) {
529 die("out of memory");
530 }
531 data = s;
532 } else {
533 s++;
534 }
535 }
536}
537
Elliott Hughesfc797672015-04-07 20:12:50 -0700538static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 fb_queue_notice("--------------------------------------------");
540 fb_queue_display("version-bootloader", "Bootloader Version...");
541 fb_queue_display("version-baseband", "Baseband Version.....");
542 fb_queue_display("serialno", "Serial Number........");
543 fb_queue_notice("--------------------------------------------");
544}
545
Rom Lemarchand622810c2013-06-28 09:54:59 -0700546static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700547{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700548 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700549 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700550 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700551 }
552
Elliott Hughesfc797672015-04-07 20:12:50 -0700553 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700554 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700555 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700556 }
557
Elliott Hughes253c18d2015-03-18 22:47:09 -0700558 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700559 if (!out_s) {
560 die("Failed to allocate sparse file array\n");
561 }
562
563 files = sparse_file_resparse(s, max_size, out_s, files);
564 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700566 }
567
568 return out_s;
569}
570
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700571static int64_t get_target_sparse_limit(usb_handle* usb) {
572 std::string max_download_size;
573 if (!fb_getvar(usb, "max-download-size", &max_download_size)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800574 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700575 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700576 }
577
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700578 uint64_t limit;
579 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800580 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700581 return 0;
582 }
583 if (limit > 0) {
584 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
585 }
Colin Crossf8387882012-05-24 17:18:41 -0700586 return limit;
587}
588
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700589static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700590 int64_t limit;
591
592 if (sparse_limit == 0) {
593 return 0;
594 } else if (sparse_limit > 0) {
595 limit = sparse_limit;
596 } else {
597 if (target_sparse_limit == -1) {
598 target_sparse_limit = get_target_sparse_limit(usb);
599 }
600 if (target_sparse_limit > 0) {
601 limit = target_sparse_limit;
602 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700603 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700604 }
605 }
606
607 if (size > limit) {
608 return limit;
609 }
610
611 return 0;
612}
613
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700614// Until we get lazy inode table init working in make_ext4fs, we need to
615// erase partitions of type ext4 before flashing a filesystem so no stale
616// inodes are left lying around. Otherwise, e2fsck gets very upset.
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800617static bool needs_erase(usb_handle* usb, const char* partition) {
618 std::string partition_type;
619 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
620 return false;
621 }
622 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700623}
624
Elliott Hughesfc797672015-04-07 20:12:50 -0700625static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
626 int64_t sz = get_file_size(fd);
627 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000628 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700629 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700630
Elliott Hughesfc797672015-04-07 20:12:50 -0700631 lseek64(fd, 0, SEEK_SET);
632 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700633 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700634 sparse_file** s = load_sparse_files(fd, limit);
635 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700636 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700637 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638 buf->type = FB_BUFFER_SPARSE;
639 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700640 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700641 void* data = load_fd(fd, &sz);
642 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700644 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000645 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700646 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700647
648 return 0;
649}
650
651static int load_buf(usb_handle *usb, const char *fname,
652 struct fastboot_buffer *buf)
653{
654 int fd;
655
656 fd = open(fname, O_RDONLY | O_BINARY);
657 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700658 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700659 }
660
661 return load_buf_fd(usb, fd, buf);
662}
663
664static void flash_buf(const char *pname, struct fastboot_buffer *buf)
665{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700666 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700667
668 switch (buf->type) {
669 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700670 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700671 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700672 int64_t sz = sparse_file_len(*s, true, false);
673 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700674 }
675 break;
676 case FB_BUFFER:
677 fb_queue_flash(pname, buf->data, buf->sz);
678 break;
679 default:
680 die("unknown buffer type: %d", buf->type);
681 }
682}
683
Elliott Hughesfc797672015-04-07 20:12:50 -0700684static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700685 struct fastboot_buffer buf;
686
687 if (load_buf(usb, fname, &buf)) {
688 die("cannot load '%s'", fname);
689 }
690 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700691}
692
Elliott Hughesfc797672015-04-07 20:12:50 -0700693static void do_update_signature(ZipArchiveHandle zip, char* fn) {
694 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700695 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700696 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 fb_queue_download("signature", data, sz);
698 fb_queue_command("signature", "installing signature");
699}
700
Elliott Hughesfc797672015-04-07 20:12:50 -0700701static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 queue_info_dump();
703
Wink Savilleb98762f2011-04-04 17:54:59 -0700704 fb_queue_query_save("product", cur_product, sizeof(cur_product));
705
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700706 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700707 int error = OpenArchive(filename, &zip);
708 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100709 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700710 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700711 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712
Elliott Hughesfc797672015-04-07 20:12:50 -0700713 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700714 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700715 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100716 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700717 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 }
719
Elliott Hughes253c18d2015-03-18 22:47:09 -0700720 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721
Elliott Hughesacdbe922015-06-02 21:37:05 -0700722 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700723 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700724 if (fd == -1) {
725 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700726 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700727 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100728 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700729 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700730 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700731 fastboot_buffer buf;
732 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700733 if (rc) die("cannot load %s from flash", images[i].img_name);
734 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700735 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700736 fb_queue_erase(images[i].part_name);
737 }
738 flash_buf(images[i].part_name, &buf);
739 /* not closing the fd here since the sparse code keeps the fd around
740 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
741 * program exits.
742 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700744
745 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746}
747
Elliott Hughesfc797672015-04-07 20:12:50 -0700748static void do_send_signature(char* fn) {
749 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700751
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800753
Elliott Hughesfc797672015-04-07 20:12:50 -0700754 strcpy(xtn, ".sig");
755
756 int64_t sz;
757 void* data = load_file(fn, &sz);
758 strcpy(xtn, ".img");
759 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 fb_queue_download("signature", data, sz);
761 fb_queue_command("signature", "installing signature");
762}
763
Elliott Hughesfc797672015-04-07 20:12:50 -0700764static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 queue_info_dump();
766
Wink Savilleb98762f2011-04-04 17:54:59 -0700767 fb_queue_query_save("product", cur_product, sizeof(cur_product));
768
Elliott Hughes253c18d2015-03-18 22:47:09 -0700769 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700770 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700773 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700774 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700775
776 setup_requirements(reinterpret_cast<char*>(data), sz);
777
778 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700780 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700781 if (load_buf(usb, fname, &buf)) {
782 if (images[i].is_optional)
783 continue;
784 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700785 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700786 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700787 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700788 fb_queue_erase(images[i].part_name);
789 }
790 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792}
793
794#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800795#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796
Elliott Hughesfc797672015-04-07 20:12:50 -0700797static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 char command[256];
799 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800800
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801 command[0] = 0;
802 while(1) {
803 strcat(command,*argv);
804 skip(1);
805 if(argc == 0) break;
806 strcat(command," ");
807 }
808
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800809 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 return 0;
811}
812
Colin Crossf8387882012-05-24 17:18:41 -0700813static int64_t parse_num(const char *arg)
814{
815 char *endptr;
816 unsigned long long num;
817
818 num = strtoull(arg, &endptr, 0);
819 if (endptr == arg) {
820 return -1;
821 }
822
823 if (*endptr == 'k' || *endptr == 'K') {
824 if (num >= (-1ULL) / 1024) {
825 return -1;
826 }
827 num *= 1024LL;
828 endptr++;
829 } else if (*endptr == 'm' || *endptr == 'M') {
830 if (num >= (-1ULL) / (1024 * 1024)) {
831 return -1;
832 }
833 num *= 1024LL * 1024LL;
834 endptr++;
835 } else if (*endptr == 'g' || *endptr == 'G') {
836 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
837 return -1;
838 }
839 num *= 1024LL * 1024LL * 1024LL;
840 endptr++;
841 }
842
843 if (*endptr != '\0') {
844 return -1;
845 }
846
847 if (num > INT64_MAX) {
848 return -1;
849 }
850
851 return num;
852}
853
Elliott Hughesfc797672015-04-07 20:12:50 -0700854static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700855 const char* partition, int skip_if_not_supported,
856 const char* type_override, const char* size_override) {
857 std::string partition_type, partition_size;
858
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700859 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700860 const char* errMsg = nullptr;
861 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700862 int fd;
863
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700864 unsigned int limit = INT_MAX;
865 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700866 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700867 }
868 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700869 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700870 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700871
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700872 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700873 errMsg = "Can't determine partition type.\n";
874 goto failed;
875 }
JP Abgrall7e859742014-05-06 15:14:15 -0700876 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700877 if (partition_type != type_override) {
878 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
879 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700880 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700881 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700882 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700883
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700884 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700885 errMsg = "Unable to get partition size\n";
886 goto failed;
887 }
JP Abgrall7e859742014-05-06 15:14:15 -0700888 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700889 if (partition_size != size_override) {
890 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
891 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700892 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700893 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700894 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700895
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800896 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700897 if (!gen) {
898 if (skip_if_not_supported) {
899 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700900 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700901 return;
902 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700903 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
904 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700905 return;
906 }
907
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700908 int64_t size;
909 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
910 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
911 return;
912 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700913
914 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700915 if (fs_generator_generate(gen, fd, size)) {
916 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700917 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700918 return;
919 }
920
921 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700922 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700923 close(fd);
924 return;
925 }
926 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700927 return;
928
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700929failed:
930 if (skip_if_not_supported) {
931 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700932 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700933 }
934 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
935}
936
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800937int main(int argc, char **argv)
938{
939 int wants_wipe = 0;
940 int wants_reboot = 0;
941 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700942 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800943 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700944 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100945 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946
JP Abgrall7b8970c2013-03-07 17:06:41 -0800947 const struct option longopts[] = {
948 {"base", required_argument, 0, 'b'},
949 {"kernel_offset", required_argument, 0, 'k'},
950 {"page_size", required_argument, 0, 'n'},
951 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800952 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700953 {"help", no_argument, 0, 'h'},
954 {"unbuffered", no_argument, 0, 0},
955 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800956 {0, 0, 0, 0}
957 };
Colin Cross8879f982012-05-22 17:53:34 -0700958
959 serial = getenv("ANDROID_SERIAL");
960
961 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700962 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 -0700963 if (c < 0) {
964 break;
965 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800966 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700967 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700968 case 'b':
969 base_addr = strtoul(optarg, 0, 16);
970 break;
Colin Cross8879f982012-05-22 17:53:34 -0700971 case 'c':
972 cmdline = optarg;
973 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800974 case 'h':
975 usage();
976 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700977 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -0700978 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -0700979 unsigned long val;
980
981 val = strtoul(optarg, &endptr, 0);
982 if (!endptr || *endptr != '\0' || (val & ~0xffff))
983 die("invalid vendor id '%s'", optarg);
984 vendor_id = (unsigned short)val;
985 break;
986 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800987 case 'k':
988 kernel_offset = strtoul(optarg, 0, 16);
989 break;
990 case 'l':
991 long_listing = 1;
992 break;
993 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -0700994 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -0800995 if (!page_size) die("invalid page size");
996 break;
997 case 'p':
998 product = optarg;
999 break;
1000 case 'r':
1001 ramdisk_offset = strtoul(optarg, 0, 16);
1002 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001003 case 't':
1004 tags_offset = strtoul(optarg, 0, 16);
1005 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001006 case 's':
1007 serial = optarg;
1008 break;
1009 case 'S':
1010 sparse_limit = parse_num(optarg);
1011 if (sparse_limit < 0) {
1012 die("invalid sparse limit");
1013 }
1014 break;
1015 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001016 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001017 break;
1018 case 'w':
1019 wants_wipe = 1;
1020 break;
Colin Cross8879f982012-05-22 17:53:34 -07001021 case '?':
1022 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001023 case 0:
1024 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001025 setvbuf(stdout, nullptr, _IONBF, 0);
1026 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001027 } else if (strcmp("version", longopts[longindex].name) == 0) {
1028 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1029 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001030 }
1031 break;
Colin Cross8879f982012-05-22 17:53:34 -07001032 default:
1033 abort();
1034 }
1035 }
1036
1037 argc -= optind;
1038 argv += optind;
1039
1040 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001042 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001043 }
1044
Colin Cross8fb6e062012-07-24 16:36:41 -07001045 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001046 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001048 return 0;
1049 }
1050
Colin Crossc7b75dc2012-08-29 18:17:06 -07001051 if (argc > 0 && !strcmp(*argv, "help")) {
1052 usage();
1053 return 0;
1054 }
1055
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001056 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001057
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001058 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001059 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060 require(2);
1061 fb_queue_display(argv[1], argv[1]);
1062 skip(2);
1063 } else if(!strcmp(*argv, "erase")) {
1064 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001065
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001066 std::string partition_type;
1067 if (fb_getvar(usb, std::string("partition-type:") + argv[1], &partition_type) &&
1068 fs_get_generator(partition_type) != nullptr) {
1069 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1070 partition_type.c_str());
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001071 }
1072
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001073 fb_queue_erase(argv[1]);
1074 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001075 } else if(!strncmp(*argv, "format", strlen("format"))) {
1076 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001077 char *type_override = nullptr;
1078 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001079 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001080 /*
1081 * Parsing for: "format[:[type][:[size]]]"
1082 * Some valid things:
1083 * - select ontly the size, and leave default fs type:
1084 * format::0x4000000 userdata
1085 * - default fs type and size:
1086 * format userdata
1087 * format:: userdata
1088 */
1089 overrides = strchr(*argv, ':');
1090 if (overrides) {
1091 overrides++;
1092 size_override = strchr(overrides, ':');
1093 if (size_override) {
1094 size_override[0] = '\0';
1095 size_override++;
1096 }
1097 type_override = overrides;
1098 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001099 if (type_override && !type_override[0]) type_override = nullptr;
1100 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001101 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001102 fb_queue_erase(argv[1]);
1103 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001104 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001105 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001106 } else if(!strcmp(*argv, "signature")) {
1107 require(2);
1108 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001109 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110 if (sz != 256) die("signature must be 256 bytes");
1111 fb_queue_download("signature", data, sz);
1112 fb_queue_command("signature", "installing signature");
1113 skip(2);
1114 } else if(!strcmp(*argv, "reboot")) {
1115 wants_reboot = 1;
1116 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001117 if (argc > 0) {
1118 if (!strcmp(*argv, "bootloader")) {
1119 wants_reboot = 0;
1120 wants_reboot_bootloader = 1;
1121 skip(1);
1122 }
1123 }
1124 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001125 } else if(!strcmp(*argv, "reboot-bootloader")) {
1126 wants_reboot_bootloader = 1;
1127 skip(1);
1128 } else if (!strcmp(*argv, "continue")) {
1129 fb_queue_command("continue", "resuming boot");
1130 skip(1);
1131 } else if(!strcmp(*argv, "boot")) {
1132 char *kname = 0;
1133 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001134 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001135 skip(1);
1136 if (argc > 0) {
1137 kname = argv[0];
1138 skip(1);
1139 }
1140 if (argc > 0) {
1141 rname = argv[0];
1142 skip(1);
1143 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001144 if (argc > 0) {
1145 sname = argv[0];
1146 skip(1);
1147 }
1148 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 if (data == 0) return 1;
1150 fb_queue_download("boot.img", data, sz);
1151 fb_queue_command("boot", "booting");
1152 } else if(!strcmp(*argv, "flash")) {
1153 char *pname = argv[1];
1154 char *fname = 0;
1155 require(2);
1156 if (argc > 2) {
1157 fname = argv[2];
1158 skip(3);
1159 } else {
1160 fname = find_item(pname, product);
1161 skip(2);
1162 }
1163 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001164 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001165 fb_queue_erase(pname);
1166 }
Colin Crossf8387882012-05-24 17:18:41 -07001167 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 } else if(!strcmp(*argv, "flash:raw")) {
1169 char *pname = argv[1];
1170 char *kname = argv[2];
1171 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001172 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001173 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001174 skip(3);
1175 if (argc > 0) {
1176 rname = argv[0];
1177 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001179 if (argc > 0) {
1180 sname = argv[0];
1181 skip(1);
1182 }
1183 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 if (data == 0) die("cannot load bootable image");
1185 fb_queue_flash(pname, data, sz);
1186 } else if(!strcmp(*argv, "flashall")) {
1187 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001188 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189 wants_reboot = 1;
1190 } else if(!strcmp(*argv, "update")) {
1191 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001192 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 skip(2);
1194 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001195 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196 skip(1);
1197 }
1198 wants_reboot = 1;
1199 } else if(!strcmp(*argv, "oem")) {
1200 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001201 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1202 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1203 || !strcmp(*(argv+1), "unlock_critical")
1204 || !strcmp(*(argv+1), "lock_critical")
1205 || !strcmp(*(argv+1), "get_unlock_ability")) {
1206 argc = do_oem_command(argc, argv);
1207 } else {
1208 usage();
1209 return 1;
1210 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001211 } else {
1212 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001213 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 }
1215 }
1216
1217 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001218 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001220 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001221
1222 std::string cache_type;
1223 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1224 fprintf(stderr, "wiping cache...\n");
1225 fb_queue_erase("cache");
1226 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1227 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228 }
1229 if (wants_reboot) {
1230 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001231 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001232 } else if (wants_reboot_bootloader) {
1233 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001234 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235 }
1236
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001237 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001238}