blob: 544893bd8f3d63331dcdd31e52776732652c32fc [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{
JP Abgralla032ded2012-06-06 11:53:33 -0700154 return match_fastboot_with_serial(info, serial);
155}
156
157int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
158{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400160 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800161 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700162 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800163 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700164 (info->dev_vendor != 0x0fce) && // Sony Ericsson
165 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400166 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800167 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800168 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800169 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800170 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400171 (info->dev_vendor != 0x0bb4)) // HTC
172 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 if(info->ifc_class != 0xff) return -1;
174 if(info->ifc_subclass != 0x42) return -1;
175 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700176 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700178 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
179 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return 0;
181}
182
183int list_devices_callback(usb_ifc_info *info)
184{
JP Abgralla032ded2012-06-06 11:53:33 -0700185 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700187 if (!info->writable) {
188 serial = "no permissions"; // like "adb devices"
189 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 if (!serial[0]) {
191 serial = "????????????";
192 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700193 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700194 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700195 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700196 } else if (!info->device_path) {
197 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700198 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700199 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700200 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 }
202
203 return -1;
204}
205
206usb_handle *open_device(void)
207{
208 static usb_handle *usb = 0;
209 int announce = 1;
210
211 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800212
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 for(;;) {
214 usb = usb_open(match_fastboot);
215 if(usb) return usb;
216 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800217 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 fprintf(stderr,"< waiting for device >\n");
219 }
220 sleep(1);
221 }
222}
223
224void list_devices(void) {
225 // We don't actually open a USB device here,
226 // just getting our callback called so we can
227 // list all the connected devices.
228 usb_open(list_devices_callback);
229}
230
231void usage(void)
232{
233 fprintf(stderr,
234/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
235 "usage: fastboot [ <option> ] <command>\n"
236 "\n"
237 "commands:\n"
238 " update <filename> reflash device from update.zip\n"
239 " flashall flash boot + recovery + system\n"
240 " flash <partition> [ <filename> ] write a file to a flash partition\n"
241 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800242 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 " getvar <variable> display a bootloader variable\n"
244 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
245 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
246 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700247 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 " reboot reboot device normally\n"
249 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800250 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 "\n"
252 "options:\n"
253 " -w erase userdata and cache\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700254 " -s <specific device> specify device serial number\n"
255 " or path to device port\n"
256 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 " -p <product> specify product name\n"
258 " -c <cmdline> override kernel commandline\n"
259 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700260 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800261 " -n <page size> specify the nand page size. default: 2048\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263}
264
Dima Zavin931175a2010-02-12 20:26:33 -0800265void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 unsigned *sz, const char *cmdline)
267{
268 void *kdata = 0, *rdata = 0;
269 unsigned ksize = 0, rsize = 0;
270 void *bdata;
271 unsigned bsize;
272
273 if(kernel == 0) {
274 fprintf(stderr, "no image specified\n");
275 return 0;
276 }
277
278 kdata = load_file(kernel, &ksize);
279 if(kdata == 0) {
280 fprintf(stderr, "cannot load '%s'\n", kernel);
281 return 0;
282 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 /* is this actually a boot image? */
285 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
286 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800287
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 if(ramdisk) {
289 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
290 return 0;
291 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800292
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 *sz = ksize;
294 return kdata;
295 }
296
297 if(ramdisk) {
298 rdata = load_file(ramdisk, &rsize);
299 if(rdata == 0) {
300 fprintf(stderr,"cannot load '%s'\n", ramdisk);
301 return 0;
302 }
303 }
304
305 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800306 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 if(bdata == 0) {
308 fprintf(stderr,"failed to create boot.img\n");
309 return 0;
310 }
311 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
312 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
313 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800314
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 return bdata;
316}
317
318void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
319{
320 void *data;
321 zipentry_t entry;
322 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800323
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 entry = lookup_zipentry(zip, name);
325 if (entry == NULL) {
326 fprintf(stderr, "archive does not contain '%s'\n", name);
327 return 0;
328 }
329
330 *sz = get_zipentry_size(entry);
331
332 datasz = *sz * 1.001;
333 data = malloc(datasz);
334
335 if(data == 0) {
336 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
337 return 0;
338 }
339
340 if (decompress_zipentry(entry, data, datasz)) {
341 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
342 free(data);
343 return 0;
344 }
345
346 return data;
347}
348
349static char *strip(char *s)
350{
351 int n;
352 while(*s && isspace(*s)) s++;
353 n = strlen(s);
354 while(n-- > 0) {
355 if(!isspace(s[n])) break;
356 s[n] = 0;
357 }
358 return s;
359}
360
361#define MAX_OPTIONS 32
362static int setup_requirement_line(char *name)
363{
364 char *val[MAX_OPTIONS];
365 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700366 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 unsigned n, count;
368 char *x;
369 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 if (!strncmp(name, "reject ", 7)) {
372 name += 7;
373 invert = 1;
374 } else if (!strncmp(name, "require ", 8)) {
375 name += 8;
376 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700377 } else if (!strncmp(name, "require-for-product:", 20)) {
378 // Get the product and point name past it
379 prod = name + 20;
380 name = strchr(name, ' ');
381 if (!name) return -1;
382 *name = 0;
383 name += 1;
384 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 }
386
387 x = strchr(name, '=');
388 if (x == 0) return 0;
389 *x = 0;
390 val[0] = x + 1;
391
392 for(count = 1; count < MAX_OPTIONS; count++) {
393 x = strchr(val[count - 1],'|');
394 if (x == 0) break;
395 *x = 0;
396 val[count] = x + 1;
397 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800398
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 name = strip(name);
400 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800401
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 name = strip(name);
403 if (name == 0) return -1;
404
405 /* work around an unfortunate name mismatch */
406 if (!strcmp(name,"board")) name = "product";
407
408 out = malloc(sizeof(char*) * count);
409 if (out == 0) return -1;
410
411 for(n = 0; n < count; n++) {
412 out[n] = strdup(strip(val[n]));
413 if (out[n] == 0) return -1;
414 }
415
Wink Savilleb98762f2011-04-04 17:54:59 -0700416 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 return 0;
418}
419
420static void setup_requirements(char *data, unsigned sz)
421{
422 char *s;
423
424 s = data;
425 while (sz-- > 0) {
426 if(*s == '\n') {
427 *s++ = 0;
428 if (setup_requirement_line(data)) {
429 die("out of memory");
430 }
431 data = s;
432 } else {
433 s++;
434 }
435 }
436}
437
438void queue_info_dump(void)
439{
440 fb_queue_notice("--------------------------------------------");
441 fb_queue_display("version-bootloader", "Bootloader Version...");
442 fb_queue_display("version-baseband", "Baseband Version.....");
443 fb_queue_display("serialno", "Serial Number........");
444 fb_queue_notice("--------------------------------------------");
445}
446
447void do_update_signature(zipfile_t zip, char *fn)
448{
449 void *data;
450 unsigned sz;
451 data = unzip_file(zip, fn, &sz);
452 if (data == 0) return;
453 fb_queue_download("signature", data, sz);
454 fb_queue_command("signature", "installing signature");
455}
456
457void do_update(char *fn)
458{
459 void *zdata;
460 unsigned zsize;
461 void *data;
462 unsigned sz;
463 zipfile_t zip;
464
465 queue_info_dump();
466
Wink Savilleb98762f2011-04-04 17:54:59 -0700467 fb_queue_query_save("product", cur_product, sizeof(cur_product));
468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 zdata = load_file(fn, &zsize);
470 if (zdata == 0) die("failed to load '%s'", fn);
471
472 zip = init_zipfile(zdata, zsize);
473 if(zip == 0) die("failed to access zipdata in '%s'");
474
475 data = unzip_file(zip, "android-info.txt", &sz);
476 if (data == 0) {
477 char *tmp;
478 /* fallback for older zipfiles */
479 data = unzip_file(zip, "android-product.txt", &sz);
480 if ((data == 0) || (sz < 1)) {
481 die("update package has no android-info.txt or android-product.txt");
482 }
483 tmp = malloc(sz + 128);
484 if (tmp == 0) die("out of memory");
485 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
486 data = tmp;
487 sz = strlen(tmp);
488 }
489
490 setup_requirements(data, sz);
491
492 data = unzip_file(zip, "boot.img", &sz);
493 if (data == 0) die("update package missing boot.img");
494 do_update_signature(zip, "boot.sig");
495 fb_queue_flash("boot", data, sz);
496
497 data = unzip_file(zip, "recovery.img", &sz);
498 if (data != 0) {
499 do_update_signature(zip, "recovery.sig");
500 fb_queue_flash("recovery", data, sz);
501 }
502
503 data = unzip_file(zip, "system.img", &sz);
504 if (data == 0) die("update package missing system.img");
505 do_update_signature(zip, "system.sig");
506 fb_queue_flash("system", data, sz);
507}
508
509void do_send_signature(char *fn)
510{
511 void *data;
512 unsigned sz;
513 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800514
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 xtn = strrchr(fn, '.');
516 if (!xtn) return;
517 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800518
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 strcpy(xtn,".sig");
520 data = load_file(fn, &sz);
521 strcpy(xtn,".img");
522 if (data == 0) return;
523 fb_queue_download("signature", data, sz);
524 fb_queue_command("signature", "installing signature");
525}
526
527void do_flashall(void)
528{
529 char *fname;
530 void *data;
531 unsigned sz;
532
533 queue_info_dump();
534
Wink Savilleb98762f2011-04-04 17:54:59 -0700535 fb_queue_query_save("product", cur_product, sizeof(cur_product));
536
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 fname = find_item("info", product);
538 if (fname == 0) die("cannot find android-info.txt");
539 data = load_file(fname, &sz);
540 if (data == 0) die("could not load android-info.txt");
541 setup_requirements(data, sz);
542
543 fname = find_item("boot", product);
544 data = load_file(fname, &sz);
545 if (data == 0) die("could not load boot.img");
546 do_send_signature(fname);
547 fb_queue_flash("boot", data, sz);
548
549 fname = find_item("recovery", product);
550 data = load_file(fname, &sz);
551 if (data != 0) {
552 do_send_signature(fname);
553 fb_queue_flash("recovery", data, sz);
554 }
555
556 fname = find_item("system", product);
557 data = load_file(fname, &sz);
558 if (data == 0) die("could not load system.img");
559 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800560 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561}
562
563#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800564#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565
566int do_oem_command(int argc, char **argv)
567{
568 int i;
569 char command[256];
570 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800571
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 command[0] = 0;
573 while(1) {
574 strcat(command,*argv);
575 skip(1);
576 if(argc == 0) break;
577 strcat(command," ");
578 }
579
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800580 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800581 return 0;
582}
583
584int main(int argc, char **argv)
585{
586 int wants_wipe = 0;
587 int wants_reboot = 0;
588 int wants_reboot_bootloader = 0;
Scott Anderson13081c62012-04-06 12:39:30 -0700589 int wants_device_list = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800590 void *data;
591 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800592 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700593 int status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594
595 skip(1);
596 if (argc == 0) {
597 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700598 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 }
600
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800601 if (!strcmp(*argv, "help")) {
602 usage();
603 return 0;
604 }
605
606
Elliott Hughes31dbed72009-10-07 15:38:53 -0700607 serial = getenv("ANDROID_SERIAL");
608
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 while (argc > 0) {
610 if(!strcmp(*argv, "-w")) {
611 wants_wipe = 1;
612 skip(1);
Brian Swetland2a63bb72009-04-28 16:05:07 -0700613 } else if(!strcmp(*argv, "-b")) {
614 require(2);
615 base_addr = strtoul(argv[1], 0, 16);
616 skip(2);
Dima Zavin931175a2010-02-12 20:26:33 -0800617 } else if(!strcmp(*argv, "-n")) {
618 require(2);
619 page_size = (unsigned)strtoul(argv[1], NULL, 0);
620 if (!page_size) die("invalid page size");
621 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 } else if(!strcmp(*argv, "-s")) {
623 require(2);
624 serial = argv[1];
625 skip(2);
Scott Anderson13081c62012-04-06 12:39:30 -0700626 } else if(!strcmp(*argv, "-l")) {
627 long_listing = 1;
628 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 } else if(!strcmp(*argv, "-p")) {
630 require(2);
631 product = argv[1];
632 skip(2);
633 } else if(!strcmp(*argv, "-c")) {
634 require(2);
635 cmdline = argv[1];
636 skip(2);
637 } else if(!strcmp(*argv, "-i")) {
638 char *endptr = NULL;
639 unsigned long val;
640
641 require(2);
642 val = strtoul(argv[1], &endptr, 0);
643 if (!endptr || *endptr != '\0' || (val & ~0xffff))
644 die("invalid vendor id '%s'", argv[1]);
645 vendor_id = (unsigned short)val;
646 skip(2);
Scott Anderson13081c62012-04-06 12:39:30 -0700647 } else if (!strcmp(*argv, "devices")) {
648 skip(1);
649 wants_device_list = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 } else if(!strcmp(*argv, "getvar")) {
651 require(2);
652 fb_queue_display(argv[1], argv[1]);
653 skip(2);
654 } else if(!strcmp(*argv, "erase")) {
655 require(2);
656 fb_queue_erase(argv[1]);
657 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800658 } else if(!strcmp(*argv, "format")) {
659 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700660 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800661 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 } else if(!strcmp(*argv, "signature")) {
663 require(2);
664 data = load_file(argv[1], &sz);
665 if (data == 0) die("could not load '%s'", argv[1]);
666 if (sz != 256) die("signature must be 256 bytes");
667 fb_queue_download("signature", data, sz);
668 fb_queue_command("signature", "installing signature");
669 skip(2);
670 } else if(!strcmp(*argv, "reboot")) {
671 wants_reboot = 1;
672 skip(1);
673 } else if(!strcmp(*argv, "reboot-bootloader")) {
674 wants_reboot_bootloader = 1;
675 skip(1);
676 } else if (!strcmp(*argv, "continue")) {
677 fb_queue_command("continue", "resuming boot");
678 skip(1);
679 } else if(!strcmp(*argv, "boot")) {
680 char *kname = 0;
681 char *rname = 0;
682 skip(1);
683 if (argc > 0) {
684 kname = argv[0];
685 skip(1);
686 }
687 if (argc > 0) {
688 rname = argv[0];
689 skip(1);
690 }
Dima Zavin931175a2010-02-12 20:26:33 -0800691 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 if (data == 0) return 1;
693 fb_queue_download("boot.img", data, sz);
694 fb_queue_command("boot", "booting");
695 } else if(!strcmp(*argv, "flash")) {
696 char *pname = argv[1];
697 char *fname = 0;
698 require(2);
699 if (argc > 2) {
700 fname = argv[2];
701 skip(3);
702 } else {
703 fname = find_item(pname, product);
704 skip(2);
705 }
706 if (fname == 0) die("cannot determine image filename for '%s'", pname);
707 data = load_file(fname, &sz);
708 if (data == 0) die("cannot load '%s'\n", fname);
709 fb_queue_flash(pname, data, sz);
710 } else if(!strcmp(*argv, "flash:raw")) {
711 char *pname = argv[1];
712 char *kname = argv[2];
713 char *rname = 0;
714 require(3);
715 if(argc > 3) {
716 rname = argv[3];
717 skip(4);
718 } else {
719 skip(3);
720 }
Dima Zavin931175a2010-02-12 20:26:33 -0800721 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 if (data == 0) die("cannot load bootable image");
723 fb_queue_flash(pname, data, sz);
724 } else if(!strcmp(*argv, "flashall")) {
725 skip(1);
726 do_flashall();
727 wants_reboot = 1;
728 } else if(!strcmp(*argv, "update")) {
729 if (argc > 1) {
730 do_update(argv[1]);
731 skip(2);
732 } else {
733 do_update("update.zip");
734 skip(1);
735 }
736 wants_reboot = 1;
737 } else if(!strcmp(*argv, "oem")) {
738 argc = do_oem_command(argc, argv);
739 } else {
740 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800741 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742 }
743 }
744
Scott Anderson13081c62012-04-06 12:39:30 -0700745 if (wants_device_list)
746 list_devices();
747
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 if (wants_wipe) {
749 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -0700750 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -0700752 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 }
754 if (wants_reboot) {
755 fb_queue_reboot();
756 } else if (wants_reboot_bootloader) {
757 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
758 }
759
Scott Anderson13081c62012-04-06 12:39:30 -0700760 if (fb_queue_is_empty())
761 return 0;
762
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 usb = open_device();
764
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700765 status = fb_execute_queue(usb);
766 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767}