blob: c9def7b79a36c7b1ded0b983fc43b7aedc152149 [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
Wink Savilleb98762f2011-04-04 17:54:59 -070057char cur_product[FB_RESPONSE_SZ + 1];
58
Brian Swetland2a63bb72009-04-28 16:05:07 -070059void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
60
61boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size,
62 void *ramdisk, unsigned ramdisk_size,
63 void *second, unsigned second_size,
64 unsigned page_size, unsigned base,
65 unsigned *bootimg_size);
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067static usb_handle *usb = 0;
68static const char *serial = 0;
69static const char *product = 0;
70static const char *cmdline = 0;
71static int wipe_data = 0;
72static unsigned short vendor_id = 0;
Colin Crossf8387882012-05-24 17:18:41 -070073static int64_t sparse_limit = -1;
74static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Brian Swetland2a63bb72009-04-28 16:05:07 -070076static unsigned base_addr = 0x10000000;
77
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078void die(const char *fmt, ...)
79{
80 va_list ap;
81 va_start(ap, fmt);
82 fprintf(stderr,"error: ");
83 vfprintf(stderr, fmt, ap);
84 fprintf(stderr,"\n");
85 va_end(ap);
86 exit(1);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080087}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
89void get_my_path(char *path);
90
91char *find_item(const char *item, const char *product)
92{
93 char *dir;
94 char *fn;
95 char path[PATH_MAX + 128];
96
97 if(!strcmp(item,"boot")) {
98 fn = "boot.img";
99 } else if(!strcmp(item,"recovery")) {
100 fn = "recovery.img";
101 } else if(!strcmp(item,"system")) {
102 fn = "system.img";
103 } else if(!strcmp(item,"userdata")) {
104 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700105 } else if(!strcmp(item,"cache")) {
106 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 } else if(!strcmp(item,"info")) {
108 fn = "android-info.txt";
109 } else {
110 fprintf(stderr,"unknown partition '%s'\n", item);
111 return 0;
112 }
113
114 if(product) {
115 get_my_path(path);
116 sprintf(path + strlen(path),
117 "../../../target/product/%s/%s", product, fn);
118 return strdup(path);
119 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 dir = getenv("ANDROID_PRODUCT_OUT");
122 if((dir == 0) || (dir[0] == 0)) {
123 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
124 return 0;
125 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800126
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 sprintf(path, "%s/%s", dir, fn);
128 return strdup(path);
129}
130
131#ifdef _WIN32
132void *load_file(const char *fn, unsigned *_sz);
Colin Crossf8387882012-05-24 17:18:41 -0700133int64_t file_size(const char *fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134#else
Colin Crossf8387882012-05-24 17:18:41 -0700135#if defined(__APPLE__) && defined(__MACH__)
136#define lseek64 lseek
137#define off64_t off_t
138#endif
139
140int64_t file_size(const char *fn)
141{
142 off64_t off;
143 int fd;
144
145 fd = open(fn, O_RDONLY);
146 if (fd < 0) return -1;
147
148 off = lseek64(fd, 0, SEEK_END);
149 close(fd);
150
151 return off;
152}
153
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154void *load_file(const char *fn, unsigned *_sz)
155{
156 char *data;
157 int sz;
158 int fd;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800159 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
161 data = 0;
162 fd = open(fn, O_RDONLY);
163 if(fd < 0) return 0;
164
165 sz = lseek(fd, 0, SEEK_END);
166 if(sz < 0) goto oops;
167
168 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
169
170 data = (char*) malloc(sz);
171 if(data == 0) goto oops;
172
173 if(read(fd, data, sz) != sz) goto oops;
174 close(fd);
175
176 if(_sz) *_sz = sz;
177 return data;
178
179oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800180 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 close(fd);
182 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 return 0;
185}
186#endif
187
188int match_fastboot(usb_ifc_info *info)
189{
190 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400191 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800192 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700193 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800194 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700195 (info->dev_vendor != 0x0fce) && // Sony Ericsson
196 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400197 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800198 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800199 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800200 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800201 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400202 (info->dev_vendor != 0x0bb4)) // HTC
203 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 if(info->ifc_class != 0xff) return -1;
205 if(info->ifc_subclass != 0x42) return -1;
206 if(info->ifc_protocol != 0x03) return -1;
207 // require matching serial number if a serial number is specified
208 // at the command line with the -s option.
209 if (serial && strcmp(serial, info->serial_number) != 0) return -1;
210 return 0;
211}
212
213int list_devices_callback(usb_ifc_info *info)
214{
215 if (match_fastboot(info) == 0) {
216 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700217 if (!info->writable) {
218 serial = "no permissions"; // like "adb devices"
219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if (!serial[0]) {
221 serial = "????????????";
222 }
223 // output compatible with "adb devices"
224 printf("%s\tfastboot\n", serial);
225 }
226
227 return -1;
228}
229
230usb_handle *open_device(void)
231{
232 static usb_handle *usb = 0;
233 int announce = 1;
234
235 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800236
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 for(;;) {
238 usb = usb_open(match_fastboot);
239 if(usb) return usb;
240 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800241 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 fprintf(stderr,"< waiting for device >\n");
243 }
244 sleep(1);
245 }
246}
247
248void list_devices(void) {
249 // We don't actually open a USB device here,
250 // just getting our callback called so we can
251 // list all the connected devices.
252 usb_open(list_devices_callback);
253}
254
255void usage(void)
256{
257 fprintf(stderr,
258/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
259 "usage: fastboot [ <option> ] <command>\n"
260 "\n"
261 "commands:\n"
262 " update <filename> reflash device from update.zip\n"
263 " flashall flash boot + recovery + system\n"
264 " flash <partition> [ <filename> ] write a file to a flash partition\n"
265 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800266 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 " getvar <variable> display a bootloader variable\n"
268 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
269 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
270 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700271 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 " reboot reboot device normally\n"
273 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800274 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 "\n"
276 "options:\n"
277 " -w erase userdata and cache\n"
278 " -s <serial number> specify device serial number\n"
279 " -p <product> specify product name\n"
280 " -c <cmdline> override kernel commandline\n"
281 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700282 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800283 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700284 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700285 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287}
288
Dima Zavin931175a2010-02-12 20:26:33 -0800289void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 unsigned *sz, const char *cmdline)
291{
292 void *kdata = 0, *rdata = 0;
293 unsigned ksize = 0, rsize = 0;
294 void *bdata;
295 unsigned bsize;
296
297 if(kernel == 0) {
298 fprintf(stderr, "no image specified\n");
299 return 0;
300 }
301
302 kdata = load_file(kernel, &ksize);
303 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800304 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 return 0;
306 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800307
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 /* is this actually a boot image? */
309 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
310 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800311
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 if(ramdisk) {
313 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
314 return 0;
315 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800316
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 *sz = ksize;
318 return kdata;
319 }
320
321 if(ramdisk) {
322 rdata = load_file(ramdisk, &rsize);
323 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800324 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 return 0;
326 }
327 }
328
329 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800330 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 if(bdata == 0) {
332 fprintf(stderr,"failed to create boot.img\n");
333 return 0;
334 }
335 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
336 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
337 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800338
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 return bdata;
340}
341
342void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
343{
344 void *data;
345 zipentry_t entry;
346 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800347
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 entry = lookup_zipentry(zip, name);
349 if (entry == NULL) {
350 fprintf(stderr, "archive does not contain '%s'\n", name);
351 return 0;
352 }
353
354 *sz = get_zipentry_size(entry);
355
356 datasz = *sz * 1.001;
357 data = malloc(datasz);
358
359 if(data == 0) {
360 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
361 return 0;
362 }
363
364 if (decompress_zipentry(entry, data, datasz)) {
365 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
366 free(data);
367 return 0;
368 }
369
370 return data;
371}
372
373static char *strip(char *s)
374{
375 int n;
376 while(*s && isspace(*s)) s++;
377 n = strlen(s);
378 while(n-- > 0) {
379 if(!isspace(s[n])) break;
380 s[n] = 0;
381 }
382 return s;
383}
384
385#define MAX_OPTIONS 32
386static int setup_requirement_line(char *name)
387{
388 char *val[MAX_OPTIONS];
389 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700390 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 unsigned n, count;
392 char *x;
393 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800394
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 if (!strncmp(name, "reject ", 7)) {
396 name += 7;
397 invert = 1;
398 } else if (!strncmp(name, "require ", 8)) {
399 name += 8;
400 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700401 } else if (!strncmp(name, "require-for-product:", 20)) {
402 // Get the product and point name past it
403 prod = name + 20;
404 name = strchr(name, ' ');
405 if (!name) return -1;
406 *name = 0;
407 name += 1;
408 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 }
410
411 x = strchr(name, '=');
412 if (x == 0) return 0;
413 *x = 0;
414 val[0] = x + 1;
415
416 for(count = 1; count < MAX_OPTIONS; count++) {
417 x = strchr(val[count - 1],'|');
418 if (x == 0) break;
419 *x = 0;
420 val[count] = x + 1;
421 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800422
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 name = strip(name);
424 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800425
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 name = strip(name);
427 if (name == 0) return -1;
428
429 /* work around an unfortunate name mismatch */
430 if (!strcmp(name,"board")) name = "product";
431
432 out = malloc(sizeof(char*) * count);
433 if (out == 0) return -1;
434
435 for(n = 0; n < count; n++) {
436 out[n] = strdup(strip(val[n]));
437 if (out[n] == 0) return -1;
438 }
439
Wink Savilleb98762f2011-04-04 17:54:59 -0700440 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 return 0;
442}
443
444static void setup_requirements(char *data, unsigned sz)
445{
446 char *s;
447
448 s = data;
449 while (sz-- > 0) {
450 if(*s == '\n') {
451 *s++ = 0;
452 if (setup_requirement_line(data)) {
453 die("out of memory");
454 }
455 data = s;
456 } else {
457 s++;
458 }
459 }
460}
461
462void queue_info_dump(void)
463{
464 fb_queue_notice("--------------------------------------------");
465 fb_queue_display("version-bootloader", "Bootloader Version...");
466 fb_queue_display("version-baseband", "Baseband Version.....");
467 fb_queue_display("serialno", "Serial Number........");
468 fb_queue_notice("--------------------------------------------");
469}
470
Colin Crossf8387882012-05-24 17:18:41 -0700471
472struct sparse_file **load_sparse_files(const char *fname, int max_size)
473{
474 int fd;
475 struct sparse_file *s;
476 int files;
477 struct sparse_file **out_s;
478
479 fd = open(fname, O_RDONLY | O_BINARY);
480 if (fd < 0) {
481 die("cannot open '%s'\n", fname);
482 }
483
484 s = sparse_file_import_auto(fd, false);
485 if (!s) {
486 die("cannot sparse read file '%s'\n", fname);
487 }
488
489 files = sparse_file_resparse(s, max_size, NULL, 0);
490 if (files < 0) {
491 die("Failed to resparse '%s'\n", fname);
492 }
493
494 out_s = calloc(sizeof(struct sparse_file *), files + 1);
495 if (!out_s) {
496 die("Failed to allocate sparse file array\n");
497 }
498
499 files = sparse_file_resparse(s, max_size, out_s, files);
500 if (files < 0) {
501 die("Failed to resparse '%s'\n", fname);
502 }
503
504 return out_s;
505}
506
507static int64_t get_target_sparse_limit(struct usb_handle *usb)
508{
509 int64_t limit = 0;
510 char response[FB_RESPONSE_SZ + 1];
511 int status = fb_getvar(usb, response, "max-download-size");
512
513 if (!status) {
514 limit = strtoul(response, NULL, 0);
515 if (limit > 0) {
516 fprintf(stderr, "target reported max download size of %lld bytes\n",
517 limit);
518 }
519 }
520
521 return limit;
522}
523
524static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
525{
526 int64_t limit;
527
528 if (sparse_limit == 0) {
529 return 0;
530 } else if (sparse_limit > 0) {
531 limit = sparse_limit;
532 } else {
533 if (target_sparse_limit == -1) {
534 target_sparse_limit = get_target_sparse_limit(usb);
535 }
536 if (target_sparse_limit > 0) {
537 limit = target_sparse_limit;
538 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700539 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700540 }
541 }
542
543 if (size > limit) {
544 return limit;
545 }
546
547 return 0;
548}
549
550void do_flash(usb_handle *usb, const char *pname, const char *fname)
551{
552 int64_t sz64;
553 void *data;
554 int64_t limit;
555
556 sz64 = file_size(fname);
557 limit = get_sparse_limit(usb, sz64);
558 if (limit) {
559 struct sparse_file **s = load_sparse_files(fname, limit);
560 if (s == NULL) {
561 die("cannot sparse load '%s'\n", fname);
562 }
563 while (*s) {
564 sz64 = sparse_file_len(*s, true, false);
565 fb_queue_flash_sparse(pname, *s++, sz64);
566 }
567 } else {
568 unsigned int sz;
569 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800570 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700571 fb_queue_flash(pname, data, sz);
572 }
573}
574
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575void do_update_signature(zipfile_t zip, char *fn)
576{
577 void *data;
578 unsigned sz;
579 data = unzip_file(zip, fn, &sz);
580 if (data == 0) return;
581 fb_queue_download("signature", data, sz);
582 fb_queue_command("signature", "installing signature");
583}
584
585void do_update(char *fn)
586{
587 void *zdata;
588 unsigned zsize;
589 void *data;
590 unsigned sz;
591 zipfile_t zip;
592
593 queue_info_dump();
594
Wink Savilleb98762f2011-04-04 17:54:59 -0700595 fb_queue_query_save("product", cur_product, sizeof(cur_product));
596
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800598 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599
600 zip = init_zipfile(zdata, zsize);
601 if(zip == 0) die("failed to access zipdata in '%s'");
602
603 data = unzip_file(zip, "android-info.txt", &sz);
604 if (data == 0) {
605 char *tmp;
606 /* fallback for older zipfiles */
607 data = unzip_file(zip, "android-product.txt", &sz);
608 if ((data == 0) || (sz < 1)) {
609 die("update package has no android-info.txt or android-product.txt");
610 }
611 tmp = malloc(sz + 128);
612 if (tmp == 0) die("out of memory");
613 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
614 data = tmp;
615 sz = strlen(tmp);
616 }
617
618 setup_requirements(data, sz);
619
620 data = unzip_file(zip, "boot.img", &sz);
621 if (data == 0) die("update package missing boot.img");
622 do_update_signature(zip, "boot.sig");
623 fb_queue_flash("boot", data, sz);
624
625 data = unzip_file(zip, "recovery.img", &sz);
626 if (data != 0) {
627 do_update_signature(zip, "recovery.sig");
628 fb_queue_flash("recovery", data, sz);
629 }
630
631 data = unzip_file(zip, "system.img", &sz);
632 if (data == 0) die("update package missing system.img");
633 do_update_signature(zip, "system.sig");
634 fb_queue_flash("system", data, sz);
635}
636
637void do_send_signature(char *fn)
638{
639 void *data;
640 unsigned sz;
641 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800642
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643 xtn = strrchr(fn, '.');
644 if (!xtn) return;
645 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800646
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647 strcpy(xtn,".sig");
648 data = load_file(fn, &sz);
649 strcpy(xtn,".img");
650 if (data == 0) return;
651 fb_queue_download("signature", data, sz);
652 fb_queue_command("signature", "installing signature");
653}
654
655void do_flashall(void)
656{
657 char *fname;
658 void *data;
659 unsigned sz;
660
661 queue_info_dump();
662
Wink Savilleb98762f2011-04-04 17:54:59 -0700663 fb_queue_query_save("product", cur_product, sizeof(cur_product));
664
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 fname = find_item("info", product);
666 if (fname == 0) die("cannot find android-info.txt");
667 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800668 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800669 setup_requirements(data, sz);
670
671 fname = find_item("boot", product);
672 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800673 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 do_send_signature(fname);
675 fb_queue_flash("boot", data, sz);
676
677 fname = find_item("recovery", product);
678 data = load_file(fname, &sz);
679 if (data != 0) {
680 do_send_signature(fname);
681 fb_queue_flash("recovery", data, sz);
682 }
683
684 fname = find_item("system", product);
685 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800686 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800688 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689}
690
691#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800692#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693
694int do_oem_command(int argc, char **argv)
695{
696 int i;
697 char command[256];
698 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800699
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700 command[0] = 0;
701 while(1) {
702 strcat(command,*argv);
703 skip(1);
704 if(argc == 0) break;
705 strcat(command," ");
706 }
707
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800708 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 return 0;
710}
711
Colin Crossf8387882012-05-24 17:18:41 -0700712static int64_t parse_num(const char *arg)
713{
714 char *endptr;
715 unsigned long long num;
716
717 num = strtoull(arg, &endptr, 0);
718 if (endptr == arg) {
719 return -1;
720 }
721
722 if (*endptr == 'k' || *endptr == 'K') {
723 if (num >= (-1ULL) / 1024) {
724 return -1;
725 }
726 num *= 1024LL;
727 endptr++;
728 } else if (*endptr == 'm' || *endptr == 'M') {
729 if (num >= (-1ULL) / (1024 * 1024)) {
730 return -1;
731 }
732 num *= 1024LL * 1024LL;
733 endptr++;
734 } else if (*endptr == 'g' || *endptr == 'G') {
735 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
736 return -1;
737 }
738 num *= 1024LL * 1024LL * 1024LL;
739 endptr++;
740 }
741
742 if (*endptr != '\0') {
743 return -1;
744 }
745
746 if (num > INT64_MAX) {
747 return -1;
748 }
749
750 return num;
751}
752
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753int main(int argc, char **argv)
754{
755 int wants_wipe = 0;
756 int wants_reboot = 0;
757 int wants_reboot_bootloader = 0;
758 void *data;
759 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800760 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700761 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700762 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700763 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800764
Colin Crossf8387882012-05-24 17:18:41 -0700765 const struct option longopts = { 0, 0, 0, 0 };
Colin Cross8879f982012-05-22 17:53:34 -0700766
767 serial = getenv("ANDROID_SERIAL");
768
769 while (1) {
Colin Crossf8387882012-05-24 17:18:41 -0700770 c = getopt_long(argc, argv, "wb:n:s:S:p:c:i:m:h", &longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700771 if (c < 0) {
772 break;
773 }
774
775 switch (c) {
776 case 'w':
777 wants_wipe = 1;
778 break;
779 case 'b':
780 base_addr = strtoul(optarg, 0, 16);
781 break;
782 case 'n':
783 page_size = (unsigned)strtoul(optarg, NULL, 0);
784 if (!page_size) die("invalid page size");
785 break;
786 case 's':
787 serial = optarg;
788 break;
Colin Crossf8387882012-05-24 17:18:41 -0700789 case 'S':
790 sparse_limit = parse_num(optarg);
791 if (sparse_limit < 0) {
792 die("invalid sparse limit");
793 }
794 break;
Colin Cross8879f982012-05-22 17:53:34 -0700795 case 'p':
796 product = optarg;
797 break;
798 case 'c':
799 cmdline = optarg;
800 break;
801 case 'i': {
802 char *endptr = NULL;
803 unsigned long val;
804
805 val = strtoul(optarg, &endptr, 0);
806 if (!endptr || *endptr != '\0' || (val & ~0xffff))
807 die("invalid vendor id '%s'", optarg);
808 vendor_id = (unsigned short)val;
809 break;
810 }
811 case 'h':
812 usage();
813 return 1;
814 case '?':
815 return 1;
816 default:
817 abort();
818 }
819 }
820
821 argc -= optind;
822 argv += optind;
823
824 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700826 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800827 }
828
Colin Cross8fb6e062012-07-24 16:36:41 -0700829 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700830 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 list_devices();
832 return 0;
833 }
834
Colin Cross80f98672012-08-29 18:17:06 -0700835 if (argc > 0 && !strcmp(*argv, "help")) {
836 usage();
837 return 0;
838 }
839
Colin Cross8879f982012-05-22 17:53:34 -0700840 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700841
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700843 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 require(2);
845 fb_queue_display(argv[1], argv[1]);
846 skip(2);
847 } else if(!strcmp(*argv, "erase")) {
848 require(2);
849 fb_queue_erase(argv[1]);
850 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800851 } else if(!strcmp(*argv, "format")) {
852 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700853 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800854 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800855 } else if(!strcmp(*argv, "signature")) {
856 require(2);
857 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800858 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859 if (sz != 256) die("signature must be 256 bytes");
860 fb_queue_download("signature", data, sz);
861 fb_queue_command("signature", "installing signature");
862 skip(2);
863 } else if(!strcmp(*argv, "reboot")) {
864 wants_reboot = 1;
865 skip(1);
866 } else if(!strcmp(*argv, "reboot-bootloader")) {
867 wants_reboot_bootloader = 1;
868 skip(1);
869 } else if (!strcmp(*argv, "continue")) {
870 fb_queue_command("continue", "resuming boot");
871 skip(1);
872 } else if(!strcmp(*argv, "boot")) {
873 char *kname = 0;
874 char *rname = 0;
875 skip(1);
876 if (argc > 0) {
877 kname = argv[0];
878 skip(1);
879 }
880 if (argc > 0) {
881 rname = argv[0];
882 skip(1);
883 }
Dima Zavin931175a2010-02-12 20:26:33 -0800884 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 if (data == 0) return 1;
886 fb_queue_download("boot.img", data, sz);
887 fb_queue_command("boot", "booting");
888 } else if(!strcmp(*argv, "flash")) {
889 char *pname = argv[1];
890 char *fname = 0;
891 require(2);
892 if (argc > 2) {
893 fname = argv[2];
894 skip(3);
895 } else {
896 fname = find_item(pname, product);
897 skip(2);
898 }
899 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Colin Crossf8387882012-05-24 17:18:41 -0700900 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800901 } else if(!strcmp(*argv, "flash:raw")) {
902 char *pname = argv[1];
903 char *kname = argv[2];
904 char *rname = 0;
905 require(3);
906 if(argc > 3) {
907 rname = argv[3];
908 skip(4);
909 } else {
910 skip(3);
911 }
Dima Zavin931175a2010-02-12 20:26:33 -0800912 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800913 if (data == 0) die("cannot load bootable image");
914 fb_queue_flash(pname, data, sz);
915 } else if(!strcmp(*argv, "flashall")) {
916 skip(1);
917 do_flashall();
918 wants_reboot = 1;
919 } else if(!strcmp(*argv, "update")) {
920 if (argc > 1) {
921 do_update(argv[1]);
922 skip(2);
923 } else {
924 do_update("update.zip");
925 skip(1);
926 }
927 wants_reboot = 1;
928 } else if(!strcmp(*argv, "oem")) {
929 argc = do_oem_command(argc, argv);
930 } 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}