blob: 70b838f034f198e0e05d0015cd4211bb932e2e26 [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>
Colin Crossf8387882012-05-24 17:18:41 -070033#include <stdbool.h>
34#include <stdint.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <string.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <limits.h>
40#include <ctype.h>
Colin Cross8879f982012-05-22 17:53:34 -070041#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
43#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
45
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#include <bootimg.h>
Colin Crossf8387882012-05-24 17:18:41 -070047#include <sparse/sparse.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#include <zipfile/zipfile.h>
49
50#include "fastboot.h"
51
Colin Crossf8387882012-05-24 17:18:41 -070052#ifndef O_BINARY
53#define O_BINARY 0
54#endif
55
Wink Savilleb98762f2011-04-04 17:54:59 -070056char cur_product[FB_RESPONSE_SZ + 1];
57
Brian Swetland2a63bb72009-04-28 16:05:07 -070058void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
59
JP Abgrall7b8970c2013-03-07 17:06:41 -080060boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
61 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
62 void *second, unsigned second_size, unsigned second_offset,
63 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070064 unsigned *bootimg_size);
65
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066static usb_handle *usb = 0;
67static const char *serial = 0;
68static const char *product = 0;
69static const char *cmdline = 0;
70static int wipe_data = 0;
71static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070072static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070073static int64_t sparse_limit = -1;
74static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
JP Abgrall7b8970c2013-03-07 17:06:41 -080076unsigned page_size = 2048;
77unsigned base_addr = 0x10000000;
78unsigned kernel_offset = 0x00008000;
79unsigned ramdisk_offset = 0x01000000;
80unsigned second_offset = 0x00f00000;
81unsigned tags_offset = 0x00000100;
82
Brian Swetland2a63bb72009-04-28 16:05:07 -070083
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084void get_my_path(char *path);
85
86char *find_item(const char *item, const char *product)
87{
88 char *dir;
89 char *fn;
90 char path[PATH_MAX + 128];
91
92 if(!strcmp(item,"boot")) {
93 fn = "boot.img";
94 } else if(!strcmp(item,"recovery")) {
95 fn = "recovery.img";
96 } else if(!strcmp(item,"system")) {
97 fn = "system.img";
98 } else if(!strcmp(item,"userdata")) {
99 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700100 } else if(!strcmp(item,"cache")) {
101 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 } else if(!strcmp(item,"info")) {
103 fn = "android-info.txt";
104 } else {
105 fprintf(stderr,"unknown partition '%s'\n", item);
106 return 0;
107 }
108
109 if(product) {
110 get_my_path(path);
111 sprintf(path + strlen(path),
112 "../../../target/product/%s/%s", product, fn);
113 return strdup(path);
114 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800115
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 dir = getenv("ANDROID_PRODUCT_OUT");
117 if((dir == 0) || (dir[0] == 0)) {
118 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
119 return 0;
120 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800121
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 sprintf(path, "%s/%s", dir, fn);
123 return strdup(path);
124}
125
126#ifdef _WIN32
127void *load_file(const char *fn, unsigned *_sz);
Colin Crossf8387882012-05-24 17:18:41 -0700128int64_t file_size(const char *fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129#else
Colin Crossf8387882012-05-24 17:18:41 -0700130#if defined(__APPLE__) && defined(__MACH__)
131#define lseek64 lseek
132#define off64_t off_t
133#endif
134
135int64_t file_size(const char *fn)
136{
137 off64_t off;
138 int fd;
139
140 fd = open(fn, O_RDONLY);
141 if (fd < 0) return -1;
142
143 off = lseek64(fd, 0, SEEK_END);
144 close(fd);
145
146 return off;
147}
148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149void *load_file(const char *fn, unsigned *_sz)
150{
151 char *data;
152 int sz;
153 int fd;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800154 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
156 data = 0;
157 fd = open(fn, O_RDONLY);
158 if(fd < 0) return 0;
159
160 sz = lseek(fd, 0, SEEK_END);
161 if(sz < 0) goto oops;
162
163 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
164
165 data = (char*) malloc(sz);
166 if(data == 0) goto oops;
167
168 if(read(fd, data, sz) != sz) goto oops;
169 close(fd);
170
171 if(_sz) *_sz = sz;
172 return data;
173
174oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return 0;
180}
181#endif
182
JP Abgralla032ded2012-06-06 11:53:33 -0700183int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
184{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400186 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800187 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700188 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800189 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700190 (info->dev_vendor != 0x0fce) && // Sony Ericsson
191 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400192 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800193 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800194 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800195 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800196 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400197 (info->dev_vendor != 0x0bb4)) // HTC
198 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 if(info->ifc_class != 0xff) return -1;
200 if(info->ifc_subclass != 0x42) return -1;
201 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700202 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700204 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
205 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return 0;
207}
208
JP Abgrall7b8970c2013-03-07 17:06:41 -0800209int match_fastboot(usb_ifc_info *info)
210{
211 return match_fastboot_with_serial(info, serial);
212}
213
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214int list_devices_callback(usb_ifc_info *info)
215{
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 if (!info->writable) {
219 serial = "no permissions"; // like "adb devices"
220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 if (!serial[0]) {
222 serial = "????????????";
223 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700224 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700225 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700226 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700227 } else if (!info->device_path) {
228 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700229 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700230 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
233
234 return -1;
235}
236
237usb_handle *open_device(void)
238{
239 static usb_handle *usb = 0;
240 int announce = 1;
241
242 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800243
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 for(;;) {
245 usb = usb_open(match_fastboot);
246 if(usb) return usb;
247 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 fprintf(stderr,"< waiting for device >\n");
250 }
251 sleep(1);
252 }
253}
254
255void list_devices(void) {
256 // We don't actually open a USB device here,
257 // just getting our callback called so we can
258 // list all the connected devices.
259 usb_open(list_devices_callback);
260}
261
262void usage(void)
263{
264 fprintf(stderr,
265/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
266 "usage: fastboot [ <option> ] <command>\n"
267 "\n"
268 "commands:\n"
269 " update <filename> reflash device from update.zip\n"
270 " flashall flash boot + recovery + system\n"
271 " flash <partition> [ <filename> ] write a file to a flash partition\n"
272 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800273 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 " getvar <variable> display a bootloader variable\n"
275 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
276 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
277 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700278 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 " reboot reboot device normally\n"
280 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800281 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 "\n"
283 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700284 " -w erase userdata and cache (and format\n"
285 " if supported by partition type)\n"
286 " -u do not first erase partition before\n"
287 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700288 " -s <specific device> specify device serial number\n"
289 " or path to device port\n"
290 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " -p <product> specify product name\n"
292 " -c <cmdline> override kernel commandline\n"
293 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800294 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800295 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700296 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700297 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299}
300
JP Abgrall7b8970c2013-03-07 17:06:41 -0800301void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 unsigned *sz, const char *cmdline)
303{
304 void *kdata = 0, *rdata = 0;
305 unsigned ksize = 0, rsize = 0;
306 void *bdata;
307 unsigned bsize;
308
309 if(kernel == 0) {
310 fprintf(stderr, "no image specified\n");
311 return 0;
312 }
313
314 kdata = load_file(kernel, &ksize);
315 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800316 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 return 0;
318 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800319
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 /* is this actually a boot image? */
321 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
322 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800323
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 if(ramdisk) {
325 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
326 return 0;
327 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800328
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 *sz = ksize;
330 return kdata;
331 }
332
333 if(ramdisk) {
334 rdata = load_file(ramdisk, &rsize);
335 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800336 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 return 0;
338 }
339 }
340
341 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800342 bdata = mkbootimg(kdata, ksize, kernel_offset,
343 rdata, rsize, ramdisk_offset,
344 0, 0, second_offset,
345 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 if(bdata == 0) {
347 fprintf(stderr,"failed to create boot.img\n");
348 return 0;
349 }
350 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
351 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
352 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800353
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 return bdata;
355}
356
357void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
358{
359 void *data;
360 zipentry_t entry;
361 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800362
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363 entry = lookup_zipentry(zip, name);
364 if (entry == NULL) {
365 fprintf(stderr, "archive does not contain '%s'\n", name);
366 return 0;
367 }
368
369 *sz = get_zipentry_size(entry);
370
371 datasz = *sz * 1.001;
372 data = malloc(datasz);
373
374 if(data == 0) {
375 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
376 return 0;
377 }
378
379 if (decompress_zipentry(entry, data, datasz)) {
380 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
381 free(data);
382 return 0;
383 }
384
385 return data;
386}
387
388static char *strip(char *s)
389{
390 int n;
391 while(*s && isspace(*s)) s++;
392 n = strlen(s);
393 while(n-- > 0) {
394 if(!isspace(s[n])) break;
395 s[n] = 0;
396 }
397 return s;
398}
399
400#define MAX_OPTIONS 32
401static int setup_requirement_line(char *name)
402{
403 char *val[MAX_OPTIONS];
404 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700405 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406 unsigned n, count;
407 char *x;
408 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800409
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 if (!strncmp(name, "reject ", 7)) {
411 name += 7;
412 invert = 1;
413 } else if (!strncmp(name, "require ", 8)) {
414 name += 8;
415 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700416 } else if (!strncmp(name, "require-for-product:", 20)) {
417 // Get the product and point name past it
418 prod = name + 20;
419 name = strchr(name, ' ');
420 if (!name) return -1;
421 *name = 0;
422 name += 1;
423 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 }
425
426 x = strchr(name, '=');
427 if (x == 0) return 0;
428 *x = 0;
429 val[0] = x + 1;
430
431 for(count = 1; count < MAX_OPTIONS; count++) {
432 x = strchr(val[count - 1],'|');
433 if (x == 0) break;
434 *x = 0;
435 val[count] = x + 1;
436 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 name = strip(name);
439 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 name = strip(name);
442 if (name == 0) return -1;
443
444 /* work around an unfortunate name mismatch */
445 if (!strcmp(name,"board")) name = "product";
446
447 out = malloc(sizeof(char*) * count);
448 if (out == 0) return -1;
449
450 for(n = 0; n < count; n++) {
451 out[n] = strdup(strip(val[n]));
452 if (out[n] == 0) return -1;
453 }
454
Wink Savilleb98762f2011-04-04 17:54:59 -0700455 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 return 0;
457}
458
459static void setup_requirements(char *data, unsigned sz)
460{
461 char *s;
462
463 s = data;
464 while (sz-- > 0) {
465 if(*s == '\n') {
466 *s++ = 0;
467 if (setup_requirement_line(data)) {
468 die("out of memory");
469 }
470 data = s;
471 } else {
472 s++;
473 }
474 }
475}
476
477void queue_info_dump(void)
478{
479 fb_queue_notice("--------------------------------------------");
480 fb_queue_display("version-bootloader", "Bootloader Version...");
481 fb_queue_display("version-baseband", "Baseband Version.....");
482 fb_queue_display("serialno", "Serial Number........");
483 fb_queue_notice("--------------------------------------------");
484}
485
Colin Crossf8387882012-05-24 17:18:41 -0700486
487struct sparse_file **load_sparse_files(const char *fname, int max_size)
488{
489 int fd;
490 struct sparse_file *s;
491 int files;
492 struct sparse_file **out_s;
493
494 fd = open(fname, O_RDONLY | O_BINARY);
495 if (fd < 0) {
496 die("cannot open '%s'\n", fname);
497 }
498
499 s = sparse_file_import_auto(fd, false);
500 if (!s) {
501 die("cannot sparse read file '%s'\n", fname);
502 }
503
504 files = sparse_file_resparse(s, max_size, NULL, 0);
505 if (files < 0) {
506 die("Failed to resparse '%s'\n", fname);
507 }
508
509 out_s = calloc(sizeof(struct sparse_file *), files + 1);
510 if (!out_s) {
511 die("Failed to allocate sparse file array\n");
512 }
513
514 files = sparse_file_resparse(s, max_size, out_s, files);
515 if (files < 0) {
516 die("Failed to resparse '%s'\n", fname);
517 }
518
519 return out_s;
520}
521
522static int64_t get_target_sparse_limit(struct usb_handle *usb)
523{
524 int64_t limit = 0;
525 char response[FB_RESPONSE_SZ + 1];
526 int status = fb_getvar(usb, response, "max-download-size");
527
528 if (!status) {
529 limit = strtoul(response, NULL, 0);
530 if (limit > 0) {
531 fprintf(stderr, "target reported max download size of %lld bytes\n",
532 limit);
533 }
534 }
535
536 return limit;
537}
538
539static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
540{
541 int64_t limit;
542
543 if (sparse_limit == 0) {
544 return 0;
545 } else if (sparse_limit > 0) {
546 limit = sparse_limit;
547 } else {
548 if (target_sparse_limit == -1) {
549 target_sparse_limit = get_target_sparse_limit(usb);
550 }
551 if (target_sparse_limit > 0) {
552 limit = target_sparse_limit;
553 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700554 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700555 }
556 }
557
558 if (size > limit) {
559 return limit;
560 }
561
562 return 0;
563}
564
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700565/* Until we get lazy inode table init working in make_ext4fs, we need to
566 * erase partitions of type ext4 before flashing a filesystem so no stale
567 * inodes are left lying around. Otherwise, e2fsck gets very upset.
568 */
569static int needs_erase(const char *part)
570{
571 /* The function fb_format_supported() currently returns the value
572 * we want, so just call it.
573 */
574 return fb_format_supported(usb, part);
575}
576
Colin Crossf8387882012-05-24 17:18:41 -0700577void do_flash(usb_handle *usb, const char *pname, const char *fname)
578{
579 int64_t sz64;
580 void *data;
581 int64_t limit;
582
583 sz64 = file_size(fname);
584 limit = get_sparse_limit(usb, sz64);
585 if (limit) {
586 struct sparse_file **s = load_sparse_files(fname, limit);
587 if (s == NULL) {
588 die("cannot sparse load '%s'\n", fname);
589 }
590 while (*s) {
591 sz64 = sparse_file_len(*s, true, false);
592 fb_queue_flash_sparse(pname, *s++, sz64);
593 }
594 } else {
595 unsigned int sz;
596 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800597 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700598 fb_queue_flash(pname, data, sz);
599 }
600}
601
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800602void do_update_signature(zipfile_t zip, char *fn)
603{
604 void *data;
605 unsigned sz;
606 data = unzip_file(zip, fn, &sz);
607 if (data == 0) return;
608 fb_queue_download("signature", data, sz);
609 fb_queue_command("signature", "installing signature");
610}
611
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700612void do_update(char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800613{
614 void *zdata;
615 unsigned zsize;
616 void *data;
617 unsigned sz;
618 zipfile_t zip;
619
620 queue_info_dump();
621
Wink Savilleb98762f2011-04-04 17:54:59 -0700622 fb_queue_query_save("product", cur_product, sizeof(cur_product));
623
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800624 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800625 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800626
627 zip = init_zipfile(zdata, zsize);
628 if(zip == 0) die("failed to access zipdata in '%s'");
629
630 data = unzip_file(zip, "android-info.txt", &sz);
631 if (data == 0) {
632 char *tmp;
633 /* fallback for older zipfiles */
634 data = unzip_file(zip, "android-product.txt", &sz);
635 if ((data == 0) || (sz < 1)) {
636 die("update package has no android-info.txt or android-product.txt");
637 }
638 tmp = malloc(sz + 128);
639 if (tmp == 0) die("out of memory");
640 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
641 data = tmp;
642 sz = strlen(tmp);
643 }
644
645 setup_requirements(data, sz);
646
647 data = unzip_file(zip, "boot.img", &sz);
648 if (data == 0) die("update package missing boot.img");
649 do_update_signature(zip, "boot.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700650 if (erase_first && needs_erase("boot")) {
651 fb_queue_erase("boot");
652 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 fb_queue_flash("boot", data, sz);
654
655 data = unzip_file(zip, "recovery.img", &sz);
656 if (data != 0) {
657 do_update_signature(zip, "recovery.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700658 if (erase_first && needs_erase("recovery")) {
659 fb_queue_erase("recovery");
660 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800661 fb_queue_flash("recovery", data, sz);
662 }
663
664 data = unzip_file(zip, "system.img", &sz);
665 if (data == 0) die("update package missing system.img");
666 do_update_signature(zip, "system.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700667 if (erase_first && needs_erase("system")) {
668 fb_queue_erase("system");
669 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 fb_queue_flash("system", data, sz);
671}
672
673void do_send_signature(char *fn)
674{
675 void *data;
676 unsigned sz;
677 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800678
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 xtn = strrchr(fn, '.');
680 if (!xtn) return;
681 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800682
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 strcpy(xtn,".sig");
684 data = load_file(fn, &sz);
685 strcpy(xtn,".img");
686 if (data == 0) return;
687 fb_queue_download("signature", data, sz);
688 fb_queue_command("signature", "installing signature");
689}
690
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700691void do_flashall(int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692{
693 char *fname;
694 void *data;
695 unsigned sz;
696
697 queue_info_dump();
698
Wink Savilleb98762f2011-04-04 17:54:59 -0700699 fb_queue_query_save("product", cur_product, sizeof(cur_product));
700
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 fname = find_item("info", product);
702 if (fname == 0) die("cannot find android-info.txt");
703 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800704 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 setup_requirements(data, sz);
706
707 fname = find_item("boot", product);
708 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800709 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700711 if (erase_first && needs_erase("boot")) {
712 fb_queue_erase("boot");
713 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 fb_queue_flash("boot", data, sz);
715
716 fname = find_item("recovery", product);
717 data = load_file(fname, &sz);
718 if (data != 0) {
719 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700720 if (erase_first && needs_erase("recovery")) {
721 fb_queue_erase("recovery");
722 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 fb_queue_flash("recovery", data, sz);
724 }
725
726 fname = find_item("system", product);
727 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800728 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700730 if (erase_first && needs_erase("system")) {
731 fb_queue_erase("system");
732 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800733 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734}
735
736#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800737#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800738
739int do_oem_command(int argc, char **argv)
740{
741 int i;
742 char command[256];
743 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800744
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745 command[0] = 0;
746 while(1) {
747 strcat(command,*argv);
748 skip(1);
749 if(argc == 0) break;
750 strcat(command," ");
751 }
752
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800753 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 return 0;
755}
756
Colin Crossf8387882012-05-24 17:18:41 -0700757static int64_t parse_num(const char *arg)
758{
759 char *endptr;
760 unsigned long long num;
761
762 num = strtoull(arg, &endptr, 0);
763 if (endptr == arg) {
764 return -1;
765 }
766
767 if (*endptr == 'k' || *endptr == 'K') {
768 if (num >= (-1ULL) / 1024) {
769 return -1;
770 }
771 num *= 1024LL;
772 endptr++;
773 } else if (*endptr == 'm' || *endptr == 'M') {
774 if (num >= (-1ULL) / (1024 * 1024)) {
775 return -1;
776 }
777 num *= 1024LL * 1024LL;
778 endptr++;
779 } else if (*endptr == 'g' || *endptr == 'G') {
780 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
781 return -1;
782 }
783 num *= 1024LL * 1024LL * 1024LL;
784 endptr++;
785 }
786
787 if (*endptr != '\0') {
788 return -1;
789 }
790
791 if (num > INT64_MAX) {
792 return -1;
793 }
794
795 return num;
796}
797
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798int main(int argc, char **argv)
799{
800 int wants_wipe = 0;
801 int wants_reboot = 0;
802 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700803 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 void *data;
805 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700806 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700807 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700808 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809
JP Abgrall7b8970c2013-03-07 17:06:41 -0800810 const struct option longopts[] = {
811 {"base", required_argument, 0, 'b'},
812 {"kernel_offset", required_argument, 0, 'k'},
813 {"page_size", required_argument, 0, 'n'},
814 {"ramdisk_offset", required_argument, 0, 'r'},
815 {"help", 0, 0, 'h'},
816 {0, 0, 0, 0}
817 };
Colin Cross8879f982012-05-22 17:53:34 -0700818
819 serial = getenv("ANDROID_SERIAL");
820
821 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800822 int option_index = 0;
823 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 -0700824 if (c < 0) {
825 break;
826 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800827 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700828 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700829 case 'b':
830 base_addr = strtoul(optarg, 0, 16);
831 break;
Colin Cross8879f982012-05-22 17:53:34 -0700832 case 'c':
833 cmdline = optarg;
834 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800835 case 'h':
836 usage();
837 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700838 case 'i': {
839 char *endptr = NULL;
840 unsigned long val;
841
842 val = strtoul(optarg, &endptr, 0);
843 if (!endptr || *endptr != '\0' || (val & ~0xffff))
844 die("invalid vendor id '%s'", optarg);
845 vendor_id = (unsigned short)val;
846 break;
847 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800848 case 'k':
849 kernel_offset = strtoul(optarg, 0, 16);
850 break;
851 case 'l':
852 long_listing = 1;
853 break;
854 case 'n':
855 page_size = (unsigned)strtoul(optarg, NULL, 0);
856 if (!page_size) die("invalid page size");
857 break;
858 case 'p':
859 product = optarg;
860 break;
861 case 'r':
862 ramdisk_offset = strtoul(optarg, 0, 16);
863 break;
864 case 's':
865 serial = optarg;
866 break;
867 case 'S':
868 sparse_limit = parse_num(optarg);
869 if (sparse_limit < 0) {
870 die("invalid sparse limit");
871 }
872 break;
873 case 'u':
874 erase_first = 0;
875 break;
876 case 'w':
877 wants_wipe = 1;
878 break;
Colin Cross8879f982012-05-22 17:53:34 -0700879 case '?':
880 return 1;
881 default:
882 abort();
883 }
884 }
885
886 argc -= optind;
887 argv += optind;
888
889 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700891 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800892 }
893
Colin Cross8fb6e062012-07-24 16:36:41 -0700894 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700895 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800897 return 0;
898 }
899
Colin Crossc7b75dc2012-08-29 18:17:06 -0700900 if (argc > 0 && !strcmp(*argv, "help")) {
901 usage();
902 return 0;
903 }
904
Colin Cross8879f982012-05-22 17:53:34 -0700905 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700906
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800907 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700908 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800909 require(2);
910 fb_queue_display(argv[1], argv[1]);
911 skip(2);
912 } else if(!strcmp(*argv, "erase")) {
913 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700914
915 if (fb_format_supported(usb, argv[1])) {
916 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
917 }
918
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919 fb_queue_erase(argv[1]);
920 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800921 } else if(!strcmp(*argv, "format")) {
922 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700923 if (erase_first && needs_erase(argv[1])) {
924 fb_queue_erase(argv[1]);
925 }
JP Abgrall30ae5802012-05-07 20:25:24 -0700926 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800927 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800928 } else if(!strcmp(*argv, "signature")) {
929 require(2);
930 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800931 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800932 if (sz != 256) die("signature must be 256 bytes");
933 fb_queue_download("signature", data, sz);
934 fb_queue_command("signature", "installing signature");
935 skip(2);
936 } else if(!strcmp(*argv, "reboot")) {
937 wants_reboot = 1;
938 skip(1);
939 } else if(!strcmp(*argv, "reboot-bootloader")) {
940 wants_reboot_bootloader = 1;
941 skip(1);
942 } else if (!strcmp(*argv, "continue")) {
943 fb_queue_command("continue", "resuming boot");
944 skip(1);
945 } else if(!strcmp(*argv, "boot")) {
946 char *kname = 0;
947 char *rname = 0;
948 skip(1);
949 if (argc > 0) {
950 kname = argv[0];
951 skip(1);
952 }
953 if (argc > 0) {
954 rname = argv[0];
955 skip(1);
956 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800957 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958 if (data == 0) return 1;
959 fb_queue_download("boot.img", data, sz);
960 fb_queue_command("boot", "booting");
961 } else if(!strcmp(*argv, "flash")) {
962 char *pname = argv[1];
963 char *fname = 0;
964 require(2);
965 if (argc > 2) {
966 fname = argv[2];
967 skip(3);
968 } else {
969 fname = find_item(pname, product);
970 skip(2);
971 }
972 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700973 if (erase_first && needs_erase(pname)) {
974 fb_queue_erase(pname);
975 }
Colin Crossf8387882012-05-24 17:18:41 -0700976 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977 } else if(!strcmp(*argv, "flash:raw")) {
978 char *pname = argv[1];
979 char *kname = argv[2];
980 char *rname = 0;
981 require(3);
982 if(argc > 3) {
983 rname = argv[3];
984 skip(4);
985 } else {
986 skip(3);
987 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800988 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800989 if (data == 0) die("cannot load bootable image");
990 fb_queue_flash(pname, data, sz);
991 } else if(!strcmp(*argv, "flashall")) {
992 skip(1);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700993 do_flashall(erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800994 wants_reboot = 1;
995 } else if(!strcmp(*argv, "update")) {
996 if (argc > 1) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700997 do_update(argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800998 skip(2);
999 } else {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001000 do_update("update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 skip(1);
1002 }
1003 wants_reboot = 1;
1004 } else if(!strcmp(*argv, "oem")) {
1005 argc = do_oem_command(argc, argv);
1006 } else {
1007 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001008 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001009 }
1010 }
1011
1012 if (wants_wipe) {
1013 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001014 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001016 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001017 }
1018 if (wants_reboot) {
1019 fb_queue_reboot();
1020 } else if (wants_reboot_bootloader) {
1021 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001022 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001023 }
1024
Scott Anderson13081c62012-04-06 12:39:30 -07001025 if (fb_queue_is_empty())
1026 return 0;
1027
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001028 status = fb_execute_queue(usb);
1029 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001030}