blob: da2af416ebd5d690f095ff0797456689173f9999 [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]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700452 if (out[n] == 0) {
453 for(size_t i = 0; i < n; ++i) {
454 free((char*) out[i]);
455 }
456 free(out);
457 return -1;
458 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 }
460
Wink Savilleb98762f2011-04-04 17:54:59 -0700461 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 return 0;
463}
464
465static void setup_requirements(char *data, unsigned sz)
466{
467 char *s;
468
469 s = data;
470 while (sz-- > 0) {
471 if(*s == '\n') {
472 *s++ = 0;
473 if (setup_requirement_line(data)) {
474 die("out of memory");
475 }
476 data = s;
477 } else {
478 s++;
479 }
480 }
481}
482
483void queue_info_dump(void)
484{
485 fb_queue_notice("--------------------------------------------");
486 fb_queue_display("version-bootloader", "Bootloader Version...");
487 fb_queue_display("version-baseband", "Baseband Version.....");
488 fb_queue_display("serialno", "Serial Number........");
489 fb_queue_notice("--------------------------------------------");
490}
491
Colin Crossf8387882012-05-24 17:18:41 -0700492
493struct sparse_file **load_sparse_files(const char *fname, int max_size)
494{
495 int fd;
496 struct sparse_file *s;
497 int files;
498 struct sparse_file **out_s;
499
500 fd = open(fname, O_RDONLY | O_BINARY);
501 if (fd < 0) {
502 die("cannot open '%s'\n", fname);
503 }
504
505 s = sparse_file_import_auto(fd, false);
506 if (!s) {
507 die("cannot sparse read file '%s'\n", fname);
508 }
509
510 files = sparse_file_resparse(s, max_size, NULL, 0);
511 if (files < 0) {
512 die("Failed to resparse '%s'\n", fname);
513 }
514
515 out_s = calloc(sizeof(struct sparse_file *), files + 1);
516 if (!out_s) {
517 die("Failed to allocate sparse file array\n");
518 }
519
520 files = sparse_file_resparse(s, max_size, out_s, files);
521 if (files < 0) {
522 die("Failed to resparse '%s'\n", fname);
523 }
524
525 return out_s;
526}
527
528static int64_t get_target_sparse_limit(struct usb_handle *usb)
529{
530 int64_t limit = 0;
531 char response[FB_RESPONSE_SZ + 1];
532 int status = fb_getvar(usb, response, "max-download-size");
533
534 if (!status) {
535 limit = strtoul(response, NULL, 0);
536 if (limit > 0) {
537 fprintf(stderr, "target reported max download size of %lld bytes\n",
538 limit);
539 }
540 }
541
542 return limit;
543}
544
545static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
546{
547 int64_t limit;
548
549 if (sparse_limit == 0) {
550 return 0;
551 } else if (sparse_limit > 0) {
552 limit = sparse_limit;
553 } else {
554 if (target_sparse_limit == -1) {
555 target_sparse_limit = get_target_sparse_limit(usb);
556 }
557 if (target_sparse_limit > 0) {
558 limit = target_sparse_limit;
559 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700560 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700561 }
562 }
563
564 if (size > limit) {
565 return limit;
566 }
567
568 return 0;
569}
570
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700571/* Until we get lazy inode table init working in make_ext4fs, we need to
572 * erase partitions of type ext4 before flashing a filesystem so no stale
573 * inodes are left lying around. Otherwise, e2fsck gets very upset.
574 */
575static int needs_erase(const char *part)
576{
577 /* The function fb_format_supported() currently returns the value
578 * we want, so just call it.
579 */
580 return fb_format_supported(usb, part);
581}
582
Colin Crossf8387882012-05-24 17:18:41 -0700583void do_flash(usb_handle *usb, const char *pname, const char *fname)
584{
585 int64_t sz64;
586 void *data;
587 int64_t limit;
588
589 sz64 = file_size(fname);
590 limit = get_sparse_limit(usb, sz64);
591 if (limit) {
592 struct sparse_file **s = load_sparse_files(fname, limit);
593 if (s == NULL) {
594 die("cannot sparse load '%s'\n", fname);
595 }
596 while (*s) {
597 sz64 = sparse_file_len(*s, true, false);
598 fb_queue_flash_sparse(pname, *s++, sz64);
599 }
600 } else {
601 unsigned int sz;
602 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800603 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700604 fb_queue_flash(pname, data, sz);
605 }
606}
607
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608void do_update_signature(zipfile_t zip, char *fn)
609{
610 void *data;
611 unsigned sz;
612 data = unzip_file(zip, fn, &sz);
613 if (data == 0) return;
614 fb_queue_download("signature", data, sz);
615 fb_queue_command("signature", "installing signature");
616}
617
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700618void do_update(char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800619{
620 void *zdata;
621 unsigned zsize;
622 void *data;
623 unsigned sz;
624 zipfile_t zip;
625
626 queue_info_dump();
627
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 fb_queue_query_save("product", cur_product, sizeof(cur_product));
629
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800631 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800632
633 zip = init_zipfile(zdata, zsize);
634 if(zip == 0) die("failed to access zipdata in '%s'");
635
636 data = unzip_file(zip, "android-info.txt", &sz);
637 if (data == 0) {
638 char *tmp;
639 /* fallback for older zipfiles */
640 data = unzip_file(zip, "android-product.txt", &sz);
641 if ((data == 0) || (sz < 1)) {
642 die("update package has no android-info.txt or android-product.txt");
643 }
644 tmp = malloc(sz + 128);
645 if (tmp == 0) die("out of memory");
646 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
647 data = tmp;
648 sz = strlen(tmp);
649 }
650
651 setup_requirements(data, sz);
652
653 data = unzip_file(zip, "boot.img", &sz);
654 if (data == 0) die("update package missing boot.img");
655 do_update_signature(zip, "boot.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700656 if (erase_first && needs_erase("boot")) {
657 fb_queue_erase("boot");
658 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 fb_queue_flash("boot", data, sz);
660
661 data = unzip_file(zip, "recovery.img", &sz);
662 if (data != 0) {
663 do_update_signature(zip, "recovery.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700664 if (erase_first && needs_erase("recovery")) {
665 fb_queue_erase("recovery");
666 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667 fb_queue_flash("recovery", data, sz);
668 }
669
670 data = unzip_file(zip, "system.img", &sz);
671 if (data == 0) die("update package missing system.img");
672 do_update_signature(zip, "system.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700673 if (erase_first && needs_erase("system")) {
674 fb_queue_erase("system");
675 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 fb_queue_flash("system", data, sz);
677}
678
679void do_send_signature(char *fn)
680{
681 void *data;
682 unsigned sz;
683 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800684
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 xtn = strrchr(fn, '.');
686 if (!xtn) return;
687 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800688
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 strcpy(xtn,".sig");
690 data = load_file(fn, &sz);
691 strcpy(xtn,".img");
692 if (data == 0) return;
693 fb_queue_download("signature", data, sz);
694 fb_queue_command("signature", "installing signature");
695}
696
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700697void do_flashall(int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698{
699 char *fname;
700 void *data;
701 unsigned sz;
702
703 queue_info_dump();
704
Wink Savilleb98762f2011-04-04 17:54:59 -0700705 fb_queue_query_save("product", cur_product, sizeof(cur_product));
706
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 fname = find_item("info", product);
708 if (fname == 0) die("cannot find android-info.txt");
709 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800710 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 setup_requirements(data, sz);
712
713 fname = find_item("boot", product);
714 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800715 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700717 if (erase_first && needs_erase("boot")) {
718 fb_queue_erase("boot");
719 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 fb_queue_flash("boot", data, sz);
721
722 fname = find_item("recovery", product);
723 data = load_file(fname, &sz);
724 if (data != 0) {
725 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700726 if (erase_first && needs_erase("recovery")) {
727 fb_queue_erase("recovery");
728 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 fb_queue_flash("recovery", data, sz);
730 }
731
732 fname = find_item("system", product);
733 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800734 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700736 if (erase_first && needs_erase("system")) {
737 fb_queue_erase("system");
738 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800739 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740}
741
742#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800743#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744
745int do_oem_command(int argc, char **argv)
746{
747 int i;
748 char command[256];
749 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800750
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751 command[0] = 0;
752 while(1) {
753 strcat(command,*argv);
754 skip(1);
755 if(argc == 0) break;
756 strcat(command," ");
757 }
758
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800759 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 return 0;
761}
762
Colin Crossf8387882012-05-24 17:18:41 -0700763static int64_t parse_num(const char *arg)
764{
765 char *endptr;
766 unsigned long long num;
767
768 num = strtoull(arg, &endptr, 0);
769 if (endptr == arg) {
770 return -1;
771 }
772
773 if (*endptr == 'k' || *endptr == 'K') {
774 if (num >= (-1ULL) / 1024) {
775 return -1;
776 }
777 num *= 1024LL;
778 endptr++;
779 } else if (*endptr == 'm' || *endptr == 'M') {
780 if (num >= (-1ULL) / (1024 * 1024)) {
781 return -1;
782 }
783 num *= 1024LL * 1024LL;
784 endptr++;
785 } else if (*endptr == 'g' || *endptr == 'G') {
786 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
787 return -1;
788 }
789 num *= 1024LL * 1024LL * 1024LL;
790 endptr++;
791 }
792
793 if (*endptr != '\0') {
794 return -1;
795 }
796
797 if (num > INT64_MAX) {
798 return -1;
799 }
800
801 return num;
802}
803
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804int main(int argc, char **argv)
805{
806 int wants_wipe = 0;
807 int wants_reboot = 0;
808 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700809 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 void *data;
811 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700812 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700813 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700814 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800815
JP Abgrall7b8970c2013-03-07 17:06:41 -0800816 const struct option longopts[] = {
817 {"base", required_argument, 0, 'b'},
818 {"kernel_offset", required_argument, 0, 'k'},
819 {"page_size", required_argument, 0, 'n'},
820 {"ramdisk_offset", required_argument, 0, 'r'},
821 {"help", 0, 0, 'h'},
822 {0, 0, 0, 0}
823 };
Colin Cross8879f982012-05-22 17:53:34 -0700824
825 serial = getenv("ANDROID_SERIAL");
826
827 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800828 int option_index = 0;
829 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 -0700830 if (c < 0) {
831 break;
832 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800833 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700834 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700835 case 'b':
836 base_addr = strtoul(optarg, 0, 16);
837 break;
Colin Cross8879f982012-05-22 17:53:34 -0700838 case 'c':
839 cmdline = optarg;
840 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800841 case 'h':
842 usage();
843 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700844 case 'i': {
845 char *endptr = NULL;
846 unsigned long val;
847
848 val = strtoul(optarg, &endptr, 0);
849 if (!endptr || *endptr != '\0' || (val & ~0xffff))
850 die("invalid vendor id '%s'", optarg);
851 vendor_id = (unsigned short)val;
852 break;
853 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800854 case 'k':
855 kernel_offset = strtoul(optarg, 0, 16);
856 break;
857 case 'l':
858 long_listing = 1;
859 break;
860 case 'n':
861 page_size = (unsigned)strtoul(optarg, NULL, 0);
862 if (!page_size) die("invalid page size");
863 break;
864 case 'p':
865 product = optarg;
866 break;
867 case 'r':
868 ramdisk_offset = strtoul(optarg, 0, 16);
869 break;
870 case 's':
871 serial = optarg;
872 break;
873 case 'S':
874 sparse_limit = parse_num(optarg);
875 if (sparse_limit < 0) {
876 die("invalid sparse limit");
877 }
878 break;
879 case 'u':
880 erase_first = 0;
881 break;
882 case 'w':
883 wants_wipe = 1;
884 break;
Colin Cross8879f982012-05-22 17:53:34 -0700885 case '?':
886 return 1;
887 default:
888 abort();
889 }
890 }
891
892 argc -= optind;
893 argv += optind;
894
895 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800896 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700897 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800898 }
899
Colin Cross8fb6e062012-07-24 16:36:41 -0700900 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700901 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800902 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800903 return 0;
904 }
905
Colin Crossc7b75dc2012-08-29 18:17:06 -0700906 if (argc > 0 && !strcmp(*argv, "help")) {
907 usage();
908 return 0;
909 }
910
Colin Cross8879f982012-05-22 17:53:34 -0700911 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700912
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800913 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700914 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800915 require(2);
916 fb_queue_display(argv[1], argv[1]);
917 skip(2);
918 } else if(!strcmp(*argv, "erase")) {
919 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700920
921 if (fb_format_supported(usb, argv[1])) {
922 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
923 }
924
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800925 fb_queue_erase(argv[1]);
926 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800927 } else if(!strcmp(*argv, "format")) {
928 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700929 if (erase_first && needs_erase(argv[1])) {
930 fb_queue_erase(argv[1]);
931 }
JP Abgrall30ae5802012-05-07 20:25:24 -0700932 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800933 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800934 } else if(!strcmp(*argv, "signature")) {
935 require(2);
936 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800937 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938 if (sz != 256) die("signature must be 256 bytes");
939 fb_queue_download("signature", data, sz);
940 fb_queue_command("signature", "installing signature");
941 skip(2);
942 } else if(!strcmp(*argv, "reboot")) {
943 wants_reboot = 1;
944 skip(1);
945 } else if(!strcmp(*argv, "reboot-bootloader")) {
946 wants_reboot_bootloader = 1;
947 skip(1);
948 } else if (!strcmp(*argv, "continue")) {
949 fb_queue_command("continue", "resuming boot");
950 skip(1);
951 } else if(!strcmp(*argv, "boot")) {
952 char *kname = 0;
953 char *rname = 0;
954 skip(1);
955 if (argc > 0) {
956 kname = argv[0];
957 skip(1);
958 }
959 if (argc > 0) {
960 rname = argv[0];
961 skip(1);
962 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800963 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800964 if (data == 0) return 1;
965 fb_queue_download("boot.img", data, sz);
966 fb_queue_command("boot", "booting");
967 } else if(!strcmp(*argv, "flash")) {
968 char *pname = argv[1];
969 char *fname = 0;
970 require(2);
971 if (argc > 2) {
972 fname = argv[2];
973 skip(3);
974 } else {
975 fname = find_item(pname, product);
976 skip(2);
977 }
978 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700979 if (erase_first && needs_erase(pname)) {
980 fb_queue_erase(pname);
981 }
Colin Crossf8387882012-05-24 17:18:41 -0700982 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800983 } else if(!strcmp(*argv, "flash:raw")) {
984 char *pname = argv[1];
985 char *kname = argv[2];
986 char *rname = 0;
987 require(3);
988 if(argc > 3) {
989 rname = argv[3];
990 skip(4);
991 } else {
992 skip(3);
993 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800994 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800995 if (data == 0) die("cannot load bootable image");
996 fb_queue_flash(pname, data, sz);
997 } else if(!strcmp(*argv, "flashall")) {
998 skip(1);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700999 do_flashall(erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001000 wants_reboot = 1;
1001 } else if(!strcmp(*argv, "update")) {
1002 if (argc > 1) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001003 do_update(argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001004 skip(2);
1005 } else {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001006 do_update("update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007 skip(1);
1008 }
1009 wants_reboot = 1;
1010 } else if(!strcmp(*argv, "oem")) {
1011 argc = do_oem_command(argc, argv);
1012 } else {
1013 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001014 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001015 }
1016 }
1017
1018 if (wants_wipe) {
1019 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001020 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001021 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001022 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001023 }
1024 if (wants_reboot) {
1025 fb_queue_reboot();
1026 } else if (wants_reboot_bootloader) {
1027 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001028 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029 }
1030
Scott Anderson13081c62012-04-06 12:39:30 -07001031 if (fb_queue_is_empty())
1032 return 0;
1033
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001034 status = fb_execute_queue(usb);
1035 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001036}