blob: 2a63905d5f86551b22b1b7cb9fb1ca2ed5318a4c [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;
Scott Anderson13081c62012-04-06 12:39:30 -070073static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070074static int64_t sparse_limit = -1;
75static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Brian Swetland2a63bb72009-04-28 16:05:07 -070077static unsigned base_addr = 0x10000000;
78
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079void die(const char *fmt, ...)
80{
81 va_list ap;
82 va_start(ap, fmt);
83 fprintf(stderr,"error: ");
84 vfprintf(stderr, fmt, ap);
85 fprintf(stderr,"\n");
86 va_end(ap);
87 exit(1);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080088}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
90void get_my_path(char *path);
91
92char *find_item(const char *item, const char *product)
93{
94 char *dir;
95 char *fn;
96 char path[PATH_MAX + 128];
97
98 if(!strcmp(item,"boot")) {
99 fn = "boot.img";
100 } else if(!strcmp(item,"recovery")) {
101 fn = "recovery.img";
102 } else if(!strcmp(item,"system")) {
103 fn = "system.img";
104 } else if(!strcmp(item,"userdata")) {
105 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700106 } else if(!strcmp(item,"cache")) {
107 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 } else if(!strcmp(item,"info")) {
109 fn = "android-info.txt";
110 } else {
111 fprintf(stderr,"unknown partition '%s'\n", item);
112 return 0;
113 }
114
115 if(product) {
116 get_my_path(path);
117 sprintf(path + strlen(path),
118 "../../../target/product/%s/%s", product, fn);
119 return strdup(path);
120 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 dir = getenv("ANDROID_PRODUCT_OUT");
123 if((dir == 0) || (dir[0] == 0)) {
124 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
125 return 0;
126 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800127
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 sprintf(path, "%s/%s", dir, fn);
129 return strdup(path);
130}
131
132#ifdef _WIN32
133void *load_file(const char *fn, unsigned *_sz);
Colin Crossf8387882012-05-24 17:18:41 -0700134int64_t file_size(const char *fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135#else
Colin Crossf8387882012-05-24 17:18:41 -0700136#if defined(__APPLE__) && defined(__MACH__)
137#define lseek64 lseek
138#define off64_t off_t
139#endif
140
141int64_t file_size(const char *fn)
142{
143 off64_t off;
144 int fd;
145
146 fd = open(fn, O_RDONLY);
147 if (fd < 0) return -1;
148
149 off = lseek64(fd, 0, SEEK_END);
150 close(fd);
151
152 return off;
153}
154
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155void *load_file(const char *fn, unsigned *_sz)
156{
157 char *data;
158 int sz;
159 int fd;
160
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:
180 close(fd);
181 if(data != 0) free(data);
182 return 0;
183}
184#endif
185
186int match_fastboot(usb_ifc_info *info)
187{
JP Abgralla032ded2012-06-06 11:53:33 -0700188 return match_fastboot_with_serial(info, serial);
189}
190
191int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
192{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400194 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800195 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700196 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800197 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700198 (info->dev_vendor != 0x0fce) && // Sony Ericsson
199 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400200 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800201 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800202 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800203 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800204 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400205 (info->dev_vendor != 0x0bb4)) // HTC
206 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 if(info->ifc_class != 0xff) return -1;
208 if(info->ifc_subclass != 0x42) return -1;
209 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700210 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700212 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
213 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 return 0;
215}
216
217int list_devices_callback(usb_ifc_info *info)
218{
JP Abgralla032ded2012-06-06 11:53:33 -0700219 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 if (!info->writable) {
222 serial = "no permissions"; // like "adb devices"
223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 if (!serial[0]) {
225 serial = "????????????";
226 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700227 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700228 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700229 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700230 } else if (!info->device_path) {
231 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700232 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700233 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700234 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236
237 return -1;
238}
239
240usb_handle *open_device(void)
241{
242 static usb_handle *usb = 0;
243 int announce = 1;
244
245 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800246
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 for(;;) {
248 usb = usb_open(match_fastboot);
249 if(usb) return usb;
250 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800251 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 fprintf(stderr,"< waiting for device >\n");
253 }
254 sleep(1);
255 }
256}
257
258void list_devices(void) {
259 // We don't actually open a USB device here,
260 // just getting our callback called so we can
261 // list all the connected devices.
262 usb_open(list_devices_callback);
263}
264
265void usage(void)
266{
267 fprintf(stderr,
268/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
269 "usage: fastboot [ <option> ] <command>\n"
270 "\n"
271 "commands:\n"
272 " update <filename> reflash device from update.zip\n"
273 " flashall flash boot + recovery + system\n"
274 " flash <partition> [ <filename> ] write a file to a flash partition\n"
275 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800276 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 " getvar <variable> display a bootloader variable\n"
278 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
279 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
280 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700281 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 " reboot reboot device normally\n"
283 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800284 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 "\n"
286 "options:\n"
287 " -w erase userdata and cache\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700288 " -s <specific device> specify device serial number\n"
289 " or path to device port\n"
290 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " -p <product> specify product name\n"
292 " -c <cmdline> override kernel commandline\n"
293 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700294 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800295 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700296 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700297 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299}
300
Dima Zavin931175a2010-02-12 20:26:33 -0800301void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 unsigned *sz, const char *cmdline)
303{
304 void *kdata = 0, *rdata = 0;
305 unsigned ksize = 0, rsize = 0;
306 void *bdata;
307 unsigned bsize;
308
309 if(kernel == 0) {
310 fprintf(stderr, "no image specified\n");
311 return 0;
312 }
313
314 kdata = load_file(kernel, &ksize);
315 if(kdata == 0) {
316 fprintf(stderr, "cannot load '%s'\n", kernel);
317 return 0;
318 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800319
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 /* is this actually a boot image? */
321 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
322 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800323
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 if(ramdisk) {
325 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
326 return 0;
327 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800328
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 *sz = ksize;
330 return kdata;
331 }
332
333 if(ramdisk) {
334 rdata = load_file(ramdisk, &rsize);
335 if(rdata == 0) {
336 fprintf(stderr,"cannot load '%s'\n", ramdisk);
337 return 0;
338 }
339 }
340
341 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800342 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 if(bdata == 0) {
344 fprintf(stderr,"failed to create boot.img\n");
345 return 0;
346 }
347 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
348 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
349 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 return bdata;
352}
353
354void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
355{
356 void *data;
357 zipentry_t entry;
358 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800359
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 entry = lookup_zipentry(zip, name);
361 if (entry == NULL) {
362 fprintf(stderr, "archive does not contain '%s'\n", name);
363 return 0;
364 }
365
366 *sz = get_zipentry_size(entry);
367
368 datasz = *sz * 1.001;
369 data = malloc(datasz);
370
371 if(data == 0) {
372 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
373 return 0;
374 }
375
376 if (decompress_zipentry(entry, data, datasz)) {
377 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
378 free(data);
379 return 0;
380 }
381
382 return data;
383}
384
385static char *strip(char *s)
386{
387 int n;
388 while(*s && isspace(*s)) s++;
389 n = strlen(s);
390 while(n-- > 0) {
391 if(!isspace(s[n])) break;
392 s[n] = 0;
393 }
394 return s;
395}
396
397#define MAX_OPTIONS 32
398static int setup_requirement_line(char *name)
399{
400 char *val[MAX_OPTIONS];
401 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700402 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 unsigned n, count;
404 char *x;
405 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800406
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 if (!strncmp(name, "reject ", 7)) {
408 name += 7;
409 invert = 1;
410 } else if (!strncmp(name, "require ", 8)) {
411 name += 8;
412 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700413 } else if (!strncmp(name, "require-for-product:", 20)) {
414 // Get the product and point name past it
415 prod = name + 20;
416 name = strchr(name, ' ');
417 if (!name) return -1;
418 *name = 0;
419 name += 1;
420 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 }
422
423 x = strchr(name, '=');
424 if (x == 0) return 0;
425 *x = 0;
426 val[0] = x + 1;
427
428 for(count = 1; count < MAX_OPTIONS; count++) {
429 x = strchr(val[count - 1],'|');
430 if (x == 0) break;
431 *x = 0;
432 val[count] = x + 1;
433 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 name = strip(name);
436 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 name = strip(name);
439 if (name == 0) return -1;
440
441 /* work around an unfortunate name mismatch */
442 if (!strcmp(name,"board")) name = "product";
443
444 out = malloc(sizeof(char*) * count);
445 if (out == 0) return -1;
446
447 for(n = 0; n < count; n++) {
448 out[n] = strdup(strip(val[n]));
449 if (out[n] == 0) return -1;
450 }
451
Wink Savilleb98762f2011-04-04 17:54:59 -0700452 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 return 0;
454}
455
456static void setup_requirements(char *data, unsigned sz)
457{
458 char *s;
459
460 s = data;
461 while (sz-- > 0) {
462 if(*s == '\n') {
463 *s++ = 0;
464 if (setup_requirement_line(data)) {
465 die("out of memory");
466 }
467 data = s;
468 } else {
469 s++;
470 }
471 }
472}
473
474void queue_info_dump(void)
475{
476 fb_queue_notice("--------------------------------------------");
477 fb_queue_display("version-bootloader", "Bootloader Version...");
478 fb_queue_display("version-baseband", "Baseband Version.....");
479 fb_queue_display("serialno", "Serial Number........");
480 fb_queue_notice("--------------------------------------------");
481}
482
Colin Crossf8387882012-05-24 17:18:41 -0700483
484struct sparse_file **load_sparse_files(const char *fname, int max_size)
485{
486 int fd;
487 struct sparse_file *s;
488 int files;
489 struct sparse_file **out_s;
490
491 fd = open(fname, O_RDONLY | O_BINARY);
492 if (fd < 0) {
493 die("cannot open '%s'\n", fname);
494 }
495
496 s = sparse_file_import_auto(fd, false);
497 if (!s) {
498 die("cannot sparse read file '%s'\n", fname);
499 }
500
501 files = sparse_file_resparse(s, max_size, NULL, 0);
502 if (files < 0) {
503 die("Failed to resparse '%s'\n", fname);
504 }
505
506 out_s = calloc(sizeof(struct sparse_file *), files + 1);
507 if (!out_s) {
508 die("Failed to allocate sparse file array\n");
509 }
510
511 files = sparse_file_resparse(s, max_size, out_s, files);
512 if (files < 0) {
513 die("Failed to resparse '%s'\n", fname);
514 }
515
516 return out_s;
517}
518
519static int64_t get_target_sparse_limit(struct usb_handle *usb)
520{
521 int64_t limit = 0;
522 char response[FB_RESPONSE_SZ + 1];
523 int status = fb_getvar(usb, response, "max-download-size");
524
525 if (!status) {
526 limit = strtoul(response, NULL, 0);
527 if (limit > 0) {
528 fprintf(stderr, "target reported max download size of %lld bytes\n",
529 limit);
530 }
531 }
532
533 return limit;
534}
535
536static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
537{
538 int64_t limit;
539
540 if (sparse_limit == 0) {
541 return 0;
542 } else if (sparse_limit > 0) {
543 limit = sparse_limit;
544 } else {
545 if (target_sparse_limit == -1) {
546 target_sparse_limit = get_target_sparse_limit(usb);
547 }
548 if (target_sparse_limit > 0) {
549 limit = target_sparse_limit;
550 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700551 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700552 }
553 }
554
555 if (size > limit) {
556 return limit;
557 }
558
559 return 0;
560}
561
562void do_flash(usb_handle *usb, const char *pname, const char *fname)
563{
564 int64_t sz64;
565 void *data;
566 int64_t limit;
567
568 sz64 = file_size(fname);
569 limit = get_sparse_limit(usb, sz64);
570 if (limit) {
571 struct sparse_file **s = load_sparse_files(fname, limit);
572 if (s == NULL) {
573 die("cannot sparse load '%s'\n", fname);
574 }
575 while (*s) {
576 sz64 = sparse_file_len(*s, true, false);
577 fb_queue_flash_sparse(pname, *s++, sz64);
578 }
579 } else {
580 unsigned int sz;
581 data = load_file(fname, &sz);
582 if (data == 0) die("cannot load '%s'\n", fname);
583 fb_queue_flash(pname, data, sz);
584 }
585}
586
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800587void do_update_signature(zipfile_t zip, char *fn)
588{
589 void *data;
590 unsigned sz;
591 data = unzip_file(zip, fn, &sz);
592 if (data == 0) return;
593 fb_queue_download("signature", data, sz);
594 fb_queue_command("signature", "installing signature");
595}
596
597void do_update(char *fn)
598{
599 void *zdata;
600 unsigned zsize;
601 void *data;
602 unsigned sz;
603 zipfile_t zip;
604
605 queue_info_dump();
606
Wink Savilleb98762f2011-04-04 17:54:59 -0700607 fb_queue_query_save("product", cur_product, sizeof(cur_product));
608
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609 zdata = load_file(fn, &zsize);
610 if (zdata == 0) die("failed to load '%s'", fn);
611
612 zip = init_zipfile(zdata, zsize);
613 if(zip == 0) die("failed to access zipdata in '%s'");
614
615 data = unzip_file(zip, "android-info.txt", &sz);
616 if (data == 0) {
617 char *tmp;
618 /* fallback for older zipfiles */
619 data = unzip_file(zip, "android-product.txt", &sz);
620 if ((data == 0) || (sz < 1)) {
621 die("update package has no android-info.txt or android-product.txt");
622 }
623 tmp = malloc(sz + 128);
624 if (tmp == 0) die("out of memory");
625 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
626 data = tmp;
627 sz = strlen(tmp);
628 }
629
630 setup_requirements(data, sz);
631
632 data = unzip_file(zip, "boot.img", &sz);
633 if (data == 0) die("update package missing boot.img");
634 do_update_signature(zip, "boot.sig");
635 fb_queue_flash("boot", data, sz);
636
637 data = unzip_file(zip, "recovery.img", &sz);
638 if (data != 0) {
639 do_update_signature(zip, "recovery.sig");
640 fb_queue_flash("recovery", data, sz);
641 }
642
643 data = unzip_file(zip, "system.img", &sz);
644 if (data == 0) die("update package missing system.img");
645 do_update_signature(zip, "system.sig");
646 fb_queue_flash("system", data, sz);
647}
648
649void do_send_signature(char *fn)
650{
651 void *data;
652 unsigned sz;
653 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800654
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 xtn = strrchr(fn, '.');
656 if (!xtn) return;
657 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800658
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 strcpy(xtn,".sig");
660 data = load_file(fn, &sz);
661 strcpy(xtn,".img");
662 if (data == 0) return;
663 fb_queue_download("signature", data, sz);
664 fb_queue_command("signature", "installing signature");
665}
666
667void do_flashall(void)
668{
669 char *fname;
670 void *data;
671 unsigned sz;
672
673 queue_info_dump();
674
Wink Savilleb98762f2011-04-04 17:54:59 -0700675 fb_queue_query_save("product", cur_product, sizeof(cur_product));
676
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 fname = find_item("info", product);
678 if (fname == 0) die("cannot find android-info.txt");
679 data = load_file(fname, &sz);
680 if (data == 0) die("could not load android-info.txt");
681 setup_requirements(data, sz);
682
683 fname = find_item("boot", product);
684 data = load_file(fname, &sz);
685 if (data == 0) die("could not load boot.img");
686 do_send_signature(fname);
687 fb_queue_flash("boot", data, sz);
688
689 fname = find_item("recovery", product);
690 data = load_file(fname, &sz);
691 if (data != 0) {
692 do_send_signature(fname);
693 fb_queue_flash("recovery", data, sz);
694 }
695
696 fname = find_item("system", product);
697 data = load_file(fname, &sz);
698 if (data == 0) die("could not load system.img");
699 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800700 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701}
702
703#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800704#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705
706int do_oem_command(int argc, char **argv)
707{
708 int i;
709 char command[256];
710 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800711
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 command[0] = 0;
713 while(1) {
714 strcat(command,*argv);
715 skip(1);
716 if(argc == 0) break;
717 strcat(command," ");
718 }
719
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800720 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 return 0;
722}
723
Colin Crossf8387882012-05-24 17:18:41 -0700724static int64_t parse_num(const char *arg)
725{
726 char *endptr;
727 unsigned long long num;
728
729 num = strtoull(arg, &endptr, 0);
730 if (endptr == arg) {
731 return -1;
732 }
733
734 if (*endptr == 'k' || *endptr == 'K') {
735 if (num >= (-1ULL) / 1024) {
736 return -1;
737 }
738 num *= 1024LL;
739 endptr++;
740 } else if (*endptr == 'm' || *endptr == 'M') {
741 if (num >= (-1ULL) / (1024 * 1024)) {
742 return -1;
743 }
744 num *= 1024LL * 1024LL;
745 endptr++;
746 } else if (*endptr == 'g' || *endptr == 'G') {
747 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
748 return -1;
749 }
750 num *= 1024LL * 1024LL * 1024LL;
751 endptr++;
752 }
753
754 if (*endptr != '\0') {
755 return -1;
756 }
757
758 if (num > INT64_MAX) {
759 return -1;
760 }
761
762 return num;
763}
764
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765int main(int argc, char **argv)
766{
767 int wants_wipe = 0;
768 int wants_reboot = 0;
769 int wants_reboot_bootloader = 0;
770 void *data;
771 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800772 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700773 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700774 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700775 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776
Colin Crossf8387882012-05-24 17:18:41 -0700777 const struct option longopts = { 0, 0, 0, 0 };
Colin Cross8879f982012-05-22 17:53:34 -0700778
779 serial = getenv("ANDROID_SERIAL");
780
781 while (1) {
Colin Cross9a70e5c2012-07-17 23:35:21 -0700782 c = getopt_long(argc, argv, "wb:n:s:S:lp:c:i:m:h", &longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700783 if (c < 0) {
784 break;
785 }
786
787 switch (c) {
788 case 'w':
789 wants_wipe = 1;
790 break;
791 case 'b':
792 base_addr = strtoul(optarg, 0, 16);
793 break;
794 case 'n':
795 page_size = (unsigned)strtoul(optarg, NULL, 0);
796 if (!page_size) die("invalid page size");
797 break;
798 case 's':
799 serial = optarg;
800 break;
Colin Crossf8387882012-05-24 17:18:41 -0700801 case 'S':
802 sparse_limit = parse_num(optarg);
803 if (sparse_limit < 0) {
804 die("invalid sparse limit");
805 }
806 break;
Colin Cross9a70e5c2012-07-17 23:35:21 -0700807 case 'l':
808 long_listing = 1;
809 break;
Colin Cross8879f982012-05-22 17:53:34 -0700810 case 'p':
811 product = optarg;
812 break;
813 case 'c':
814 cmdline = optarg;
815 break;
816 case 'i': {
817 char *endptr = NULL;
818 unsigned long val;
819
820 val = strtoul(optarg, &endptr, 0);
821 if (!endptr || *endptr != '\0' || (val & ~0xffff))
822 die("invalid vendor id '%s'", optarg);
823 vendor_id = (unsigned short)val;
824 break;
825 }
826 case 'h':
827 usage();
828 return 1;
829 case '?':
830 return 1;
831 default:
832 abort();
833 }
834 }
835
836 argc -= optind;
837 argv += optind;
838
839 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700841 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 }
843
Colin Cross8fb6e062012-07-24 16:36:41 -0700844 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700845 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800846 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800847 return 0;
848 }
849
Colin Cross8879f982012-05-22 17:53:34 -0700850 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700851
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700853 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800854 require(2);
855 fb_queue_display(argv[1], argv[1]);
856 skip(2);
857 } else if(!strcmp(*argv, "erase")) {
858 require(2);
859 fb_queue_erase(argv[1]);
860 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800861 } else if(!strcmp(*argv, "format")) {
862 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700863 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800864 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865 } else if(!strcmp(*argv, "signature")) {
866 require(2);
867 data = load_file(argv[1], &sz);
868 if (data == 0) die("could not load '%s'", argv[1]);
869 if (sz != 256) die("signature must be 256 bytes");
870 fb_queue_download("signature", data, sz);
871 fb_queue_command("signature", "installing signature");
872 skip(2);
873 } else if(!strcmp(*argv, "reboot")) {
874 wants_reboot = 1;
875 skip(1);
876 } else if(!strcmp(*argv, "reboot-bootloader")) {
877 wants_reboot_bootloader = 1;
878 skip(1);
879 } else if (!strcmp(*argv, "continue")) {
880 fb_queue_command("continue", "resuming boot");
881 skip(1);
882 } else if(!strcmp(*argv, "boot")) {
883 char *kname = 0;
884 char *rname = 0;
885 skip(1);
886 if (argc > 0) {
887 kname = argv[0];
888 skip(1);
889 }
890 if (argc > 0) {
891 rname = argv[0];
892 skip(1);
893 }
Dima Zavin931175a2010-02-12 20:26:33 -0800894 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 if (data == 0) return 1;
896 fb_queue_download("boot.img", data, sz);
897 fb_queue_command("boot", "booting");
898 } else if(!strcmp(*argv, "flash")) {
899 char *pname = argv[1];
900 char *fname = 0;
901 require(2);
902 if (argc > 2) {
903 fname = argv[2];
904 skip(3);
905 } else {
906 fname = find_item(pname, product);
907 skip(2);
908 }
909 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Colin Crossf8387882012-05-24 17:18:41 -0700910 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 } else if(!strcmp(*argv, "flash:raw")) {
912 char *pname = argv[1];
913 char *kname = argv[2];
914 char *rname = 0;
915 require(3);
916 if(argc > 3) {
917 rname = argv[3];
918 skip(4);
919 } else {
920 skip(3);
921 }
Dima Zavin931175a2010-02-12 20:26:33 -0800922 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800923 if (data == 0) die("cannot load bootable image");
924 fb_queue_flash(pname, data, sz);
925 } else if(!strcmp(*argv, "flashall")) {
926 skip(1);
927 do_flashall();
928 wants_reboot = 1;
929 } else if(!strcmp(*argv, "update")) {
930 if (argc > 1) {
931 do_update(argv[1]);
932 skip(2);
933 } else {
934 do_update("update.zip");
935 skip(1);
936 }
937 wants_reboot = 1;
938 } else if(!strcmp(*argv, "oem")) {
939 argc = do_oem_command(argc, argv);
Colin Cross8879f982012-05-22 17:53:34 -0700940 } else if (!strcmp(*argv, "help")) {
941 usage();
942 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800943 } else {
944 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800945 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800946 }
947 }
948
949 if (wants_wipe) {
950 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -0700951 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800952 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -0700953 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800954 }
955 if (wants_reboot) {
956 fb_queue_reboot();
957 } else if (wants_reboot_bootloader) {
958 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
959 }
960
Scott Anderson13081c62012-04-06 12:39:30 -0700961 if (fb_queue_is_empty())
962 return 0;
963
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700964 status = fb_execute_queue(usb);
965 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800966}