blob: f98268e4f19478f9d2c0298ab8d3a7797ac14ef1 [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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include <stdio.h>
32#include <stdlib.h>
Colin Crossf8387882012-05-24 17:18:41 -070033#include <stdbool.h>
34#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <string.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <limits.h>
40#include <ctype.h>
Colin Cross8879f982012-05-22 17:53:34 -070041#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
Rom Lemarchand622810c2013-06-28 09:54:59 -070045#include <sys/stat.h>
Colin Crossf8387882012-05-24 17:18:41 -070046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include <bootimg.h>
Colin Crossf8387882012-05-24 17:18:41 -070048#include <sparse/sparse.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049#include <zipfile/zipfile.h>
50
51#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
Brian Swetland2a63bb72009-04-28 16:05:07 -070062void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
63
JP Abgrall7b8970c2013-03-07 17:06:41 -080064boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
65 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
66 void *second, unsigned second_size, unsigned second_offset,
67 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070068 unsigned *bootimg_size);
69
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static usb_handle *usb = 0;
71static const char *serial = 0;
72static const char *product = 0;
73static const char *cmdline = 0;
74static int wipe_data = 0;
75static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070076static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070077static int64_t sparse_limit = -1;
78static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
JP Abgrall7b8970c2013-03-07 17:06:41 -080080unsigned page_size = 2048;
81unsigned base_addr = 0x10000000;
82unsigned kernel_offset = 0x00008000;
83unsigned ramdisk_offset = 0x01000000;
84unsigned second_offset = 0x00f00000;
85unsigned tags_offset = 0x00000100;
86
Rom Lemarchand622810c2013-06-28 09:54:59 -070087enum fb_buffer_type {
88 FB_BUFFER,
89 FB_BUFFER_SPARSE,
90};
91
92struct fastboot_buffer {
93 enum fb_buffer_type type;
94 void *data;
95 unsigned int sz;
96};
97
98static struct {
99 char img_name[13];
100 char sig_name[13];
101 char part_name[9];
102 bool is_optional;
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700103} images[4] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 {"boot.img", "boot.sig", "boot", false},
105 {"recovery.img", "recovery.sig", "recovery", true},
106 {"system.img", "system.sig", "system", false},
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700107 {"tos.img", "tos.sig", "tos", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110void get_my_path(char *path);
111
112char *find_item(const char *item, const char *product)
113{
114 char *dir;
115 char *fn;
116 char path[PATH_MAX + 128];
117
118 if(!strcmp(item,"boot")) {
119 fn = "boot.img";
120 } else if(!strcmp(item,"recovery")) {
121 fn = "recovery.img";
122 } else if(!strcmp(item,"system")) {
123 fn = "system.img";
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700124 } else if(!strcmp(item,"tos")) {
125 fn = "tos.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"userdata")) {
127 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700128 } else if(!strcmp(item,"cache")) {
129 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 } else if(!strcmp(item,"info")) {
131 fn = "android-info.txt";
132 } else {
133 fprintf(stderr,"unknown partition '%s'\n", item);
134 return 0;
135 }
136
137 if(product) {
138 get_my_path(path);
139 sprintf(path + strlen(path),
140 "../../../target/product/%s/%s", product, fn);
141 return strdup(path);
142 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800143
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 dir = getenv("ANDROID_PRODUCT_OUT");
145 if((dir == 0) || (dir[0] == 0)) {
146 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
147 return 0;
148 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800149
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 sprintf(path, "%s/%s", dir, fn);
151 return strdup(path);
152}
153
Rom Lemarchand622810c2013-06-28 09:54:59 -0700154static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700155{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700156 struct stat st;
157 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700158
Rom Lemarchand622810c2013-06-28 09:54:59 -0700159 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700160
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700162}
163
Rom Lemarchand622810c2013-06-28 09:54:59 -0700164static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165{
166 char *data;
167 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800168 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
170 data = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Rom Lemarchand622810c2013-06-28 09:54:59 -0700172 sz = file_size(fd);
173 if (sz < 0) {
174 goto oops;
175 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
177 data = (char*) malloc(sz);
178 if(data == 0) goto oops;
179
180 if(read(fd, data, sz) != sz) goto oops;
181 close(fd);
182
183 if(_sz) *_sz = sz;
184 return data;
185
186oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800187 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 close(fd);
189 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800190 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return 0;
192}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Rom Lemarchand622810c2013-06-28 09:54:59 -0700194static void *load_file(const char *fn, unsigned *_sz)
195{
196 int fd;
197
198 fd = open(fn, O_RDONLY | O_BINARY);
199 if(fd < 0) return 0;
200
201 return load_fd(fd, _sz);
202}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
JP Abgralla032ded2012-06-06 11:53:33 -0700204int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
205{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400207 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800208 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700209 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800210 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700211 (info->dev_vendor != 0x0fce) && // Sony Ericsson
212 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400213 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800214 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800215 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800216 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800217 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400218 (info->dev_vendor != 0x0bb4)) // HTC
219 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if(info->ifc_class != 0xff) return -1;
221 if(info->ifc_subclass != 0x42) return -1;
222 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700223 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700225 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
226 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 return 0;
228}
229
JP Abgrall7b8970c2013-03-07 17:06:41 -0800230int match_fastboot(usb_ifc_info *info)
231{
232 return match_fastboot_with_serial(info, serial);
233}
234
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235int list_devices_callback(usb_ifc_info *info)
236{
JP Abgralla032ded2012-06-06 11:53:33 -0700237 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700239 if (!info->writable) {
240 serial = "no permissions"; // like "adb devices"
241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 if (!serial[0]) {
243 serial = "????????????";
244 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700245 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700246 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700247 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700248 } else if (!info->device_path) {
249 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700250 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700251 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700252 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 }
254
255 return -1;
256}
257
258usb_handle *open_device(void)
259{
260 static usb_handle *usb = 0;
261 int announce = 1;
262
263 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800264
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265 for(;;) {
266 usb = usb_open(match_fastboot);
267 if(usb) return usb;
268 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800269 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 fprintf(stderr,"< waiting for device >\n");
271 }
272 sleep(1);
273 }
274}
275
276void list_devices(void) {
277 // We don't actually open a USB device here,
278 // just getting our callback called so we can
279 // list all the connected devices.
280 usb_open(list_devices_callback);
281}
282
283void usage(void)
284{
285 fprintf(stderr,
286/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
287 "usage: fastboot [ <option> ] <command>\n"
288 "\n"
289 "commands:\n"
290 " update <filename> reflash device from update.zip\n"
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700291 " flashall flash boot, system, and if found, recovery, tos\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 " flash <partition> [ <filename> ] write a file to a flash partition\n"
293 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800294 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 " getvar <variable> display a bootloader variable\n"
296 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
297 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
298 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700299 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 " reboot reboot device normally\n"
301 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800302 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 "\n"
304 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700305 " -w erase userdata and cache (and format\n"
306 " if supported by partition type)\n"
307 " -u do not first erase partition before\n"
308 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700309 " -s <specific device> specify device serial number\n"
310 " or path to device port\n"
311 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 " -p <product> specify product name\n"
313 " -c <cmdline> override kernel commandline\n"
314 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800315 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800316 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700317 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700318 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320}
321
JP Abgrall7b8970c2013-03-07 17:06:41 -0800322void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 unsigned *sz, const char *cmdline)
324{
325 void *kdata = 0, *rdata = 0;
326 unsigned ksize = 0, rsize = 0;
327 void *bdata;
328 unsigned bsize;
329
330 if(kernel == 0) {
331 fprintf(stderr, "no image specified\n");
332 return 0;
333 }
334
335 kdata = load_file(kernel, &ksize);
336 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800337 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 return 0;
339 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 /* is this actually a boot image? */
342 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
343 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800344
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 if(ramdisk) {
346 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
347 return 0;
348 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800349
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 *sz = ksize;
351 return kdata;
352 }
353
354 if(ramdisk) {
355 rdata = load_file(ramdisk, &rsize);
356 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800357 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 return 0;
359 }
360 }
361
362 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800363 bdata = mkbootimg(kdata, ksize, kernel_offset,
364 rdata, rsize, ramdisk_offset,
365 0, 0, second_offset,
366 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 if(bdata == 0) {
368 fprintf(stderr,"failed to create boot.img\n");
369 return 0;
370 }
371 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
372 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
373 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 return bdata;
376}
377
378void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
379{
380 void *data;
381 zipentry_t entry;
382 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800383
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 entry = lookup_zipentry(zip, name);
385 if (entry == NULL) {
386 fprintf(stderr, "archive does not contain '%s'\n", name);
387 return 0;
388 }
389
390 *sz = get_zipentry_size(entry);
391
392 datasz = *sz * 1.001;
393 data = malloc(datasz);
394
395 if(data == 0) {
396 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
397 return 0;
398 }
399
400 if (decompress_zipentry(entry, data, datasz)) {
401 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
402 free(data);
403 return 0;
404 }
405
406 return data;
407}
408
Rom Lemarchand622810c2013-06-28 09:54:59 -0700409static int unzip_to_file(zipfile_t zip, char *name)
410{
411 int fd;
412 char *data;
413 unsigned sz;
414
415 fd = fileno(tmpfile());
416 if (fd < 0) {
417 return -1;
418 }
419
420 data = unzip_file(zip, name, &sz);
421 if (data == 0) {
422 return -1;
423 }
424
425 if (write(fd, data, sz) != sz) {
426 fd = -1;
427 }
428
429 free(data);
430 lseek(fd, 0, SEEK_SET);
431 return fd;
432}
433
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434static char *strip(char *s)
435{
436 int n;
437 while(*s && isspace(*s)) s++;
438 n = strlen(s);
439 while(n-- > 0) {
440 if(!isspace(s[n])) break;
441 s[n] = 0;
442 }
443 return s;
444}
445
446#define MAX_OPTIONS 32
447static int setup_requirement_line(char *name)
448{
449 char *val[MAX_OPTIONS];
450 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700451 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 unsigned n, count;
453 char *x;
454 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 if (!strncmp(name, "reject ", 7)) {
457 name += 7;
458 invert = 1;
459 } else if (!strncmp(name, "require ", 8)) {
460 name += 8;
461 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700462 } else if (!strncmp(name, "require-for-product:", 20)) {
463 // Get the product and point name past it
464 prod = name + 20;
465 name = strchr(name, ' ');
466 if (!name) return -1;
467 *name = 0;
468 name += 1;
469 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 }
471
472 x = strchr(name, '=');
473 if (x == 0) return 0;
474 *x = 0;
475 val[0] = x + 1;
476
477 for(count = 1; count < MAX_OPTIONS; count++) {
478 x = strchr(val[count - 1],'|');
479 if (x == 0) break;
480 *x = 0;
481 val[count] = x + 1;
482 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800483
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 name = strip(name);
485 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 name = strip(name);
488 if (name == 0) return -1;
489
490 /* work around an unfortunate name mismatch */
491 if (!strcmp(name,"board")) name = "product";
492
493 out = malloc(sizeof(char*) * count);
494 if (out == 0) return -1;
495
496 for(n = 0; n < count; n++) {
497 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700498 if (out[n] == 0) {
499 for(size_t i = 0; i < n; ++i) {
500 free((char*) out[i]);
501 }
502 free(out);
503 return -1;
504 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 }
506
Wink Savilleb98762f2011-04-04 17:54:59 -0700507 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 return 0;
509}
510
511static void setup_requirements(char *data, unsigned sz)
512{
513 char *s;
514
515 s = data;
516 while (sz-- > 0) {
517 if(*s == '\n') {
518 *s++ = 0;
519 if (setup_requirement_line(data)) {
520 die("out of memory");
521 }
522 data = s;
523 } else {
524 s++;
525 }
526 }
527}
528
529void queue_info_dump(void)
530{
531 fb_queue_notice("--------------------------------------------");
532 fb_queue_display("version-bootloader", "Bootloader Version...");
533 fb_queue_display("version-baseband", "Baseband Version.....");
534 fb_queue_display("serialno", "Serial Number........");
535 fb_queue_notice("--------------------------------------------");
536}
537
Rom Lemarchand622810c2013-06-28 09:54:59 -0700538static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700539{
Colin Crossf8387882012-05-24 17:18:41 -0700540 struct sparse_file *s;
541 int files;
542 struct sparse_file **out_s;
543
Colin Crossf8387882012-05-24 17:18:41 -0700544 s = sparse_file_import_auto(fd, false);
545 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700546 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700547 }
548
549 files = sparse_file_resparse(s, max_size, NULL, 0);
550 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700551 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700552 }
553
554 out_s = calloc(sizeof(struct sparse_file *), files + 1);
555 if (!out_s) {
556 die("Failed to allocate sparse file array\n");
557 }
558
559 files = sparse_file_resparse(s, max_size, out_s, files);
560 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
564 return out_s;
565}
566
567static int64_t get_target_sparse_limit(struct usb_handle *usb)
568{
569 int64_t limit = 0;
570 char response[FB_RESPONSE_SZ + 1];
571 int status = fb_getvar(usb, response, "max-download-size");
572
573 if (!status) {
574 limit = strtoul(response, NULL, 0);
575 if (limit > 0) {
576 fprintf(stderr, "target reported max download size of %lld bytes\n",
577 limit);
578 }
579 }
580
581 return limit;
582}
583
584static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
585{
586 int64_t limit;
587
588 if (sparse_limit == 0) {
589 return 0;
590 } else if (sparse_limit > 0) {
591 limit = sparse_limit;
592 } else {
593 if (target_sparse_limit == -1) {
594 target_sparse_limit = get_target_sparse_limit(usb);
595 }
596 if (target_sparse_limit > 0) {
597 limit = target_sparse_limit;
598 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700599 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700600 }
601 }
602
603 if (size > limit) {
604 return limit;
605 }
606
607 return 0;
608}
609
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700610/* Until we get lazy inode table init working in make_ext4fs, we need to
611 * erase partitions of type ext4 before flashing a filesystem so no stale
612 * inodes are left lying around. Otherwise, e2fsck gets very upset.
613 */
614static int needs_erase(const char *part)
615{
616 /* The function fb_format_supported() currently returns the value
617 * we want, so just call it.
618 */
619 return fb_format_supported(usb, part);
620}
621
Rom Lemarchand622810c2013-06-28 09:54:59 -0700622static int load_buf_fd(usb_handle *usb, int fd,
623 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700624{
625 int64_t sz64;
626 void *data;
627 int64_t limit;
628
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700629
Rom Lemarchand622810c2013-06-28 09:54:59 -0700630 sz64 = file_size(fd);
631 if (sz64 < 0) {
632 return -1;
633 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700634
635 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700636 limit = get_sparse_limit(usb, sz64);
637 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700639 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700640 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700641 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700642 buf->type = FB_BUFFER_SPARSE;
643 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700644 } else {
645 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 data = load_fd(fd, &sz);
647 if (data == 0) return -1;
648 buf->type = FB_BUFFER;
649 buf->data = data;
650 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700651 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700652
653 return 0;
654}
655
656static int load_buf(usb_handle *usb, const char *fname,
657 struct fastboot_buffer *buf)
658{
659 int fd;
660
661 fd = open(fname, O_RDONLY | O_BINARY);
662 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700663 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700664 }
665
666 return load_buf_fd(usb, fd, buf);
667}
668
669static void flash_buf(const char *pname, struct fastboot_buffer *buf)
670{
671 struct sparse_file **s;
672
673 switch (buf->type) {
674 case FB_BUFFER_SPARSE:
675 s = buf->data;
676 while (*s) {
677 int64_t sz64 = sparse_file_len(*s, true, false);
678 fb_queue_flash_sparse(pname, *s++, sz64);
679 }
680 break;
681 case FB_BUFFER:
682 fb_queue_flash(pname, buf->data, buf->sz);
683 break;
684 default:
685 die("unknown buffer type: %d", buf->type);
686 }
687}
688
689void do_flash(usb_handle *usb, const char *pname, const char *fname)
690{
691 struct fastboot_buffer buf;
692
693 if (load_buf(usb, fname, &buf)) {
694 die("cannot load '%s'", fname);
695 }
696 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700697}
698
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699void do_update_signature(zipfile_t zip, char *fn)
700{
701 void *data;
702 unsigned sz;
703 data = unzip_file(zip, fn, &sz);
704 if (data == 0) return;
705 fb_queue_download("signature", data, sz);
706 fb_queue_command("signature", "installing signature");
707}
708
Rom Lemarchand622810c2013-06-28 09:54:59 -0700709void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710{
711 void *zdata;
712 unsigned zsize;
713 void *data;
714 unsigned sz;
715 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700716 int fd;
717 int rc;
718 struct fastboot_buffer buf;
719 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720
721 queue_info_dump();
722
Wink Savilleb98762f2011-04-04 17:54:59 -0700723 fb_queue_query_save("product", cur_product, sizeof(cur_product));
724
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800726 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727
728 zip = init_zipfile(zdata, zsize);
729 if(zip == 0) die("failed to access zipdata in '%s'");
730
731 data = unzip_file(zip, "android-info.txt", &sz);
732 if (data == 0) {
733 char *tmp;
734 /* fallback for older zipfiles */
735 data = unzip_file(zip, "android-product.txt", &sz);
736 if ((data == 0) || (sz < 1)) {
737 die("update package has no android-info.txt or android-product.txt");
738 }
739 tmp = malloc(sz + 128);
740 if (tmp == 0) die("out of memory");
741 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
742 data = tmp;
743 sz = strlen(tmp);
744 }
745
746 setup_requirements(data, sz);
747
Rom Lemarchand622810c2013-06-28 09:54:59 -0700748 for (i = 0; i < ARRAY_SIZE(images); i++) {
749 fd = unzip_to_file(zip, images[i].img_name);
750 if (fd < 0) {
751 if (images[i].is_optional)
752 continue;
753 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700754 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700755 rc = load_buf_fd(usb, fd, &buf);
756 if (rc) die("cannot load %s from flash", images[i].img_name);
757 do_update_signature(zip, images[i].sig_name);
758 if (erase_first && needs_erase(images[i].part_name)) {
759 fb_queue_erase(images[i].part_name);
760 }
761 flash_buf(images[i].part_name, &buf);
762 /* not closing the fd here since the sparse code keeps the fd around
763 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
764 * program exits.
765 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767}
768
769void do_send_signature(char *fn)
770{
771 void *data;
772 unsigned sz;
773 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800774
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775 xtn = strrchr(fn, '.');
776 if (!xtn) return;
777 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800778
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779 strcpy(xtn,".sig");
780 data = load_file(fn, &sz);
781 strcpy(xtn,".img");
782 if (data == 0) return;
783 fb_queue_download("signature", data, sz);
784 fb_queue_command("signature", "installing signature");
785}
786
Rom Lemarchand622810c2013-06-28 09:54:59 -0700787void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800788{
789 char *fname;
790 void *data;
791 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700792 struct fastboot_buffer buf;
793 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794
795 queue_info_dump();
796
Wink Savilleb98762f2011-04-04 17:54:59 -0700797 fb_queue_query_save("product", cur_product, sizeof(cur_product));
798
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800799 fname = find_item("info", product);
800 if (fname == 0) die("cannot find android-info.txt");
801 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800802 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803 setup_requirements(data, sz);
804
Rom Lemarchand622810c2013-06-28 09:54:59 -0700805 for (i = 0; i < ARRAY_SIZE(images); i++) {
806 fname = find_item(images[i].part_name, product);
807 if (load_buf(usb, fname, &buf)) {
808 if (images[i].is_optional)
809 continue;
810 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700811 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812 do_send_signature(fname);
813 if (erase_first && needs_erase(images[i].part_name)) {
814 fb_queue_erase(images[i].part_name);
815 }
816 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800818}
819
820#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800821#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822
823int do_oem_command(int argc, char **argv)
824{
825 int i;
826 char command[256];
827 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800828
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 command[0] = 0;
830 while(1) {
831 strcat(command,*argv);
832 skip(1);
833 if(argc == 0) break;
834 strcat(command," ");
835 }
836
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800837 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 return 0;
839}
840
Colin Crossf8387882012-05-24 17:18:41 -0700841static int64_t parse_num(const char *arg)
842{
843 char *endptr;
844 unsigned long long num;
845
846 num = strtoull(arg, &endptr, 0);
847 if (endptr == arg) {
848 return -1;
849 }
850
851 if (*endptr == 'k' || *endptr == 'K') {
852 if (num >= (-1ULL) / 1024) {
853 return -1;
854 }
855 num *= 1024LL;
856 endptr++;
857 } else if (*endptr == 'm' || *endptr == 'M') {
858 if (num >= (-1ULL) / (1024 * 1024)) {
859 return -1;
860 }
861 num *= 1024LL * 1024LL;
862 endptr++;
863 } else if (*endptr == 'g' || *endptr == 'G') {
864 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
865 return -1;
866 }
867 num *= 1024LL * 1024LL * 1024LL;
868 endptr++;
869 }
870
871 if (*endptr != '\0') {
872 return -1;
873 }
874
875 if (num > INT64_MAX) {
876 return -1;
877 }
878
879 return num;
880}
881
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700882void fb_perform_format(const char *partition, int skip_if_not_supported)
883{
884 char pType[FB_RESPONSE_SZ + 1], pSize[FB_RESPONSE_SZ + 1];
885 unsigned int limit = INT_MAX;
886 struct fastboot_buffer buf;
887 const char *errMsg = NULL;
888 const struct fs_generator *gen;
889 uint64_t pSz;
890 int status;
891 int fd;
892
893 if (target_sparse_limit > 0 && target_sparse_limit < limit)
894 limit = target_sparse_limit;
895 if (sparse_limit > 0 && sparse_limit < limit)
896 limit = sparse_limit;
897
898 status = fb_getvar(usb, pType, "partition-type:%s", partition);
899 if (status) {
900 errMsg = "Can't determine partition type.\n";
901 goto failed;
902 }
903
904 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
905 if (status) {
906 errMsg = "Unable to get partition size\n";
907 goto failed;
908 }
909
910 gen = fs_get_generator(pType);
911 if (!gen) {
912 if (skip_if_not_supported) {
913 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
914 fprintf(stderr, "File system type %s not supported.\n", pType);
915 return;
916 }
917 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
918 return;
919 }
920
921 pSz = strtoll(pSize, (char **)NULL, 16);
922
923 fd = fileno(tmpfile());
924 if (fs_generator_generate(gen, fd, pSz)) {
925 close(fd);
926 fprintf(stderr, "Cannot generate image.\n");
927 return;
928 }
929
930 if (load_buf_fd(usb, fd, &buf)) {
931 fprintf(stderr, "Cannot read image.\n");
932 close(fd);
933 return;
934 }
935 flash_buf(partition, &buf);
936
937 return;
938
939
940failed:
941 if (skip_if_not_supported) {
942 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
943 if (errMsg)
944 fprintf(stderr, "%s", errMsg);
945 }
946 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
947}
948
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800949int main(int argc, char **argv)
950{
951 int wants_wipe = 0;
952 int wants_reboot = 0;
953 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700954 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800955 void *data;
956 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700957 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700958 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700959 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800960
JP Abgrall7b8970c2013-03-07 17:06:41 -0800961 const struct option longopts[] = {
962 {"base", required_argument, 0, 'b'},
963 {"kernel_offset", required_argument, 0, 'k'},
964 {"page_size", required_argument, 0, 'n'},
965 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800966 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800967 {"help", 0, 0, 'h'},
968 {0, 0, 0, 0}
969 };
Colin Cross8879f982012-05-22 17:53:34 -0700970
971 serial = getenv("ANDROID_SERIAL");
972
973 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800974 int option_index = 0;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800975 c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700976 if (c < 0) {
977 break;
978 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800979 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700980 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700981 case 'b':
982 base_addr = strtoul(optarg, 0, 16);
983 break;
Colin Cross8879f982012-05-22 17:53:34 -0700984 case 'c':
985 cmdline = optarg;
986 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800987 case 'h':
988 usage();
989 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700990 case 'i': {
991 char *endptr = NULL;
992 unsigned long val;
993
994 val = strtoul(optarg, &endptr, 0);
995 if (!endptr || *endptr != '\0' || (val & ~0xffff))
996 die("invalid vendor id '%s'", optarg);
997 vendor_id = (unsigned short)val;
998 break;
999 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001000 case 'k':
1001 kernel_offset = strtoul(optarg, 0, 16);
1002 break;
1003 case 'l':
1004 long_listing = 1;
1005 break;
1006 case 'n':
1007 page_size = (unsigned)strtoul(optarg, NULL, 0);
1008 if (!page_size) die("invalid page size");
1009 break;
1010 case 'p':
1011 product = optarg;
1012 break;
1013 case 'r':
1014 ramdisk_offset = strtoul(optarg, 0, 16);
1015 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001016 case 't':
1017 tags_offset = strtoul(optarg, 0, 16);
1018 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001019 case 's':
1020 serial = optarg;
1021 break;
1022 case 'S':
1023 sparse_limit = parse_num(optarg);
1024 if (sparse_limit < 0) {
1025 die("invalid sparse limit");
1026 }
1027 break;
1028 case 'u':
1029 erase_first = 0;
1030 break;
1031 case 'w':
1032 wants_wipe = 1;
1033 break;
Colin Cross8879f982012-05-22 17:53:34 -07001034 case '?':
1035 return 1;
1036 default:
1037 abort();
1038 }
1039 }
1040
1041 argc -= optind;
1042 argv += optind;
1043
1044 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001045 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001046 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047 }
1048
Colin Cross8fb6e062012-07-24 16:36:41 -07001049 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001050 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001051 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001052 return 0;
1053 }
1054
Colin Crossc7b75dc2012-08-29 18:17:06 -07001055 if (argc > 0 && !strcmp(*argv, "help")) {
1056 usage();
1057 return 0;
1058 }
1059
Colin Cross8879f982012-05-22 17:53:34 -07001060 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001061
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001062 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001063 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 require(2);
1065 fb_queue_display(argv[1], argv[1]);
1066 skip(2);
1067 } else if(!strcmp(*argv, "erase")) {
1068 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001069
1070 if (fb_format_supported(usb, argv[1])) {
1071 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1072 }
1073
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 fb_queue_erase(argv[1]);
1075 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001076 } else if(!strcmp(*argv, "format")) {
1077 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001078 if (erase_first && needs_erase(argv[1])) {
1079 fb_queue_erase(argv[1]);
1080 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001081 fb_perform_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001082 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083 } else if(!strcmp(*argv, "signature")) {
1084 require(2);
1085 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001086 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 if (sz != 256) die("signature must be 256 bytes");
1088 fb_queue_download("signature", data, sz);
1089 fb_queue_command("signature", "installing signature");
1090 skip(2);
1091 } else if(!strcmp(*argv, "reboot")) {
1092 wants_reboot = 1;
1093 skip(1);
1094 } else if(!strcmp(*argv, "reboot-bootloader")) {
1095 wants_reboot_bootloader = 1;
1096 skip(1);
1097 } else if (!strcmp(*argv, "continue")) {
1098 fb_queue_command("continue", "resuming boot");
1099 skip(1);
1100 } else if(!strcmp(*argv, "boot")) {
1101 char *kname = 0;
1102 char *rname = 0;
1103 skip(1);
1104 if (argc > 0) {
1105 kname = argv[0];
1106 skip(1);
1107 }
1108 if (argc > 0) {
1109 rname = argv[0];
1110 skip(1);
1111 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001112 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113 if (data == 0) return 1;
1114 fb_queue_download("boot.img", data, sz);
1115 fb_queue_command("boot", "booting");
1116 } else if(!strcmp(*argv, "flash")) {
1117 char *pname = argv[1];
1118 char *fname = 0;
1119 require(2);
1120 if (argc > 2) {
1121 fname = argv[2];
1122 skip(3);
1123 } else {
1124 fname = find_item(pname, product);
1125 skip(2);
1126 }
1127 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001128 if (erase_first && needs_erase(pname)) {
1129 fb_queue_erase(pname);
1130 }
Colin Crossf8387882012-05-24 17:18:41 -07001131 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001132 } else if(!strcmp(*argv, "flash:raw")) {
1133 char *pname = argv[1];
1134 char *kname = argv[2];
1135 char *rname = 0;
1136 require(3);
1137 if(argc > 3) {
1138 rname = argv[3];
1139 skip(4);
1140 } else {
1141 skip(3);
1142 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001143 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144 if (data == 0) die("cannot load bootable image");
1145 fb_queue_flash(pname, data, sz);
1146 } else if(!strcmp(*argv, "flashall")) {
1147 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001148 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 wants_reboot = 1;
1150 } else if(!strcmp(*argv, "update")) {
1151 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001152 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001153 skip(2);
1154 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001155 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001156 skip(1);
1157 }
1158 wants_reboot = 1;
1159 } else if(!strcmp(*argv, "oem")) {
1160 argc = do_oem_command(argc, argv);
1161 } else {
1162 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001163 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164 }
1165 }
1166
1167 if (wants_wipe) {
1168 fb_queue_erase("userdata");
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001169 fb_perform_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001170 fb_queue_erase("cache");
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001171 fb_perform_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001172 }
1173 if (wants_reboot) {
1174 fb_queue_reboot();
1175 } else if (wants_reboot_bootloader) {
1176 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001177 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178 }
1179
Scott Anderson13081c62012-04-06 12:39:30 -07001180 if (fb_queue_is_empty())
1181 return 0;
1182
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001183 status = fb_execute_queue(usb);
1184 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185}