blob: c44f9379ea78b07457ccc16cf7066629271d89a4 [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
29#include <stdio.h>
30#include <stdlib.h>
31#include <stdarg.h>
32#include <string.h>
33#include <errno.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <limits.h>
37#include <ctype.h>
38
39#include <sys/time.h>
40#include <bootimg.h>
41#include <zipfile/zipfile.h>
42
43#include "fastboot.h"
44
Wink Savilleb98762f2011-04-04 17:54:59 -070045char cur_product[FB_RESPONSE_SZ + 1];
46
Brian Swetland2a63bb72009-04-28 16:05:07 -070047void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
48
49boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
50 void *ramdisk, unsigned ramdisk_size,
51 void *second, unsigned second_size,
52 unsigned page_size, unsigned base,
53 unsigned *bootimg_size);
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055static usb_handle *usb = 0;
56static const char *serial = 0;
57static const char *product = 0;
58static const char *cmdline = 0;
59static int wipe_data = 0;
60static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070061static int long_listing = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Brian Swetland2a63bb72009-04-28 16:05:07 -070063static unsigned base_addr = 0x10000000;
64
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065void die(const char *fmt, ...)
66{
67 va_list ap;
68 va_start(ap, fmt);
69 fprintf(stderr,"error: ");
70 vfprintf(stderr, fmt, ap);
71 fprintf(stderr,"\n");
72 va_end(ap);
73 exit(1);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080074}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
76void get_my_path(char *path);
77
78char *find_item(const char *item, const char *product)
79{
80 char *dir;
81 char *fn;
82 char path[PATH_MAX + 128];
83
84 if(!strcmp(item,"boot")) {
85 fn = "boot.img";
86 } else if(!strcmp(item,"recovery")) {
87 fn = "recovery.img";
88 } else if(!strcmp(item,"system")) {
89 fn = "system.img";
90 } else if(!strcmp(item,"userdata")) {
91 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -070092 } else if(!strcmp(item,"cache")) {
93 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 } else if(!strcmp(item,"info")) {
95 fn = "android-info.txt";
96 } else {
97 fprintf(stderr,"unknown partition '%s'\n", item);
98 return 0;
99 }
100
101 if(product) {
102 get_my_path(path);
103 sprintf(path + strlen(path),
104 "../../../target/product/%s/%s", product, fn);
105 return strdup(path);
106 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 dir = getenv("ANDROID_PRODUCT_OUT");
109 if((dir == 0) || (dir[0] == 0)) {
110 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
111 return 0;
112 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800113
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 sprintf(path, "%s/%s", dir, fn);
115 return strdup(path);
116}
117
118#ifdef _WIN32
119void *load_file(const char *fn, unsigned *_sz);
120#else
121void *load_file(const char *fn, unsigned *_sz)
122{
123 char *data;
124 int sz;
125 int fd;
126
127 data = 0;
128 fd = open(fn, O_RDONLY);
129 if(fd < 0) return 0;
130
131 sz = lseek(fd, 0, SEEK_END);
132 if(sz < 0) goto oops;
133
134 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
135
136 data = (char*) malloc(sz);
137 if(data == 0) goto oops;
138
139 if(read(fd, data, sz) != sz) goto oops;
140 close(fd);
141
142 if(_sz) *_sz = sz;
143 return data;
144
145oops:
146 close(fd);
147 if(data != 0) free(data);
148 return 0;
149}
150#endif
151
152int match_fastboot(usb_ifc_info *info)
153{
154 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400155 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800156 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700157 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800158 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700159 (info->dev_vendor != 0x0fce) && // Sony Ericsson
160 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400161 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800162 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800163 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800164 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800165 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400166 (info->dev_vendor != 0x0bb4)) // HTC
167 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 if(info->ifc_class != 0xff) return -1;
169 if(info->ifc_subclass != 0x42) return -1;
170 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700171 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 // at the command line with the -s option.
Scott Anderson13081c62012-04-06 12:39:30 -0700173 if (serial && (strcmp(serial, info->serial_number) != 0 &&
174 strcmp(serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 return 0;
176}
177
178int list_devices_callback(usb_ifc_info *info)
179{
180 if (match_fastboot(info) == 0) {
181 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700182 if (!info->writable) {
183 serial = "no permissions"; // like "adb devices"
184 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 if (!serial[0]) {
186 serial = "????????????";
187 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700188 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700189 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700190 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700191 } else if (!info->device_path) {
192 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700193 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700194 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 }
197
198 return -1;
199}
200
201usb_handle *open_device(void)
202{
203 static usb_handle *usb = 0;
204 int announce = 1;
205
206 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800207
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 for(;;) {
209 usb = usb_open(match_fastboot);
210 if(usb) return usb;
211 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800212 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 fprintf(stderr,"< waiting for device >\n");
214 }
215 sleep(1);
216 }
217}
218
219void list_devices(void) {
220 // We don't actually open a USB device here,
221 // just getting our callback called so we can
222 // list all the connected devices.
223 usb_open(list_devices_callback);
224}
225
226void usage(void)
227{
228 fprintf(stderr,
229/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
230 "usage: fastboot [ <option> ] <command>\n"
231 "\n"
232 "commands:\n"
233 " update <filename> reflash device from update.zip\n"
234 " flashall flash boot + recovery + system\n"
235 " flash <partition> [ <filename> ] write a file to a flash partition\n"
236 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800237 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 " getvar <variable> display a bootloader variable\n"
239 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
240 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
241 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700242 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 " reboot reboot device normally\n"
244 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800245 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 "\n"
247 "options:\n"
248 " -w erase userdata and cache\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700249 " -s <specific device> specify device serial number\n"
250 " or path to device port\n"
251 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 " -p <product> specify product name\n"
253 " -c <cmdline> override kernel commandline\n"
254 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700255 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800256 " -n <page size> specify the nand page size. default: 2048\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258}
259
Dima Zavin931175a2010-02-12 20:26:33 -0800260void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 unsigned *sz, const char *cmdline)
262{
263 void *kdata = 0, *rdata = 0;
264 unsigned ksize = 0, rsize = 0;
265 void *bdata;
266 unsigned bsize;
267
268 if(kernel == 0) {
269 fprintf(stderr, "no image specified\n");
270 return 0;
271 }
272
273 kdata = load_file(kernel, &ksize);
274 if(kdata == 0) {
275 fprintf(stderr, "cannot load '%s'\n", kernel);
276 return 0;
277 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800278
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 /* is this actually a boot image? */
280 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
281 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800282
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 if(ramdisk) {
284 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
285 return 0;
286 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800287
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 *sz = ksize;
289 return kdata;
290 }
291
292 if(ramdisk) {
293 rdata = load_file(ramdisk, &rsize);
294 if(rdata == 0) {
295 fprintf(stderr,"cannot load '%s'\n", ramdisk);
296 return 0;
297 }
298 }
299
300 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800301 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 if(bdata == 0) {
303 fprintf(stderr,"failed to create boot.img\n");
304 return 0;
305 }
306 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
307 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
308 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800309
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 return bdata;
311}
312
313void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
314{
315 void *data;
316 zipentry_t entry;
317 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800318
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 entry = lookup_zipentry(zip, name);
320 if (entry == NULL) {
321 fprintf(stderr, "archive does not contain '%s'\n", name);
322 return 0;
323 }
324
325 *sz = get_zipentry_size(entry);
326
327 datasz = *sz * 1.001;
328 data = malloc(datasz);
329
330 if(data == 0) {
331 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
332 return 0;
333 }
334
335 if (decompress_zipentry(entry, data, datasz)) {
336 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
337 free(data);
338 return 0;
339 }
340
341 return data;
342}
343
344static char *strip(char *s)
345{
346 int n;
347 while(*s && isspace(*s)) s++;
348 n = strlen(s);
349 while(n-- > 0) {
350 if(!isspace(s[n])) break;
351 s[n] = 0;
352 }
353 return s;
354}
355
356#define MAX_OPTIONS 32
357static int setup_requirement_line(char *name)
358{
359 char *val[MAX_OPTIONS];
360 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700361 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 unsigned n, count;
363 char *x;
364 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 if (!strncmp(name, "reject ", 7)) {
367 name += 7;
368 invert = 1;
369 } else if (!strncmp(name, "require ", 8)) {
370 name += 8;
371 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700372 } else if (!strncmp(name, "require-for-product:", 20)) {
373 // Get the product and point name past it
374 prod = name + 20;
375 name = strchr(name, ' ');
376 if (!name) return -1;
377 *name = 0;
378 name += 1;
379 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 }
381
382 x = strchr(name, '=');
383 if (x == 0) return 0;
384 *x = 0;
385 val[0] = x + 1;
386
387 for(count = 1; count < MAX_OPTIONS; count++) {
388 x = strchr(val[count - 1],'|');
389 if (x == 0) break;
390 *x = 0;
391 val[count] = x + 1;
392 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800393
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 name = strip(name);
395 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800396
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 name = strip(name);
398 if (name == 0) return -1;
399
400 /* work around an unfortunate name mismatch */
401 if (!strcmp(name,"board")) name = "product";
402
403 out = malloc(sizeof(char*) * count);
404 if (out == 0) return -1;
405
406 for(n = 0; n < count; n++) {
407 out[n] = strdup(strip(val[n]));
408 if (out[n] == 0) return -1;
409 }
410
Wink Savilleb98762f2011-04-04 17:54:59 -0700411 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 return 0;
413}
414
415static void setup_requirements(char *data, unsigned sz)
416{
417 char *s;
418
419 s = data;
420 while (sz-- > 0) {
421 if(*s == '\n') {
422 *s++ = 0;
423 if (setup_requirement_line(data)) {
424 die("out of memory");
425 }
426 data = s;
427 } else {
428 s++;
429 }
430 }
431}
432
433void queue_info_dump(void)
434{
435 fb_queue_notice("--------------------------------------------");
436 fb_queue_display("version-bootloader", "Bootloader Version...");
437 fb_queue_display("version-baseband", "Baseband Version.....");
438 fb_queue_display("serialno", "Serial Number........");
439 fb_queue_notice("--------------------------------------------");
440}
441
442void do_update_signature(zipfile_t zip, char *fn)
443{
444 void *data;
445 unsigned sz;
446 data = unzip_file(zip, fn, &sz);
447 if (data == 0) return;
448 fb_queue_download("signature", data, sz);
449 fb_queue_command("signature", "installing signature");
450}
451
452void do_update(char *fn)
453{
454 void *zdata;
455 unsigned zsize;
456 void *data;
457 unsigned sz;
458 zipfile_t zip;
459
460 queue_info_dump();
461
Wink Savilleb98762f2011-04-04 17:54:59 -0700462 fb_queue_query_save("product", cur_product, sizeof(cur_product));
463
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 zdata = load_file(fn, &zsize);
465 if (zdata == 0) die("failed to load '%s'", fn);
466
467 zip = init_zipfile(zdata, zsize);
468 if(zip == 0) die("failed to access zipdata in '%s'");
469
470 data = unzip_file(zip, "android-info.txt", &sz);
471 if (data == 0) {
472 char *tmp;
473 /* fallback for older zipfiles */
474 data = unzip_file(zip, "android-product.txt", &sz);
475 if ((data == 0) || (sz < 1)) {
476 die("update package has no android-info.txt or android-product.txt");
477 }
478 tmp = malloc(sz + 128);
479 if (tmp == 0) die("out of memory");
480 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
481 data = tmp;
482 sz = strlen(tmp);
483 }
484
485 setup_requirements(data, sz);
486
487 data = unzip_file(zip, "boot.img", &sz);
488 if (data == 0) die("update package missing boot.img");
489 do_update_signature(zip, "boot.sig");
490 fb_queue_flash("boot", data, sz);
491
492 data = unzip_file(zip, "recovery.img", &sz);
493 if (data != 0) {
494 do_update_signature(zip, "recovery.sig");
495 fb_queue_flash("recovery", data, sz);
496 }
497
498 data = unzip_file(zip, "system.img", &sz);
499 if (data == 0) die("update package missing system.img");
500 do_update_signature(zip, "system.sig");
501 fb_queue_flash("system", data, sz);
502}
503
504void do_send_signature(char *fn)
505{
506 void *data;
507 unsigned sz;
508 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800509
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 xtn = strrchr(fn, '.');
511 if (!xtn) return;
512 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800513
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 strcpy(xtn,".sig");
515 data = load_file(fn, &sz);
516 strcpy(xtn,".img");
517 if (data == 0) return;
518 fb_queue_download("signature", data, sz);
519 fb_queue_command("signature", "installing signature");
520}
521
522void do_flashall(void)
523{
524 char *fname;
525 void *data;
526 unsigned sz;
527
528 queue_info_dump();
529
Wink Savilleb98762f2011-04-04 17:54:59 -0700530 fb_queue_query_save("product", cur_product, sizeof(cur_product));
531
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532 fname = find_item("info", product);
533 if (fname == 0) die("cannot find android-info.txt");
534 data = load_file(fname, &sz);
535 if (data == 0) die("could not load android-info.txt");
536 setup_requirements(data, sz);
537
538 fname = find_item("boot", product);
539 data = load_file(fname, &sz);
540 if (data == 0) die("could not load boot.img");
541 do_send_signature(fname);
542 fb_queue_flash("boot", data, sz);
543
544 fname = find_item("recovery", product);
545 data = load_file(fname, &sz);
546 if (data != 0) {
547 do_send_signature(fname);
548 fb_queue_flash("recovery", data, sz);
549 }
550
551 fname = find_item("system", product);
552 data = load_file(fname, &sz);
553 if (data == 0) die("could not load system.img");
554 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800555 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556}
557
558#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800559#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800560
561int do_oem_command(int argc, char **argv)
562{
563 int i;
564 char command[256];
565 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800566
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800567 command[0] = 0;
568 while(1) {
569 strcat(command,*argv);
570 skip(1);
571 if(argc == 0) break;
572 strcat(command," ");
573 }
574
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800575 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800576 return 0;
577}
578
579int main(int argc, char **argv)
580{
581 int wants_wipe = 0;
582 int wants_reboot = 0;
583 int wants_reboot_bootloader = 0;
Scott Anderson13081c62012-04-06 12:39:30 -0700584 int wants_device_list = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585 void *data;
586 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800587 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700588 int status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589
590 skip(1);
591 if (argc == 0) {
592 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700593 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594 }
595
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800596 if (!strcmp(*argv, "help")) {
597 usage();
598 return 0;
599 }
600
601
Elliott Hughes31dbed72009-10-07 15:38:53 -0700602 serial = getenv("ANDROID_SERIAL");
603
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 while (argc > 0) {
605 if(!strcmp(*argv, "-w")) {
606 wants_wipe = 1;
607 skip(1);
Brian Swetland2a63bb72009-04-28 16:05:07 -0700608 } else if(!strcmp(*argv, "-b")) {
609 require(2);
610 base_addr = strtoul(argv[1], 0, 16);
611 skip(2);
Dima Zavin931175a2010-02-12 20:26:33 -0800612 } else if(!strcmp(*argv, "-n")) {
613 require(2);
614 page_size = (unsigned)strtoul(argv[1], NULL, 0);
615 if (!page_size) die("invalid page size");
616 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800617 } else if(!strcmp(*argv, "-s")) {
618 require(2);
619 serial = argv[1];
620 skip(2);
Scott Anderson13081c62012-04-06 12:39:30 -0700621 } else if(!strcmp(*argv, "-l")) {
622 long_listing = 1;
623 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800624 } else if(!strcmp(*argv, "-p")) {
625 require(2);
626 product = argv[1];
627 skip(2);
628 } else if(!strcmp(*argv, "-c")) {
629 require(2);
630 cmdline = argv[1];
631 skip(2);
632 } else if(!strcmp(*argv, "-i")) {
633 char *endptr = NULL;
634 unsigned long val;
635
636 require(2);
637 val = strtoul(argv[1], &endptr, 0);
638 if (!endptr || *endptr != '\0' || (val & ~0xffff))
639 die("invalid vendor id '%s'", argv[1]);
640 vendor_id = (unsigned short)val;
641 skip(2);
Scott Anderson13081c62012-04-06 12:39:30 -0700642 } else if (!strcmp(*argv, "devices")) {
643 skip(1);
644 wants_device_list = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 } else if(!strcmp(*argv, "getvar")) {
646 require(2);
647 fb_queue_display(argv[1], argv[1]);
648 skip(2);
649 } else if(!strcmp(*argv, "erase")) {
650 require(2);
651 fb_queue_erase(argv[1]);
652 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800653 } else if(!strcmp(*argv, "format")) {
654 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700655 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800656 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 } else if(!strcmp(*argv, "signature")) {
658 require(2);
659 data = load_file(argv[1], &sz);
660 if (data == 0) die("could not load '%s'", argv[1]);
661 if (sz != 256) die("signature must be 256 bytes");
662 fb_queue_download("signature", data, sz);
663 fb_queue_command("signature", "installing signature");
664 skip(2);
665 } else if(!strcmp(*argv, "reboot")) {
666 wants_reboot = 1;
667 skip(1);
668 } else if(!strcmp(*argv, "reboot-bootloader")) {
669 wants_reboot_bootloader = 1;
670 skip(1);
671 } else if (!strcmp(*argv, "continue")) {
672 fb_queue_command("continue", "resuming boot");
673 skip(1);
674 } else if(!strcmp(*argv, "boot")) {
675 char *kname = 0;
676 char *rname = 0;
677 skip(1);
678 if (argc > 0) {
679 kname = argv[0];
680 skip(1);
681 }
682 if (argc > 0) {
683 rname = argv[0];
684 skip(1);
685 }
Dima Zavin931175a2010-02-12 20:26:33 -0800686 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 if (data == 0) return 1;
688 fb_queue_download("boot.img", data, sz);
689 fb_queue_command("boot", "booting");
690 } else if(!strcmp(*argv, "flash")) {
691 char *pname = argv[1];
692 char *fname = 0;
693 require(2);
694 if (argc > 2) {
695 fname = argv[2];
696 skip(3);
697 } else {
698 fname = find_item(pname, product);
699 skip(2);
700 }
701 if (fname == 0) die("cannot determine image filename for '%s'", pname);
702 data = load_file(fname, &sz);
703 if (data == 0) die("cannot load '%s'\n", fname);
704 fb_queue_flash(pname, data, sz);
705 } else if(!strcmp(*argv, "flash:raw")) {
706 char *pname = argv[1];
707 char *kname = argv[2];
708 char *rname = 0;
709 require(3);
710 if(argc > 3) {
711 rname = argv[3];
712 skip(4);
713 } else {
714 skip(3);
715 }
Dima Zavin931175a2010-02-12 20:26:33 -0800716 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 if (data == 0) die("cannot load bootable image");
718 fb_queue_flash(pname, data, sz);
719 } else if(!strcmp(*argv, "flashall")) {
720 skip(1);
721 do_flashall();
722 wants_reboot = 1;
723 } else if(!strcmp(*argv, "update")) {
724 if (argc > 1) {
725 do_update(argv[1]);
726 skip(2);
727 } else {
728 do_update("update.zip");
729 skip(1);
730 }
731 wants_reboot = 1;
732 } else if(!strcmp(*argv, "oem")) {
733 argc = do_oem_command(argc, argv);
734 } else {
735 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800736 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 }
738 }
739
Scott Anderson13081c62012-04-06 12:39:30 -0700740 if (wants_device_list)
741 list_devices();
742
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 if (wants_wipe) {
744 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -0700745 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -0700747 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 }
749 if (wants_reboot) {
750 fb_queue_reboot();
751 } else if (wants_reboot_bootloader) {
752 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
753 }
754
Scott Anderson13081c62012-04-06 12:39:30 -0700755 if (fb_queue_is_empty())
756 return 0;
757
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 usb = open_device();
759
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700760 status = fb_execute_queue(usb);
761 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800762}