blob: 6e42436a4451144e8d79d664e4b09144fb6eb435 [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";
92 } else if(!strcmp(item,"info")) {
93 fn = "android-info.txt";
94 } else {
95 fprintf(stderr,"unknown partition '%s'\n", item);
96 return 0;
97 }
98
99 if(product) {
100 get_my_path(path);
101 sprintf(path + strlen(path),
102 "../../../target/product/%s/%s", product, fn);
103 return strdup(path);
104 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800105
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 dir = getenv("ANDROID_PRODUCT_OUT");
107 if((dir == 0) || (dir[0] == 0)) {
108 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
109 return 0;
110 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 sprintf(path, "%s/%s", dir, fn);
113 return strdup(path);
114}
115
116#ifdef _WIN32
117void *load_file(const char *fn, unsigned *_sz);
118#else
119void *load_file(const char *fn, unsigned *_sz)
120{
121 char *data;
122 int sz;
123 int fd;
124
125 data = 0;
126 fd = open(fn, O_RDONLY);
127 if(fd < 0) return 0;
128
129 sz = lseek(fd, 0, SEEK_END);
130 if(sz < 0) goto oops;
131
132 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
133
134 data = (char*) malloc(sz);
135 if(data == 0) goto oops;
136
137 if(read(fd, data, sz) != sz) goto oops;
138 close(fd);
139
140 if(_sz) *_sz = sz;
141 return data;
142
143oops:
144 close(fd);
145 if(data != 0) free(data);
146 return 0;
147}
148#endif
149
150int match_fastboot(usb_ifc_info *info)
151{
152 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400153 (info->dev_vendor != 0x18d1) && // Google
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700154 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800155 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700156 (info->dev_vendor != 0x0fce) && // Sony Ericsson
157 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400158 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800159 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800160 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800161 (info->dev_vendor != 0x2314) && // INQ Mobile
Mike Lockwood09070d92009-08-05 17:04:36 -0400162 (info->dev_vendor != 0x0bb4)) // HTC
163 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 if(info->ifc_class != 0xff) return -1;
165 if(info->ifc_subclass != 0x42) return -1;
166 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700167 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 // at the command line with the -s option.
Scott Anderson13081c62012-04-06 12:39:30 -0700169 if (serial && (strcmp(serial, info->serial_number) != 0 &&
170 strcmp(serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 return 0;
172}
173
174int list_devices_callback(usb_ifc_info *info)
175{
176 if (match_fastboot(info) == 0) {
177 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700178 if (!info->writable) {
179 serial = "no permissions"; // like "adb devices"
180 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 if (!serial[0]) {
182 serial = "????????????";
183 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700184 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700185 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700186 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700187 } else if (!info->device_path) {
188 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700189 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700190 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700191 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 }
193
194 return -1;
195}
196
197usb_handle *open_device(void)
198{
199 static usb_handle *usb = 0;
200 int announce = 1;
201
202 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800203
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 for(;;) {
205 usb = usb_open(match_fastboot);
206 if(usb) return usb;
207 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800208 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 fprintf(stderr,"< waiting for device >\n");
210 }
211 sleep(1);
212 }
213}
214
215void list_devices(void) {
216 // We don't actually open a USB device here,
217 // just getting our callback called so we can
218 // list all the connected devices.
219 usb_open(list_devices_callback);
220}
221
222void usage(void)
223{
224 fprintf(stderr,
225/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
226 "usage: fastboot [ <option> ] <command>\n"
227 "\n"
228 "commands:\n"
229 " update <filename> reflash device from update.zip\n"
230 " flashall flash boot + recovery + system\n"
231 " flash <partition> [ <filename> ] write a file to a flash partition\n"
232 " erase <partition> erase a flash partition\n"
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800233 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 " getvar <variable> display a bootloader variable\n"
235 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
236 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
237 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700238 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 " reboot reboot device normally\n"
240 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800241 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 "\n"
243 "options:\n"
244 " -w erase userdata and cache\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700245 " -s <specific device> specify device serial number\n"
246 " or path to device port\n"
247 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 " -p <product> specify product name\n"
249 " -c <cmdline> override kernel commandline\n"
250 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700251 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800252 " -n <page size> specify the nand page size. default: 2048\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254}
255
Dima Zavin931175a2010-02-12 20:26:33 -0800256void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 unsigned *sz, const char *cmdline)
258{
259 void *kdata = 0, *rdata = 0;
260 unsigned ksize = 0, rsize = 0;
261 void *bdata;
262 unsigned bsize;
263
264 if(kernel == 0) {
265 fprintf(stderr, "no image specified\n");
266 return 0;
267 }
268
269 kdata = load_file(kernel, &ksize);
270 if(kdata == 0) {
271 fprintf(stderr, "cannot load '%s'\n", kernel);
272 return 0;
273 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800274
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 /* is this actually a boot image? */
276 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
277 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800278
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 if(ramdisk) {
280 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
281 return 0;
282 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 *sz = ksize;
285 return kdata;
286 }
287
288 if(ramdisk) {
289 rdata = load_file(ramdisk, &rsize);
290 if(rdata == 0) {
291 fprintf(stderr,"cannot load '%s'\n", ramdisk);
292 return 0;
293 }
294 }
295
296 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800297 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 if(bdata == 0) {
299 fprintf(stderr,"failed to create boot.img\n");
300 return 0;
301 }
302 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
303 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
304 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800305
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 return bdata;
307}
308
309void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
310{
311 void *data;
312 zipentry_t entry;
313 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800314
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 entry = lookup_zipentry(zip, name);
316 if (entry == NULL) {
317 fprintf(stderr, "archive does not contain '%s'\n", name);
318 return 0;
319 }
320
321 *sz = get_zipentry_size(entry);
322
323 datasz = *sz * 1.001;
324 data = malloc(datasz);
325
326 if(data == 0) {
327 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
328 return 0;
329 }
330
331 if (decompress_zipentry(entry, data, datasz)) {
332 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
333 free(data);
334 return 0;
335 }
336
337 return data;
338}
339
340static char *strip(char *s)
341{
342 int n;
343 while(*s && isspace(*s)) s++;
344 n = strlen(s);
345 while(n-- > 0) {
346 if(!isspace(s[n])) break;
347 s[n] = 0;
348 }
349 return s;
350}
351
352#define MAX_OPTIONS 32
353static int setup_requirement_line(char *name)
354{
355 char *val[MAX_OPTIONS];
356 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700357 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 unsigned n, count;
359 char *x;
360 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800361
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 if (!strncmp(name, "reject ", 7)) {
363 name += 7;
364 invert = 1;
365 } else if (!strncmp(name, "require ", 8)) {
366 name += 8;
367 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700368 } else if (!strncmp(name, "require-for-product:", 20)) {
369 // Get the product and point name past it
370 prod = name + 20;
371 name = strchr(name, ' ');
372 if (!name) return -1;
373 *name = 0;
374 name += 1;
375 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 }
377
378 x = strchr(name, '=');
379 if (x == 0) return 0;
380 *x = 0;
381 val[0] = x + 1;
382
383 for(count = 1; count < MAX_OPTIONS; count++) {
384 x = strchr(val[count - 1],'|');
385 if (x == 0) break;
386 *x = 0;
387 val[count] = x + 1;
388 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800389
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 name = strip(name);
391 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800392
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 name = strip(name);
394 if (name == 0) return -1;
395
396 /* work around an unfortunate name mismatch */
397 if (!strcmp(name,"board")) name = "product";
398
399 out = malloc(sizeof(char*) * count);
400 if (out == 0) return -1;
401
402 for(n = 0; n < count; n++) {
403 out[n] = strdup(strip(val[n]));
404 if (out[n] == 0) return -1;
405 }
406
Wink Savilleb98762f2011-04-04 17:54:59 -0700407 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 return 0;
409}
410
411static void setup_requirements(char *data, unsigned sz)
412{
413 char *s;
414
415 s = data;
416 while (sz-- > 0) {
417 if(*s == '\n') {
418 *s++ = 0;
419 if (setup_requirement_line(data)) {
420 die("out of memory");
421 }
422 data = s;
423 } else {
424 s++;
425 }
426 }
427}
428
429void queue_info_dump(void)
430{
431 fb_queue_notice("--------------------------------------------");
432 fb_queue_display("version-bootloader", "Bootloader Version...");
433 fb_queue_display("version-baseband", "Baseband Version.....");
434 fb_queue_display("serialno", "Serial Number........");
435 fb_queue_notice("--------------------------------------------");
436}
437
438void do_update_signature(zipfile_t zip, char *fn)
439{
440 void *data;
441 unsigned sz;
442 data = unzip_file(zip, fn, &sz);
443 if (data == 0) return;
444 fb_queue_download("signature", data, sz);
445 fb_queue_command("signature", "installing signature");
446}
447
448void do_update(char *fn)
449{
450 void *zdata;
451 unsigned zsize;
452 void *data;
453 unsigned sz;
454 zipfile_t zip;
455
456 queue_info_dump();
457
Wink Savilleb98762f2011-04-04 17:54:59 -0700458 fb_queue_query_save("product", cur_product, sizeof(cur_product));
459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 zdata = load_file(fn, &zsize);
461 if (zdata == 0) die("failed to load '%s'", fn);
462
463 zip = init_zipfile(zdata, zsize);
464 if(zip == 0) die("failed to access zipdata in '%s'");
465
466 data = unzip_file(zip, "android-info.txt", &sz);
467 if (data == 0) {
468 char *tmp;
469 /* fallback for older zipfiles */
470 data = unzip_file(zip, "android-product.txt", &sz);
471 if ((data == 0) || (sz < 1)) {
472 die("update package has no android-info.txt or android-product.txt");
473 }
474 tmp = malloc(sz + 128);
475 if (tmp == 0) die("out of memory");
476 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
477 data = tmp;
478 sz = strlen(tmp);
479 }
480
481 setup_requirements(data, sz);
482
483 data = unzip_file(zip, "boot.img", &sz);
484 if (data == 0) die("update package missing boot.img");
485 do_update_signature(zip, "boot.sig");
486 fb_queue_flash("boot", data, sz);
487
488 data = unzip_file(zip, "recovery.img", &sz);
489 if (data != 0) {
490 do_update_signature(zip, "recovery.sig");
491 fb_queue_flash("recovery", data, sz);
492 }
493
494 data = unzip_file(zip, "system.img", &sz);
495 if (data == 0) die("update package missing system.img");
496 do_update_signature(zip, "system.sig");
497 fb_queue_flash("system", data, sz);
498}
499
500void do_send_signature(char *fn)
501{
502 void *data;
503 unsigned sz;
504 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800505
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 xtn = strrchr(fn, '.');
507 if (!xtn) return;
508 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800509
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 strcpy(xtn,".sig");
511 data = load_file(fn, &sz);
512 strcpy(xtn,".img");
513 if (data == 0) return;
514 fb_queue_download("signature", data, sz);
515 fb_queue_command("signature", "installing signature");
516}
517
518void do_flashall(void)
519{
520 char *fname;
521 void *data;
522 unsigned sz;
523
524 queue_info_dump();
525
Wink Savilleb98762f2011-04-04 17:54:59 -0700526 fb_queue_query_save("product", cur_product, sizeof(cur_product));
527
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 fname = find_item("info", product);
529 if (fname == 0) die("cannot find android-info.txt");
530 data = load_file(fname, &sz);
531 if (data == 0) die("could not load android-info.txt");
532 setup_requirements(data, sz);
533
534 fname = find_item("boot", product);
535 data = load_file(fname, &sz);
536 if (data == 0) die("could not load boot.img");
537 do_send_signature(fname);
538 fb_queue_flash("boot", data, sz);
539
540 fname = find_item("recovery", product);
541 data = load_file(fname, &sz);
542 if (data != 0) {
543 do_send_signature(fname);
544 fb_queue_flash("recovery", data, sz);
545 }
546
547 fname = find_item("system", product);
548 data = load_file(fname, &sz);
549 if (data == 0) die("could not load system.img");
550 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800551 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800552}
553
554#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800555#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556
557int do_oem_command(int argc, char **argv)
558{
559 int i;
560 char command[256];
561 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800562
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 command[0] = 0;
564 while(1) {
565 strcat(command,*argv);
566 skip(1);
567 if(argc == 0) break;
568 strcat(command," ");
569 }
570
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800571 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 return 0;
573}
574
575int main(int argc, char **argv)
576{
577 int wants_wipe = 0;
578 int wants_reboot = 0;
579 int wants_reboot_bootloader = 0;
Scott Anderson13081c62012-04-06 12:39:30 -0700580 int wants_device_list = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 void *data;
582 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800583 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700584 int status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585
586 skip(1);
587 if (argc == 0) {
588 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700589 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 }
591
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800592 if (!strcmp(*argv, "help")) {
593 usage();
594 return 0;
595 }
596
597
Elliott Hughes31dbed72009-10-07 15:38:53 -0700598 serial = getenv("ANDROID_SERIAL");
599
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800600 while (argc > 0) {
601 if(!strcmp(*argv, "-w")) {
602 wants_wipe = 1;
603 skip(1);
Brian Swetland2a63bb72009-04-28 16:05:07 -0700604 } else if(!strcmp(*argv, "-b")) {
605 require(2);
606 base_addr = strtoul(argv[1], 0, 16);
607 skip(2);
Dima Zavin931175a2010-02-12 20:26:33 -0800608 } else if(!strcmp(*argv, "-n")) {
609 require(2);
610 page_size = (unsigned)strtoul(argv[1], NULL, 0);
611 if (!page_size) die("invalid page size");
612 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613 } else if(!strcmp(*argv, "-s")) {
614 require(2);
615 serial = argv[1];
616 skip(2);
Scott Anderson13081c62012-04-06 12:39:30 -0700617 } else if(!strcmp(*argv, "-l")) {
618 long_listing = 1;
619 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 } else if(!strcmp(*argv, "-p")) {
621 require(2);
622 product = argv[1];
623 skip(2);
624 } else if(!strcmp(*argv, "-c")) {
625 require(2);
626 cmdline = argv[1];
627 skip(2);
628 } else if(!strcmp(*argv, "-i")) {
629 char *endptr = NULL;
630 unsigned long val;
631
632 require(2);
633 val = strtoul(argv[1], &endptr, 0);
634 if (!endptr || *endptr != '\0' || (val & ~0xffff))
635 die("invalid vendor id '%s'", argv[1]);
636 vendor_id = (unsigned short)val;
637 skip(2);
Scott Anderson13081c62012-04-06 12:39:30 -0700638 } else if (!strcmp(*argv, "devices")) {
639 skip(1);
640 wants_device_list = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641 } else if(!strcmp(*argv, "getvar")) {
642 require(2);
643 fb_queue_display(argv[1], argv[1]);
644 skip(2);
645 } else if(!strcmp(*argv, "erase")) {
646 require(2);
647 fb_queue_erase(argv[1]);
648 skip(2);
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800649 } else if(!strcmp(*argv, "format")) {
650 require(2);
Mike J. Chen714052b2012-02-04 16:22:22 -0800651 fb_queue_format(argv[1], 0);
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800652 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 } else if(!strcmp(*argv, "signature")) {
654 require(2);
655 data = load_file(argv[1], &sz);
656 if (data == 0) die("could not load '%s'", argv[1]);
657 if (sz != 256) die("signature must be 256 bytes");
658 fb_queue_download("signature", data, sz);
659 fb_queue_command("signature", "installing signature");
660 skip(2);
661 } else if(!strcmp(*argv, "reboot")) {
662 wants_reboot = 1;
663 skip(1);
664 } else if(!strcmp(*argv, "reboot-bootloader")) {
665 wants_reboot_bootloader = 1;
666 skip(1);
667 } else if (!strcmp(*argv, "continue")) {
668 fb_queue_command("continue", "resuming boot");
669 skip(1);
670 } else if(!strcmp(*argv, "boot")) {
671 char *kname = 0;
672 char *rname = 0;
673 skip(1);
674 if (argc > 0) {
675 kname = argv[0];
676 skip(1);
677 }
678 if (argc > 0) {
679 rname = argv[0];
680 skip(1);
681 }
Dima Zavin931175a2010-02-12 20:26:33 -0800682 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 if (data == 0) return 1;
684 fb_queue_download("boot.img", data, sz);
685 fb_queue_command("boot", "booting");
686 } else if(!strcmp(*argv, "flash")) {
687 char *pname = argv[1];
688 char *fname = 0;
689 require(2);
690 if (argc > 2) {
691 fname = argv[2];
692 skip(3);
693 } else {
694 fname = find_item(pname, product);
695 skip(2);
696 }
697 if (fname == 0) die("cannot determine image filename for '%s'", pname);
698 data = load_file(fname, &sz);
699 if (data == 0) die("cannot load '%s'\n", fname);
700 fb_queue_flash(pname, data, sz);
701 } else if(!strcmp(*argv, "flash:raw")) {
702 char *pname = argv[1];
703 char *kname = argv[2];
704 char *rname = 0;
705 require(3);
706 if(argc > 3) {
707 rname = argv[3];
708 skip(4);
709 } else {
710 skip(3);
711 }
Dima Zavin931175a2010-02-12 20:26:33 -0800712 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 if (data == 0) die("cannot load bootable image");
714 fb_queue_flash(pname, data, sz);
715 } else if(!strcmp(*argv, "flashall")) {
716 skip(1);
717 do_flashall();
718 wants_reboot = 1;
719 } else if(!strcmp(*argv, "update")) {
720 if (argc > 1) {
721 do_update(argv[1]);
722 skip(2);
723 } else {
724 do_update("update.zip");
725 skip(1);
726 }
727 wants_reboot = 1;
728 } else if(!strcmp(*argv, "oem")) {
729 argc = do_oem_command(argc, argv);
730 } else {
731 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800732 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733 }
734 }
735
Scott Anderson13081c62012-04-06 12:39:30 -0700736 if (wants_device_list)
737 list_devices();
738
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800739 if (wants_wipe) {
740 fb_queue_erase("userdata");
Mike J. Chen714052b2012-02-04 16:22:22 -0800741 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 fb_queue_erase("cache");
Mike J. Chen714052b2012-02-04 16:22:22 -0800743 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 }
745 if (wants_reboot) {
746 fb_queue_reboot();
747 } else if (wants_reboot_bootloader) {
748 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
749 }
750
Scott Anderson13081c62012-04-06 12:39:30 -0700751 if (fb_queue_is_empty())
752 return 0;
753
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 usb = open_device();
755
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700756 status = fb_execute_queue(usb);
757 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758}