blob: e469d97f0c295568898199b5ef4a2c08fe189143 [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
Colin Crossf8387882012-05-24 17:18:41 -0700138#if defined(__APPLE__) && defined(__MACH__)
139#define lseek64 lseek
140#define off64_t off_t
141#endif
142
Rom Lemarchandc9cce4b2013-06-28 09:45:08 -0700143static int64_t file_size(const char *fn)
Colin Crossf8387882012-05-24 17:18:41 -0700144{
145 off64_t off;
146 int fd;
147
Rom Lemarchandc9cce4b2013-06-28 09:45:08 -0700148 fd = open(fn, O_RDONLY | O_BINARY);
Colin Crossf8387882012-05-24 17:18:41 -0700149 if (fd < 0) return -1;
150
151 off = lseek64(fd, 0, SEEK_END);
152 close(fd);
153
154 return off;
155}
156
Rom Lemarchandc9cce4b2013-06-28 09:45:08 -0700157static void *load_file(const char *fn, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158{
159 char *data;
160 int sz;
161 int fd;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800162 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
164 data = 0;
Rom Lemarchandc9cce4b2013-06-28 09:45:08 -0700165 fd = open(fn, O_RDONLY | O_BINARY);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 if(fd < 0) return 0;
167
168 sz = lseek(fd, 0, SEEK_END);
169 if(sz < 0) goto oops;
170
171 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
172
173 data = (char*) malloc(sz);
174 if(data == 0) goto oops;
175
176 if(read(fd, data, sz) != sz) goto oops;
177 close(fd);
178
179 if(_sz) *_sz = sz;
180 return data;
181
182oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 close(fd);
185 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 return 0;
188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
JP Abgralla032ded2012-06-06 11:53:33 -0700190int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
191{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400193 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800194 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700195 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800196 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700197 (info->dev_vendor != 0x0fce) && // Sony Ericsson
198 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400199 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800200 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800201 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800202 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800203 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400204 (info->dev_vendor != 0x0bb4)) // HTC
205 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 if(info->ifc_class != 0xff) return -1;
207 if(info->ifc_subclass != 0x42) return -1;
208 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700209 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700211 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
212 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 return 0;
214}
215
JP Abgrall7b8970c2013-03-07 17:06:41 -0800216int match_fastboot(usb_ifc_info *info)
217{
218 return match_fastboot_with_serial(info, serial);
219}
220
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221int list_devices_callback(usb_ifc_info *info)
222{
JP Abgralla032ded2012-06-06 11:53:33 -0700223 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700225 if (!info->writable) {
226 serial = "no permissions"; // like "adb devices"
227 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 if (!serial[0]) {
229 serial = "????????????";
230 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700231 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700232 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700233 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700234 } else if (!info->device_path) {
235 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700236 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700237 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700238 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
240
241 return -1;
242}
243
244usb_handle *open_device(void)
245{
246 static usb_handle *usb = 0;
247 int announce = 1;
248
249 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800250
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 for(;;) {
252 usb = usb_open(match_fastboot);
253 if(usb) return usb;
254 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800255 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 fprintf(stderr,"< waiting for device >\n");
257 }
258 sleep(1);
259 }
260}
261
262void list_devices(void) {
263 // We don't actually open a USB device here,
264 // just getting our callback called so we can
265 // list all the connected devices.
266 usb_open(list_devices_callback);
267}
268
269void usage(void)
270{
271 fprintf(stderr,
272/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
273 "usage: fastboot [ <option> ] <command>\n"
274 "\n"
275 "commands:\n"
276 " update <filename> reflash device from update.zip\n"
277 " flashall flash boot + recovery + system\n"
278 " flash <partition> [ <filename> ] write a file to a flash partition\n"
279 " erase <partition> erase a flash partition\n"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800280 " format <partition> format a flash partition \n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 " getvar <variable> display a bootloader variable\n"
282 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
283 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
284 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700285 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 " reboot reboot device normally\n"
287 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800288 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 "\n"
290 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700291 " -w erase userdata and cache (and format\n"
292 " if supported by partition type)\n"
293 " -u do not first erase partition before\n"
294 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700295 " -s <specific device> specify device serial number\n"
296 " or path to device port\n"
297 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 " -p <product> specify product name\n"
299 " -c <cmdline> override kernel commandline\n"
300 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7b8970c2013-03-07 17:06:41 -0800301 " -b <base_addr> specify a custom kernel base address. default: 0x10000000\n"
Dima Zavin931175a2010-02-12 20:26:33 -0800302 " -n <page size> specify the nand page size. default: 2048\n"
Colin Crossf8387882012-05-24 17:18:41 -0700303 " -S <size>[K|M|G] automatically sparse files greater than\n"
Colin Cross0bbfb392012-07-24 18:05:21 -0700304 " size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306}
307
JP Abgrall7b8970c2013-03-07 17:06:41 -0800308void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 unsigned *sz, const char *cmdline)
310{
311 void *kdata = 0, *rdata = 0;
312 unsigned ksize = 0, rsize = 0;
313 void *bdata;
314 unsigned bsize;
315
316 if(kernel == 0) {
317 fprintf(stderr, "no image specified\n");
318 return 0;
319 }
320
321 kdata = load_file(kernel, &ksize);
322 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800323 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 return 0;
325 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800326
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 /* is this actually a boot image? */
328 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
329 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800330
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 if(ramdisk) {
332 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
333 return 0;
334 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800335
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 *sz = ksize;
337 return kdata;
338 }
339
340 if(ramdisk) {
341 rdata = load_file(ramdisk, &rsize);
342 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800343 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 return 0;
345 }
346 }
347
348 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800349 bdata = mkbootimg(kdata, ksize, kernel_offset,
350 rdata, rsize, ramdisk_offset,
351 0, 0, second_offset,
352 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 if(bdata == 0) {
354 fprintf(stderr,"failed to create boot.img\n");
355 return 0;
356 }
357 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
358 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
359 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 return bdata;
362}
363
364void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
365{
366 void *data;
367 zipentry_t entry;
368 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800369
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 entry = lookup_zipentry(zip, name);
371 if (entry == NULL) {
372 fprintf(stderr, "archive does not contain '%s'\n", name);
373 return 0;
374 }
375
376 *sz = get_zipentry_size(entry);
377
378 datasz = *sz * 1.001;
379 data = malloc(datasz);
380
381 if(data == 0) {
382 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
383 return 0;
384 }
385
386 if (decompress_zipentry(entry, data, datasz)) {
387 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
388 free(data);
389 return 0;
390 }
391
392 return data;
393}
394
395static char *strip(char *s)
396{
397 int n;
398 while(*s && isspace(*s)) s++;
399 n = strlen(s);
400 while(n-- > 0) {
401 if(!isspace(s[n])) break;
402 s[n] = 0;
403 }
404 return s;
405}
406
407#define MAX_OPTIONS 32
408static int setup_requirement_line(char *name)
409{
410 char *val[MAX_OPTIONS];
411 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700412 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 unsigned n, count;
414 char *x;
415 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800416
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 if (!strncmp(name, "reject ", 7)) {
418 name += 7;
419 invert = 1;
420 } else if (!strncmp(name, "require ", 8)) {
421 name += 8;
422 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700423 } else if (!strncmp(name, "require-for-product:", 20)) {
424 // Get the product and point name past it
425 prod = name + 20;
426 name = strchr(name, ' ');
427 if (!name) return -1;
428 *name = 0;
429 name += 1;
430 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 }
432
433 x = strchr(name, '=');
434 if (x == 0) return 0;
435 *x = 0;
436 val[0] = x + 1;
437
438 for(count = 1; count < MAX_OPTIONS; count++) {
439 x = strchr(val[count - 1],'|');
440 if (x == 0) break;
441 *x = 0;
442 val[count] = x + 1;
443 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800444
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 name = strip(name);
446 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 name = strip(name);
449 if (name == 0) return -1;
450
451 /* work around an unfortunate name mismatch */
452 if (!strcmp(name,"board")) name = "product";
453
454 out = malloc(sizeof(char*) * count);
455 if (out == 0) return -1;
456
457 for(n = 0; n < count; n++) {
458 out[n] = strdup(strip(val[n]));
459 if (out[n] == 0) return -1;
460 }
461
Wink Savilleb98762f2011-04-04 17:54:59 -0700462 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 return 0;
464}
465
466static void setup_requirements(char *data, unsigned sz)
467{
468 char *s;
469
470 s = data;
471 while (sz-- > 0) {
472 if(*s == '\n') {
473 *s++ = 0;
474 if (setup_requirement_line(data)) {
475 die("out of memory");
476 }
477 data = s;
478 } else {
479 s++;
480 }
481 }
482}
483
484void queue_info_dump(void)
485{
486 fb_queue_notice("--------------------------------------------");
487 fb_queue_display("version-bootloader", "Bootloader Version...");
488 fb_queue_display("version-baseband", "Baseband Version.....");
489 fb_queue_display("serialno", "Serial Number........");
490 fb_queue_notice("--------------------------------------------");
491}
492
Colin Crossf8387882012-05-24 17:18:41 -0700493
494struct sparse_file **load_sparse_files(const char *fname, int max_size)
495{
496 int fd;
497 struct sparse_file *s;
498 int files;
499 struct sparse_file **out_s;
500
501 fd = open(fname, O_RDONLY | O_BINARY);
502 if (fd < 0) {
503 die("cannot open '%s'\n", fname);
504 }
505
506 s = sparse_file_import_auto(fd, false);
507 if (!s) {
508 die("cannot sparse read file '%s'\n", fname);
509 }
510
511 files = sparse_file_resparse(s, max_size, NULL, 0);
512 if (files < 0) {
513 die("Failed to resparse '%s'\n", fname);
514 }
515
516 out_s = calloc(sizeof(struct sparse_file *), files + 1);
517 if (!out_s) {
518 die("Failed to allocate sparse file array\n");
519 }
520
521 files = sparse_file_resparse(s, max_size, out_s, files);
522 if (files < 0) {
523 die("Failed to resparse '%s'\n", fname);
524 }
525
526 return out_s;
527}
528
529static int64_t get_target_sparse_limit(struct usb_handle *usb)
530{
531 int64_t limit = 0;
532 char response[FB_RESPONSE_SZ + 1];
533 int status = fb_getvar(usb, response, "max-download-size");
534
535 if (!status) {
536 limit = strtoul(response, NULL, 0);
537 if (limit > 0) {
538 fprintf(stderr, "target reported max download size of %lld bytes\n",
539 limit);
540 }
541 }
542
543 return limit;
544}
545
546static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
547{
548 int64_t limit;
549
550 if (sparse_limit == 0) {
551 return 0;
552 } else if (sparse_limit > 0) {
553 limit = sparse_limit;
554 } else {
555 if (target_sparse_limit == -1) {
556 target_sparse_limit = get_target_sparse_limit(usb);
557 }
558 if (target_sparse_limit > 0) {
559 limit = target_sparse_limit;
560 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700561 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700562 }
563 }
564
565 if (size > limit) {
566 return limit;
567 }
568
569 return 0;
570}
571
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700572/* Until we get lazy inode table init working in make_ext4fs, we need to
573 * erase partitions of type ext4 before flashing a filesystem so no stale
574 * inodes are left lying around. Otherwise, e2fsck gets very upset.
575 */
576static int needs_erase(const char *part)
577{
578 /* The function fb_format_supported() currently returns the value
579 * we want, so just call it.
580 */
581 return fb_format_supported(usb, part);
582}
583
Colin Crossf8387882012-05-24 17:18:41 -0700584void do_flash(usb_handle *usb, const char *pname, const char *fname)
585{
586 int64_t sz64;
587 void *data;
588 int64_t limit;
589
590 sz64 = file_size(fname);
591 limit = get_sparse_limit(usb, sz64);
592 if (limit) {
593 struct sparse_file **s = load_sparse_files(fname, limit);
594 if (s == NULL) {
595 die("cannot sparse load '%s'\n", fname);
596 }
597 while (*s) {
598 sz64 = sparse_file_len(*s, true, false);
599 fb_queue_flash_sparse(pname, *s++, sz64);
600 }
601 } else {
602 unsigned int sz;
603 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800604 if (data == 0) die("cannot load '%s': %s\n", fname, strerror(errno));
Colin Crossf8387882012-05-24 17:18:41 -0700605 fb_queue_flash(pname, data, sz);
606 }
607}
608
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800609void do_update_signature(zipfile_t zip, char *fn)
610{
611 void *data;
612 unsigned sz;
613 data = unzip_file(zip, fn, &sz);
614 if (data == 0) return;
615 fb_queue_download("signature", data, sz);
616 fb_queue_command("signature", "installing signature");
617}
618
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700619void do_update(char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620{
621 void *zdata;
622 unsigned zsize;
623 void *data;
624 unsigned sz;
625 zipfile_t zip;
626
627 queue_info_dump();
628
Wink Savilleb98762f2011-04-04 17:54:59 -0700629 fb_queue_query_save("product", cur_product, sizeof(cur_product));
630
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800632 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633
634 zip = init_zipfile(zdata, zsize);
635 if(zip == 0) die("failed to access zipdata in '%s'");
636
637 data = unzip_file(zip, "android-info.txt", &sz);
638 if (data == 0) {
639 char *tmp;
640 /* fallback for older zipfiles */
641 data = unzip_file(zip, "android-product.txt", &sz);
642 if ((data == 0) || (sz < 1)) {
643 die("update package has no android-info.txt or android-product.txt");
644 }
645 tmp = malloc(sz + 128);
646 if (tmp == 0) die("out of memory");
647 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
648 data = tmp;
649 sz = strlen(tmp);
650 }
651
652 setup_requirements(data, sz);
653
654 data = unzip_file(zip, "boot.img", &sz);
655 if (data == 0) die("update package missing boot.img");
656 do_update_signature(zip, "boot.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700657 if (erase_first && needs_erase("boot")) {
658 fb_queue_erase("boot");
659 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 fb_queue_flash("boot", data, sz);
661
662 data = unzip_file(zip, "recovery.img", &sz);
663 if (data != 0) {
664 do_update_signature(zip, "recovery.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700665 if (erase_first && needs_erase("recovery")) {
666 fb_queue_erase("recovery");
667 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 fb_queue_flash("recovery", data, sz);
669 }
670
671 data = unzip_file(zip, "system.img", &sz);
672 if (data == 0) die("update package missing system.img");
673 do_update_signature(zip, "system.sig");
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700674 if (erase_first && needs_erase("system")) {
675 fb_queue_erase("system");
676 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 fb_queue_flash("system", data, sz);
678}
679
680void do_send_signature(char *fn)
681{
682 void *data;
683 unsigned sz;
684 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800685
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 xtn = strrchr(fn, '.');
687 if (!xtn) return;
688 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800689
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 strcpy(xtn,".sig");
691 data = load_file(fn, &sz);
692 strcpy(xtn,".img");
693 if (data == 0) return;
694 fb_queue_download("signature", data, sz);
695 fb_queue_command("signature", "installing signature");
696}
697
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700698void do_flashall(int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699{
700 char *fname;
701 void *data;
702 unsigned sz;
703
704 queue_info_dump();
705
Wink Savilleb98762f2011-04-04 17:54:59 -0700706 fb_queue_query_save("product", cur_product, sizeof(cur_product));
707
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 fname = find_item("info", product);
709 if (fname == 0) die("cannot find android-info.txt");
710 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800711 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 setup_requirements(data, sz);
713
714 fname = find_item("boot", product);
715 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800716 if (data == 0) die("could not load boot.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700718 if (erase_first && needs_erase("boot")) {
719 fb_queue_erase("boot");
720 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 fb_queue_flash("boot", data, sz);
722
723 fname = find_item("recovery", product);
724 data = load_file(fname, &sz);
725 if (data != 0) {
726 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700727 if (erase_first && needs_erase("recovery")) {
728 fb_queue_erase("recovery");
729 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730 fb_queue_flash("recovery", data, sz);
731 }
732
733 fname = find_item("system", product);
734 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800735 if (data == 0) die("could not load system.img: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 do_send_signature(fname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700737 if (erase_first && needs_erase("system")) {
738 fb_queue_erase("system");
739 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800740 fb_queue_flash("system", data, sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741}
742
743#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800744#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745
746int do_oem_command(int argc, char **argv)
747{
748 int i;
749 char command[256];
750 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800751
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 command[0] = 0;
753 while(1) {
754 strcat(command,*argv);
755 skip(1);
756 if(argc == 0) break;
757 strcat(command," ");
758 }
759
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800760 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 return 0;
762}
763
Colin Crossf8387882012-05-24 17:18:41 -0700764static int64_t parse_num(const char *arg)
765{
766 char *endptr;
767 unsigned long long num;
768
769 num = strtoull(arg, &endptr, 0);
770 if (endptr == arg) {
771 return -1;
772 }
773
774 if (*endptr == 'k' || *endptr == 'K') {
775 if (num >= (-1ULL) / 1024) {
776 return -1;
777 }
778 num *= 1024LL;
779 endptr++;
780 } else if (*endptr == 'm' || *endptr == 'M') {
781 if (num >= (-1ULL) / (1024 * 1024)) {
782 return -1;
783 }
784 num *= 1024LL * 1024LL;
785 endptr++;
786 } else if (*endptr == 'g' || *endptr == 'G') {
787 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
788 return -1;
789 }
790 num *= 1024LL * 1024LL * 1024LL;
791 endptr++;
792 }
793
794 if (*endptr != '\0') {
795 return -1;
796 }
797
798 if (num > INT64_MAX) {
799 return -1;
800 }
801
802 return num;
803}
804
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805int main(int argc, char **argv)
806{
807 int wants_wipe = 0;
808 int wants_reboot = 0;
809 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700810 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811 void *data;
812 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700813 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700814 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700815 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800816
JP Abgrall7b8970c2013-03-07 17:06:41 -0800817 const struct option longopts[] = {
818 {"base", required_argument, 0, 'b'},
819 {"kernel_offset", required_argument, 0, 'k'},
820 {"page_size", required_argument, 0, 'n'},
821 {"ramdisk_offset", required_argument, 0, 'r'},
822 {"help", 0, 0, 'h'},
823 {0, 0, 0, 0}
824 };
Colin Cross8879f982012-05-22 17:53:34 -0700825
826 serial = getenv("ANDROID_SERIAL");
827
828 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800829 int option_index = 0;
830 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 -0700831 if (c < 0) {
832 break;
833 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800834 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700835 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700836 case 'b':
837 base_addr = strtoul(optarg, 0, 16);
838 break;
Colin Cross8879f982012-05-22 17:53:34 -0700839 case 'c':
840 cmdline = optarg;
841 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800842 case 'h':
843 usage();
844 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700845 case 'i': {
846 char *endptr = NULL;
847 unsigned long val;
848
849 val = strtoul(optarg, &endptr, 0);
850 if (!endptr || *endptr != '\0' || (val & ~0xffff))
851 die("invalid vendor id '%s'", optarg);
852 vendor_id = (unsigned short)val;
853 break;
854 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800855 case 'k':
856 kernel_offset = strtoul(optarg, 0, 16);
857 break;
858 case 'l':
859 long_listing = 1;
860 break;
861 case 'n':
862 page_size = (unsigned)strtoul(optarg, NULL, 0);
863 if (!page_size) die("invalid page size");
864 break;
865 case 'p':
866 product = optarg;
867 break;
868 case 'r':
869 ramdisk_offset = strtoul(optarg, 0, 16);
870 break;
871 case 's':
872 serial = optarg;
873 break;
874 case 'S':
875 sparse_limit = parse_num(optarg);
876 if (sparse_limit < 0) {
877 die("invalid sparse limit");
878 }
879 break;
880 case 'u':
881 erase_first = 0;
882 break;
883 case 'w':
884 wants_wipe = 1;
885 break;
Colin Cross8879f982012-05-22 17:53:34 -0700886 case '?':
887 return 1;
888 default:
889 abort();
890 }
891 }
892
893 argc -= optind;
894 argv += optind;
895
896 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800897 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700898 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800899 }
900
Colin Cross8fb6e062012-07-24 16:36:41 -0700901 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -0700902 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800903 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800904 return 0;
905 }
906
Colin Crossc7b75dc2012-08-29 18:17:06 -0700907 if (argc > 0 && !strcmp(*argv, "help")) {
908 usage();
909 return 0;
910 }
911
Colin Cross8879f982012-05-22 17:53:34 -0700912 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -0700913
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800914 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -0700915 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800916 require(2);
917 fb_queue_display(argv[1], argv[1]);
918 skip(2);
919 } else if(!strcmp(*argv, "erase")) {
920 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700921
922 if (fb_format_supported(usb, argv[1])) {
923 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
924 }
925
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926 fb_queue_erase(argv[1]);
927 skip(2);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800928 } else if(!strcmp(*argv, "format")) {
929 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700930 if (erase_first && needs_erase(argv[1])) {
931 fb_queue_erase(argv[1]);
932 }
JP Abgrall30ae5802012-05-07 20:25:24 -0700933 fb_queue_format(argv[1], 0);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -0800934 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800935 } else if(!strcmp(*argv, "signature")) {
936 require(2);
937 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800938 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800939 if (sz != 256) die("signature must be 256 bytes");
940 fb_queue_download("signature", data, sz);
941 fb_queue_command("signature", "installing signature");
942 skip(2);
943 } else if(!strcmp(*argv, "reboot")) {
944 wants_reboot = 1;
945 skip(1);
946 } else if(!strcmp(*argv, "reboot-bootloader")) {
947 wants_reboot_bootloader = 1;
948 skip(1);
949 } else if (!strcmp(*argv, "continue")) {
950 fb_queue_command("continue", "resuming boot");
951 skip(1);
952 } else if(!strcmp(*argv, "boot")) {
953 char *kname = 0;
954 char *rname = 0;
955 skip(1);
956 if (argc > 0) {
957 kname = argv[0];
958 skip(1);
959 }
960 if (argc > 0) {
961 rname = argv[0];
962 skip(1);
963 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800964 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800965 if (data == 0) return 1;
966 fb_queue_download("boot.img", data, sz);
967 fb_queue_command("boot", "booting");
968 } else if(!strcmp(*argv, "flash")) {
969 char *pname = argv[1];
970 char *fname = 0;
971 require(2);
972 if (argc > 2) {
973 fname = argv[2];
974 skip(3);
975 } else {
976 fname = find_item(pname, product);
977 skip(2);
978 }
979 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700980 if (erase_first && needs_erase(pname)) {
981 fb_queue_erase(pname);
982 }
Colin Crossf8387882012-05-24 17:18:41 -0700983 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800984 } else if(!strcmp(*argv, "flash:raw")) {
985 char *pname = argv[1];
986 char *kname = argv[2];
987 char *rname = 0;
988 require(3);
989 if(argc > 3) {
990 rname = argv[3];
991 skip(4);
992 } else {
993 skip(3);
994 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800995 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996 if (data == 0) die("cannot load bootable image");
997 fb_queue_flash(pname, data, sz);
998 } else if(!strcmp(*argv, "flashall")) {
999 skip(1);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001000 do_flashall(erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001001 wants_reboot = 1;
1002 } else if(!strcmp(*argv, "update")) {
1003 if (argc > 1) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001004 do_update(argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001005 skip(2);
1006 } else {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001007 do_update("update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001008 skip(1);
1009 }
1010 wants_reboot = 1;
1011 } else if(!strcmp(*argv, "oem")) {
1012 argc = do_oem_command(argc, argv);
1013 } else {
1014 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001015 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001016 }
1017 }
1018
1019 if (wants_wipe) {
1020 fb_queue_erase("userdata");
JP Abgrall30ae5802012-05-07 20:25:24 -07001021 fb_queue_format("userdata", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001022 fb_queue_erase("cache");
JP Abgrall30ae5802012-05-07 20:25:24 -07001023 fb_queue_format("cache", 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001024 }
1025 if (wants_reboot) {
1026 fb_queue_reboot();
1027 } else if (wants_reboot_bootloader) {
1028 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
1029 }
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}