blob: 4d3e0afd43d0311df67c52e684253e17993f9c43 [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>
JP Abgrall7e859742014-05-06 15:14:15 -070036
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#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>
Rom Lemarchand622810c2013-06-28 09:54:59 -070046#include <sys/stat.h>
Colin Crossf8387882012-05-24 17:18:41 -070047
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#include <bootimg.h>
Colin Crossf8387882012-05-24 17:18:41 -070049#include <sparse/sparse.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#include <zipfile/zipfile.h>
51
52#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070053#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
Colin Crossf8387882012-05-24 17:18:41 -070055#ifndef O_BINARY
56#define O_BINARY 0
57#endif
58
Rom Lemarchand622810c2013-06-28 09:54:59 -070059#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
60
Wink Savilleb98762f2011-04-04 17:54:59 -070061char cur_product[FB_RESPONSE_SZ + 1];
62
Brian Swetland2a63bb72009-04-28 16:05:07 -070063void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
64
JP Abgrall7b8970c2013-03-07 17:06:41 -080065boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
66 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
67 void *second, unsigned second_size, unsigned second_offset,
68 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070069 unsigned *bootimg_size);
70
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071static usb_handle *usb = 0;
72static const char *serial = 0;
73static const char *product = 0;
74static const char *cmdline = 0;
75static int wipe_data = 0;
76static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
JP Abgrall7b8970c2013-03-07 17:06:41 -080081unsigned page_size = 2048;
82unsigned base_addr = 0x10000000;
83unsigned kernel_offset = 0x00008000;
84unsigned ramdisk_offset = 0x01000000;
85unsigned second_offset = 0x00f00000;
86unsigned tags_offset = 0x00000100;
87
Rom Lemarchand622810c2013-06-28 09:54:59 -070088enum fb_buffer_type {
89 FB_BUFFER,
90 FB_BUFFER_SPARSE,
91};
92
93struct fastboot_buffer {
94 enum fb_buffer_type type;
95 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000096 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070097};
98
99static struct {
100 char img_name[13];
101 char sig_name[13];
102 char part_name[9];
103 bool is_optional;
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700104} images[4] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 {"boot.img", "boot.sig", "boot", false},
106 {"recovery.img", "recovery.sig", "recovery", true},
107 {"system.img", "system.sig", "system", false},
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700108 {"tos.img", "tos.sig", "tos", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700109};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700110
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111void get_my_path(char *path);
112
113char *find_item(const char *item, const char *product)
114{
115 char *dir;
116 char *fn;
117 char path[PATH_MAX + 128];
118
119 if(!strcmp(item,"boot")) {
120 fn = "boot.img";
121 } else if(!strcmp(item,"recovery")) {
122 fn = "recovery.img";
123 } else if(!strcmp(item,"system")) {
124 fn = "system.img";
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700125 } else if(!strcmp(item,"tos")) {
126 fn = "tos.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 } else if(!strcmp(item,"userdata")) {
128 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700129 } else if(!strcmp(item,"cache")) {
130 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 } else if(!strcmp(item,"info")) {
132 fn = "android-info.txt";
133 } else {
134 fprintf(stderr,"unknown partition '%s'\n", item);
135 return 0;
136 }
137
138 if(product) {
139 get_my_path(path);
140 sprintf(path + strlen(path),
141 "../../../target/product/%s/%s", product, fn);
142 return strdup(path);
143 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 dir = getenv("ANDROID_PRODUCT_OUT");
146 if((dir == 0) || (dir[0] == 0)) {
147 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
148 return 0;
149 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800150
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 sprintf(path, "%s/%s", dir, fn);
152 return strdup(path);
153}
154
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000155static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700156{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700157 struct stat st;
158 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700159
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700161
Rom Lemarchand622810c2013-06-28 09:54:59 -0700162 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700163}
164
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000165static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000167 char *data;
168 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800169 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000171 data = 0;
172
173 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700174 if (sz < 0) {
175 goto oops;
176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
178 data = (char*) malloc(sz);
179 if(data == 0) goto oops;
180
181 if(read(fd, data, sz) != sz) goto oops;
182 close(fd);
183
184 if(_sz) *_sz = sz;
185 return data;
186
187oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800188 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 close(fd);
190 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800191 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 return 0;
193}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000195static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700196{
197 int fd;
198
199 fd = open(fn, O_RDONLY | O_BINARY);
200 if(fd < 0) return 0;
201
202 return load_fd(fd, _sz);
203}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
JP Abgralla032ded2012-06-06 11:53:33 -0700205int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
206{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400208 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800209 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700210 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800211 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700212 (info->dev_vendor != 0x0fce) && // Sony Ericsson
213 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400214 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800215 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800216 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800217 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800218 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400219 (info->dev_vendor != 0x0bb4)) // HTC
220 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 if(info->ifc_class != 0xff) return -1;
222 if(info->ifc_subclass != 0x42) return -1;
223 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700224 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700226 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
227 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 return 0;
229}
230
JP Abgrall7b8970c2013-03-07 17:06:41 -0800231int match_fastboot(usb_ifc_info *info)
232{
233 return match_fastboot_with_serial(info, serial);
234}
235
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236int list_devices_callback(usb_ifc_info *info)
237{
JP Abgralla032ded2012-06-06 11:53:33 -0700238 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700240 if (!info->writable) {
241 serial = "no permissions"; // like "adb devices"
242 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 if (!serial[0]) {
244 serial = "????????????";
245 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700246 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700247 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700248 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700249 } else if (!info->device_path) {
250 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700251 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700252 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700253 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 }
255
256 return -1;
257}
258
259usb_handle *open_device(void)
260{
261 static usb_handle *usb = 0;
262 int announce = 1;
263
264 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800265
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 for(;;) {
267 usb = usb_open(match_fastboot);
268 if(usb) return usb;
269 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800270 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 fprintf(stderr,"< waiting for device >\n");
272 }
273 sleep(1);
274 }
275}
276
277void list_devices(void) {
278 // We don't actually open a USB device here,
279 // just getting our callback called so we can
280 // list all the connected devices.
281 usb_open(list_devices_callback);
282}
283
284void usage(void)
285{
286 fprintf(stderr,
287/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
288 "usage: fastboot [ <option> ] <command>\n"
289 "\n"
290 "commands:\n"
291 " update <filename> reflash device from update.zip\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700292 " flashall flash boot, system, and if found,\n"
293 " recovery, tos\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 " flash <partition> [ <filename> ] write a file to a flash partition\n"
295 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700296 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
297 " Can override the fs type and/or\n"
298 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 " getvar <variable> display a bootloader variable\n"
300 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
301 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
302 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700303 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 " reboot reboot device normally\n"
305 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800306 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 "\n"
308 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700309 " -w erase userdata and cache (and format\n"
310 " if supported by partition type)\n"
311 " -u do not first erase partition before\n"
312 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700313 " -s <specific device> specify device serial number\n"
314 " or path to device port\n"
315 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 " -p <product> specify product name\n"
317 " -c <cmdline> override kernel commandline\n"
318 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700319 " -b <base_addr> specify a custom kernel base address.\n"
320 " default: 0x10000000\n"
321 " -n <page size> specify the nand page size.\n"
322 " default: 2048\n"
323 " -S <size>[K|M|G] automatically sparse files greater\n"
324 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326}
327
JP Abgrall7b8970c2013-03-07 17:06:41 -0800328void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 unsigned *sz, const char *cmdline)
330{
331 void *kdata = 0, *rdata = 0;
332 unsigned ksize = 0, rsize = 0;
333 void *bdata;
334 unsigned bsize;
335
336 if(kernel == 0) {
337 fprintf(stderr, "no image specified\n");
338 return 0;
339 }
340
341 kdata = load_file(kernel, &ksize);
342 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800343 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 return 0;
345 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 /* is this actually a boot image? */
348 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
349 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800350
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 if(ramdisk) {
352 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
353 return 0;
354 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800355
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 *sz = ksize;
357 return kdata;
358 }
359
360 if(ramdisk) {
361 rdata = load_file(ramdisk, &rsize);
362 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800363 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 return 0;
365 }
366 }
367
368 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800369 bdata = mkbootimg(kdata, ksize, kernel_offset,
370 rdata, rsize, ramdisk_offset,
371 0, 0, second_offset,
372 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 if(bdata == 0) {
374 fprintf(stderr,"failed to create boot.img\n");
375 return 0;
376 }
377 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
378 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
379 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800380
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 return bdata;
382}
383
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000384void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385{
386 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000387 zipentry_t entry;
388 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800389
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000390 entry = lookup_zipentry(zip, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 if (entry == NULL) {
392 fprintf(stderr, "archive does not contain '%s'\n", name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000393 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 }
395
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000396 *sz = get_zipentry_size(entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000398 datasz = *sz * 1.001;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 data = malloc(datasz);
400
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000401 if(data == 0) {
402 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
403 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 }
405
406 if (decompress_zipentry(entry, data, datasz)) {
407 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
408 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000409 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000411
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 return data;
413}
414
Rom Lemarchand622810c2013-06-28 09:54:59 -0700415static int unzip_to_file(zipfile_t zip, char *name)
416{
417 int fd;
418 char *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000419 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700420
421 fd = fileno(tmpfile());
422 if (fd < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700423 return -1;
424 }
425
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000426 data = unzip_file(zip, name, &sz);
427 if (data == 0) {
428 return -1;
429 }
430
431 if (write(fd, data, sz) != sz) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700432 fd = -1;
433 }
434
435 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000436 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700437 return fd;
438}
439
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440static char *strip(char *s)
441{
442 int n;
443 while(*s && isspace(*s)) s++;
444 n = strlen(s);
445 while(n-- > 0) {
446 if(!isspace(s[n])) break;
447 s[n] = 0;
448 }
449 return s;
450}
451
452#define MAX_OPTIONS 32
453static int setup_requirement_line(char *name)
454{
455 char *val[MAX_OPTIONS];
456 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700457 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 unsigned n, count;
459 char *x;
460 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 if (!strncmp(name, "reject ", 7)) {
463 name += 7;
464 invert = 1;
465 } else if (!strncmp(name, "require ", 8)) {
466 name += 8;
467 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700468 } else if (!strncmp(name, "require-for-product:", 20)) {
469 // Get the product and point name past it
470 prod = name + 20;
471 name = strchr(name, ' ');
472 if (!name) return -1;
473 *name = 0;
474 name += 1;
475 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 }
477
478 x = strchr(name, '=');
479 if (x == 0) return 0;
480 *x = 0;
481 val[0] = x + 1;
482
483 for(count = 1; count < MAX_OPTIONS; count++) {
484 x = strchr(val[count - 1],'|');
485 if (x == 0) break;
486 *x = 0;
487 val[count] = x + 1;
488 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800489
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 name = strip(name);
491 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800492
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 name = strip(name);
494 if (name == 0) return -1;
495
496 /* work around an unfortunate name mismatch */
497 if (!strcmp(name,"board")) name = "product";
498
499 out = malloc(sizeof(char*) * count);
500 if (out == 0) return -1;
501
502 for(n = 0; n < count; n++) {
503 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700504 if (out[n] == 0) {
505 for(size_t i = 0; i < n; ++i) {
506 free((char*) out[i]);
507 }
508 free(out);
509 return -1;
510 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 }
512
Wink Savilleb98762f2011-04-04 17:54:59 -0700513 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514 return 0;
515}
516
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000517static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000519 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000521 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 while (sz-- > 0) {
523 if(*s == '\n') {
524 *s++ = 0;
525 if (setup_requirement_line(data)) {
526 die("out of memory");
527 }
528 data = s;
529 } else {
530 s++;
531 }
532 }
533}
534
535void queue_info_dump(void)
536{
537 fb_queue_notice("--------------------------------------------");
538 fb_queue_display("version-bootloader", "Bootloader Version...");
539 fb_queue_display("version-baseband", "Baseband Version.....");
540 fb_queue_display("serialno", "Serial Number........");
541 fb_queue_notice("--------------------------------------------");
542}
543
Rom Lemarchand622810c2013-06-28 09:54:59 -0700544static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700545{
Colin Crossf8387882012-05-24 17:18:41 -0700546 struct sparse_file *s;
547 int files;
548 struct sparse_file **out_s;
549
Colin Crossf8387882012-05-24 17:18:41 -0700550 s = sparse_file_import_auto(fd, false);
551 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700552 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700553 }
554
555 files = sparse_file_resparse(s, max_size, NULL, 0);
556 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700557 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700558 }
559
560 out_s = calloc(sizeof(struct sparse_file *), files + 1);
561 if (!out_s) {
562 die("Failed to allocate sparse file array\n");
563 }
564
565 files = sparse_file_resparse(s, max_size, out_s, files);
566 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700567 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700568 }
569
570 return out_s;
571}
572
573static int64_t get_target_sparse_limit(struct usb_handle *usb)
574{
575 int64_t limit = 0;
576 char response[FB_RESPONSE_SZ + 1];
577 int status = fb_getvar(usb, response, "max-download-size");
578
579 if (!status) {
580 limit = strtoul(response, NULL, 0);
581 if (limit > 0) {
582 fprintf(stderr, "target reported max download size of %lld bytes\n",
583 limit);
584 }
585 }
586
587 return limit;
588}
589
590static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
591{
592 int64_t limit;
593
594 if (sparse_limit == 0) {
595 return 0;
596 } else if (sparse_limit > 0) {
597 limit = sparse_limit;
598 } else {
599 if (target_sparse_limit == -1) {
600 target_sparse_limit = get_target_sparse_limit(usb);
601 }
602 if (target_sparse_limit > 0) {
603 limit = target_sparse_limit;
604 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700605 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700606 }
607 }
608
609 if (size > limit) {
610 return limit;
611 }
612
613 return 0;
614}
615
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700616/* Until we get lazy inode table init working in make_ext4fs, we need to
617 * erase partitions of type ext4 before flashing a filesystem so no stale
618 * inodes are left lying around. Otherwise, e2fsck gets very upset.
619 */
620static int needs_erase(const char *part)
621{
622 /* The function fb_format_supported() currently returns the value
623 * we want, so just call it.
624 */
JP Abgrall7e859742014-05-06 15:14:15 -0700625 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700626}
627
Rom Lemarchand622810c2013-06-28 09:54:59 -0700628static int load_buf_fd(usb_handle *usb, int fd,
629 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700630{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700631 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000632 void *data;
633 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700634
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000635
636 sz64 = file_size(fd);
637 if (sz64 < 0) {
638 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700640
641 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700642 limit = get_sparse_limit(usb, sz64);
643 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700644 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700645 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700647 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700648 buf->type = FB_BUFFER_SPARSE;
649 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700650 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000651 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700652 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000653 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700654 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700655 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000656 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700657 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700658
659 return 0;
660}
661
662static int load_buf(usb_handle *usb, const char *fname,
663 struct fastboot_buffer *buf)
664{
665 int fd;
666
667 fd = open(fname, O_RDONLY | O_BINARY);
668 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700669 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700670 }
671
672 return load_buf_fd(usb, fd, buf);
673}
674
675static void flash_buf(const char *pname, struct fastboot_buffer *buf)
676{
677 struct sparse_file **s;
678
679 switch (buf->type) {
680 case FB_BUFFER_SPARSE:
681 s = buf->data;
682 while (*s) {
683 int64_t sz64 = sparse_file_len(*s, true, false);
684 fb_queue_flash_sparse(pname, *s++, sz64);
685 }
686 break;
687 case FB_BUFFER:
688 fb_queue_flash(pname, buf->data, buf->sz);
689 break;
690 default:
691 die("unknown buffer type: %d", buf->type);
692 }
693}
694
695void do_flash(usb_handle *usb, const char *pname, const char *fname)
696{
697 struct fastboot_buffer buf;
698
699 if (load_buf(usb, fname, &buf)) {
700 die("cannot load '%s'", fname);
701 }
702 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700703}
704
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705void do_update_signature(zipfile_t zip, char *fn)
706{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000707 void *data;
708 unsigned sz;
709 data = unzip_file(zip, fn, &sz);
710 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 fb_queue_download("signature", data, sz);
712 fb_queue_command("signature", "installing signature");
713}
714
Rom Lemarchand622810c2013-06-28 09:54:59 -0700715void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716{
717 void *zdata;
718 unsigned zsize;
719 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000720 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800721 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700722 int fd;
723 int rc;
724 struct fastboot_buffer buf;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000725 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726
727 queue_info_dump();
728
Wink Savilleb98762f2011-04-04 17:54:59 -0700729 fb_queue_query_save("product", cur_product, sizeof(cur_product));
730
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800732 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733
734 zip = init_zipfile(zdata, zsize);
735 if(zip == 0) die("failed to access zipdata in '%s'");
736
737 data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000738 if (data == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800739 char *tmp;
740 /* fallback for older zipfiles */
741 data = unzip_file(zip, "android-product.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000742 if ((data == 0) || (sz < 1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 die("update package has no android-info.txt or android-product.txt");
744 }
745 tmp = malloc(sz + 128);
746 if (tmp == 0) die("out of memory");
747 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
748 data = tmp;
749 sz = strlen(tmp);
750 }
751
752 setup_requirements(data, sz);
753
Rom Lemarchand622810c2013-06-28 09:54:59 -0700754 for (i = 0; i < ARRAY_SIZE(images); i++) {
755 fd = unzip_to_file(zip, images[i].img_name);
756 if (fd < 0) {
757 if (images[i].is_optional)
758 continue;
759 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700760 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700761 rc = load_buf_fd(usb, fd, &buf);
762 if (rc) die("cannot load %s from flash", images[i].img_name);
763 do_update_signature(zip, images[i].sig_name);
764 if (erase_first && needs_erase(images[i].part_name)) {
765 fb_queue_erase(images[i].part_name);
766 }
767 flash_buf(images[i].part_name, &buf);
768 /* not closing the fd here since the sparse code keeps the fd around
769 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
770 * program exits.
771 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800772 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773}
774
775void do_send_signature(char *fn)
776{
777 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000778 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800780
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781 xtn = strrchr(fn, '.');
782 if (!xtn) return;
783 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800784
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 strcpy(xtn,".sig");
786 data = load_file(fn, &sz);
787 strcpy(xtn,".img");
788 if (data == 0) return;
789 fb_queue_download("signature", data, sz);
790 fb_queue_command("signature", "installing signature");
791}
792
Rom Lemarchand622810c2013-06-28 09:54:59 -0700793void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800794{
795 char *fname;
796 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000797 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798 struct fastboot_buffer buf;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000799 int i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800800
801 queue_info_dump();
802
Wink Savilleb98762f2011-04-04 17:54:59 -0700803 fb_queue_query_save("product", cur_product, sizeof(cur_product));
804
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800805 fname = find_item("info", product);
806 if (fname == 0) die("cannot find android-info.txt");
807 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800808 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 setup_requirements(data, sz);
810
Rom Lemarchand622810c2013-06-28 09:54:59 -0700811 for (i = 0; i < ARRAY_SIZE(images); i++) {
812 fname = find_item(images[i].part_name, product);
813 if (load_buf(usb, fname, &buf)) {
814 if (images[i].is_optional)
815 continue;
816 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700817 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700818 do_send_signature(fname);
819 if (erase_first && needs_erase(images[i].part_name)) {
820 fb_queue_erase(images[i].part_name);
821 }
822 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800823 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824}
825
826#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800827#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828
829int do_oem_command(int argc, char **argv)
830{
831 int i;
832 char command[256];
833 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800834
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835 command[0] = 0;
836 while(1) {
837 strcat(command,*argv);
838 skip(1);
839 if(argc == 0) break;
840 strcat(command," ");
841 }
842
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800843 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 return 0;
845}
846
Colin Crossf8387882012-05-24 17:18:41 -0700847static int64_t parse_num(const char *arg)
848{
849 char *endptr;
850 unsigned long long num;
851
852 num = strtoull(arg, &endptr, 0);
853 if (endptr == arg) {
854 return -1;
855 }
856
857 if (*endptr == 'k' || *endptr == 'K') {
858 if (num >= (-1ULL) / 1024) {
859 return -1;
860 }
861 num *= 1024LL;
862 endptr++;
863 } else if (*endptr == 'm' || *endptr == 'M') {
864 if (num >= (-1ULL) / (1024 * 1024)) {
865 return -1;
866 }
867 num *= 1024LL * 1024LL;
868 endptr++;
869 } else if (*endptr == 'g' || *endptr == 'G') {
870 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
871 return -1;
872 }
873 num *= 1024LL * 1024LL * 1024LL;
874 endptr++;
875 }
876
877 if (*endptr != '\0') {
878 return -1;
879 }
880
881 if (num > INT64_MAX) {
882 return -1;
883 }
884
885 return num;
886}
887
JP Abgrall7e859742014-05-06 15:14:15 -0700888void fb_perform_format(const char *partition, int skip_if_not_supported,
889 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700890{
JP Abgrall7e859742014-05-06 15:14:15 -0700891 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
892 char *pType = pTypeBuff;
893 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700894 unsigned int limit = INT_MAX;
895 struct fastboot_buffer buf;
896 const char *errMsg = NULL;
897 const struct fs_generator *gen;
898 uint64_t pSz;
899 int status;
900 int fd;
901
902 if (target_sparse_limit > 0 && target_sparse_limit < limit)
903 limit = target_sparse_limit;
904 if (sparse_limit > 0 && sparse_limit < limit)
905 limit = sparse_limit;
906
907 status = fb_getvar(usb, pType, "partition-type:%s", partition);
908 if (status) {
909 errMsg = "Can't determine partition type.\n";
910 goto failed;
911 }
JP Abgrall7e859742014-05-06 15:14:15 -0700912 if (type_override) {
913 if (strcmp(type_override, pType)) {
914 fprintf(stderr,
915 "Warning: %s type is %s, but %s was requested for formating.\n",
916 partition, pType, type_override);
917 }
918 pType = type_override;
919 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700920
921 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
922 if (status) {
923 errMsg = "Unable to get partition size\n";
924 goto failed;
925 }
JP Abgrall7e859742014-05-06 15:14:15 -0700926 if (size_override) {
927 if (strcmp(size_override, pSize)) {
928 fprintf(stderr,
929 "Warning: %s size is %s, but %s was requested for formating.\n",
930 partition, pSize, size_override);
931 }
932 pSize = size_override;
933 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700934
935 gen = fs_get_generator(pType);
936 if (!gen) {
937 if (skip_if_not_supported) {
938 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
939 fprintf(stderr, "File system type %s not supported.\n", pType);
940 return;
941 }
942 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
943 return;
944 }
945
946 pSz = strtoll(pSize, (char **)NULL, 16);
947
948 fd = fileno(tmpfile());
949 if (fs_generator_generate(gen, fd, pSz)) {
950 close(fd);
951 fprintf(stderr, "Cannot generate image.\n");
952 return;
953 }
954
955 if (load_buf_fd(usb, fd, &buf)) {
956 fprintf(stderr, "Cannot read image.\n");
957 close(fd);
958 return;
959 }
960 flash_buf(partition, &buf);
961
962 return;
963
964
965failed:
966 if (skip_if_not_supported) {
967 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
968 if (errMsg)
969 fprintf(stderr, "%s", errMsg);
970 }
971 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
972}
973
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974int main(int argc, char **argv)
975{
976 int wants_wipe = 0;
977 int wants_reboot = 0;
978 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700979 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000981 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700982 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700983 int c;
Colin Crossf8387882012-05-24 17:18:41 -0700984 int r;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800985
JP Abgrall7b8970c2013-03-07 17:06:41 -0800986 const struct option longopts[] = {
987 {"base", required_argument, 0, 'b'},
988 {"kernel_offset", required_argument, 0, 'k'},
989 {"page_size", required_argument, 0, 'n'},
990 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800991 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800992 {"help", 0, 0, 'h'},
993 {0, 0, 0, 0}
994 };
Colin Cross8879f982012-05-22 17:53:34 -0700995
996 serial = getenv("ANDROID_SERIAL");
997
998 while (1) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800999 int option_index = 0;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001000 c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, NULL);
Colin Cross8879f982012-05-22 17:53:34 -07001001 if (c < 0) {
1002 break;
1003 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001004 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001005 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001006 case 'b':
1007 base_addr = strtoul(optarg, 0, 16);
1008 break;
Colin Cross8879f982012-05-22 17:53:34 -07001009 case 'c':
1010 cmdline = optarg;
1011 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001012 case 'h':
1013 usage();
1014 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001015 case 'i': {
1016 char *endptr = NULL;
1017 unsigned long val;
1018
1019 val = strtoul(optarg, &endptr, 0);
1020 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1021 die("invalid vendor id '%s'", optarg);
1022 vendor_id = (unsigned short)val;
1023 break;
1024 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001025 case 'k':
1026 kernel_offset = strtoul(optarg, 0, 16);
1027 break;
1028 case 'l':
1029 long_listing = 1;
1030 break;
1031 case 'n':
1032 page_size = (unsigned)strtoul(optarg, NULL, 0);
1033 if (!page_size) die("invalid page size");
1034 break;
1035 case 'p':
1036 product = optarg;
1037 break;
1038 case 'r':
1039 ramdisk_offset = strtoul(optarg, 0, 16);
1040 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001041 case 't':
1042 tags_offset = strtoul(optarg, 0, 16);
1043 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001044 case 's':
1045 serial = optarg;
1046 break;
1047 case 'S':
1048 sparse_limit = parse_num(optarg);
1049 if (sparse_limit < 0) {
1050 die("invalid sparse limit");
1051 }
1052 break;
1053 case 'u':
1054 erase_first = 0;
1055 break;
1056 case 'w':
1057 wants_wipe = 1;
1058 break;
Colin Cross8879f982012-05-22 17:53:34 -07001059 case '?':
1060 return 1;
1061 default:
1062 abort();
1063 }
1064 }
1065
1066 argc -= optind;
1067 argv += optind;
1068
1069 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001070 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001071 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072 }
1073
Colin Cross8fb6e062012-07-24 16:36:41 -07001074 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001075 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001076 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001077 return 0;
1078 }
1079
Colin Crossc7b75dc2012-08-29 18:17:06 -07001080 if (argc > 0 && !strcmp(*argv, "help")) {
1081 usage();
1082 return 0;
1083 }
1084
Colin Cross8879f982012-05-22 17:53:34 -07001085 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001086
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001088 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089 require(2);
1090 fb_queue_display(argv[1], argv[1]);
1091 skip(2);
1092 } else if(!strcmp(*argv, "erase")) {
1093 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001094
JP Abgrall7e859742014-05-06 15:14:15 -07001095 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001096 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1097 }
1098
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001099 fb_queue_erase(argv[1]);
1100 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001101 } else if(!strncmp(*argv, "format", strlen("format"))) {
1102 char *overrides;
1103 char *type_override = NULL;
1104 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001105 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001106 /*
1107 * Parsing for: "format[:[type][:[size]]]"
1108 * Some valid things:
1109 * - select ontly the size, and leave default fs type:
1110 * format::0x4000000 userdata
1111 * - default fs type and size:
1112 * format userdata
1113 * format:: userdata
1114 */
1115 overrides = strchr(*argv, ':');
1116 if (overrides) {
1117 overrides++;
1118 size_override = strchr(overrides, ':');
1119 if (size_override) {
1120 size_override[0] = '\0';
1121 size_override++;
1122 }
1123 type_override = overrides;
1124 }
1125 if (type_override && !type_override[0]) type_override = NULL;
1126 if (size_override && !size_override[0]) size_override = NULL;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001127 if (erase_first && needs_erase(argv[1])) {
1128 fb_queue_erase(argv[1]);
1129 }
JP Abgrall7e859742014-05-06 15:14:15 -07001130 fb_perform_format(argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001131 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001132 } else if(!strcmp(*argv, "signature")) {
1133 require(2);
1134 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001135 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001136 if (sz != 256) die("signature must be 256 bytes");
1137 fb_queue_download("signature", data, sz);
1138 fb_queue_command("signature", "installing signature");
1139 skip(2);
1140 } else if(!strcmp(*argv, "reboot")) {
1141 wants_reboot = 1;
1142 skip(1);
1143 } else if(!strcmp(*argv, "reboot-bootloader")) {
1144 wants_reboot_bootloader = 1;
1145 skip(1);
1146 } else if (!strcmp(*argv, "continue")) {
1147 fb_queue_command("continue", "resuming boot");
1148 skip(1);
1149 } else if(!strcmp(*argv, "boot")) {
1150 char *kname = 0;
1151 char *rname = 0;
1152 skip(1);
1153 if (argc > 0) {
1154 kname = argv[0];
1155 skip(1);
1156 }
1157 if (argc > 0) {
1158 rname = argv[0];
1159 skip(1);
1160 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001161 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001162 if (data == 0) return 1;
1163 fb_queue_download("boot.img", data, sz);
1164 fb_queue_command("boot", "booting");
1165 } else if(!strcmp(*argv, "flash")) {
1166 char *pname = argv[1];
1167 char *fname = 0;
1168 require(2);
1169 if (argc > 2) {
1170 fname = argv[2];
1171 skip(3);
1172 } else {
1173 fname = find_item(pname, product);
1174 skip(2);
1175 }
1176 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001177 if (erase_first && needs_erase(pname)) {
1178 fb_queue_erase(pname);
1179 }
Colin Crossf8387882012-05-24 17:18:41 -07001180 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001181 } else if(!strcmp(*argv, "flash:raw")) {
1182 char *pname = argv[1];
1183 char *kname = argv[2];
1184 char *rname = 0;
1185 require(3);
1186 if(argc > 3) {
1187 rname = argv[3];
1188 skip(4);
1189 } else {
1190 skip(3);
1191 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001192 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 if (data == 0) die("cannot load bootable image");
1194 fb_queue_flash(pname, data, sz);
1195 } else if(!strcmp(*argv, "flashall")) {
1196 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001197 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198 wants_reboot = 1;
1199 } else if(!strcmp(*argv, "update")) {
1200 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001201 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 skip(2);
1203 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001204 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205 skip(1);
1206 }
1207 wants_reboot = 1;
1208 } else if(!strcmp(*argv, "oem")) {
1209 argc = do_oem_command(argc, argv);
1210 } else {
1211 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001212 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213 }
1214 }
1215
1216 if (wants_wipe) {
1217 fb_queue_erase("userdata");
JP Abgrall7e859742014-05-06 15:14:15 -07001218 fb_perform_format("userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 fb_queue_erase("cache");
JP Abgrall7e859742014-05-06 15:14:15 -07001220 fb_perform_format("cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 }
1222 if (wants_reboot) {
1223 fb_queue_reboot();
1224 } else if (wants_reboot_bootloader) {
1225 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001226 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001227 }
1228
Scott Anderson13081c62012-04-06 12:39:30 -07001229 if (fb_queue_is_empty())
1230 return 0;
1231
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001232 status = fb_execute_queue(usb);
1233 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001234}