blob: 6a1430197252c1da27ec3e8d256542000a5c0085 [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;
Scott Anderson13081c62012-04-06 12:39:30 -070075static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070076static int64_t sparse_limit = -1;
77static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
Brian Swetland2a63bb72009-04-28 16:05:07 -070079static unsigned base_addr = 0x10000000;
80
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081void die(const char *fmt, ...)
82{
83 va_list ap;
84 va_start(ap, fmt);
85 fprintf(stderr,"error: ");
86 vfprintf(stderr, fmt, ap);
87 fprintf(stderr,"\n");
88 va_end(ap);
89 exit(1);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080090}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
92void get_my_path(char *path);
93
94char *find_item(const char *item, const char *product)
95{
96 char *dir;
97 char *fn;
98 char path[PATH_MAX + 128];
99
100 if(!strcmp(item,"boot")) {
101 fn = "boot.img";
102 } else if(!strcmp(item,"recovery")) {
103 fn = "recovery.img";
104 } else if(!strcmp(item,"system")) {
105 fn = "system.img";
106 } else if(!strcmp(item,"userdata")) {
107 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700108 } else if(!strcmp(item,"cache")) {
109 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 } else if(!strcmp(item,"info")) {
111 fn = "android-info.txt";
112 } else {
113 fprintf(stderr,"unknown partition '%s'\n", item);
114 return 0;
115 }
116
117 if(product) {
118 get_my_path(path);
119 sprintf(path + strlen(path),
120 "../../../target/product/%s/%s", product, fn);
121 return strdup(path);
122 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800123
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 dir = getenv("ANDROID_PRODUCT_OUT");
125 if((dir == 0) || (dir[0] == 0)) {
126 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
127 return 0;
128 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 sprintf(path, "%s/%s", dir, fn);
131 return strdup(path);
132}
133
134#ifdef _WIN32
135void *load_file(const char *fn, unsigned *_sz);
Colin Crossf8387882012-05-24 17:18:41 -0700136int64_t file_size(const char *fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137#else
Colin Crossf8387882012-05-24 17:18:41 -0700138#if defined(__APPLE__) && defined(__MACH__)
139#define lseek64 lseek
140#define off64_t off_t
141#endif
142
143int64_t file_size(const char *fn)
144{
145 off64_t off;
146 int fd;
147
148 fd = open(fn, O_RDONLY);
149 if (fd < 0) return -1;
150
151 off = lseek64(fd, 0, SEEK_END);
152 close(fd);
153
154 return off;
155}
156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157void *load_file(const char *fn, unsigned *_sz)
158{
159 char *data;
160 int sz;
161 int fd;
162
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:
182 close(fd);
183 if(data != 0) free(data);
184 return 0;
185}
186#endif
187
188int match_fastboot(usb_ifc_info *info)
189{
JP Abgralla032ded2012-06-06 11:53:33 -0700190 return match_fastboot_with_serial(info, serial);
191}
192
193int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
194{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400196 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800197 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700198 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800199 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700200 (info->dev_vendor != 0x0fce) && // Sony Ericsson
201 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400202 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800203 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800204 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800205 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800206 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400207 (info->dev_vendor != 0x0bb4)) // HTC
208 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 if(info->ifc_class != 0xff) return -1;
210 if(info->ifc_subclass != 0x42) return -1;
211 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700212 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700214 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
215 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 return 0;
217}
218
219int list_devices_callback(usb_ifc_info *info)
220{
JP Abgralla032ded2012-06-06 11:53:33 -0700221 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700223 if (!info->writable) {
224 serial = "no permissions"; // like "adb devices"
225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 if (!serial[0]) {
227 serial = "????????????";
228 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700229 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700230 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700231 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700232 } else if (!info->device_path) {
233 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700234 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700235 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700236 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 }
238
239 return -1;
240}
241
242usb_handle *open_device(void)
243{
244 static usb_handle *usb = 0;
245 int announce = 1;
246
247 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 for(;;) {
250 usb = usb_open(match_fastboot);
251 if(usb) return usb;
252 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 fprintf(stderr,"< waiting for device >\n");
255 }
256 sleep(1);
257 }
258}
259
260void list_devices(void) {
261 // We don't actually open a USB device here,
262 // just getting our callback called so we can
263 // list all the connected devices.
264 usb_open(list_devices_callback);
265}
266
267void usage(void)
268{
269 fprintf(stderr,
270/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
271 "usage: fastboot [ <option> ] <command>\n"
272 "\n"
273 "commands:\n"
274 " update <filename> reflash device from update.zip\n"
275 " flashall flash boot + recovery + system\n"
276 " flash <partition> [ <filename> ] write a file to a flash partition\n"
277 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800278 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 " getvar <variable> display a bootloader variable\n"
280 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
281 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
282 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700283 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 " reboot reboot device normally\n"
285 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800286 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 "\n"
288 "options:\n"
289 " -w erase userdata and cache\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700290 " -s <specific device> specify device serial number\n"
291 " or path to device port\n"
292 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 " -p <product> specify product name\n"
294 " -c <cmdline> override kernel commandline\n"
295 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700296 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800297 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700298 " -S <size>[K|M|G] automatically sparse files greater than\n"
299 " size. default: 256M, 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301}
302
Dima Zavin931175a2010-02-12 20:26:33 -0800303void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 unsigned *sz, const char *cmdline)
305{
306 void *kdata = 0, *rdata = 0;
307 unsigned ksize = 0, rsize = 0;
308 void *bdata;
309 unsigned bsize;
310
311 if(kernel == 0) {
312 fprintf(stderr, "no image specified\n");
313 return 0;
314 }
315
316 kdata = load_file(kernel, &ksize);
317 if(kdata == 0) {
318 fprintf(stderr, "cannot load '%s'\n", kernel);
319 return 0;
320 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800321
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 /* is this actually a boot image? */
323 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
324 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800325
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 if(ramdisk) {
327 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
328 return 0;
329 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800330
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 *sz = ksize;
332 return kdata;
333 }
334
335 if(ramdisk) {
336 rdata = load_file(ramdisk, &rsize);
337 if(rdata == 0) {
338 fprintf(stderr,"cannot load '%s'\n", ramdisk);
339 return 0;
340 }
341 }
342
343 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800344 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 if(bdata == 0) {
346 fprintf(stderr,"failed to create boot.img\n");
347 return 0;
348 }
349 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
350 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
351 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800352
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 return bdata;
354}
355
356void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
357{
358 void *data;
359 zipentry_t entry;
360 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800361
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 entry = lookup_zipentry(zip, name);
363 if (entry == NULL) {
364 fprintf(stderr, "archive does not contain '%s'\n", name);
365 return 0;
366 }
367
368 *sz = get_zipentry_size(entry);
369
370 datasz = *sz * 1.001;
371 data = malloc(datasz);
372
373 if(data == 0) {
374 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
375 return 0;
376 }
377
378 if (decompress_zipentry(entry, data, datasz)) {
379 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
380 free(data);
381 return 0;
382 }
383
384 return data;
385}
386
387static char *strip(char *s)
388{
389 int n;
390 while(*s && isspace(*s)) s++;
391 n = strlen(s);
392 while(n-- > 0) {
393 if(!isspace(s[n])) break;
394 s[n] = 0;
395 }
396 return s;
397}
398
399#define MAX_OPTIONS 32
400static int setup_requirement_line(char *name)
401{
402 char *val[MAX_OPTIONS];
403 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700404 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 unsigned n, count;
406 char *x;
407 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800408
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 if (!strncmp(name, "reject ", 7)) {
410 name += 7;
411 invert = 1;
412 } else if (!strncmp(name, "require ", 8)) {
413 name += 8;
414 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700415 } else if (!strncmp(name, "require-for-product:", 20)) {
416 // Get the product and point name past it
417 prod = name + 20;
418 name = strchr(name, ' ');
419 if (!name) return -1;
420 *name = 0;
421 name += 1;
422 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 }
424
425 x = strchr(name, '=');
426 if (x == 0) return 0;
427 *x = 0;
428 val[0] = x + 1;
429
430 for(count = 1; count < MAX_OPTIONS; count++) {
431 x = strchr(val[count - 1],'|');
432 if (x == 0) break;
433 *x = 0;
434 val[count] = x + 1;
435 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800436
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 name = strip(name);
438 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 name = strip(name);
441 if (name == 0) return -1;
442
443 /* work around an unfortunate name mismatch */
444 if (!strcmp(name,"board")) name = "product";
445
446 out = malloc(sizeof(char*) * count);
447 if (out == 0) return -1;
448
449 for(n = 0; n < count; n++) {
450 out[n] = strdup(strip(val[n]));
451 if (out[n] == 0) return -1;
452 }
453
Wink Savilleb98762f2011-04-04 17:54:59 -0700454 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return 0;
456}
457
458static void setup_requirements(char *data, unsigned sz)
459{
460 char *s;
461
462 s = data;
463 while (sz-- > 0) {
464 if(*s == '\n') {
465 *s++ = 0;
466 if (setup_requirement_line(data)) {
467 die("out of memory");
468 }
469 data = s;
470 } else {
471 s++;
472 }
473 }
474}
475
476void queue_info_dump(void)
477{
478 fb_queue_notice("--------------------------------------------");
479 fb_queue_display("version-bootloader", "Bootloader Version...");
480 fb_queue_display("version-baseband", "Baseband Version.....");
481 fb_queue_display("serialno", "Serial Number........");
482 fb_queue_notice("--------------------------------------------");
483}
484
Colin Crossf8387882012-05-24 17:18:41 -0700485
486struct sparse_file **load_sparse_files(const char *fname, int max_size)
487{
488 int fd;
489 struct sparse_file *s;
490 int files;
491 struct sparse_file **out_s;
492
493 fd = open(fname, O_RDONLY | O_BINARY);
494 if (fd < 0) {
495 die("cannot open '%s'\n", fname);
496 }
497
498 s = sparse_file_import_auto(fd, false);
499 if (!s) {
500 die("cannot sparse read file '%s'\n", fname);
501 }
502
503 files = sparse_file_resparse(s, max_size, NULL, 0);
504 if (files < 0) {
505 die("Failed to resparse '%s'\n", fname);
506 }
507
508 out_s = calloc(sizeof(struct sparse_file *), files + 1);
509 if (!out_s) {
510 die("Failed to allocate sparse file array\n");
511 }
512
513 files = sparse_file_resparse(s, max_size, out_s, files);
514 if (files < 0) {
515 die("Failed to resparse '%s'\n", fname);
516 }
517
518 return out_s;
519}
520
521static int64_t get_target_sparse_limit(struct usb_handle *usb)
522{
523 int64_t limit = 0;
524 char response[FB_RESPONSE_SZ + 1];
525 int status = fb_getvar(usb, response, "max-download-size");
526
527 if (!status) {
528 limit = strtoul(response, NULL, 0);
529 if (limit > 0) {
530 fprintf(stderr, "target reported max download size of %lld bytes\n",
531 limit);
532 }
533 }
534
535 return limit;
536}
537
538static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
539{
540 int64_t limit;
541
542 if (sparse_limit == 0) {
543 return 0;
544 } else if (sparse_limit > 0) {
545 limit = sparse_limit;
546 } else {
547 if (target_sparse_limit == -1) {
548 target_sparse_limit = get_target_sparse_limit(usb);
549 }
550 if (target_sparse_limit > 0) {
551 limit = target_sparse_limit;
552 } else {
553 limit = DEFAULT_SPARSE_LIMIT;
554 }
555 }
556
557 if (size > limit) {
558 return limit;
559 }
560
561 return 0;
562}
563
564void do_flash(usb_handle *usb, const char *pname, const char *fname)
565{
566 int64_t sz64;
567 void *data;
568 int64_t limit;
569
570 sz64 = file_size(fname);
571 limit = get_sparse_limit(usb, sz64);
572 if (limit) {
573 struct sparse_file **s = load_sparse_files(fname, limit);
574 if (s == NULL) {
575 die("cannot sparse load '%s'\n", fname);
576 }
577 while (*s) {
578 sz64 = sparse_file_len(*s, true, false);
579 fb_queue_flash_sparse(pname, *s++, sz64);
580 }
581 } else {
582 unsigned int sz;
583 data = load_file(fname, &sz);
584 if (data == 0) die("cannot load '%s'\n", fname);
585 fb_queue_flash(pname, data, sz);
586 }
587}
588
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589void do_update_signature(zipfile_t zip, char *fn)
590{
591 void *data;
592 unsigned sz;
593 data = unzip_file(zip, fn, &sz);
594 if (data == 0) return;
595 fb_queue_download("signature", data, sz);
596 fb_queue_command("signature", "installing signature");
597}
598
599void do_update(char *fn)
600{
601 void *zdata;
602 unsigned zsize;
603 void *data;
604 unsigned sz;
605 zipfile_t zip;
606
607 queue_info_dump();
608
Wink Savilleb98762f2011-04-04 17:54:59 -0700609 fb_queue_query_save("product", cur_product, sizeof(cur_product));
610
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 zdata = load_file(fn, &zsize);
612 if (zdata == 0) die("failed to load '%s'", fn);
613
614 zip = init_zipfile(zdata, zsize);
615 if(zip == 0) die("failed to access zipdata in '%s'");
616
617 data = unzip_file(zip, "android-info.txt", &sz);
618 if (data == 0) {
619 char *tmp;
620 /* fallback for older zipfiles */
621 data = unzip_file(zip, "android-product.txt", &sz);
622 if ((data == 0) || (sz < 1)) {
623 die("update package has no android-info.txt or android-product.txt");
624 }
625 tmp = malloc(sz + 128);
626 if (tmp == 0) die("out of memory");
627 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
628 data = tmp;
629 sz = strlen(tmp);
630 }
631
632 setup_requirements(data, sz);
633
634 data = unzip_file(zip, "boot.img", &sz);
635 if (data == 0) die("update package missing boot.img");
636 do_update_signature(zip, "boot.sig");
637 fb_queue_flash("boot", data, sz);
638
639 data = unzip_file(zip, "recovery.img", &sz);
640 if (data != 0) {
641 do_update_signature(zip, "recovery.sig");
642 fb_queue_flash("recovery", data, sz);
643 }
644
645 data = unzip_file(zip, "system.img", &sz);
646 if (data == 0) die("update package missing system.img");
647 do_update_signature(zip, "system.sig");
648 fb_queue_flash("system", data, sz);
649}
650
651void do_send_signature(char *fn)
652{
653 void *data;
654 unsigned sz;
655 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800656
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657 xtn = strrchr(fn, '.');
658 if (!xtn) return;
659 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800660
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 strcpy(xtn,".sig");
662 data = load_file(fn, &sz);
663 strcpy(xtn,".img");
664 if (data == 0) return;
665 fb_queue_download("signature", data, sz);
666 fb_queue_command("signature", "installing signature");
667}
668
669void do_flashall(void)
670{
671 char *fname;
672 void *data;
673 unsigned sz;
674
675 queue_info_dump();
676
Wink Savilleb98762f2011-04-04 17:54:59 -0700677 fb_queue_query_save("product", cur_product, sizeof(cur_product));
678
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 fname = find_item("info", product);
680 if (fname == 0) die("cannot find android-info.txt");
681 data = load_file(fname, &sz);
682 if (data == 0) die("could not load android-info.txt");
683 setup_requirements(data, sz);
684
685 fname = find_item("boot", product);
686 data = load_file(fname, &sz);
687 if (data == 0) die("could not load boot.img");
688 do_send_signature(fname);
689 fb_queue_flash("boot", data, sz);
690
691 fname = find_item("recovery", product);
692 data = load_file(fname, &sz);
693 if (data != 0) {
694 do_send_signature(fname);
695 fb_queue_flash("recovery", data, sz);
696 }
697
698 fname = find_item("system", product);
699 data = load_file(fname, &sz);
700 if (data == 0) die("could not load system.img");
701 do_send_signature(fname);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800702 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703}
704
705#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800706#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707
708int do_oem_command(int argc, char **argv)
709{
710 int i;
711 char command[256];
712 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800713
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 command[0] = 0;
715 while(1) {
716 strcat(command,*argv);
717 skip(1);
718 if(argc == 0) break;
719 strcat(command," ");
720 }
721
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800722 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 return 0;
724}
725
Colin Crossf8387882012-05-24 17:18:41 -0700726static int64_t parse_num(const char *arg)
727{
728 char *endptr;
729 unsigned long long num;
730
731 num = strtoull(arg, &endptr, 0);
732 if (endptr == arg) {
733 return -1;
734 }
735
736 if (*endptr == 'k' || *endptr == 'K') {
737 if (num >= (-1ULL) / 1024) {
738 return -1;
739 }
740 num *= 1024LL;
741 endptr++;
742 } else if (*endptr == 'm' || *endptr == 'M') {
743 if (num >= (-1ULL) / (1024 * 1024)) {
744 return -1;
745 }
746 num *= 1024LL * 1024LL;
747 endptr++;
748 } else if (*endptr == 'g' || *endptr == 'G') {
749 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
750 return -1;
751 }
752 num *= 1024LL * 1024LL * 1024LL;
753 endptr++;
754 }
755
756 if (*endptr != '\0') {
757 return -1;
758 }
759
760 if (num > INT64_MAX) {
761 return -1;
762 }
763
764 return num;
765}
766
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800767int main(int argc, char **argv)
768{
769 int wants_wipe = 0;
770 int wants_reboot = 0;
771 int wants_reboot_bootloader = 0;
772 void *data;
773 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800774 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700775 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700776 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700777 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778
Colin Crossf8387882012-05-24 17:18:41 -0700779 const struct option longopts = { 0, 0, 0, 0 };
Colin Cross8879f982012-05-22 17:53:34 -0700780
781 serial = getenv("ANDROID_SERIAL");
782
783 while (1) {
Colin Cross9a70e5c2012-07-17 23:35:21 -0700784 c = getopt_long(argc, argv, "wb:n:s:S:lp:c:i:m:h", &longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700785 if (c < 0) {
786 break;
787 }
788
789 switch (c) {
790 case 'w':
791 wants_wipe = 1;
792 break;
793 case 'b':
794 base_addr = strtoul(optarg, 0, 16);
795 break;
796 case 'n':
797 page_size = (unsigned)strtoul(optarg, NULL, 0);
798 if (!page_size) die("invalid page size");
799 break;
800 case 's':
801 serial = optarg;
802 break;
Colin Crossf8387882012-05-24 17:18:41 -0700803 case 'S':
804 sparse_limit = parse_num(optarg);
805 if (sparse_limit < 0) {
806 die("invalid sparse limit");
807 }
808 break;
Colin Cross9a70e5c2012-07-17 23:35:21 -0700809 case 'l':
810 long_listing = 1;
811 break;
Colin Cross8879f982012-05-22 17:53:34 -0700812 case 'p':
813 product = optarg;
814 break;
815 case 'c':
816 cmdline = optarg;
817 break;
818 case 'i': {
819 char *endptr = NULL;
820 unsigned long val;
821
822 val = strtoul(optarg, &endptr, 0);
823 if (!endptr || *endptr != '\0' || (val & ~0xffff))
824 die("invalid vendor id '%s'", optarg);
825 vendor_id = (unsigned short)val;
826 break;
827 }
828 case 'h':
829 usage();
830 return 1;
831 case '?':
832 return 1;
833 default:
834 abort();
835 }
836 }
837
838 argc -= optind;
839 argv += optind;
840
841 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800842 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700843 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 }
845
846 if (!strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700847 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800848 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800849 return 0;
850 }
851
Colin Cross8879f982012-05-22 17:53:34 -0700852 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700853
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800854 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700855 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 require(2);
857 fb_queue_display(argv[1], argv[1]);
858 skip(2);
859 } else if(!strcmp(*argv, "erase")) {
860 require(2);
861 fb_queue_erase(argv[1]);
862 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800863 } else if(!strcmp(*argv, "format")) {
864 require(2);
JP Abgrall30ae5802012-05-07 20:25:24 -0700865 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800866 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800867 } else if(!strcmp(*argv, "signature")) {
868 require(2);
869 data = load_file(argv[1], &sz);
870 if (data == 0) die("could not load '%s'", argv[1]);
871 if (sz != 256) die("signature must be 256 bytes");
872 fb_queue_download("signature", data, sz);
873 fb_queue_command("signature", "installing signature");
874 skip(2);
875 } else if(!strcmp(*argv, "reboot")) {
876 wants_reboot = 1;
877 skip(1);
878 } else if(!strcmp(*argv, "reboot-bootloader")) {
879 wants_reboot_bootloader = 1;
880 skip(1);
881 } else if (!strcmp(*argv, "continue")) {
882 fb_queue_command("continue", "resuming boot");
883 skip(1);
884 } else if(!strcmp(*argv, "boot")) {
885 char *kname = 0;
886 char *rname = 0;
887 skip(1);
888 if (argc > 0) {
889 kname = argv[0];
890 skip(1);
891 }
892 if (argc > 0) {
893 rname = argv[0];
894 skip(1);
895 }
Dima Zavin931175a2010-02-12 20:26:33 -0800896 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897 if (data == 0) return 1;
898 fb_queue_download("boot.img", data, sz);
899 fb_queue_command("boot", "booting");
900 } else if(!strcmp(*argv, "flash")) {
901 char *pname = argv[1];
902 char *fname = 0;
903 require(2);
904 if (argc > 2) {
905 fname = argv[2];
906 skip(3);
907 } else {
908 fname = find_item(pname, product);
909 skip(2);
910 }
911 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Colin Crossf8387882012-05-24 17:18:41 -0700912 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800913 } else if(!strcmp(*argv, "flash:raw")) {
914 char *pname = argv[1];
915 char *kname = argv[2];
916 char *rname = 0;
917 require(3);
918 if(argc > 3) {
919 rname = argv[3];
920 skip(4);
921 } else {
922 skip(3);
923 }
Dima Zavin931175a2010-02-12 20:26:33 -0800924 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925 if (data == 0) die("cannot load bootable image");
926 fb_queue_flash(pname, data, sz);
927 } else if(!strcmp(*argv, "flashall")) {
928 skip(1);
929 do_flashall();
930 wants_reboot = 1;
931 } else if(!strcmp(*argv, "update")) {
932 if (argc > 1) {
933 do_update(argv[1]);
934 skip(2);
935 } else {
936 do_update("update.zip");
937 skip(1);
938 }
939 wants_reboot = 1;
940 } else if(!strcmp(*argv, "oem")) {
941 argc = do_oem_command(argc, argv);
Colin Cross8879f982012-05-22 17:53:34 -0700942 } else if (!strcmp(*argv, "help")) {
943 usage();
944 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800945 } else {
946 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800947 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800948 }
949 }
950
951 if (wants_wipe) {
952 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -0700953 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800954 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -0700955 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800956 }
957 if (wants_reboot) {
958 fb_queue_reboot();
959 } else if (wants_reboot_bootloader) {
960 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
961 }
962
Scott Anderson13081c62012-04-06 12:39:30 -0700963 if (fb_queue_is_empty())
964 return 0;
965
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700966 status = fb_execute_queue(usb);
967 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800968}