blob: ff9917300d4c3e0b3633bdf3c3802e16cbc628d4 [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>
33#include <stdarg.h>
Colin Crossf8387882012-05-24 17:18:41 -070034#include <stdbool.h>
35#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <string.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <limits.h>
41#include <ctype.h>
Colin Cross8879f982012-05-22 17:53:34 -070042#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
44#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070045#include <sys/types.h>
46
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"
52
Colin Crossf8387882012-05-24 17:18:41 -070053#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
57#define DEFAULT_SPARSE_LIMIT (256 * 1024 * 1024)
58
Wink Savilleb98762f2011-04-04 17:54:59 -070059char cur_product[FB_RESPONSE_SZ + 1];
60
Brian Swetland2a63bb72009-04-28 16:05:07 -070061void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
62
63boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
64 void *ramdisk, unsigned ramdisk_size,
65 void *second, unsigned second_size,
66 unsigned page_size, unsigned base,
67 unsigned *bootimg_size);
68
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069static usb_handle *usb = 0;
70static const char *serial = 0;
71static const char *product = 0;
72static const char *cmdline = 0;
73static int wipe_data = 0;
74static unsigned short vendor_id = 0;
Colin Crossf8387882012-05-24 17:18:41 -070075static int64_t sparse_limit = -1;
76static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Brian Swetland2a63bb72009-04-28 16:05:07 -070078static unsigned base_addr = 0x10000000;
79
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080void die(const char *fmt, ...)
81{
82 va_list ap;
83 va_start(ap, fmt);
84 fprintf(stderr,"error: ");
85 vfprintf(stderr, fmt, ap);
86 fprintf(stderr,"\n");
87 va_end(ap);
88 exit(1);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080089}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
91void get_my_path(char *path);
92
93char *find_item(const char *item, const char *product)
94{
95 char *dir;
96 char *fn;
97 char path[PATH_MAX + 128];
98
99 if(!strcmp(item,"boot")) {
100 fn = "boot.img";
101 } else if(!strcmp(item,"recovery")) {
102 fn = "recovery.img";
103 } else if(!strcmp(item,"system")) {
104 fn = "system.img";
105 } else if(!strcmp(item,"userdata")) {
106 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700107 } else if(!strcmp(item,"cache")) {
108 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 } else if(!strcmp(item,"info")) {
110 fn = "android-info.txt";
111 } else {
112 fprintf(stderr,"unknown partition '%s'\n", item);
113 return 0;
114 }
115
116 if(product) {
117 get_my_path(path);
118 sprintf(path + strlen(path),
119 "../../../target/product/%s/%s", product, fn);
120 return strdup(path);
121 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800122
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 dir = getenv("ANDROID_PRODUCT_OUT");
124 if((dir == 0) || (dir[0] == 0)) {
125 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
126 return 0;
127 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800128
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 sprintf(path, "%s/%s", dir, fn);
130 return strdup(path);
131}
132
133#ifdef _WIN32
134void *load_file(const char *fn, unsigned *_sz);
Colin Crossf8387882012-05-24 17:18:41 -0700135int64_t file_size(const char *fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136#else
Colin Crossf8387882012-05-24 17:18:41 -0700137#if defined(__APPLE__) && defined(__MACH__)
138#define lseek64 lseek
139#define off64_t off_t
140#endif
141
142int64_t file_size(const char *fn)
143{
144 off64_t off;
145 int fd;
146
147 fd = open(fn, O_RDONLY);
148 if (fd < 0) return -1;
149
150 off = lseek64(fd, 0, SEEK_END);
151 close(fd);
152
153 return off;
154}
155
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156void *load_file(const char *fn, unsigned *_sz)
157{
158 char *data;
159 int sz;
160 int fd;
161
162 data = 0;
163 fd = open(fn, O_RDONLY);
164 if(fd < 0) return 0;
165
166 sz = lseek(fd, 0, SEEK_END);
167 if(sz < 0) goto oops;
168
169 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
170
171 data = (char*) malloc(sz);
172 if(data == 0) goto oops;
173
174 if(read(fd, data, sz) != sz) goto oops;
175 close(fd);
176
177 if(_sz) *_sz = sz;
178 return data;
179
180oops:
181 close(fd);
182 if(data != 0) free(data);
183 return 0;
184}
185#endif
186
187int match_fastboot(usb_ifc_info *info)
188{
189 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400190 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800191 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700192 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800193 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700194 (info->dev_vendor != 0x0fce) && // Sony Ericsson
195 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400196 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800197 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800198 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800199 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800200 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400201 (info->dev_vendor != 0x0bb4)) // HTC
202 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 if(info->ifc_class != 0xff) return -1;
204 if(info->ifc_subclass != 0x42) return -1;
205 if(info->ifc_protocol != 0x03) return -1;
206 // require matching serial number if a serial number is specified
207 // at the command line with the -s option.
208 if (serial && strcmp(serial, info->serial_number) != 0) return -1;
209 return 0;
210}
211
212int list_devices_callback(usb_ifc_info *info)
213{
214 if (match_fastboot(info) == 0) {
215 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 if (!info->writable) {
217 serial = "no permissions"; // like "adb devices"
218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 if (!serial[0]) {
220 serial = "????????????";
221 }
222 // output compatible with "adb devices"
223 printf("%s\tfastboot\n", serial);
224 }
225
226 return -1;
227}
228
229usb_handle *open_device(void)
230{
231 static usb_handle *usb = 0;
232 int announce = 1;
233
234 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800235
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 for(;;) {
237 usb = usb_open(match_fastboot);
238 if(usb) return usb;
239 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800240 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 fprintf(stderr,"< waiting for device >\n");
242 }
243 sleep(1);
244 }
245}
246
247void list_devices(void) {
248 // We don't actually open a USB device here,
249 // just getting our callback called so we can
250 // list all the connected devices.
251 usb_open(list_devices_callback);
252}
253
254void usage(void)
255{
256 fprintf(stderr,
257/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
258 "usage: fastboot [ <option> ] <command>\n"
259 "\n"
260 "commands:\n"
261 " update <filename> reflash device from update.zip\n"
262 " flashall flash boot + recovery + system\n"
263 " flash <partition> [ <filename> ] write a file to a flash partition\n"
264 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800265 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 " getvar <variable> display a bootloader variable\n"
267 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
268 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
269 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700270 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 " reboot reboot device normally\n"
272 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800273 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 "\n"
275 "options:\n"
276 " -w erase userdata and cache\n"
277 " -s <serial number> specify device serial number\n"
278 " -p <product> specify product name\n"
279 " -c <cmdline> override kernel commandline\n"
280 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700281 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800282 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700283 " -S <size>[K|M|G] automatically sparse files greater than\n"
284 " size. default: 256M, 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286}
287
Dima Zavin931175a2010-02-12 20:26:33 -0800288void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 unsigned *sz, const char *cmdline)
290{
291 void *kdata = 0, *rdata = 0;
292 unsigned ksize = 0, rsize = 0;
293 void *bdata;
294 unsigned bsize;
295
296 if(kernel == 0) {
297 fprintf(stderr, "no image specified\n");
298 return 0;
299 }
300
301 kdata = load_file(kernel, &ksize);
302 if(kdata == 0) {
303 fprintf(stderr, "cannot load '%s'\n", kernel);
304 return 0;
305 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800306
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 /* is this actually a boot image? */
308 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
309 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800310
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 if(ramdisk) {
312 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
313 return 0;
314 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800315
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 *sz = ksize;
317 return kdata;
318 }
319
320 if(ramdisk) {
321 rdata = load_file(ramdisk, &rsize);
322 if(rdata == 0) {
323 fprintf(stderr,"cannot load '%s'\n", ramdisk);
324 return 0;
325 }
326 }
327
328 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800329 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 if(bdata == 0) {
331 fprintf(stderr,"failed to create boot.img\n");
332 return 0;
333 }
334 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
335 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
336 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 return bdata;
339}
340
341void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
342{
343 void *data;
344 zipentry_t entry;
345 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 entry = lookup_zipentry(zip, name);
348 if (entry == NULL) {
349 fprintf(stderr, "archive does not contain '%s'\n", name);
350 return 0;
351 }
352
353 *sz = get_zipentry_size(entry);
354
355 datasz = *sz * 1.001;
356 data = malloc(datasz);
357
358 if(data == 0) {
359 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
360 return 0;
361 }
362
363 if (decompress_zipentry(entry, data, datasz)) {
364 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
365 free(data);
366 return 0;
367 }
368
369 return data;
370}
371
372static char *strip(char *s)
373{
374 int n;
375 while(*s && isspace(*s)) s++;
376 n = strlen(s);
377 while(n-- > 0) {
378 if(!isspace(s[n])) break;
379 s[n] = 0;
380 }
381 return s;
382}
383
384#define MAX_OPTIONS 32
385static int setup_requirement_line(char *name)
386{
387 char *val[MAX_OPTIONS];
388 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700389 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 unsigned n, count;
391 char *x;
392 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800393
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 if (!strncmp(name, "reject ", 7)) {
395 name += 7;
396 invert = 1;
397 } else if (!strncmp(name, "require ", 8)) {
398 name += 8;
399 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700400 } else if (!strncmp(name, "require-for-product:", 20)) {
401 // Get the product and point name past it
402 prod = name + 20;
403 name = strchr(name, ' ');
404 if (!name) return -1;
405 *name = 0;
406 name += 1;
407 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 }
409
410 x = strchr(name, '=');
411 if (x == 0) return 0;
412 *x = 0;
413 val[0] = x + 1;
414
415 for(count = 1; count < MAX_OPTIONS; count++) {
416 x = strchr(val[count - 1],'|');
417 if (x == 0) break;
418 *x = 0;
419 val[count] = x + 1;
420 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 name = strip(name);
423 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800424
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 name = strip(name);
426 if (name == 0) return -1;
427
428 /* work around an unfortunate name mismatch */
429 if (!strcmp(name,"board")) name = "product";
430
431 out = malloc(sizeof(char*) * count);
432 if (out == 0) return -1;
433
434 for(n = 0; n < count; n++) {
435 out[n] = strdup(strip(val[n]));
436 if (out[n] == 0) return -1;
437 }
438
Wink Savilleb98762f2011-04-04 17:54:59 -0700439 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 return 0;
441}
442
443static void setup_requirements(char *data, unsigned sz)
444{
445 char *s;
446
447 s = data;
448 while (sz-- > 0) {
449 if(*s == '\n') {
450 *s++ = 0;
451 if (setup_requirement_line(data)) {
452 die("out of memory");
453 }
454 data = s;
455 } else {
456 s++;
457 }
458 }
459}
460
461void queue_info_dump(void)
462{
463 fb_queue_notice("--------------------------------------------");
464 fb_queue_display("version-bootloader", "Bootloader Version...");
465 fb_queue_display("version-baseband", "Baseband Version.....");
466 fb_queue_display("serialno", "Serial Number........");
467 fb_queue_notice("--------------------------------------------");
468}
469
Colin Crossf8387882012-05-24 17:18:41 -0700470
471struct sparse_file **load_sparse_files(const char *fname, int max_size)
472{
473 int fd;
474 struct sparse_file *s;
475 int files;
476 struct sparse_file **out_s;
477
478 fd = open(fname, O_RDONLY | O_BINARY);
479 if (fd < 0) {
480 die("cannot open '%s'\n", fname);
481 }
482
483 s = sparse_file_import_auto(fd, false);
484 if (!s) {
485 die("cannot sparse read file '%s'\n", fname);
486 }
487
488 files = sparse_file_resparse(s, max_size, NULL, 0);
489 if (files < 0) {
490 die("Failed to resparse '%s'\n", fname);
491 }
492
493 out_s = calloc(sizeof(struct sparse_file *), files + 1);
494 if (!out_s) {
495 die("Failed to allocate sparse file array\n");
496 }
497
498 files = sparse_file_resparse(s, max_size, out_s, files);
499 if (files < 0) {
500 die("Failed to resparse '%s'\n", fname);
501 }
502
503 return out_s;
504}
505
506static int64_t get_target_sparse_limit(struct usb_handle *usb)
507{
508 int64_t limit = 0;
509 char response[FB_RESPONSE_SZ + 1];
510 int status = fb_getvar(usb, response, "max-download-size");
511
512 if (!status) {
513 limit = strtoul(response, NULL, 0);
514 if (limit > 0) {
515 fprintf(stderr, "target reported max download size of %lld bytes\n",
516 limit);
517 }
518 }
519
520 return limit;
521}
522
523static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
524{
525 int64_t limit;
526
527 if (sparse_limit == 0) {
528 return 0;
529 } else if (sparse_limit > 0) {
530 limit = sparse_limit;
531 } else {
532 if (target_sparse_limit == -1) {
533 target_sparse_limit = get_target_sparse_limit(usb);
534 }
535 if (target_sparse_limit > 0) {
536 limit = target_sparse_limit;
537 } else {
538 limit = DEFAULT_SPARSE_LIMIT;
539 }
540 }
541
542 if (size > limit) {
543 return limit;
544 }
545
546 return 0;
547}
548
549void do_flash(usb_handle *usb, const char *pname, const char *fname)
550{
551 int64_t sz64;
552 void *data;
553 int64_t limit;
554
555 sz64 = file_size(fname);
556 limit = get_sparse_limit(usb, sz64);
557 if (limit) {
558 struct sparse_file **s = load_sparse_files(fname, limit);
559 if (s == NULL) {
560 die("cannot sparse load '%s'\n", fname);
561 }
562 while (*s) {
563 sz64 = sparse_file_len(*s, true, false);
564 fb_queue_flash_sparse(pname, *s++, sz64);
565 }
566 } else {
567 unsigned int sz;
568 data = load_file(fname, &sz);
569 if (data == 0) die("cannot load '%s'\n", fname);
570 fb_queue_flash(pname, data, sz);
571 }
572}
573
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574void do_update_signature(zipfile_t zip, char *fn)
575{
576 void *data;
577 unsigned sz;
578 data = unzip_file(zip, fn, &sz);
579 if (data == 0) return;
580 fb_queue_download("signature", data, sz);
581 fb_queue_command("signature", "installing signature");
582}
583
584void do_update(char *fn)
585{
586 void *zdata;
587 unsigned zsize;
588 void *data;
589 unsigned sz;
590 zipfile_t zip;
591
592 queue_info_dump();
593
Wink Savilleb98762f2011-04-04 17:54:59 -0700594 fb_queue_query_save("product", cur_product, sizeof(cur_product));
595
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 zdata = load_file(fn, &zsize);
597 if (zdata == 0) die("failed to load '%s'", fn);
598
599 zip = init_zipfile(zdata, zsize);
600 if(zip == 0) die("failed to access zipdata in '%s'");
601
602 data = unzip_file(zip, "android-info.txt", &sz);
603 if (data == 0) {
604 char *tmp;
605 /* fallback for older zipfiles */
606 data = unzip_file(zip, "android-product.txt", &sz);
607 if ((data == 0) || (sz < 1)) {
608 die("update package has no android-info.txt or android-product.txt");
609 }
610 tmp = malloc(sz + 128);
611 if (tmp == 0) die("out of memory");
612 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
613 data = tmp;
614 sz = strlen(tmp);
615 }
616
617 setup_requirements(data, sz);
618
619 data = unzip_file(zip, "boot.img", &sz);
620 if (data == 0) die("update package missing boot.img");
621 do_update_signature(zip, "boot.sig");
622 fb_queue_flash("boot", data, sz);
623
624 data = unzip_file(zip, "recovery.img", &sz);
625 if (data != 0) {
626 do_update_signature(zip, "recovery.sig");
627 fb_queue_flash("recovery", data, sz);
628 }
629
630 data = unzip_file(zip, "system.img", &sz);
631 if (data == 0) die("update package missing system.img");
632 do_update_signature(zip, "system.sig");
633 fb_queue_flash("system", data, sz);
634}
635
636void do_send_signature(char *fn)
637{
638 void *data;
639 unsigned sz;
640 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800641
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 xtn = strrchr(fn, '.');
643 if (!xtn) return;
644 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800645
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 strcpy(xtn,".sig");
647 data = load_file(fn, &sz);
648 strcpy(xtn,".img");
649 if (data == 0) return;
650 fb_queue_download("signature", data, sz);
651 fb_queue_command("signature", "installing signature");
652}
653
654void do_flashall(void)
655{
656 char *fname;
657 void *data;
658 unsigned sz;
659
660 queue_info_dump();
661
Wink Savilleb98762f2011-04-04 17:54:59 -0700662 fb_queue_query_save("product", cur_product, sizeof(cur_product));
663
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 fname = find_item("info", product);
665 if (fname == 0) die("cannot find android-info.txt");
666 data = load_file(fname, &sz);
667 if (data == 0) die("could not load android-info.txt");
668 setup_requirements(data, sz);
669
670 fname = find_item("boot", product);
671 data = load_file(fname, &sz);
672 if (data == 0) die("could not load boot.img");
673 do_send_signature(fname);
674 fb_queue_flash("boot", data, sz);
675
676 fname = find_item("recovery", product);
677 data = load_file(fname, &sz);
678 if (data != 0) {
679 do_send_signature(fname);
680 fb_queue_flash("recovery", data, sz);
681 }
682
683 fname = find_item("system", product);
684 data = load_file(fname, &sz);
685 if (data == 0) die("could not load system.img");
686 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800687 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688}
689
690#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800691#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692
693int do_oem_command(int argc, char **argv)
694{
695 int i;
696 char command[256];
697 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800698
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 command[0] = 0;
700 while(1) {
701 strcat(command,*argv);
702 skip(1);
703 if(argc == 0) break;
704 strcat(command," ");
705 }
706
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800707 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 return 0;
709}
710
Colin Crossf8387882012-05-24 17:18:41 -0700711static int64_t parse_num(const char *arg)
712{
713 char *endptr;
714 unsigned long long num;
715
716 num = strtoull(arg, &endptr, 0);
717 if (endptr == arg) {
718 return -1;
719 }
720
721 if (*endptr == 'k' || *endptr == 'K') {
722 if (num >= (-1ULL) / 1024) {
723 return -1;
724 }
725 num *= 1024LL;
726 endptr++;
727 } else if (*endptr == 'm' || *endptr == 'M') {
728 if (num >= (-1ULL) / (1024 * 1024)) {
729 return -1;
730 }
731 num *= 1024LL * 1024LL;
732 endptr++;
733 } else if (*endptr == 'g' || *endptr == 'G') {
734 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
735 return -1;
736 }
737 num *= 1024LL * 1024LL * 1024LL;
738 endptr++;
739 }
740
741 if (*endptr != '\0') {
742 return -1;
743 }
744
745 if (num > INT64_MAX) {
746 return -1;
747 }
748
749 return num;
750}
751
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752int main(int argc, char **argv)
753{
754 int wants_wipe = 0;
755 int wants_reboot = 0;
756 int wants_reboot_bootloader = 0;
757 void *data;
758 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800759 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700760 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700761 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700762 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763
Colin Crossf8387882012-05-24 17:18:41 -0700764 const struct option longopts = { 0, 0, 0, 0 };
Colin Cross8879f982012-05-22 17:53:34 -0700765
766 serial = getenv("ANDROID_SERIAL");
767
768 while (1) {
Colin Crossf8387882012-05-24 17:18:41 -0700769 c = getopt_long(argc, argv, "wb:n:s:S:p:c:i:m:h", &longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700770 if (c < 0) {
771 break;
772 }
773
774 switch (c) {
775 case 'w':
776 wants_wipe = 1;
777 break;
778 case 'b':
779 base_addr = strtoul(optarg, 0, 16);
780 break;
781 case 'n':
782 page_size = (unsigned)strtoul(optarg, NULL, 0);
783 if (!page_size) die("invalid page size");
784 break;
785 case 's':
786 serial = optarg;
787 break;
Colin Crossf8387882012-05-24 17:18:41 -0700788 case 'S':
789 sparse_limit = parse_num(optarg);
790 if (sparse_limit < 0) {
791 die("invalid sparse limit");
792 }
793 break;
Colin Cross8879f982012-05-22 17:53:34 -0700794 case 'p':
795 product = optarg;
796 break;
797 case 'c':
798 cmdline = optarg;
799 break;
800 case 'i': {
801 char *endptr = NULL;
802 unsigned long val;
803
804 val = strtoul(optarg, &endptr, 0);
805 if (!endptr || *endptr != '\0' || (val & ~0xffff))
806 die("invalid vendor id '%s'", optarg);
807 vendor_id = (unsigned short)val;
808 break;
809 }
810 case 'h':
811 usage();
812 return 1;
813 case '?':
814 return 1;
815 default:
816 abort();
817 }
818 }
819
820 argc -= optind;
821 argv += optind;
822
823 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700825 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826 }
827
828 if (!strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700829 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 list_devices();
831 return 0;
832 }
833
Colin Cross8879f982012-05-22 17:53:34 -0700834 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700835
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800836 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700837 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800838 require(2);
839 fb_queue_display(argv[1], argv[1]);
840 skip(2);
841 } else if(!strcmp(*argv, "erase")) {
842 require(2);
843 fb_queue_erase(argv[1]);
844 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800845 } else if(!strcmp(*argv, "format")) {
846 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700847 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800848 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 } else if(!strcmp(*argv, "signature")) {
850 require(2);
851 data = load_file(argv[1], &sz);
852 if (data == 0) die("could not load '%s'", argv[1]);
853 if (sz != 256) die("signature must be 256 bytes");
854 fb_queue_download("signature", data, sz);
855 fb_queue_command("signature", "installing signature");
856 skip(2);
857 } else if(!strcmp(*argv, "reboot")) {
858 wants_reboot = 1;
859 skip(1);
860 } else if(!strcmp(*argv, "reboot-bootloader")) {
861 wants_reboot_bootloader = 1;
862 skip(1);
863 } else if (!strcmp(*argv, "continue")) {
864 fb_queue_command("continue", "resuming boot");
865 skip(1);
866 } else if(!strcmp(*argv, "boot")) {
867 char *kname = 0;
868 char *rname = 0;
869 skip(1);
870 if (argc > 0) {
871 kname = argv[0];
872 skip(1);
873 }
874 if (argc > 0) {
875 rname = argv[0];
876 skip(1);
877 }
Dima Zavin931175a2010-02-12 20:26:33 -0800878 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800879 if (data == 0) return 1;
880 fb_queue_download("boot.img", data, sz);
881 fb_queue_command("boot", "booting");
882 } else if(!strcmp(*argv, "flash")) {
883 char *pname = argv[1];
884 char *fname = 0;
885 require(2);
886 if (argc > 2) {
887 fname = argv[2];
888 skip(3);
889 } else {
890 fname = find_item(pname, product);
891 skip(2);
892 }
893 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Colin Crossf8387882012-05-24 17:18:41 -0700894 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 } else if(!strcmp(*argv, "flash:raw")) {
896 char *pname = argv[1];
897 char *kname = argv[2];
898 char *rname = 0;
899 require(3);
900 if(argc > 3) {
901 rname = argv[3];
902 skip(4);
903 } else {
904 skip(3);
905 }
Dima Zavin931175a2010-02-12 20:26:33 -0800906 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800907 if (data == 0) die("cannot load bootable image");
908 fb_queue_flash(pname, data, sz);
909 } else if(!strcmp(*argv, "flashall")) {
910 skip(1);
911 do_flashall();
912 wants_reboot = 1;
913 } else if(!strcmp(*argv, "update")) {
914 if (argc > 1) {
915 do_update(argv[1]);
916 skip(2);
917 } else {
918 do_update("update.zip");
919 skip(1);
920 }
921 wants_reboot = 1;
922 } else if(!strcmp(*argv, "oem")) {
923 argc = do_oem_command(argc, argv);
Colin Cross8879f982012-05-22 17:53:34 -0700924 } else if (!strcmp(*argv, "help")) {
925 usage();
926 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 } else {
928 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800929 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 }
931 }
932
933 if (wants_wipe) {
934 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -0700935 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -0700937 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 }
939 if (wants_reboot) {
940 fb_queue_reboot();
941 } else if (wants_reboot_bootloader) {
942 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
943 }
944
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700945 status = fb_execute_queue(usb);
946 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800947}