blob: 3de6d7df989540aca1a802e5dcb2511f44276a8f [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;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800160 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
162 data = 0;
163 fd = open(fn, O_RDONLY);
164 if(fd < 0) return 0;
165
166 sz = lseek(fd, 0, SEEK_END);
167 if(sz < 0) goto oops;
168
169 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
170
171 data = (char*) malloc(sz);
172 if(data == 0) goto oops;
173
174 if(read(fd, data, sz) != sz) goto oops;
175 close(fd);
176
177 if(_sz) *_sz = sz;
178 return data;
179
180oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800181 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 close(fd);
183 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800184 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 return 0;
186}
187#endif
188
189int match_fastboot(usb_ifc_info *info)
190{
JP Abgralla032ded2012-06-06 11:53:33 -0700191 return match_fastboot_with_serial(info, serial);
192}
193
194int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
195{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400197 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800198 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700199 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800200 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700201 (info->dev_vendor != 0x0fce) && // Sony Ericsson
202 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400203 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800204 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800205 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800206 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800207 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400208 (info->dev_vendor != 0x0bb4)) // HTC
209 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 if(info->ifc_class != 0xff) return -1;
211 if(info->ifc_subclass != 0x42) return -1;
212 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700213 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700215 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
216 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 return 0;
218}
219
220int list_devices_callback(usb_ifc_info *info)
221{
JP Abgralla032ded2012-06-06 11:53:33 -0700222 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700224 if (!info->writable) {
225 serial = "no permissions"; // like "adb devices"
226 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 if (!serial[0]) {
228 serial = "????????????";
229 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700230 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700231 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700232 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700233 } else if (!info->device_path) {
234 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700235 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700236 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 }
239
240 return -1;
241}
242
243usb_handle *open_device(void)
244{
245 static usb_handle *usb = 0;
246 int announce = 1;
247
248 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800249
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250 for(;;) {
251 usb = usb_open(match_fastboot);
252 if(usb) return usb;
253 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800254 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 fprintf(stderr,"< waiting for device >\n");
256 }
257 sleep(1);
258 }
259}
260
261void list_devices(void) {
262 // We don't actually open a USB device here,
263 // just getting our callback called so we can
264 // list all the connected devices.
265 usb_open(list_devices_callback);
266}
267
268void usage(void)
269{
270 fprintf(stderr,
271/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
272 "usage: fastboot [ <option> ] <command>\n"
273 "\n"
274 "commands:\n"
275 " update <filename> reflash device from update.zip\n"
276 " flashall flash boot + recovery + system\n"
277 " flash <partition> [ <filename> ] write a file to a flash partition\n"
278 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800279 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800280 " getvar <variable> display a bootloader variable\n"
281 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
282 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
283 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700284 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285 " reboot reboot device normally\n"
286 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800287 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 "\n"
289 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700290 " -w erase userdata and cache (and format\n"
291 " if supported by partition type)\n"
292 " -u do not first erase partition before\n"
293 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700294 " -s <specific device> specify device serial number\n"
295 " or path to device port\n"
296 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 " -p <product> specify product name\n"
298 " -c <cmdline> override kernel commandline\n"
299 " -i <vendor id> specify a custom USB vendor id\n"
Dima Zavin95ec9832009-04-30 15:03:05 -0700300 " -b <base_addr> specify a custom kernel base address\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800301 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700302 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700303 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305}
306
Dima Zavin931175a2010-02-12 20:26:33 -0800307void *load_bootable_image(unsigned page_size, const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 unsigned *sz, const char *cmdline)
309{
310 void *kdata = 0, *rdata = 0;
311 unsigned ksize = 0, rsize = 0;
312 void *bdata;
313 unsigned bsize;
314
315 if(kernel == 0) {
316 fprintf(stderr, "no image specified\n");
317 return 0;
318 }
319
320 kdata = load_file(kernel, &ksize);
321 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800322 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 return 0;
324 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800325
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 /* is this actually a boot image? */
327 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
328 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800329
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 if(ramdisk) {
331 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
332 return 0;
333 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800334
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 *sz = ksize;
336 return kdata;
337 }
338
339 if(ramdisk) {
340 rdata = load_file(ramdisk, &rsize);
341 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800342 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 return 0;
344 }
345 }
346
347 fprintf(stderr,"creating boot image...\n");
Dima Zavin931175a2010-02-12 20:26:33 -0800348 bdata = mkbootimg(kdata, ksize, rdata, rsize, 0, 0, page_size, base_addr, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 if(bdata == 0) {
350 fprintf(stderr,"failed to create boot.img\n");
351 return 0;
352 }
353 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
354 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
355 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800356
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 return bdata;
358}
359
360void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
361{
362 void *data;
363 zipentry_t entry;
364 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 entry = lookup_zipentry(zip, name);
367 if (entry == NULL) {
368 fprintf(stderr, "archive does not contain '%s'\n", name);
369 return 0;
370 }
371
372 *sz = get_zipentry_size(entry);
373
374 datasz = *sz * 1.001;
375 data = malloc(datasz);
376
377 if(data == 0) {
378 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
379 return 0;
380 }
381
382 if (decompress_zipentry(entry, data, datasz)) {
383 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
384 free(data);
385 return 0;
386 }
387
388 return data;
389}
390
391static char *strip(char *s)
392{
393 int n;
394 while(*s && isspace(*s)) s++;
395 n = strlen(s);
396 while(n-- > 0) {
397 if(!isspace(s[n])) break;
398 s[n] = 0;
399 }
400 return s;
401}
402
403#define MAX_OPTIONS 32
404static int setup_requirement_line(char *name)
405{
406 char *val[MAX_OPTIONS];
407 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700408 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 unsigned n, count;
410 char *x;
411 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 if (!strncmp(name, "reject ", 7)) {
414 name += 7;
415 invert = 1;
416 } else if (!strncmp(name, "require ", 8)) {
417 name += 8;
418 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700419 } else if (!strncmp(name, "require-for-product:", 20)) {
420 // Get the product and point name past it
421 prod = name + 20;
422 name = strchr(name, ' ');
423 if (!name) return -1;
424 *name = 0;
425 name += 1;
426 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 }
428
429 x = strchr(name, '=');
430 if (x == 0) return 0;
431 *x = 0;
432 val[0] = x + 1;
433
434 for(count = 1; count < MAX_OPTIONS; count++) {
435 x = strchr(val[count - 1],'|');
436 if (x == 0) break;
437 *x = 0;
438 val[count] = x + 1;
439 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 name = strip(name);
442 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800443
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 name = strip(name);
445 if (name == 0) return -1;
446
447 /* work around an unfortunate name mismatch */
448 if (!strcmp(name,"board")) name = "product";
449
450 out = malloc(sizeof(char*) * count);
451 if (out == 0) return -1;
452
453 for(n = 0; n < count; n++) {
454 out[n] = strdup(strip(val[n]));
455 if (out[n] == 0) return -1;
456 }
457
Wink Savilleb98762f2011-04-04 17:54:59 -0700458 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 return 0;
460}
461
462static void setup_requirements(char *data, unsigned sz)
463{
464 char *s;
465
466 s = data;
467 while (sz-- > 0) {
468 if(*s == '\n') {
469 *s++ = 0;
470 if (setup_requirement_line(data)) {
471 die("out of memory");
472 }
473 data = s;
474 } else {
475 s++;
476 }
477 }
478}
479
480void queue_info_dump(void)
481{
482 fb_queue_notice("--------------------------------------------");
483 fb_queue_display("version-bootloader", "Bootloader Version...");
484 fb_queue_display("version-baseband", "Baseband Version.....");
485 fb_queue_display("serialno", "Serial Number........");
486 fb_queue_notice("--------------------------------------------");
487}
488
Colin Crossf8387882012-05-24 17:18:41 -0700489
490struct sparse_file **load_sparse_files(const char *fname, int max_size)
491{
492 int fd;
493 struct sparse_file *s;
494 int files;
495 struct sparse_file **out_s;
496
497 fd = open(fname, O_RDONLY | O_BINARY);
498 if (fd < 0) {
499 die("cannot open '%s'\n", fname);
500 }
501
502 s = sparse_file_import_auto(fd, false);
503 if (!s) {
504 die("cannot sparse read file '%s'\n", fname);
505 }
506
507 files = sparse_file_resparse(s, max_size, NULL, 0);
508 if (files < 0) {
509 die("Failed to resparse '%s'\n", fname);
510 }
511
512 out_s = calloc(sizeof(struct sparse_file *), files + 1);
513 if (!out_s) {
514 die("Failed to allocate sparse file array\n");
515 }
516
517 files = sparse_file_resparse(s, max_size, out_s, files);
518 if (files < 0) {
519 die("Failed to resparse '%s'\n", fname);
520 }
521
522 return out_s;
523}
524
525static int64_t get_target_sparse_limit(struct usb_handle *usb)
526{
527 int64_t limit = 0;
528 char response[FB_RESPONSE_SZ + 1];
529 int status = fb_getvar(usb, response, "max-download-size");
530
531 if (!status) {
532 limit = strtoul(response, NULL, 0);
533 if (limit > 0) {
534 fprintf(stderr, "target reported max download size of %lld bytes\n",
535 limit);
536 }
537 }
538
539 return limit;
540}
541
542static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
543{
544 int64_t limit;
545
546 if (sparse_limit == 0) {
547 return 0;
548 } else if (sparse_limit > 0) {
549 limit = sparse_limit;
550 } else {
551 if (target_sparse_limit == -1) {
552 target_sparse_limit = get_target_sparse_limit(usb);
553 }
554 if (target_sparse_limit > 0) {
555 limit = target_sparse_limit;
556 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700557 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700558 }
559 }
560
561 if (size > limit) {
562 return limit;
563 }
564
565 return 0;
566}
567
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700568/* Until we get lazy inode table init working in make_ext4fs, we need to
569 * erase partitions of type ext4 before flashing a filesystem so no stale
570 * inodes are left lying around. Otherwise, e2fsck gets very upset.
571 */
572static int needs_erase(const char *part)
573{
574 /* The function fb_format_supported() currently returns the value
575 * we want, so just call it.
576 */
577 return fb_format_supported(usb, part);
578}
579
Colin Crossf8387882012-05-24 17:18:41 -0700580void do_flash(usb_handle *usb, const char *pname, const char *fname)
581{
582 int64_t sz64;
583 void *data;
584 int64_t limit;
585
586 sz64 = file_size(fname);
587 limit = get_sparse_limit(usb, sz64);
588 if (limit) {
589 struct sparse_file **s = load_sparse_files(fname, limit);
590 if (s == NULL) {
591 die("cannot sparse load '%s'\n", fname);
592 }
593 while (*s) {
594 sz64 = sparse_file_len(*s, true, false);
595 fb_queue_flash_sparse(pname, *s++, sz64);
596 }
597 } else {
598 unsigned int sz;
599 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800600 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700601 fb_queue_flash(pname, data, sz);
602 }
603}
604
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605void do_update_signature(zipfile_t zip, char *fn)
606{
607 void *data;
608 unsigned sz;
609 data = unzip_file(zip, fn, &sz);
610 if (data == 0) return;
611 fb_queue_download("signature", data, sz);
612 fb_queue_command("signature", "installing signature");
613}
614
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700615void do_update(char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616{
617 void *zdata;
618 unsigned zsize;
619 void *data;
620 unsigned sz;
621 zipfile_t zip;
622
623 queue_info_dump();
624
Wink Savilleb98762f2011-04-04 17:54:59 -0700625 fb_queue_query_save("product", cur_product, sizeof(cur_product));
626
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800627 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800628 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629
630 zip = init_zipfile(zdata, zsize);
631 if(zip == 0) die("failed to access zipdata in '%s'");
632
633 data = unzip_file(zip, "android-info.txt", &sz);
634 if (data == 0) {
635 char *tmp;
636 /* fallback for older zipfiles */
637 data = unzip_file(zip, "android-product.txt", &sz);
638 if ((data == 0) || (sz < 1)) {
639 die("update package has no android-info.txt or android-product.txt");
640 }
641 tmp = malloc(sz + 128);
642 if (tmp == 0) die("out of memory");
643 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
644 data = tmp;
645 sz = strlen(tmp);
646 }
647
648 setup_requirements(data, sz);
649
650 data = unzip_file(zip, "boot.img", &sz);
651 if (data == 0) die("update package missing boot.img");
652 do_update_signature(zip, "boot.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700653 if (erase_first && needs_erase("boot")) {
654 fb_queue_erase("boot");
655 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 fb_queue_flash("boot", data, sz);
657
658 data = unzip_file(zip, "recovery.img", &sz);
659 if (data != 0) {
660 do_update_signature(zip, "recovery.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700661 if (erase_first && needs_erase("recovery")) {
662 fb_queue_erase("recovery");
663 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 fb_queue_flash("recovery", data, sz);
665 }
666
667 data = unzip_file(zip, "system.img", &sz);
668 if (data == 0) die("update package missing system.img");
669 do_update_signature(zip, "system.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700670 if (erase_first && needs_erase("system")) {
671 fb_queue_erase("system");
672 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 fb_queue_flash("system", data, sz);
674}
675
676void do_send_signature(char *fn)
677{
678 void *data;
679 unsigned sz;
680 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800681
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 xtn = strrchr(fn, '.');
683 if (!xtn) return;
684 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800685
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 strcpy(xtn,".sig");
687 data = load_file(fn, &sz);
688 strcpy(xtn,".img");
689 if (data == 0) return;
690 fb_queue_download("signature", data, sz);
691 fb_queue_command("signature", "installing signature");
692}
693
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700694void do_flashall(int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695{
696 char *fname;
697 void *data;
698 unsigned sz;
699
700 queue_info_dump();
701
Wink Savilleb98762f2011-04-04 17:54:59 -0700702 fb_queue_query_save("product", cur_product, sizeof(cur_product));
703
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704 fname = find_item("info", product);
705 if (fname == 0) die("cannot find android-info.txt");
706 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800707 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 setup_requirements(data, sz);
709
710 fname = find_item("boot", product);
711 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800712 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700714 if (erase_first && needs_erase("boot")) {
715 fb_queue_erase("boot");
716 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 fb_queue_flash("boot", data, sz);
718
719 fname = find_item("recovery", product);
720 data = load_file(fname, &sz);
721 if (data != 0) {
722 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700723 if (erase_first && needs_erase("recovery")) {
724 fb_queue_erase("recovery");
725 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 fb_queue_flash("recovery", data, sz);
727 }
728
729 fname = find_item("system", product);
730 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800731 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700733 if (erase_first && needs_erase("system")) {
734 fb_queue_erase("system");
735 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800736 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737}
738
739#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800740#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741
742int do_oem_command(int argc, char **argv)
743{
744 int i;
745 char command[256];
746 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800747
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 command[0] = 0;
749 while(1) {
750 strcat(command,*argv);
751 skip(1);
752 if(argc == 0) break;
753 strcat(command," ");
754 }
755
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800756 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757 return 0;
758}
759
Colin Crossf8387882012-05-24 17:18:41 -0700760static int64_t parse_num(const char *arg)
761{
762 char *endptr;
763 unsigned long long num;
764
765 num = strtoull(arg, &endptr, 0);
766 if (endptr == arg) {
767 return -1;
768 }
769
770 if (*endptr == 'k' || *endptr == 'K') {
771 if (num >= (-1ULL) / 1024) {
772 return -1;
773 }
774 num *= 1024LL;
775 endptr++;
776 } else if (*endptr == 'm' || *endptr == 'M') {
777 if (num >= (-1ULL) / (1024 * 1024)) {
778 return -1;
779 }
780 num *= 1024LL * 1024LL;
781 endptr++;
782 } else if (*endptr == 'g' || *endptr == 'G') {
783 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
784 return -1;
785 }
786 num *= 1024LL * 1024LL * 1024LL;
787 endptr++;
788 }
789
790 if (*endptr != '\0') {
791 return -1;
792 }
793
794 if (num > INT64_MAX) {
795 return -1;
796 }
797
798 return num;
799}
800
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801int main(int argc, char **argv)
802{
803 int wants_wipe = 0;
804 int wants_reboot = 0;
805 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700806 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 void *data;
808 unsigned sz;
Dima Zavin931175a2010-02-12 20:26:33 -0800809 unsigned page_size = 2048;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700810 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700811 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700812 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800813
Colin Crossf8387882012-05-24 17:18:41 -0700814 const struct option longopts = { 0, 0, 0, 0 };
Colin Cross8879f982012-05-22 17:53:34 -0700815
816 serial = getenv("ANDROID_SERIAL");
817
818 while (1) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700819 c = getopt_long(argc, argv, "wub:n:s:S:lp:c:i:m:h", &longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700820 if (c < 0) {
821 break;
822 }
823
824 switch (c) {
825 case 'w':
826 wants_wipe = 1;
827 break;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700828 case 'u':
829 erase_first = 0;
830 break;
Colin Cross8879f982012-05-22 17:53:34 -0700831 case 'b':
832 base_addr = strtoul(optarg, 0, 16);
833 break;
834 case 'n':
835 page_size = (unsigned)strtoul(optarg, NULL, 0);
836 if (!page_size) die("invalid page size");
837 break;
838 case 's':
839 serial = optarg;
840 break;
Colin Crossf8387882012-05-24 17:18:41 -0700841 case 'S':
842 sparse_limit = parse_num(optarg);
843 if (sparse_limit < 0) {
844 die("invalid sparse limit");
845 }
846 break;
Colin Cross9a70e5c2012-07-17 23:35:21 -0700847 case 'l':
848 long_listing = 1;
849 break;
Colin Cross8879f982012-05-22 17:53:34 -0700850 case 'p':
851 product = optarg;
852 break;
853 case 'c':
854 cmdline = optarg;
855 break;
856 case 'i': {
857 char *endptr = NULL;
858 unsigned long val;
859
860 val = strtoul(optarg, &endptr, 0);
861 if (!endptr || *endptr != '\0' || (val & ~0xffff))
862 die("invalid vendor id '%s'", optarg);
863 vendor_id = (unsigned short)val;
864 break;
865 }
866 case 'h':
867 usage();
868 return 1;
869 case '?':
870 return 1;
871 default:
872 abort();
873 }
874 }
875
876 argc -= optind;
877 argv += optind;
878
879 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800880 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700881 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800882 }
883
Colin Cross8fb6e062012-07-24 16:36:41 -0700884 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700885 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800886 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800887 return 0;
888 }
889
Colin Crossc7b75dc2012-08-29 18:17:06 -0700890 if (argc > 0 && !strcmp(*argv, "help")) {
891 usage();
892 return 0;
893 }
894
Colin Cross8879f982012-05-22 17:53:34 -0700895 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700896
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700898 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899 require(2);
900 fb_queue_display(argv[1], argv[1]);
901 skip(2);
902 } else if(!strcmp(*argv, "erase")) {
903 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700904
905 if (fb_format_supported(usb, argv[1])) {
906 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
907 }
908
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 fb_queue_erase(argv[1]);
910 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800911 } else if(!strcmp(*argv, "format")) {
912 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700913 if (erase_first && needs_erase(argv[1])) {
914 fb_queue_erase(argv[1]);
915 }
JP Abgrall30ae5802012-05-07 20:25:24 -0700916 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800917 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800918 } else if(!strcmp(*argv, "signature")) {
919 require(2);
920 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800921 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922 if (sz != 256) die("signature must be 256 bytes");
923 fb_queue_download("signature", data, sz);
924 fb_queue_command("signature", "installing signature");
925 skip(2);
926 } else if(!strcmp(*argv, "reboot")) {
927 wants_reboot = 1;
928 skip(1);
929 } else if(!strcmp(*argv, "reboot-bootloader")) {
930 wants_reboot_bootloader = 1;
931 skip(1);
932 } else if (!strcmp(*argv, "continue")) {
933 fb_queue_command("continue", "resuming boot");
934 skip(1);
935 } else if(!strcmp(*argv, "boot")) {
936 char *kname = 0;
937 char *rname = 0;
938 skip(1);
939 if (argc > 0) {
940 kname = argv[0];
941 skip(1);
942 }
943 if (argc > 0) {
944 rname = argv[0];
945 skip(1);
946 }
Dima Zavin931175a2010-02-12 20:26:33 -0800947 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800948 if (data == 0) return 1;
949 fb_queue_download("boot.img", data, sz);
950 fb_queue_command("boot", "booting");
951 } else if(!strcmp(*argv, "flash")) {
952 char *pname = argv[1];
953 char *fname = 0;
954 require(2);
955 if (argc > 2) {
956 fname = argv[2];
957 skip(3);
958 } else {
959 fname = find_item(pname, product);
960 skip(2);
961 }
962 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700963 if (erase_first && needs_erase(pname)) {
964 fb_queue_erase(pname);
965 }
Colin Crossf8387882012-05-24 17:18:41 -0700966 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800967 } else if(!strcmp(*argv, "flash:raw")) {
968 char *pname = argv[1];
969 char *kname = argv[2];
970 char *rname = 0;
971 require(3);
972 if(argc > 3) {
973 rname = argv[3];
974 skip(4);
975 } else {
976 skip(3);
977 }
Dima Zavin931175a2010-02-12 20:26:33 -0800978 data = load_bootable_image(page_size, kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800979 if (data == 0) die("cannot load bootable image");
980 fb_queue_flash(pname, data, sz);
981 } else if(!strcmp(*argv, "flashall")) {
982 skip(1);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700983 do_flashall(erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800984 wants_reboot = 1;
985 } else if(!strcmp(*argv, "update")) {
986 if (argc > 1) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700987 do_update(argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800988 skip(2);
989 } else {
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700990 do_update("update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800991 skip(1);
992 }
993 wants_reboot = 1;
994 } else if(!strcmp(*argv, "oem")) {
995 argc = do_oem_command(argc, argv);
996 } else {
997 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800998 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800999 }
1000 }
1001
1002 if (wants_wipe) {
1003 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001004 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001005 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001006 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007 }
1008 if (wants_reboot) {
1009 fb_queue_reboot();
1010 } else if (wants_reboot_bootloader) {
1011 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
1012 }
1013
Scott Anderson13081c62012-04-06 12:39:30 -07001014 if (fb_queue_is_empty())
1015 return 0;
1016
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001017 status = fb_execute_queue(usb);
1018 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001019}