blob: dc9bfa4bb700d5a0963950ee9513293a8bd7ba78 [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;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800161 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
163 data = 0;
164 fd = open(fn, O_RDONLY);
165 if(fd < 0) return 0;
166
167 sz = lseek(fd, 0, SEEK_END);
168 if(sz < 0) goto oops;
169
170 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
171
172 data = (char*) malloc(sz);
173 if(data == 0) goto oops;
174
175 if(read(fd, data, sz) != sz) goto oops;
176 close(fd);
177
178 if(_sz) *_sz = sz;
179 return data;
180
181oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 close(fd);
184 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800185 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 return 0;
187}
188#endif
189
190int match_fastboot(usb_ifc_info *info)
191{
192 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400193 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800194 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700195 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800196 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700197 (info->dev_vendor != 0x0fce) && // Sony Ericsson
198 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400199 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800200 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800201 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800202 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800203 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400204 (info->dev_vendor != 0x0bb4)) // HTC
205 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 if(info->ifc_class != 0xff) return -1;
207 if(info->ifc_subclass != 0x42) return -1;
208 if(info->ifc_protocol != 0x03) return -1;
209 // require matching serial number if a serial number is specified
210 // at the command line with the -s option.
211 if (serial && strcmp(serial, info->serial_number) != 0) return -1;
212 return 0;
213}
214
215int list_devices_callback(usb_ifc_info *info)
216{
217 if (match_fastboot(info) == 0) {
218 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 if (!info->writable) {
220 serial = "no permissions"; // like "adb devices"
221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if (!serial[0]) {
223 serial = "????????????";
224 }
225 // output compatible with "adb devices"
226 printf("%s\tfastboot\n", serial);
227 }
228
229 return -1;
230}
231
232usb_handle *open_device(void)
233{
234 static usb_handle *usb = 0;
235 int announce = 1;
236
237 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800238
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 for(;;) {
240 usb = usb_open(match_fastboot);
241 if(usb) return usb;
242 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800243 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 fprintf(stderr,"< waiting for device >\n");
245 }
246 sleep(1);
247 }
248}
249
250void list_devices(void) {
251 // We don't actually open a USB device here,
252 // just getting our callback called so we can
253 // list all the connected devices.
254 usb_open(list_devices_callback);
255}
256
257void usage(void)
258{
259 fprintf(stderr,
260/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
261 "usage: fastboot [ <option> ] <command>\n"
262 "\n"
263 "commands:\n"
264 " update <filename> reflash device from update.zip\n"
265 " flashall flash boot + recovery + system\n"
266 " flash <partition> [ <filename> ] write a file to a flash partition\n"
267 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800268 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 " getvar <variable> display a bootloader variable\n"
270 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
271 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
272 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700273 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 " reboot reboot device normally\n"
275 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800276 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 "\n"
278 "options:\n"
279 " -w erase userdata and cache\n"
280 " -s <serial number> specify device serial number\n"
281 " -p <product> specify product name\n"
282 " -c <cmdline> override kernel commandline\n"
283 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700284 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800285 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700286 " -S <size>[K|M|G] automatically sparse files greater than\n"
287 " size. default: 256M, 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289}
290
Dima Zavin931175a2010-02-12 20:26:33 -0800291void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 unsigned *sz, const char *cmdline)
293{
294 void *kdata = 0, *rdata = 0;
295 unsigned ksize = 0, rsize = 0;
296 void *bdata;
297 unsigned bsize;
298
299 if(kernel == 0) {
300 fprintf(stderr, "no image specified\n");
301 return 0;
302 }
303
304 kdata = load_file(kernel, &ksize);
305 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800306 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 return 0;
308 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800309
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 /* is this actually a boot image? */
311 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
312 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800313
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 if(ramdisk) {
315 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
316 return 0;
317 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800318
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 *sz = ksize;
320 return kdata;
321 }
322
323 if(ramdisk) {
324 rdata = load_file(ramdisk, &rsize);
325 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800326 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 return 0;
328 }
329 }
330
331 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800332 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 if(bdata == 0) {
334 fprintf(stderr,"failed to create boot.img\n");
335 return 0;
336 }
337 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
338 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
339 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 return bdata;
342}
343
344void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
345{
346 void *data;
347 zipentry_t entry;
348 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800349
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 entry = lookup_zipentry(zip, name);
351 if (entry == NULL) {
352 fprintf(stderr, "archive does not contain '%s'\n", name);
353 return 0;
354 }
355
356 *sz = get_zipentry_size(entry);
357
358 datasz = *sz * 1.001;
359 data = malloc(datasz);
360
361 if(data == 0) {
362 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
363 return 0;
364 }
365
366 if (decompress_zipentry(entry, data, datasz)) {
367 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
368 free(data);
369 return 0;
370 }
371
372 return data;
373}
374
375static char *strip(char *s)
376{
377 int n;
378 while(*s && isspace(*s)) s++;
379 n = strlen(s);
380 while(n-- > 0) {
381 if(!isspace(s[n])) break;
382 s[n] = 0;
383 }
384 return s;
385}
386
387#define MAX_OPTIONS 32
388static int setup_requirement_line(char *name)
389{
390 char *val[MAX_OPTIONS];
391 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700392 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 unsigned n, count;
394 char *x;
395 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800396
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 if (!strncmp(name, "reject ", 7)) {
398 name += 7;
399 invert = 1;
400 } else if (!strncmp(name, "require ", 8)) {
401 name += 8;
402 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700403 } else if (!strncmp(name, "require-for-product:", 20)) {
404 // Get the product and point name past it
405 prod = name + 20;
406 name = strchr(name, ' ');
407 if (!name) return -1;
408 *name = 0;
409 name += 1;
410 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 }
412
413 x = strchr(name, '=');
414 if (x == 0) return 0;
415 *x = 0;
416 val[0] = x + 1;
417
418 for(count = 1; count < MAX_OPTIONS; count++) {
419 x = strchr(val[count - 1],'|');
420 if (x == 0) break;
421 *x = 0;
422 val[count] = x + 1;
423 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800424
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 name = strip(name);
426 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800427
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428 name = strip(name);
429 if (name == 0) return -1;
430
431 /* work around an unfortunate name mismatch */
432 if (!strcmp(name,"board")) name = "product";
433
434 out = malloc(sizeof(char*) * count);
435 if (out == 0) return -1;
436
437 for(n = 0; n < count; n++) {
438 out[n] = strdup(strip(val[n]));
439 if (out[n] == 0) return -1;
440 }
441
Wink Savilleb98762f2011-04-04 17:54:59 -0700442 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 return 0;
444}
445
446static void setup_requirements(char *data, unsigned sz)
447{
448 char *s;
449
450 s = data;
451 while (sz-- > 0) {
452 if(*s == '\n') {
453 *s++ = 0;
454 if (setup_requirement_line(data)) {
455 die("out of memory");
456 }
457 data = s;
458 } else {
459 s++;
460 }
461 }
462}
463
464void queue_info_dump(void)
465{
466 fb_queue_notice("--------------------------------------------");
467 fb_queue_display("version-bootloader", "Bootloader Version...");
468 fb_queue_display("version-baseband", "Baseband Version.....");
469 fb_queue_display("serialno", "Serial Number........");
470 fb_queue_notice("--------------------------------------------");
471}
472
Colin Crossf8387882012-05-24 17:18:41 -0700473
474struct sparse_file **load_sparse_files(const char *fname, int max_size)
475{
476 int fd;
477 struct sparse_file *s;
478 int files;
479 struct sparse_file **out_s;
480
481 fd = open(fname, O_RDONLY | O_BINARY);
482 if (fd < 0) {
483 die("cannot open '%s'\n", fname);
484 }
485
486 s = sparse_file_import_auto(fd, false);
487 if (!s) {
488 die("cannot sparse read file '%s'\n", fname);
489 }
490
491 files = sparse_file_resparse(s, max_size, NULL, 0);
492 if (files < 0) {
493 die("Failed to resparse '%s'\n", fname);
494 }
495
496 out_s = calloc(sizeof(struct sparse_file *), files + 1);
497 if (!out_s) {
498 die("Failed to allocate sparse file array\n");
499 }
500
501 files = sparse_file_resparse(s, max_size, out_s, files);
502 if (files < 0) {
503 die("Failed to resparse '%s'\n", fname);
504 }
505
506 return out_s;
507}
508
509static int64_t get_target_sparse_limit(struct usb_handle *usb)
510{
511 int64_t limit = 0;
512 char response[FB_RESPONSE_SZ + 1];
513 int status = fb_getvar(usb, response, "max-download-size");
514
515 if (!status) {
516 limit = strtoul(response, NULL, 0);
517 if (limit > 0) {
518 fprintf(stderr, "target reported max download size of %lld bytes\n",
519 limit);
520 }
521 }
522
523 return limit;
524}
525
526static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
527{
528 int64_t limit;
529
530 if (sparse_limit == 0) {
531 return 0;
532 } else if (sparse_limit > 0) {
533 limit = sparse_limit;
534 } else {
535 if (target_sparse_limit == -1) {
536 target_sparse_limit = get_target_sparse_limit(usb);
537 }
538 if (target_sparse_limit > 0) {
539 limit = target_sparse_limit;
540 } else {
541 limit = DEFAULT_SPARSE_LIMIT;
542 }
543 }
544
545 if (size > limit) {
546 return limit;
547 }
548
549 return 0;
550}
551
552void do_flash(usb_handle *usb, const char *pname, const char *fname)
553{
554 int64_t sz64;
555 void *data;
556 int64_t limit;
557
558 sz64 = file_size(fname);
559 limit = get_sparse_limit(usb, sz64);
560 if (limit) {
561 struct sparse_file **s = load_sparse_files(fname, limit);
562 if (s == NULL) {
563 die("cannot sparse load '%s'\n", fname);
564 }
565 while (*s) {
566 sz64 = sparse_file_len(*s, true, false);
567 fb_queue_flash_sparse(pname, *s++, sz64);
568 }
569 } else {
570 unsigned int sz;
571 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800572 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700573 fb_queue_flash(pname, data, sz);
574 }
575}
576
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577void do_update_signature(zipfile_t zip, char *fn)
578{
579 void *data;
580 unsigned sz;
581 data = unzip_file(zip, fn, &sz);
582 if (data == 0) return;
583 fb_queue_download("signature", data, sz);
584 fb_queue_command("signature", "installing signature");
585}
586
587void do_update(char *fn)
588{
589 void *zdata;
590 unsigned zsize;
591 void *data;
592 unsigned sz;
593 zipfile_t zip;
594
595 queue_info_dump();
596
Wink Savilleb98762f2011-04-04 17:54:59 -0700597 fb_queue_query_save("product", cur_product, sizeof(cur_product));
598
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800600 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800601
602 zip = init_zipfile(zdata, zsize);
603 if(zip == 0) die("failed to access zipdata in '%s'");
604
605 data = unzip_file(zip, "android-info.txt", &sz);
606 if (data == 0) {
607 char *tmp;
608 /* fallback for older zipfiles */
609 data = unzip_file(zip, "android-product.txt", &sz);
610 if ((data == 0) || (sz < 1)) {
611 die("update package has no android-info.txt or android-product.txt");
612 }
613 tmp = malloc(sz + 128);
614 if (tmp == 0) die("out of memory");
615 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
616 data = tmp;
617 sz = strlen(tmp);
618 }
619
620 setup_requirements(data, sz);
621
622 data = unzip_file(zip, "boot.img", &sz);
623 if (data == 0) die("update package missing boot.img");
624 do_update_signature(zip, "boot.sig");
625 fb_queue_flash("boot", data, sz);
626
627 data = unzip_file(zip, "recovery.img", &sz);
628 if (data != 0) {
629 do_update_signature(zip, "recovery.sig");
630 fb_queue_flash("recovery", data, sz);
631 }
632
633 data = unzip_file(zip, "system.img", &sz);
634 if (data == 0) die("update package missing system.img");
635 do_update_signature(zip, "system.sig");
636 fb_queue_flash("system", data, sz);
637}
638
639void do_send_signature(char *fn)
640{
641 void *data;
642 unsigned sz;
643 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800644
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800645 xtn = strrchr(fn, '.');
646 if (!xtn) return;
647 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800648
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 strcpy(xtn,".sig");
650 data = load_file(fn, &sz);
651 strcpy(xtn,".img");
652 if (data == 0) return;
653 fb_queue_download("signature", data, sz);
654 fb_queue_command("signature", "installing signature");
655}
656
657void do_flashall(void)
658{
659 char *fname;
660 void *data;
661 unsigned sz;
662
663 queue_info_dump();
664
Wink Savilleb98762f2011-04-04 17:54:59 -0700665 fb_queue_query_save("product", cur_product, sizeof(cur_product));
666
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 fname = find_item("info", product);
668 if (fname == 0) die("cannot find android-info.txt");
669 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800670 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 setup_requirements(data, sz);
672
673 fname = find_item("boot", product);
674 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800675 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 do_send_signature(fname);
677 fb_queue_flash("boot", data, sz);
678
679 fname = find_item("recovery", product);
680 data = load_file(fname, &sz);
681 if (data != 0) {
682 do_send_signature(fname);
683 fb_queue_flash("recovery", data, sz);
684 }
685
686 fname = find_item("system", product);
687 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800688 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800690 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691}
692
693#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800694#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695
696int do_oem_command(int argc, char **argv)
697{
698 int i;
699 char command[256];
700 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800701
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 command[0] = 0;
703 while(1) {
704 strcat(command,*argv);
705 skip(1);
706 if(argc == 0) break;
707 strcat(command," ");
708 }
709
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800710 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 return 0;
712}
713
Colin Crossf8387882012-05-24 17:18:41 -0700714static int64_t parse_num(const char *arg)
715{
716 char *endptr;
717 unsigned long long num;
718
719 num = strtoull(arg, &endptr, 0);
720 if (endptr == arg) {
721 return -1;
722 }
723
724 if (*endptr == 'k' || *endptr == 'K') {
725 if (num >= (-1ULL) / 1024) {
726 return -1;
727 }
728 num *= 1024LL;
729 endptr++;
730 } else if (*endptr == 'm' || *endptr == 'M') {
731 if (num >= (-1ULL) / (1024 * 1024)) {
732 return -1;
733 }
734 num *= 1024LL * 1024LL;
735 endptr++;
736 } else if (*endptr == 'g' || *endptr == 'G') {
737 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
738 return -1;
739 }
740 num *= 1024LL * 1024LL * 1024LL;
741 endptr++;
742 }
743
744 if (*endptr != '\0') {
745 return -1;
746 }
747
748 if (num > INT64_MAX) {
749 return -1;
750 }
751
752 return num;
753}
754
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755int main(int argc, char **argv)
756{
757 int wants_wipe = 0;
758 int wants_reboot = 0;
759 int wants_reboot_bootloader = 0;
760 void *data;
761 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800762 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700763 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700764 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700765 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766
Colin Crossf8387882012-05-24 17:18:41 -0700767 const struct option longopts = { 0, 0, 0, 0 };
Colin Cross8879f982012-05-22 17:53:34 -0700768
769 serial = getenv("ANDROID_SERIAL");
770
771 while (1) {
Colin Crossf8387882012-05-24 17:18:41 -0700772 c = getopt_long(argc, argv, "wb:n:s:S:p:c:i:m:h", &longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700773 if (c < 0) {
774 break;
775 }
776
777 switch (c) {
778 case 'w':
779 wants_wipe = 1;
780 break;
781 case 'b':
782 base_addr = strtoul(optarg, 0, 16);
783 break;
784 case 'n':
785 page_size = (unsigned)strtoul(optarg, NULL, 0);
786 if (!page_size) die("invalid page size");
787 break;
788 case 's':
789 serial = optarg;
790 break;
Colin Crossf8387882012-05-24 17:18:41 -0700791 case 'S':
792 sparse_limit = parse_num(optarg);
793 if (sparse_limit < 0) {
794 die("invalid sparse limit");
795 }
796 break;
Colin Cross8879f982012-05-22 17:53:34 -0700797 case 'p':
798 product = optarg;
799 break;
800 case 'c':
801 cmdline = optarg;
802 break;
803 case 'i': {
804 char *endptr = NULL;
805 unsigned long val;
806
807 val = strtoul(optarg, &endptr, 0);
808 if (!endptr || *endptr != '\0' || (val & ~0xffff))
809 die("invalid vendor id '%s'", optarg);
810 vendor_id = (unsigned short)val;
811 break;
812 }
813 case 'h':
814 usage();
815 return 1;
816 case '?':
817 return 1;
818 default:
819 abort();
820 }
821 }
822
823 argc -= optind;
824 argv += optind;
825
826 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700828 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 }
830
831 if (!strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700832 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833 list_devices();
834 return 0;
835 }
836
Colin Cross8879f982012-05-22 17:53:34 -0700837 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700838
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800839 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700840 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800841 require(2);
842 fb_queue_display(argv[1], argv[1]);
843 skip(2);
844 } else if(!strcmp(*argv, "erase")) {
845 require(2);
846 fb_queue_erase(argv[1]);
847 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800848 } else if(!strcmp(*argv, "format")) {
849 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700850 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800851 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 } else if(!strcmp(*argv, "signature")) {
853 require(2);
854 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800855 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 if (sz != 256) die("signature must be 256 bytes");
857 fb_queue_download("signature", data, sz);
858 fb_queue_command("signature", "installing signature");
859 skip(2);
860 } else if(!strcmp(*argv, "reboot")) {
861 wants_reboot = 1;
862 skip(1);
863 } else if(!strcmp(*argv, "reboot-bootloader")) {
864 wants_reboot_bootloader = 1;
865 skip(1);
866 } else if (!strcmp(*argv, "continue")) {
867 fb_queue_command("continue", "resuming boot");
868 skip(1);
869 } else if(!strcmp(*argv, "boot")) {
870 char *kname = 0;
871 char *rname = 0;
872 skip(1);
873 if (argc > 0) {
874 kname = argv[0];
875 skip(1);
876 }
877 if (argc > 0) {
878 rname = argv[0];
879 skip(1);
880 }
Dima Zavin931175a2010-02-12 20:26:33 -0800881 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 if (data == 0) return 1;
883 fb_queue_download("boot.img", data, sz);
884 fb_queue_command("boot", "booting");
885 } else if(!strcmp(*argv, "flash")) {
886 char *pname = argv[1];
887 char *fname = 0;
888 require(2);
889 if (argc > 2) {
890 fname = argv[2];
891 skip(3);
892 } else {
893 fname = find_item(pname, product);
894 skip(2);
895 }
896 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Colin Crossf8387882012-05-24 17:18:41 -0700897 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800898 } else if(!strcmp(*argv, "flash:raw")) {
899 char *pname = argv[1];
900 char *kname = argv[2];
901 char *rname = 0;
902 require(3);
903 if(argc > 3) {
904 rname = argv[3];
905 skip(4);
906 } else {
907 skip(3);
908 }
Dima Zavin931175a2010-02-12 20:26:33 -0800909 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800910 if (data == 0) die("cannot load bootable image");
911 fb_queue_flash(pname, data, sz);
912 } else if(!strcmp(*argv, "flashall")) {
913 skip(1);
914 do_flashall();
915 wants_reboot = 1;
916 } else if(!strcmp(*argv, "update")) {
917 if (argc > 1) {
918 do_update(argv[1]);
919 skip(2);
920 } else {
921 do_update("update.zip");
922 skip(1);
923 }
924 wants_reboot = 1;
925 } else if(!strcmp(*argv, "oem")) {
926 argc = do_oem_command(argc, argv);
Colin Cross8879f982012-05-22 17:53:34 -0700927 } else if (!strcmp(*argv, "help")) {
928 usage();
929 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 } else {
931 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800932 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933 }
934 }
935
936 if (wants_wipe) {
937 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -0700938 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800939 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -0700940 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800941 }
942 if (wants_reboot) {
943 fb_queue_reboot();
944 } else if (wants_reboot_bootloader) {
945 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
946 }
947
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700948 status = fb_execute_queue(usb);
949 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800950}