blob: 447b2572b70fb4234245473bc55ef948a9ce84a7 [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
JP Abgrall7b8970c2013-03-07 17:06:41 -080061boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
62 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
63 void *second, unsigned second_size, unsigned second_offset,
64 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070065 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
JP Abgrall7b8970c2013-03-07 17:06:41 -080077unsigned page_size = 2048;
78unsigned base_addr = 0x10000000;
79unsigned kernel_offset = 0x00008000;
80unsigned ramdisk_offset = 0x01000000;
81unsigned second_offset = 0x00f00000;
82unsigned tags_offset = 0x00000100;
83
Brian Swetland2a63bb72009-04-28 16:05:07 -070084
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085void die(const char *fmt, ...)
86{
87 va_list ap;
88 va_start(ap, fmt);
89 fprintf(stderr,"error: ");
90 vfprintf(stderr, fmt, ap);
91 fprintf(stderr,"\n");
92 va_end(ap);
93 exit(1);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080094}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
96void get_my_path(char *path);
97
98char *find_item(const char *item, const char *product)
99{
100 char *dir;
101 char *fn;
102 char path[PATH_MAX + 128];
103
104 if(!strcmp(item,"boot")) {
105 fn = "boot.img";
106 } else if(!strcmp(item,"recovery")) {
107 fn = "recovery.img";
108 } else if(!strcmp(item,"system")) {
109 fn = "system.img";
110 } else if(!strcmp(item,"userdata")) {
111 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700112 } else if(!strcmp(item,"cache")) {
113 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 } else if(!strcmp(item,"info")) {
115 fn = "android-info.txt";
116 } else {
117 fprintf(stderr,"unknown partition '%s'\n", item);
118 return 0;
119 }
120
121 if(product) {
122 get_my_path(path);
123 sprintf(path + strlen(path),
124 "../../../target/product/%s/%s", product, fn);
125 return strdup(path);
126 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800127
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 dir = getenv("ANDROID_PRODUCT_OUT");
129 if((dir == 0) || (dir[0] == 0)) {
130 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
131 return 0;
132 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800133
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134 sprintf(path, "%s/%s", dir, fn);
135 return strdup(path);
136}
137
138#ifdef _WIN32
139void *load_file(const char *fn, unsigned *_sz);
Colin Crossf8387882012-05-24 17:18:41 -0700140int64_t file_size(const char *fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141#else
Colin Crossf8387882012-05-24 17:18:41 -0700142#if defined(__APPLE__) && defined(__MACH__)
143#define lseek64 lseek
144#define off64_t off_t
145#endif
146
147int64_t file_size(const char *fn)
148{
149 off64_t off;
150 int fd;
151
152 fd = open(fn, O_RDONLY);
153 if (fd < 0) return -1;
154
155 off = lseek64(fd, 0, SEEK_END);
156 close(fd);
157
158 return off;
159}
160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161void *load_file(const char *fn, unsigned *_sz)
162{
163 char *data;
164 int sz;
165 int fd;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800166 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
168 data = 0;
169 fd = open(fn, O_RDONLY);
170 if(fd < 0) return 0;
171
172 sz = lseek(fd, 0, SEEK_END);
173 if(sz < 0) goto oops;
174
175 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
176
177 data = (char*) malloc(sz);
178 if(data == 0) goto oops;
179
180 if(read(fd, data, sz) != sz) goto oops;
181 close(fd);
182
183 if(_sz) *_sz = sz;
184 return data;
185
186oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800187 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 close(fd);
189 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800190 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return 0;
192}
193#endif
194
JP Abgralla032ded2012-06-06 11:53:33 -0700195int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
196{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400198 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800199 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700200 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800201 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700202 (info->dev_vendor != 0x0fce) && // Sony Ericsson
203 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400204 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800205 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800206 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800207 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800208 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400209 (info->dev_vendor != 0x0bb4)) // HTC
210 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 if(info->ifc_class != 0xff) return -1;
212 if(info->ifc_subclass != 0x42) return -1;
213 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700214 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
217 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 return 0;
219}
220
JP Abgrall7b8970c2013-03-07 17:06:41 -0800221int match_fastboot(usb_ifc_info *info)
222{
223 return match_fastboot_with_serial(info, serial);
224}
225
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226int list_devices_callback(usb_ifc_info *info)
227{
JP Abgralla032ded2012-06-06 11:53:33 -0700228 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700230 if (!info->writable) {
231 serial = "no permissions"; // like "adb devices"
232 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 if (!serial[0]) {
234 serial = "????????????";
235 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700236 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700237 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700238 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700239 } else if (!info->device_path) {
240 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700241 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700242 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700243 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 }
245
246 return -1;
247}
248
249usb_handle *open_device(void)
250{
251 static usb_handle *usb = 0;
252 int announce = 1;
253
254 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800255
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 for(;;) {
257 usb = usb_open(match_fastboot);
258 if(usb) return usb;
259 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800260 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800261 fprintf(stderr,"< waiting for device >\n");
262 }
263 sleep(1);
264 }
265}
266
267void list_devices(void) {
268 // We don't actually open a USB device here,
269 // just getting our callback called so we can
270 // list all the connected devices.
271 usb_open(list_devices_callback);
272}
273
274void usage(void)
275{
276 fprintf(stderr,
277/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
278 "usage: fastboot [ <option> ] <command>\n"
279 "\n"
280 "commands:\n"
281 " update <filename> reflash device from update.zip\n"
282 " flashall flash boot + recovery + system\n"
283 " flash <partition> [ <filename> ] write a file to a flash partition\n"
284 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800285 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 " getvar <variable> display a bootloader variable\n"
287 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
288 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
289 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700290 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " reboot reboot device normally\n"
292 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800293 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 "\n"
295 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700296 " -w erase userdata and cache (and format\n"
297 " if supported by partition type)\n"
298 " -u do not first erase partition before\n"
299 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700300 " -s <specific device> specify device serial number\n"
301 " or path to device port\n"
302 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 " -p <product> specify product name\n"
304 " -c <cmdline> override kernel commandline\n"
305 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800306 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800307 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700308 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700309 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311}
312
JP Abgrall7b8970c2013-03-07 17:06:41 -0800313void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 unsigned *sz, const char *cmdline)
315{
316 void *kdata = 0, *rdata = 0;
317 unsigned ksize = 0, rsize = 0;
318 void *bdata;
319 unsigned bsize;
320
321 if(kernel == 0) {
322 fprintf(stderr, "no image specified\n");
323 return 0;
324 }
325
326 kdata = load_file(kernel, &ksize);
327 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800328 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 return 0;
330 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800331
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 /* is this actually a boot image? */
333 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
334 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 if(ramdisk) {
337 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
338 return 0;
339 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 *sz = ksize;
342 return kdata;
343 }
344
345 if(ramdisk) {
346 rdata = load_file(ramdisk, &rsize);
347 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800348 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 return 0;
350 }
351 }
352
353 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800354 bdata = mkbootimg(kdata, ksize, kernel_offset,
355 rdata, rsize, ramdisk_offset,
356 0, 0, second_offset,
357 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 if(bdata == 0) {
359 fprintf(stderr,"failed to create boot.img\n");
360 return 0;
361 }
362 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
363 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
364 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 return bdata;
367}
368
369void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
370{
371 void *data;
372 zipentry_t entry;
373 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 entry = lookup_zipentry(zip, name);
376 if (entry == NULL) {
377 fprintf(stderr, "archive does not contain '%s'\n", name);
378 return 0;
379 }
380
381 *sz = get_zipentry_size(entry);
382
383 datasz = *sz * 1.001;
384 data = malloc(datasz);
385
386 if(data == 0) {
387 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
388 return 0;
389 }
390
391 if (decompress_zipentry(entry, data, datasz)) {
392 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
393 free(data);
394 return 0;
395 }
396
397 return data;
398}
399
400static char *strip(char *s)
401{
402 int n;
403 while(*s && isspace(*s)) s++;
404 n = strlen(s);
405 while(n-- > 0) {
406 if(!isspace(s[n])) break;
407 s[n] = 0;
408 }
409 return s;
410}
411
412#define MAX_OPTIONS 32
413static int setup_requirement_line(char *name)
414{
415 char *val[MAX_OPTIONS];
416 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700417 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 unsigned n, count;
419 char *x;
420 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 if (!strncmp(name, "reject ", 7)) {
423 name += 7;
424 invert = 1;
425 } else if (!strncmp(name, "require ", 8)) {
426 name += 8;
427 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700428 } else if (!strncmp(name, "require-for-product:", 20)) {
429 // Get the product and point name past it
430 prod = name + 20;
431 name = strchr(name, ' ');
432 if (!name) return -1;
433 *name = 0;
434 name += 1;
435 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 }
437
438 x = strchr(name, '=');
439 if (x == 0) return 0;
440 *x = 0;
441 val[0] = x + 1;
442
443 for(count = 1; count < MAX_OPTIONS; count++) {
444 x = strchr(val[count - 1],'|');
445 if (x == 0) break;
446 *x = 0;
447 val[count] = x + 1;
448 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 name = strip(name);
451 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800452
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 name = strip(name);
454 if (name == 0) return -1;
455
456 /* work around an unfortunate name mismatch */
457 if (!strcmp(name,"board")) name = "product";
458
459 out = malloc(sizeof(char*) * count);
460 if (out == 0) return -1;
461
462 for(n = 0; n < count; n++) {
463 out[n] = strdup(strip(val[n]));
464 if (out[n] == 0) return -1;
465 }
466
Wink Savilleb98762f2011-04-04 17:54:59 -0700467 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 return 0;
469}
470
471static void setup_requirements(char *data, unsigned sz)
472{
473 char *s;
474
475 s = data;
476 while (sz-- > 0) {
477 if(*s == '\n') {
478 *s++ = 0;
479 if (setup_requirement_line(data)) {
480 die("out of memory");
481 }
482 data = s;
483 } else {
484 s++;
485 }
486 }
487}
488
489void queue_info_dump(void)
490{
491 fb_queue_notice("--------------------------------------------");
492 fb_queue_display("version-bootloader", "Bootloader Version...");
493 fb_queue_display("version-baseband", "Baseband Version.....");
494 fb_queue_display("serialno", "Serial Number........");
495 fb_queue_notice("--------------------------------------------");
496}
497
Colin Crossf8387882012-05-24 17:18:41 -0700498
499struct sparse_file **load_sparse_files(const char *fname, int max_size)
500{
501 int fd;
502 struct sparse_file *s;
503 int files;
504 struct sparse_file **out_s;
505
506 fd = open(fname, O_RDONLY | O_BINARY);
507 if (fd < 0) {
508 die("cannot open '%s'\n", fname);
509 }
510
511 s = sparse_file_import_auto(fd, false);
512 if (!s) {
513 die("cannot sparse read file '%s'\n", fname);
514 }
515
516 files = sparse_file_resparse(s, max_size, NULL, 0);
517 if (files < 0) {
518 die("Failed to resparse '%s'\n", fname);
519 }
520
521 out_s = calloc(sizeof(struct sparse_file *), files + 1);
522 if (!out_s) {
523 die("Failed to allocate sparse file array\n");
524 }
525
526 files = sparse_file_resparse(s, max_size, out_s, files);
527 if (files < 0) {
528 die("Failed to resparse '%s'\n", fname);
529 }
530
531 return out_s;
532}
533
534static int64_t get_target_sparse_limit(struct usb_handle *usb)
535{
536 int64_t limit = 0;
537 char response[FB_RESPONSE_SZ + 1];
538 int status = fb_getvar(usb, response, "max-download-size");
539
540 if (!status) {
541 limit = strtoul(response, NULL, 0);
542 if (limit > 0) {
543 fprintf(stderr, "target reported max download size of %lld bytes\n",
544 limit);
545 }
546 }
547
548 return limit;
549}
550
551static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
552{
553 int64_t limit;
554
555 if (sparse_limit == 0) {
556 return 0;
557 } else if (sparse_limit > 0) {
558 limit = sparse_limit;
559 } else {
560 if (target_sparse_limit == -1) {
561 target_sparse_limit = get_target_sparse_limit(usb);
562 }
563 if (target_sparse_limit > 0) {
564 limit = target_sparse_limit;
565 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700566 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700567 }
568 }
569
570 if (size > limit) {
571 return limit;
572 }
573
574 return 0;
575}
576
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700577/* Until we get lazy inode table init working in make_ext4fs, we need to
578 * erase partitions of type ext4 before flashing a filesystem so no stale
579 * inodes are left lying around. Otherwise, e2fsck gets very upset.
580 */
581static int needs_erase(const char *part)
582{
583 /* The function fb_format_supported() currently returns the value
584 * we want, so just call it.
585 */
586 return fb_format_supported(usb, part);
587}
588
Colin Crossf8387882012-05-24 17:18:41 -0700589void do_flash(usb_handle *usb, const char *pname, const char *fname)
590{
591 int64_t sz64;
592 void *data;
593 int64_t limit;
594
595 sz64 = file_size(fname);
596 limit = get_sparse_limit(usb, sz64);
597 if (limit) {
598 struct sparse_file **s = load_sparse_files(fname, limit);
599 if (s == NULL) {
600 die("cannot sparse load '%s'\n", fname);
601 }
602 while (*s) {
603 sz64 = sparse_file_len(*s, true, false);
604 fb_queue_flash_sparse(pname, *s++, sz64);
605 }
606 } else {
607 unsigned int sz;
608 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800609 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700610 fb_queue_flash(pname, data, sz);
611 }
612}
613
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614void do_update_signature(zipfile_t zip, char *fn)
615{
616 void *data;
617 unsigned sz;
618 data = unzip_file(zip, fn, &sz);
619 if (data == 0) return;
620 fb_queue_download("signature", data, sz);
621 fb_queue_command("signature", "installing signature");
622}
623
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700624void do_update(char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800625{
626 void *zdata;
627 unsigned zsize;
628 void *data;
629 unsigned sz;
630 zipfile_t zip;
631
632 queue_info_dump();
633
Wink Savilleb98762f2011-04-04 17:54:59 -0700634 fb_queue_query_save("product", cur_product, sizeof(cur_product));
635
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800637 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638
639 zip = init_zipfile(zdata, zsize);
640 if(zip == 0) die("failed to access zipdata in '%s'");
641
642 data = unzip_file(zip, "android-info.txt", &sz);
643 if (data == 0) {
644 char *tmp;
645 /* fallback for older zipfiles */
646 data = unzip_file(zip, "android-product.txt", &sz);
647 if ((data == 0) || (sz < 1)) {
648 die("update package has no android-info.txt or android-product.txt");
649 }
650 tmp = malloc(sz + 128);
651 if (tmp == 0) die("out of memory");
652 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
653 data = tmp;
654 sz = strlen(tmp);
655 }
656
657 setup_requirements(data, sz);
658
659 data = unzip_file(zip, "boot.img", &sz);
660 if (data == 0) die("update package missing boot.img");
661 do_update_signature(zip, "boot.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700662 if (erase_first && needs_erase("boot")) {
663 fb_queue_erase("boot");
664 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 fb_queue_flash("boot", data, sz);
666
667 data = unzip_file(zip, "recovery.img", &sz);
668 if (data != 0) {
669 do_update_signature(zip, "recovery.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700670 if (erase_first && needs_erase("recovery")) {
671 fb_queue_erase("recovery");
672 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 fb_queue_flash("recovery", data, sz);
674 }
675
676 data = unzip_file(zip, "system.img", &sz);
677 if (data == 0) die("update package missing system.img");
678 do_update_signature(zip, "system.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700679 if (erase_first && needs_erase("system")) {
680 fb_queue_erase("system");
681 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 fb_queue_flash("system", data, sz);
683}
684
685void do_send_signature(char *fn)
686{
687 void *data;
688 unsigned sz;
689 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800690
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 xtn = strrchr(fn, '.');
692 if (!xtn) return;
693 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800694
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 strcpy(xtn,".sig");
696 data = load_file(fn, &sz);
697 strcpy(xtn,".img");
698 if (data == 0) return;
699 fb_queue_download("signature", data, sz);
700 fb_queue_command("signature", "installing signature");
701}
702
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700703void do_flashall(int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704{
705 char *fname;
706 void *data;
707 unsigned sz;
708
709 queue_info_dump();
710
Wink Savilleb98762f2011-04-04 17:54:59 -0700711 fb_queue_query_save("product", cur_product, sizeof(cur_product));
712
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 fname = find_item("info", product);
714 if (fname == 0) die("cannot find android-info.txt");
715 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800716 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 setup_requirements(data, sz);
718
719 fname = find_item("boot", product);
720 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800721 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700723 if (erase_first && needs_erase("boot")) {
724 fb_queue_erase("boot");
725 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 fb_queue_flash("boot", data, sz);
727
728 fname = find_item("recovery", product);
729 data = load_file(fname, &sz);
730 if (data != 0) {
731 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700732 if (erase_first && needs_erase("recovery")) {
733 fb_queue_erase("recovery");
734 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 fb_queue_flash("recovery", data, sz);
736 }
737
738 fname = find_item("system", product);
739 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800740 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700742 if (erase_first && needs_erase("system")) {
743 fb_queue_erase("system");
744 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800745 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746}
747
748#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800749#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800750
751int do_oem_command(int argc, char **argv)
752{
753 int i;
754 char command[256];
755 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800756
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757 command[0] = 0;
758 while(1) {
759 strcat(command,*argv);
760 skip(1);
761 if(argc == 0) break;
762 strcat(command," ");
763 }
764
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800765 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 return 0;
767}
768
Colin Crossf8387882012-05-24 17:18:41 -0700769static int64_t parse_num(const char *arg)
770{
771 char *endptr;
772 unsigned long long num;
773
774 num = strtoull(arg, &endptr, 0);
775 if (endptr == arg) {
776 return -1;
777 }
778
779 if (*endptr == 'k' || *endptr == 'K') {
780 if (num >= (-1ULL) / 1024) {
781 return -1;
782 }
783 num *= 1024LL;
784 endptr++;
785 } else if (*endptr == 'm' || *endptr == 'M') {
786 if (num >= (-1ULL) / (1024 * 1024)) {
787 return -1;
788 }
789 num *= 1024LL * 1024LL;
790 endptr++;
791 } else if (*endptr == 'g' || *endptr == 'G') {
792 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
793 return -1;
794 }
795 num *= 1024LL * 1024LL * 1024LL;
796 endptr++;
797 }
798
799 if (*endptr != '\0') {
800 return -1;
801 }
802
803 if (num > INT64_MAX) {
804 return -1;
805 }
806
807 return num;
808}
809
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810int main(int argc, char **argv)
811{
812 int wants_wipe = 0;
813 int wants_reboot = 0;
814 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700815 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816 void *data;
817 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700818 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700819 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700820 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821
JP Abgrall7b8970c2013-03-07 17:06:41 -0800822 const struct option longopts[] = {
823 {"base", required_argument, 0, 'b'},
824 {"kernel_offset", required_argument, 0, 'k'},
825 {"page_size", required_argument, 0, 'n'},
826 {"ramdisk_offset", required_argument, 0, 'r'},
827 {"help", 0, 0, 'h'},
828 {0, 0, 0, 0}
829 };
Colin Cross8879f982012-05-22 17:53:34 -0700830
831 serial = getenv("ANDROID_SERIAL");
832
833 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800834 int option_index = 0;
835 c = getopt_long(argc, argv, "wub:k:n:r:s:S:lp:c:i:m:h", longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -0700836 if (c < 0) {
837 break;
838 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800839 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700840 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700841 case 'b':
842 base_addr = strtoul(optarg, 0, 16);
843 break;
Colin Cross8879f982012-05-22 17:53:34 -0700844 case 'c':
845 cmdline = optarg;
846 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800847 case 'h':
848 usage();
849 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700850 case 'i': {
851 char *endptr = NULL;
852 unsigned long val;
853
854 val = strtoul(optarg, &endptr, 0);
855 if (!endptr || *endptr != '\0' || (val & ~0xffff))
856 die("invalid vendor id '%s'", optarg);
857 vendor_id = (unsigned short)val;
858 break;
859 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800860 case 'k':
861 kernel_offset = strtoul(optarg, 0, 16);
862 break;
863 case 'l':
864 long_listing = 1;
865 break;
866 case 'n':
867 page_size = (unsigned)strtoul(optarg, NULL, 0);
868 if (!page_size) die("invalid page size");
869 break;
870 case 'p':
871 product = optarg;
872 break;
873 case 'r':
874 ramdisk_offset = strtoul(optarg, 0, 16);
875 break;
876 case 's':
877 serial = optarg;
878 break;
879 case 'S':
880 sparse_limit = parse_num(optarg);
881 if (sparse_limit < 0) {
882 die("invalid sparse limit");
883 }
884 break;
885 case 'u':
886 erase_first = 0;
887 break;
888 case 'w':
889 wants_wipe = 1;
890 break;
Colin Cross8879f982012-05-22 17:53:34 -0700891 case '?':
892 return 1;
893 default:
894 abort();
895 }
896 }
897
898 argc -= optind;
899 argv += optind;
900
901 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800902 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700903 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800904 }
905
Colin Cross8fb6e062012-07-24 16:36:41 -0700906 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700907 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800908 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800909 return 0;
910 }
911
Colin Crossc7b75dc2012-08-29 18:17:06 -0700912 if (argc > 0 && !strcmp(*argv, "help")) {
913 usage();
914 return 0;
915 }
916
Colin Cross8879f982012-05-22 17:53:34 -0700917 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700918
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700920 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800921 require(2);
922 fb_queue_display(argv[1], argv[1]);
923 skip(2);
924 } else if(!strcmp(*argv, "erase")) {
925 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700926
927 if (fb_format_supported(usb, argv[1])) {
928 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
929 }
930
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800931 fb_queue_erase(argv[1]);
932 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800933 } else if(!strcmp(*argv, "format")) {
934 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700935 if (erase_first && needs_erase(argv[1])) {
936 fb_queue_erase(argv[1]);
937 }
JP Abgrall30ae5802012-05-07 20:25:24 -0700938 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800939 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940 } else if(!strcmp(*argv, "signature")) {
941 require(2);
942 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800943 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800944 if (sz != 256) die("signature must be 256 bytes");
945 fb_queue_download("signature", data, sz);
946 fb_queue_command("signature", "installing signature");
947 skip(2);
948 } else if(!strcmp(*argv, "reboot")) {
949 wants_reboot = 1;
950 skip(1);
951 } else if(!strcmp(*argv, "reboot-bootloader")) {
952 wants_reboot_bootloader = 1;
953 skip(1);
954 } else if (!strcmp(*argv, "continue")) {
955 fb_queue_command("continue", "resuming boot");
956 skip(1);
957 } else if(!strcmp(*argv, "boot")) {
958 char *kname = 0;
959 char *rname = 0;
960 skip(1);
961 if (argc > 0) {
962 kname = argv[0];
963 skip(1);
964 }
965 if (argc > 0) {
966 rname = argv[0];
967 skip(1);
968 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800969 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800970 if (data == 0) return 1;
971 fb_queue_download("boot.img", data, sz);
972 fb_queue_command("boot", "booting");
973 } else if(!strcmp(*argv, "flash")) {
974 char *pname = argv[1];
975 char *fname = 0;
976 require(2);
977 if (argc > 2) {
978 fname = argv[2];
979 skip(3);
980 } else {
981 fname = find_item(pname, product);
982 skip(2);
983 }
984 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700985 if (erase_first && needs_erase(pname)) {
986 fb_queue_erase(pname);
987 }
Colin Crossf8387882012-05-24 17:18:41 -0700988 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800989 } else if(!strcmp(*argv, "flash:raw")) {
990 char *pname = argv[1];
991 char *kname = argv[2];
992 char *rname = 0;
993 require(3);
994 if(argc > 3) {
995 rname = argv[3];
996 skip(4);
997 } else {
998 skip(3);
999 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001000 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 if (data == 0) die("cannot load bootable image");
1002 fb_queue_flash(pname, data, sz);
1003 } else if(!strcmp(*argv, "flashall")) {
1004 skip(1);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001005 do_flashall(erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001006 wants_reboot = 1;
1007 } else if(!strcmp(*argv, "update")) {
1008 if (argc > 1) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001009 do_update(argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001010 skip(2);
1011 } else {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001012 do_update("update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001013 skip(1);
1014 }
1015 wants_reboot = 1;
1016 } else if(!strcmp(*argv, "oem")) {
1017 argc = do_oem_command(argc, argv);
1018 } else {
1019 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001020 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021 }
1022 }
1023
1024 if (wants_wipe) {
1025 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001026 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001027 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001028 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029 }
1030 if (wants_reboot) {
1031 fb_queue_reboot();
1032 } else if (wants_reboot_bootloader) {
1033 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
1034 }
1035
Scott Anderson13081c62012-04-06 12:39:30 -07001036 if (fb_queue_is_empty())
1037 return 0;
1038
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001039 status = fb_execute_queue(usb);
1040 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041}