blob: 7f49ae91dd91848ea2bfb1dfb1fffe4d1c32c90f [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;
103} images[3] = {
104 {"boot.img", "boot.sig", "boot", false},
105 {"recovery.img", "recovery.sig", "recovery", true},
106 {"system.img", "system.sig", "system", false},
107};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700108
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109void get_my_path(char *path);
110
111char *find_item(const char *item, const char *product)
112{
113 char *dir;
114 char *fn;
115 char path[PATH_MAX + 128];
116
117 if(!strcmp(item,"boot")) {
118 fn = "boot.img";
119 } else if(!strcmp(item,"recovery")) {
120 fn = "recovery.img";
121 } else if(!strcmp(item,"system")) {
122 fn = "system.img";
123 } else if(!strcmp(item,"userdata")) {
124 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700125 } else if(!strcmp(item,"cache")) {
126 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 } else if(!strcmp(item,"info")) {
128 fn = "android-info.txt";
129 } else {
130 fprintf(stderr,"unknown partition '%s'\n", item);
131 return 0;
132 }
133
134 if(product) {
135 get_my_path(path);
136 sprintf(path + strlen(path),
137 "../../../target/product/%s/%s", product, fn);
138 return strdup(path);
139 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800140
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 dir = getenv("ANDROID_PRODUCT_OUT");
142 if((dir == 0) || (dir[0] == 0)) {
143 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
144 return 0;
145 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800146
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 sprintf(path, "%s/%s", dir, fn);
148 return strdup(path);
149}
150
Rom Lemarchand622810c2013-06-28 09:54:59 -0700151static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700152{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700153 struct stat st;
154 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700155
Rom Lemarchand622810c2013-06-28 09:54:59 -0700156 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700157
Rom Lemarchand622810c2013-06-28 09:54:59 -0700158 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700159}
160
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162{
163 char *data;
164 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800165 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
167 data = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Rom Lemarchand622810c2013-06-28 09:54:59 -0700169 sz = file_size(fd);
170 if (sz < 0) {
171 goto oops;
172 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
174 data = (char*) malloc(sz);
175 if(data == 0) goto oops;
176
177 if(read(fd, data, sz) != sz) goto oops;
178 close(fd);
179
180 if(_sz) *_sz = sz;
181 return data;
182
183oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800184 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 close(fd);
186 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800187 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 return 0;
189}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190
Rom Lemarchand622810c2013-06-28 09:54:59 -0700191static void *load_file(const char *fn, unsigned *_sz)
192{
193 int fd;
194
195 fd = open(fn, O_RDONLY | O_BINARY);
196 if(fd < 0) return 0;
197
198 return load_fd(fd, _sz);
199}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
JP Abgralla032ded2012-06-06 11:53:33 -0700201int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
202{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400204 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800205 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700206 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800207 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700208 (info->dev_vendor != 0x0fce) && // Sony Ericsson
209 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400210 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800211 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800212 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800213 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800214 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400215 (info->dev_vendor != 0x0bb4)) // HTC
216 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 if(info->ifc_class != 0xff) return -1;
218 if(info->ifc_subclass != 0x42) return -1;
219 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700220 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700222 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
223 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 return 0;
225}
226
JP Abgrall7b8970c2013-03-07 17:06:41 -0800227int match_fastboot(usb_ifc_info *info)
228{
229 return match_fastboot_with_serial(info, serial);
230}
231
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232int list_devices_callback(usb_ifc_info *info)
233{
JP Abgralla032ded2012-06-06 11:53:33 -0700234 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700236 if (!info->writable) {
237 serial = "no permissions"; // like "adb devices"
238 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 if (!serial[0]) {
240 serial = "????????????";
241 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700242 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700243 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700244 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700245 } else if (!info->device_path) {
246 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700247 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700248 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700249 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 }
251
252 return -1;
253}
254
255usb_handle *open_device(void)
256{
257 static usb_handle *usb = 0;
258 int announce = 1;
259
260 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800261
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 for(;;) {
263 usb = usb_open(match_fastboot);
264 if(usb) return usb;
265 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800266 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 fprintf(stderr,"< waiting for device >\n");
268 }
269 sleep(1);
270 }
271}
272
273void list_devices(void) {
274 // We don't actually open a USB device here,
275 // just getting our callback called so we can
276 // list all the connected devices.
277 usb_open(list_devices_callback);
278}
279
280void usage(void)
281{
282 fprintf(stderr,
283/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
284 "usage: fastboot [ <option> ] <command>\n"
285 "\n"
286 "commands:\n"
287 " update <filename> reflash device from update.zip\n"
288 " flashall flash boot + recovery + system\n"
289 " flash <partition> [ <filename> ] write a file to a flash partition\n"
290 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800291 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 " getvar <variable> display a bootloader variable\n"
293 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
294 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
295 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700296 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 " reboot reboot device normally\n"
298 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800299 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 "\n"
301 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700302 " -w erase userdata and cache (and format\n"
303 " if supported by partition type)\n"
304 " -u do not first erase partition before\n"
305 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700306 " -s <specific device> specify device serial number\n"
307 " or path to device port\n"
308 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 " -p <product> specify product name\n"
310 " -c <cmdline> override kernel commandline\n"
311 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800312 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800313 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700314 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700315 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317}
318
JP Abgrall7b8970c2013-03-07 17:06:41 -0800319void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 unsigned *sz, const char *cmdline)
321{
322 void *kdata = 0, *rdata = 0;
323 unsigned ksize = 0, rsize = 0;
324 void *bdata;
325 unsigned bsize;
326
327 if(kernel == 0) {
328 fprintf(stderr, "no image specified\n");
329 return 0;
330 }
331
332 kdata = load_file(kernel, &ksize);
333 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800334 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 return 0;
336 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 /* is this actually a boot image? */
339 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
340 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800341
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 if(ramdisk) {
343 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
344 return 0;
345 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 *sz = ksize;
348 return kdata;
349 }
350
351 if(ramdisk) {
352 rdata = load_file(ramdisk, &rsize);
353 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800354 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 return 0;
356 }
357 }
358
359 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800360 bdata = mkbootimg(kdata, ksize, kernel_offset,
361 rdata, rsize, ramdisk_offset,
362 0, 0, second_offset,
363 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 if(bdata == 0) {
365 fprintf(stderr,"failed to create boot.img\n");
366 return 0;
367 }
368 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
369 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
370 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800371
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 return bdata;
373}
374
375void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
376{
377 void *data;
378 zipentry_t entry;
379 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800380
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 entry = lookup_zipentry(zip, name);
382 if (entry == NULL) {
383 fprintf(stderr, "archive does not contain '%s'\n", name);
384 return 0;
385 }
386
387 *sz = get_zipentry_size(entry);
388
389 datasz = *sz * 1.001;
390 data = malloc(datasz);
391
392 if(data == 0) {
393 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
394 return 0;
395 }
396
397 if (decompress_zipentry(entry, data, datasz)) {
398 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
399 free(data);
400 return 0;
401 }
402
403 return data;
404}
405
Rom Lemarchand622810c2013-06-28 09:54:59 -0700406static int unzip_to_file(zipfile_t zip, char *name)
407{
408 int fd;
409 char *data;
410 unsigned sz;
411
412 fd = fileno(tmpfile());
413 if (fd < 0) {
414 return -1;
415 }
416
417 data = unzip_file(zip, name, &sz);
418 if (data == 0) {
419 return -1;
420 }
421
422 if (write(fd, data, sz) != sz) {
423 fd = -1;
424 }
425
426 free(data);
427 lseek(fd, 0, SEEK_SET);
428 return fd;
429}
430
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431static char *strip(char *s)
432{
433 int n;
434 while(*s && isspace(*s)) s++;
435 n = strlen(s);
436 while(n-- > 0) {
437 if(!isspace(s[n])) break;
438 s[n] = 0;
439 }
440 return s;
441}
442
443#define MAX_OPTIONS 32
444static int setup_requirement_line(char *name)
445{
446 char *val[MAX_OPTIONS];
447 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700448 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 unsigned n, count;
450 char *x;
451 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800452
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 if (!strncmp(name, "reject ", 7)) {
454 name += 7;
455 invert = 1;
456 } else if (!strncmp(name, "require ", 8)) {
457 name += 8;
458 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700459 } else if (!strncmp(name, "require-for-product:", 20)) {
460 // Get the product and point name past it
461 prod = name + 20;
462 name = strchr(name, ' ');
463 if (!name) return -1;
464 *name = 0;
465 name += 1;
466 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 }
468
469 x = strchr(name, '=');
470 if (x == 0) return 0;
471 *x = 0;
472 val[0] = x + 1;
473
474 for(count = 1; count < MAX_OPTIONS; count++) {
475 x = strchr(val[count - 1],'|');
476 if (x == 0) break;
477 *x = 0;
478 val[count] = x + 1;
479 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800480
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 name = strip(name);
482 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800483
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 name = strip(name);
485 if (name == 0) return -1;
486
487 /* work around an unfortunate name mismatch */
488 if (!strcmp(name,"board")) name = "product";
489
490 out = malloc(sizeof(char*) * count);
491 if (out == 0) return -1;
492
493 for(n = 0; n < count; n++) {
494 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700495 if (out[n] == 0) {
496 for(size_t i = 0; i < n; ++i) {
497 free((char*) out[i]);
498 }
499 free(out);
500 return -1;
501 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
503
Wink Savilleb98762f2011-04-04 17:54:59 -0700504 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 return 0;
506}
507
508static void setup_requirements(char *data, unsigned sz)
509{
510 char *s;
511
512 s = data;
513 while (sz-- > 0) {
514 if(*s == '\n') {
515 *s++ = 0;
516 if (setup_requirement_line(data)) {
517 die("out of memory");
518 }
519 data = s;
520 } else {
521 s++;
522 }
523 }
524}
525
526void queue_info_dump(void)
527{
528 fb_queue_notice("--------------------------------------------");
529 fb_queue_display("version-bootloader", "Bootloader Version...");
530 fb_queue_display("version-baseband", "Baseband Version.....");
531 fb_queue_display("serialno", "Serial Number........");
532 fb_queue_notice("--------------------------------------------");
533}
534
Rom Lemarchand622810c2013-06-28 09:54:59 -0700535static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700536{
Colin Crossf8387882012-05-24 17:18:41 -0700537 struct sparse_file *s;
538 int files;
539 struct sparse_file **out_s;
540
Colin Crossf8387882012-05-24 17:18:41 -0700541 s = sparse_file_import_auto(fd, false);
542 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700543 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700544 }
545
546 files = sparse_file_resparse(s, max_size, NULL, 0);
547 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700548 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700549 }
550
551 out_s = calloc(sizeof(struct sparse_file *), files + 1);
552 if (!out_s) {
553 die("Failed to allocate sparse file array\n");
554 }
555
556 files = sparse_file_resparse(s, max_size, out_s, files);
557 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700558 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700559 }
560
561 return out_s;
562}
563
564static int64_t get_target_sparse_limit(struct usb_handle *usb)
565{
566 int64_t limit = 0;
567 char response[FB_RESPONSE_SZ + 1];
568 int status = fb_getvar(usb, response, "max-download-size");
569
570 if (!status) {
571 limit = strtoul(response, NULL, 0);
572 if (limit > 0) {
573 fprintf(stderr, "target reported max download size of %lld bytes\n",
574 limit);
575 }
576 }
577
578 return limit;
579}
580
581static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
582{
583 int64_t limit;
584
585 if (sparse_limit == 0) {
586 return 0;
587 } else if (sparse_limit > 0) {
588 limit = sparse_limit;
589 } else {
590 if (target_sparse_limit == -1) {
591 target_sparse_limit = get_target_sparse_limit(usb);
592 }
593 if (target_sparse_limit > 0) {
594 limit = target_sparse_limit;
595 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700596 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700597 }
598 }
599
600 if (size > limit) {
601 return limit;
602 }
603
604 return 0;
605}
606
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700607/* Until we get lazy inode table init working in make_ext4fs, we need to
608 * erase partitions of type ext4 before flashing a filesystem so no stale
609 * inodes are left lying around. Otherwise, e2fsck gets very upset.
610 */
611static int needs_erase(const char *part)
612{
613 /* The function fb_format_supported() currently returns the value
614 * we want, so just call it.
615 */
616 return fb_format_supported(usb, part);
617}
618
Rom Lemarchand622810c2013-06-28 09:54:59 -0700619static int load_buf_fd(usb_handle *usb, int fd,
620 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700621{
622 int64_t sz64;
623 void *data;
624 int64_t limit;
625
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700626
Rom Lemarchand622810c2013-06-28 09:54:59 -0700627 sz64 = file_size(fd);
628 if (sz64 < 0) {
629 return -1;
630 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700631
632 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700633 limit = get_sparse_limit(usb, sz64);
634 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700635 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700636 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700638 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639 buf->type = FB_BUFFER_SPARSE;
640 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700641 } else {
642 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 data = load_fd(fd, &sz);
644 if (data == 0) return -1;
645 buf->type = FB_BUFFER;
646 buf->data = data;
647 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700648 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700649
650 return 0;
651}
652
653static int load_buf(usb_handle *usb, const char *fname,
654 struct fastboot_buffer *buf)
655{
656 int fd;
657
658 fd = open(fname, O_RDONLY | O_BINARY);
659 if (fd < 0) {
660 die("cannot open '%s'\n", fname);
661 }
662
663 return load_buf_fd(usb, fd, buf);
664}
665
666static void flash_buf(const char *pname, struct fastboot_buffer *buf)
667{
668 struct sparse_file **s;
669
670 switch (buf->type) {
671 case FB_BUFFER_SPARSE:
672 s = buf->data;
673 while (*s) {
674 int64_t sz64 = sparse_file_len(*s, true, false);
675 fb_queue_flash_sparse(pname, *s++, sz64);
676 }
677 break;
678 case FB_BUFFER:
679 fb_queue_flash(pname, buf->data, buf->sz);
680 break;
681 default:
682 die("unknown buffer type: %d", buf->type);
683 }
684}
685
686void do_flash(usb_handle *usb, const char *pname, const char *fname)
687{
688 struct fastboot_buffer buf;
689
690 if (load_buf(usb, fname, &buf)) {
691 die("cannot load '%s'", fname);
692 }
693 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700694}
695
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696void do_update_signature(zipfile_t zip, char *fn)
697{
698 void *data;
699 unsigned sz;
700 data = unzip_file(zip, fn, &sz);
701 if (data == 0) return;
702 fb_queue_download("signature", data, sz);
703 fb_queue_command("signature", "installing signature");
704}
705
Rom Lemarchand622810c2013-06-28 09:54:59 -0700706void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707{
708 void *zdata;
709 unsigned zsize;
710 void *data;
711 unsigned sz;
712 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700713 int fd;
714 int rc;
715 struct fastboot_buffer buf;
716 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717
718 queue_info_dump();
719
Wink Savilleb98762f2011-04-04 17:54:59 -0700720 fb_queue_query_save("product", cur_product, sizeof(cur_product));
721
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800723 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724
725 zip = init_zipfile(zdata, zsize);
726 if(zip == 0) die("failed to access zipdata in '%s'");
727
728 data = unzip_file(zip, "android-info.txt", &sz);
729 if (data == 0) {
730 char *tmp;
731 /* fallback for older zipfiles */
732 data = unzip_file(zip, "android-product.txt", &sz);
733 if ((data == 0) || (sz < 1)) {
734 die("update package has no android-info.txt or android-product.txt");
735 }
736 tmp = malloc(sz + 128);
737 if (tmp == 0) die("out of memory");
738 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
739 data = tmp;
740 sz = strlen(tmp);
741 }
742
743 setup_requirements(data, sz);
744
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 for (i = 0; i < ARRAY_SIZE(images); i++) {
746 fd = unzip_to_file(zip, images[i].img_name);
747 if (fd < 0) {
748 if (images[i].is_optional)
749 continue;
750 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700751 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700752 rc = load_buf_fd(usb, fd, &buf);
753 if (rc) die("cannot load %s from flash", images[i].img_name);
754 do_update_signature(zip, images[i].sig_name);
755 if (erase_first && needs_erase(images[i].part_name)) {
756 fb_queue_erase(images[i].part_name);
757 }
758 flash_buf(images[i].part_name, &buf);
759 /* not closing the fd here since the sparse code keeps the fd around
760 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
761 * program exits.
762 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800764}
765
766void do_send_signature(char *fn)
767{
768 void *data;
769 unsigned sz;
770 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800771
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 xtn = strrchr(fn, '.');
773 if (!xtn) return;
774 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800775
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776 strcpy(xtn,".sig");
777 data = load_file(fn, &sz);
778 strcpy(xtn,".img");
779 if (data == 0) return;
780 fb_queue_download("signature", data, sz);
781 fb_queue_command("signature", "installing signature");
782}
783
Rom Lemarchand622810c2013-06-28 09:54:59 -0700784void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785{
786 char *fname;
787 void *data;
788 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700789 struct fastboot_buffer buf;
790 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791
792 queue_info_dump();
793
Wink Savilleb98762f2011-04-04 17:54:59 -0700794 fb_queue_query_save("product", cur_product, sizeof(cur_product));
795
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800796 fname = find_item("info", product);
797 if (fname == 0) die("cannot find android-info.txt");
798 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800799 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800 setup_requirements(data, sz);
801
Rom Lemarchand622810c2013-06-28 09:54:59 -0700802 for (i = 0; i < ARRAY_SIZE(images); i++) {
803 fname = find_item(images[i].part_name, product);
804 if (load_buf(usb, fname, &buf)) {
805 if (images[i].is_optional)
806 continue;
807 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700808 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 do_send_signature(fname);
810 if (erase_first && needs_erase(images[i].part_name)) {
811 fb_queue_erase(images[i].part_name);
812 }
813 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815}
816
817#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800818#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800819
820int do_oem_command(int argc, char **argv)
821{
822 int i;
823 char command[256];
824 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800825
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826 command[0] = 0;
827 while(1) {
828 strcat(command,*argv);
829 skip(1);
830 if(argc == 0) break;
831 strcat(command," ");
832 }
833
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800834 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835 return 0;
836}
837
Colin Crossf8387882012-05-24 17:18:41 -0700838static int64_t parse_num(const char *arg)
839{
840 char *endptr;
841 unsigned long long num;
842
843 num = strtoull(arg, &endptr, 0);
844 if (endptr == arg) {
845 return -1;
846 }
847
848 if (*endptr == 'k' || *endptr == 'K') {
849 if (num >= (-1ULL) / 1024) {
850 return -1;
851 }
852 num *= 1024LL;
853 endptr++;
854 } else if (*endptr == 'm' || *endptr == 'M') {
855 if (num >= (-1ULL) / (1024 * 1024)) {
856 return -1;
857 }
858 num *= 1024LL * 1024LL;
859 endptr++;
860 } else if (*endptr == 'g' || *endptr == 'G') {
861 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
862 return -1;
863 }
864 num *= 1024LL * 1024LL * 1024LL;
865 endptr++;
866 }
867
868 if (*endptr != '\0') {
869 return -1;
870 }
871
872 if (num > INT64_MAX) {
873 return -1;
874 }
875
876 return num;
877}
878
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700879void fb_perform_format(const char *partition, int skip_if_not_supported)
880{
881 char pType[FB_RESPONSE_SZ + 1], pSize[FB_RESPONSE_SZ + 1];
882 unsigned int limit = INT_MAX;
883 struct fastboot_buffer buf;
884 const char *errMsg = NULL;
885 const struct fs_generator *gen;
886 uint64_t pSz;
887 int status;
888 int fd;
889
890 if (target_sparse_limit > 0 && target_sparse_limit < limit)
891 limit = target_sparse_limit;
892 if (sparse_limit > 0 && sparse_limit < limit)
893 limit = sparse_limit;
894
895 status = fb_getvar(usb, pType, "partition-type:%s", partition);
896 if (status) {
897 errMsg = "Can't determine partition type.\n";
898 goto failed;
899 }
900
901 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
902 if (status) {
903 errMsg = "Unable to get partition size\n";
904 goto failed;
905 }
906
907 gen = fs_get_generator(pType);
908 if (!gen) {
909 if (skip_if_not_supported) {
910 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
911 fprintf(stderr, "File system type %s not supported.\n", pType);
912 return;
913 }
914 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
915 return;
916 }
917
918 pSz = strtoll(pSize, (char **)NULL, 16);
919
920 fd = fileno(tmpfile());
921 if (fs_generator_generate(gen, fd, pSz)) {
922 close(fd);
923 fprintf(stderr, "Cannot generate image.\n");
924 return;
925 }
926
927 if (load_buf_fd(usb, fd, &buf)) {
928 fprintf(stderr, "Cannot read image.\n");
929 close(fd);
930 return;
931 }
932 flash_buf(partition, &buf);
933
934 return;
935
936
937failed:
938 if (skip_if_not_supported) {
939 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
940 if (errMsg)
941 fprintf(stderr, "%s", errMsg);
942 }
943 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
944}
945
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946int main(int argc, char **argv)
947{
948 int wants_wipe = 0;
949 int wants_reboot = 0;
950 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700951 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800952 void *data;
953 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700954 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700955 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700956 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957
JP Abgrall7b8970c2013-03-07 17:06:41 -0800958 const struct option longopts[] = {
959 {"base", required_argument, 0, 'b'},
960 {"kernel_offset", required_argument, 0, 'k'},
961 {"page_size", required_argument, 0, 'n'},
962 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800963 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800964 {"help", 0, 0, 'h'},
965 {0, 0, 0, 0}
966 };
Colin Cross8879f982012-05-22 17:53:34 -0700967
968 serial = getenv("ANDROID_SERIAL");
969
970 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800971 int option_index = 0;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800972 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 -0700973 if (c < 0) {
974 break;
975 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800976 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700977 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700978 case 'b':
979 base_addr = strtoul(optarg, 0, 16);
980 break;
Colin Cross8879f982012-05-22 17:53:34 -0700981 case 'c':
982 cmdline = optarg;
983 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800984 case 'h':
985 usage();
986 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700987 case 'i': {
988 char *endptr = NULL;
989 unsigned long val;
990
991 val = strtoul(optarg, &endptr, 0);
992 if (!endptr || *endptr != '\0' || (val & ~0xffff))
993 die("invalid vendor id '%s'", optarg);
994 vendor_id = (unsigned short)val;
995 break;
996 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800997 case 'k':
998 kernel_offset = strtoul(optarg, 0, 16);
999 break;
1000 case 'l':
1001 long_listing = 1;
1002 break;
1003 case 'n':
1004 page_size = (unsigned)strtoul(optarg, NULL, 0);
1005 if (!page_size) die("invalid page size");
1006 break;
1007 case 'p':
1008 product = optarg;
1009 break;
1010 case 'r':
1011 ramdisk_offset = strtoul(optarg, 0, 16);
1012 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001013 case 't':
1014 tags_offset = strtoul(optarg, 0, 16);
1015 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001016 case 's':
1017 serial = optarg;
1018 break;
1019 case 'S':
1020 sparse_limit = parse_num(optarg);
1021 if (sparse_limit < 0) {
1022 die("invalid sparse limit");
1023 }
1024 break;
1025 case 'u':
1026 erase_first = 0;
1027 break;
1028 case 'w':
1029 wants_wipe = 1;
1030 break;
Colin Cross8879f982012-05-22 17:53:34 -07001031 case '?':
1032 return 1;
1033 default:
1034 abort();
1035 }
1036 }
1037
1038 argc -= optind;
1039 argv += optind;
1040
1041 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001042 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001043 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001044 }
1045
Colin Cross8fb6e062012-07-24 16:36:41 -07001046 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001047 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001048 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001049 return 0;
1050 }
1051
Colin Crossc7b75dc2012-08-29 18:17:06 -07001052 if (argc > 0 && !strcmp(*argv, "help")) {
1053 usage();
1054 return 0;
1055 }
1056
Colin Cross8879f982012-05-22 17:53:34 -07001057 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001058
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001059 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001060 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001061 require(2);
1062 fb_queue_display(argv[1], argv[1]);
1063 skip(2);
1064 } else if(!strcmp(*argv, "erase")) {
1065 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001066
1067 if (fb_format_supported(usb, argv[1])) {
1068 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1069 }
1070
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071 fb_queue_erase(argv[1]);
1072 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001073 } else if(!strcmp(*argv, "format")) {
1074 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001075 if (erase_first && needs_erase(argv[1])) {
1076 fb_queue_erase(argv[1]);
1077 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001078 fb_perform_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001079 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 } else if(!strcmp(*argv, "signature")) {
1081 require(2);
1082 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001083 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084 if (sz != 256) die("signature must be 256 bytes");
1085 fb_queue_download("signature", data, sz);
1086 fb_queue_command("signature", "installing signature");
1087 skip(2);
1088 } else if(!strcmp(*argv, "reboot")) {
1089 wants_reboot = 1;
1090 skip(1);
1091 } else if(!strcmp(*argv, "reboot-bootloader")) {
1092 wants_reboot_bootloader = 1;
1093 skip(1);
1094 } else if (!strcmp(*argv, "continue")) {
1095 fb_queue_command("continue", "resuming boot");
1096 skip(1);
1097 } else if(!strcmp(*argv, "boot")) {
1098 char *kname = 0;
1099 char *rname = 0;
1100 skip(1);
1101 if (argc > 0) {
1102 kname = argv[0];
1103 skip(1);
1104 }
1105 if (argc > 0) {
1106 rname = argv[0];
1107 skip(1);
1108 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001109 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110 if (data == 0) return 1;
1111 fb_queue_download("boot.img", data, sz);
1112 fb_queue_command("boot", "booting");
1113 } else if(!strcmp(*argv, "flash")) {
1114 char *pname = argv[1];
1115 char *fname = 0;
1116 require(2);
1117 if (argc > 2) {
1118 fname = argv[2];
1119 skip(3);
1120 } else {
1121 fname = find_item(pname, product);
1122 skip(2);
1123 }
1124 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001125 if (erase_first && needs_erase(pname)) {
1126 fb_queue_erase(pname);
1127 }
Colin Crossf8387882012-05-24 17:18:41 -07001128 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129 } else if(!strcmp(*argv, "flash:raw")) {
1130 char *pname = argv[1];
1131 char *kname = argv[2];
1132 char *rname = 0;
1133 require(3);
1134 if(argc > 3) {
1135 rname = argv[3];
1136 skip(4);
1137 } else {
1138 skip(3);
1139 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001140 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141 if (data == 0) die("cannot load bootable image");
1142 fb_queue_flash(pname, data, sz);
1143 } else if(!strcmp(*argv, "flashall")) {
1144 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001145 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146 wants_reboot = 1;
1147 } else if(!strcmp(*argv, "update")) {
1148 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001149 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001150 skip(2);
1151 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001152 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001153 skip(1);
1154 }
1155 wants_reboot = 1;
1156 } else if(!strcmp(*argv, "oem")) {
1157 argc = do_oem_command(argc, argv);
1158 } else {
1159 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001160 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161 }
1162 }
1163
1164 if (wants_wipe) {
1165 fb_queue_erase("userdata");
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001166 fb_perform_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001167 fb_queue_erase("cache");
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001168 fb_perform_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169 }
1170 if (wants_reboot) {
1171 fb_queue_reboot();
1172 } else if (wants_reboot_bootloader) {
1173 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001174 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001175 }
1176
Scott Anderson13081c62012-04-06 12:39:30 -07001177 if (fb_queue_is_empty())
1178 return 0;
1179
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001180 status = fb_execute_queue(usb);
1181 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182}