blob: 9c04c21ea38f6d6b62defcbea5bca130cab52c33 [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
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
37#include <stdbool.h>
38#include <stdint.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070044#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070045#include <unistd.h>
Colin Crossf8387882012-05-24 17:18:41 -070046
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"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070052#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Colin Crossf8387882012-05-24 17:18:41 -070054#ifndef O_BINARY
55#define O_BINARY 0
56#endif
57
Rom Lemarchand622810c2013-06-28 09:54:59 -070058#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
59
Wink Savilleb98762f2011-04-04 17:54:59 -070060char cur_product[FB_RESPONSE_SZ + 1];
61
Brian Swetland2a63bb72009-04-28 16:05:07 -070062void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
63
JP Abgrall7b8970c2013-03-07 17:06:41 -080064boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
65 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
66 void *second, unsigned second_size, unsigned second_offset,
67 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070068 unsigned *bootimg_size);
69
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070static usb_handle *usb = 0;
71static const char *serial = 0;
72static const char *product = 0;
73static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070075static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070076static int64_t sparse_limit = -1;
77static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
JP Abgrall7b8970c2013-03-07 17:06:41 -080079unsigned page_size = 2048;
80unsigned base_addr = 0x10000000;
81unsigned kernel_offset = 0x00008000;
82unsigned ramdisk_offset = 0x01000000;
83unsigned second_offset = 0x00f00000;
84unsigned tags_offset = 0x00000100;
85
Rom Lemarchand622810c2013-06-28 09:54:59 -070086enum fb_buffer_type {
87 FB_BUFFER,
88 FB_BUFFER_SPARSE,
89};
90
91struct fastboot_buffer {
92 enum fb_buffer_type type;
93 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000094 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070095};
96
97static struct {
98 char img_name[13];
99 char sig_name[13];
100 char part_name[9];
101 bool is_optional;
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700102} images[4] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103 {"boot.img", "boot.sig", "boot", false},
104 {"recovery.img", "recovery.sig", "recovery", true},
105 {"system.img", "system.sig", "system", false},
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700106 {"tos.img", "tos.sig", "tos", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700108
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109void get_my_path(char *path);
110
111char *find_item(const char *item, const char *product)
112{
113 char *dir;
114 char *fn;
115 char path[PATH_MAX + 128];
116
117 if(!strcmp(item,"boot")) {
118 fn = "boot.img";
119 } else if(!strcmp(item,"recovery")) {
120 fn = "recovery.img";
121 } else if(!strcmp(item,"system")) {
122 fn = "system.img";
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700123 } else if(!strcmp(item,"tos")) {
124 fn = "tos.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 } else if(!strcmp(item,"userdata")) {
126 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700127 } else if(!strcmp(item,"cache")) {
128 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 } else if(!strcmp(item,"info")) {
130 fn = "android-info.txt";
131 } else {
132 fprintf(stderr,"unknown partition '%s'\n", item);
133 return 0;
134 }
135
136 if(product) {
137 get_my_path(path);
138 sprintf(path + strlen(path),
139 "../../../target/product/%s/%s", product, fn);
140 return strdup(path);
141 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800142
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 dir = getenv("ANDROID_PRODUCT_OUT");
144 if((dir == 0) || (dir[0] == 0)) {
145 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
146 return 0;
147 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 sprintf(path, "%s/%s", dir, fn);
150 return strdup(path);
151}
152
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000153static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700154{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700155 struct stat st;
156 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700157
Rom Lemarchand622810c2013-06-28 09:54:59 -0700158 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700159
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700161}
162
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000163static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000165 char *data;
166 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800167 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000169 data = 0;
170
171 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700172 if (sz < 0) {
173 goto oops;
174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
176 data = (char*) malloc(sz);
177 if(data == 0) goto oops;
178
179 if(read(fd, data, sz) != sz) goto oops;
180 close(fd);
181
182 if(_sz) *_sz = sz;
183 return data;
184
185oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 close(fd);
188 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800189 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 return 0;
191}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000193static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700194{
195 int fd;
196
197 fd = open(fn, O_RDONLY | O_BINARY);
198 if(fd < 0) return 0;
199
200 return load_fd(fd, _sz);
201}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
JP Abgralla032ded2012-06-06 11:53:33 -0700203int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
204{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400206 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800207 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700208 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800209 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700210 (info->dev_vendor != 0x0fce) && // Sony Ericsson
211 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400212 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800213 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800214 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800215 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800216 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400217 (info->dev_vendor != 0x0bb4)) // HTC
218 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 if(info->ifc_class != 0xff) return -1;
220 if(info->ifc_subclass != 0x42) return -1;
221 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700222 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700224 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
225 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 return 0;
227}
228
JP Abgrall7b8970c2013-03-07 17:06:41 -0800229int match_fastboot(usb_ifc_info *info)
230{
231 return match_fastboot_with_serial(info, serial);
232}
233
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234int list_devices_callback(usb_ifc_info *info)
235{
JP Abgralla032ded2012-06-06 11:53:33 -0700236 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700238 if (!info->writable) {
239 serial = "no permissions"; // like "adb devices"
240 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 if (!serial[0]) {
242 serial = "????????????";
243 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700244 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700245 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700246 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700247 } else if (!info->device_path) {
248 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700249 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700250 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700251 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
253
254 return -1;
255}
256
257usb_handle *open_device(void)
258{
259 static usb_handle *usb = 0;
260 int announce = 1;
261
262 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800263
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264 for(;;) {
265 usb = usb_open(match_fastboot);
266 if(usb) return usb;
267 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800268 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 fprintf(stderr,"< waiting for device >\n");
270 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700271 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 }
273}
274
275void list_devices(void) {
276 // We don't actually open a USB device here,
277 // just getting our callback called so we can
278 // list all the connected devices.
279 usb_open(list_devices_callback);
280}
281
282void usage(void)
283{
284 fprintf(stderr,
285/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
286 "usage: fastboot [ <option> ] <command>\n"
287 "\n"
288 "commands:\n"
289 " update <filename> reflash device from update.zip\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700290 " flashall flash boot, system, and if found,\n"
291 " recovery, tos\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 " flash <partition> [ <filename> ] write a file to a flash partition\n"
293 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700294 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
295 " Can override the fs type and/or\n"
296 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 " getvar <variable> display a bootloader variable\n"
298 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
299 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
300 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700301 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 " reboot reboot device normally\n"
303 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800304 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 "\n"
306 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700307 " -w erase userdata and cache (and format\n"
308 " if supported by partition type)\n"
309 " -u do not first erase partition before\n"
310 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700311 " -s <specific device> specify device serial number\n"
312 " or path to device port\n"
313 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 " -p <product> specify product name\n"
315 " -c <cmdline> override kernel commandline\n"
316 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700317 " -b <base_addr> specify a custom kernel base address.\n"
318 " default: 0x10000000\n"
319 " -n <page size> specify the nand page size.\n"
320 " default: 2048\n"
321 " -S <size>[K|M|G] automatically sparse files greater\n"
322 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324}
325
JP Abgrall7b8970c2013-03-07 17:06:41 -0800326void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 unsigned *sz, const char *cmdline)
328{
329 void *kdata = 0, *rdata = 0;
330 unsigned ksize = 0, rsize = 0;
331 void *bdata;
332 unsigned bsize;
333
334 if(kernel == 0) {
335 fprintf(stderr, "no image specified\n");
336 return 0;
337 }
338
339 kdata = load_file(kernel, &ksize);
340 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800341 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 return 0;
343 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800344
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 /* is this actually a boot image? */
346 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
347 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800348
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 if(ramdisk) {
350 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
351 return 0;
352 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800353
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 *sz = ksize;
355 return kdata;
356 }
357
358 if(ramdisk) {
359 rdata = load_file(ramdisk, &rsize);
360 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800361 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 return 0;
363 }
364 }
365
366 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800367 bdata = mkbootimg(kdata, ksize, kernel_offset,
368 rdata, rsize, ramdisk_offset,
369 0, 0, second_offset,
370 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 if(bdata == 0) {
372 fprintf(stderr,"failed to create boot.img\n");
373 return 0;
374 }
375 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
376 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
377 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800378
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 return bdata;
380}
381
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000382void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383{
384 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000385 zipentry_t entry;
386 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800387
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000388 entry = lookup_zipentry(zip, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 if (entry == NULL) {
390 fprintf(stderr, "archive does not contain '%s'\n", name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000391 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 }
393
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000394 *sz = get_zipentry_size(entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000396 datasz = *sz * 1.001;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 data = malloc(datasz);
398
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 if(data == 0) {
400 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
401 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 }
403
404 if (decompress_zipentry(entry, data, datasz)) {
405 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
406 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000407 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000409
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 return data;
411}
412
Rom Lemarchand622810c2013-06-28 09:54:59 -0700413static int unzip_to_file(zipfile_t zip, char *name)
414{
415 int fd;
416 char *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000417 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700418
419 fd = fileno(tmpfile());
420 if (fd < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700421 return -1;
422 }
423
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000424 data = unzip_file(zip, name, &sz);
425 if (data == 0) {
426 return -1;
427 }
428
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700429 if (write(fd, data, sz) != (ssize_t)sz) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700430 fd = -1;
431 }
432
433 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000434 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700435 return fd;
436}
437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438static char *strip(char *s)
439{
440 int n;
441 while(*s && isspace(*s)) s++;
442 n = strlen(s);
443 while(n-- > 0) {
444 if(!isspace(s[n])) break;
445 s[n] = 0;
446 }
447 return s;
448}
449
450#define MAX_OPTIONS 32
451static int setup_requirement_line(char *name)
452{
453 char *val[MAX_OPTIONS];
454 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700455 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 unsigned n, count;
457 char *x;
458 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 if (!strncmp(name, "reject ", 7)) {
461 name += 7;
462 invert = 1;
463 } else if (!strncmp(name, "require ", 8)) {
464 name += 8;
465 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700466 } else if (!strncmp(name, "require-for-product:", 20)) {
467 // Get the product and point name past it
468 prod = name + 20;
469 name = strchr(name, ' ');
470 if (!name) return -1;
471 *name = 0;
472 name += 1;
473 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 }
475
476 x = strchr(name, '=');
477 if (x == 0) return 0;
478 *x = 0;
479 val[0] = x + 1;
480
481 for(count = 1; count < MAX_OPTIONS; count++) {
482 x = strchr(val[count - 1],'|');
483 if (x == 0) break;
484 *x = 0;
485 val[count] = x + 1;
486 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800487
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 name = strip(name);
489 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 name = strip(name);
492 if (name == 0) return -1;
493
494 /* work around an unfortunate name mismatch */
495 if (!strcmp(name,"board")) name = "product";
496
497 out = malloc(sizeof(char*) * count);
498 if (out == 0) return -1;
499
500 for(n = 0; n < count; n++) {
501 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700502 if (out[n] == 0) {
503 for(size_t i = 0; i < n; ++i) {
504 free((char*) out[i]);
505 }
506 free(out);
507 return -1;
508 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 }
510
Wink Savilleb98762f2011-04-04 17:54:59 -0700511 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 return 0;
513}
514
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000515static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000517 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000519 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 while (sz-- > 0) {
521 if(*s == '\n') {
522 *s++ = 0;
523 if (setup_requirement_line(data)) {
524 die("out of memory");
525 }
526 data = s;
527 } else {
528 s++;
529 }
530 }
531}
532
533void queue_info_dump(void)
534{
535 fb_queue_notice("--------------------------------------------");
536 fb_queue_display("version-bootloader", "Bootloader Version...");
537 fb_queue_display("version-baseband", "Baseband Version.....");
538 fb_queue_display("serialno", "Serial Number........");
539 fb_queue_notice("--------------------------------------------");
540}
541
Rom Lemarchand622810c2013-06-28 09:54:59 -0700542static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700543{
Colin Crossf8387882012-05-24 17:18:41 -0700544 struct sparse_file *s;
545 int files;
546 struct sparse_file **out_s;
547
Colin Crossf8387882012-05-24 17:18:41 -0700548 s = sparse_file_import_auto(fd, false);
549 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700550 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700551 }
552
553 files = sparse_file_resparse(s, max_size, NULL, 0);
554 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700555 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700556 }
557
558 out_s = calloc(sizeof(struct sparse_file *), files + 1);
559 if (!out_s) {
560 die("Failed to allocate sparse file array\n");
561 }
562
563 files = sparse_file_resparse(s, max_size, out_s, files);
564 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700566 }
567
568 return out_s;
569}
570
571static int64_t get_target_sparse_limit(struct usb_handle *usb)
572{
573 int64_t limit = 0;
574 char response[FB_RESPONSE_SZ + 1];
575 int status = fb_getvar(usb, response, "max-download-size");
576
577 if (!status) {
578 limit = strtoul(response, NULL, 0);
579 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700580 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700581 limit);
582 }
583 }
584
585 return limit;
586}
587
588static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
589{
590 int64_t limit;
591
592 if (sparse_limit == 0) {
593 return 0;
594 } else if (sparse_limit > 0) {
595 limit = sparse_limit;
596 } else {
597 if (target_sparse_limit == -1) {
598 target_sparse_limit = get_target_sparse_limit(usb);
599 }
600 if (target_sparse_limit > 0) {
601 limit = target_sparse_limit;
602 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700603 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700604 }
605 }
606
607 if (size > limit) {
608 return limit;
609 }
610
611 return 0;
612}
613
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700614/* Until we get lazy inode table init working in make_ext4fs, we need to
615 * erase partitions of type ext4 before flashing a filesystem so no stale
616 * inodes are left lying around. Otherwise, e2fsck gets very upset.
617 */
618static int needs_erase(const char *part)
619{
620 /* The function fb_format_supported() currently returns the value
621 * we want, so just call it.
622 */
JP Abgrall7e859742014-05-06 15:14:15 -0700623 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700624}
625
Rom Lemarchand622810c2013-06-28 09:54:59 -0700626static int load_buf_fd(usb_handle *usb, int fd,
627 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700628{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700629 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000630 void *data;
631 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700632
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000633
634 sz64 = file_size(fd);
635 if (sz64 < 0) {
636 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700638
639 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700640 limit = get_sparse_limit(usb, sz64);
641 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700642 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700643 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700644 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700645 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 buf->type = FB_BUFFER_SPARSE;
647 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700648 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000649 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700650 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000651 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700652 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700653 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000654 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700655 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700656
657 return 0;
658}
659
660static int load_buf(usb_handle *usb, const char *fname,
661 struct fastboot_buffer *buf)
662{
663 int fd;
664
665 fd = open(fname, O_RDONLY | O_BINARY);
666 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700667 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700668 }
669
670 return load_buf_fd(usb, fd, buf);
671}
672
673static void flash_buf(const char *pname, struct fastboot_buffer *buf)
674{
675 struct sparse_file **s;
676
677 switch (buf->type) {
678 case FB_BUFFER_SPARSE:
679 s = buf->data;
680 while (*s) {
681 int64_t sz64 = sparse_file_len(*s, true, false);
682 fb_queue_flash_sparse(pname, *s++, sz64);
683 }
684 break;
685 case FB_BUFFER:
686 fb_queue_flash(pname, buf->data, buf->sz);
687 break;
688 default:
689 die("unknown buffer type: %d", buf->type);
690 }
691}
692
693void do_flash(usb_handle *usb, const char *pname, const char *fname)
694{
695 struct fastboot_buffer buf;
696
697 if (load_buf(usb, fname, &buf)) {
698 die("cannot load '%s'", fname);
699 }
700 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700701}
702
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703void do_update_signature(zipfile_t zip, char *fn)
704{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000705 void *data;
706 unsigned sz;
707 data = unzip_file(zip, fn, &sz);
708 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 fb_queue_download("signature", data, sz);
710 fb_queue_command("signature", "installing signature");
711}
712
Rom Lemarchand622810c2013-06-28 09:54:59 -0700713void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714{
715 void *zdata;
716 unsigned zsize;
717 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000718 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700720 int fd;
721 int rc;
722 struct fastboot_buffer buf;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700723 size_t i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800724
725 queue_info_dump();
726
Wink Savilleb98762f2011-04-04 17:54:59 -0700727 fb_queue_query_save("product", cur_product, sizeof(cur_product));
728
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800730 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731
732 zip = init_zipfile(zdata, zsize);
733 if(zip == 0) die("failed to access zipdata in '%s'");
734
735 data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000736 if (data == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 char *tmp;
738 /* fallback for older zipfiles */
739 data = unzip_file(zip, "android-product.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000740 if ((data == 0) || (sz < 1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741 die("update package has no android-info.txt or android-product.txt");
742 }
743 tmp = malloc(sz + 128);
744 if (tmp == 0) die("out of memory");
745 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
746 data = tmp;
747 sz = strlen(tmp);
748 }
749
750 setup_requirements(data, sz);
751
Rom Lemarchand622810c2013-06-28 09:54:59 -0700752 for (i = 0; i < ARRAY_SIZE(images); i++) {
753 fd = unzip_to_file(zip, images[i].img_name);
754 if (fd < 0) {
755 if (images[i].is_optional)
756 continue;
757 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700758 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700759 rc = load_buf_fd(usb, fd, &buf);
760 if (rc) die("cannot load %s from flash", images[i].img_name);
761 do_update_signature(zip, images[i].sig_name);
762 if (erase_first && needs_erase(images[i].part_name)) {
763 fb_queue_erase(images[i].part_name);
764 }
765 flash_buf(images[i].part_name, &buf);
766 /* not closing the fd here since the sparse code keeps the fd around
767 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
768 * program exits.
769 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800771}
772
773void do_send_signature(char *fn)
774{
775 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000776 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800778
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779 xtn = strrchr(fn, '.');
780 if (!xtn) return;
781 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800782
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800783 strcpy(xtn,".sig");
784 data = load_file(fn, &sz);
785 strcpy(xtn,".img");
786 if (data == 0) return;
787 fb_queue_download("signature", data, sz);
788 fb_queue_command("signature", "installing signature");
789}
790
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792{
793 char *fname;
794 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000795 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 struct fastboot_buffer buf;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700797 size_t i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798
799 queue_info_dump();
800
Wink Savilleb98762f2011-04-04 17:54:59 -0700801 fb_queue_query_save("product", cur_product, sizeof(cur_product));
802
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803 fname = find_item("info", product);
804 if (fname == 0) die("cannot find android-info.txt");
805 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800806 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807 setup_requirements(data, sz);
808
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 for (i = 0; i < ARRAY_SIZE(images); i++) {
810 fname = find_item(images[i].part_name, product);
811 if (load_buf(usb, fname, &buf)) {
812 if (images[i].is_optional)
813 continue;
814 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700815 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700816 do_send_signature(fname);
817 if (erase_first && needs_erase(images[i].part_name)) {
818 fb_queue_erase(images[i].part_name);
819 }
820 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800822}
823
824#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800825#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826
827int do_oem_command(int argc, char **argv)
828{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829 char command[256];
830 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800831
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 command[0] = 0;
833 while(1) {
834 strcat(command,*argv);
835 skip(1);
836 if(argc == 0) break;
837 strcat(command," ");
838 }
839
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800840 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800841 return 0;
842}
843
Colin Crossf8387882012-05-24 17:18:41 -0700844static int64_t parse_num(const char *arg)
845{
846 char *endptr;
847 unsigned long long num;
848
849 num = strtoull(arg, &endptr, 0);
850 if (endptr == arg) {
851 return -1;
852 }
853
854 if (*endptr == 'k' || *endptr == 'K') {
855 if (num >= (-1ULL) / 1024) {
856 return -1;
857 }
858 num *= 1024LL;
859 endptr++;
860 } else if (*endptr == 'm' || *endptr == 'M') {
861 if (num >= (-1ULL) / (1024 * 1024)) {
862 return -1;
863 }
864 num *= 1024LL * 1024LL;
865 endptr++;
866 } else if (*endptr == 'g' || *endptr == 'G') {
867 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
868 return -1;
869 }
870 num *= 1024LL * 1024LL * 1024LL;
871 endptr++;
872 }
873
874 if (*endptr != '\0') {
875 return -1;
876 }
877
878 if (num > INT64_MAX) {
879 return -1;
880 }
881
882 return num;
883}
884
JP Abgrall7e859742014-05-06 15:14:15 -0700885void fb_perform_format(const char *partition, int skip_if_not_supported,
886 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700887{
JP Abgrall7e859742014-05-06 15:14:15 -0700888 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
889 char *pType = pTypeBuff;
890 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700891 unsigned int limit = INT_MAX;
892 struct fastboot_buffer buf;
893 const char *errMsg = NULL;
894 const struct fs_generator *gen;
895 uint64_t pSz;
896 int status;
897 int fd;
898
899 if (target_sparse_limit > 0 && target_sparse_limit < limit)
900 limit = target_sparse_limit;
901 if (sparse_limit > 0 && sparse_limit < limit)
902 limit = sparse_limit;
903
904 status = fb_getvar(usb, pType, "partition-type:%s", partition);
905 if (status) {
906 errMsg = "Can't determine partition type.\n";
907 goto failed;
908 }
JP Abgrall7e859742014-05-06 15:14:15 -0700909 if (type_override) {
910 if (strcmp(type_override, pType)) {
911 fprintf(stderr,
912 "Warning: %s type is %s, but %s was requested for formating.\n",
913 partition, pType, type_override);
914 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700915 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700916 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700917
918 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
919 if (status) {
920 errMsg = "Unable to get partition size\n";
921 goto failed;
922 }
JP Abgrall7e859742014-05-06 15:14:15 -0700923 if (size_override) {
924 if (strcmp(size_override, pSize)) {
925 fprintf(stderr,
926 "Warning: %s size is %s, but %s was requested for formating.\n",
927 partition, pSize, size_override);
928 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700929 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700930 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700931
932 gen = fs_get_generator(pType);
933 if (!gen) {
934 if (skip_if_not_supported) {
935 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
936 fprintf(stderr, "File system type %s not supported.\n", pType);
937 return;
938 }
939 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
940 return;
941 }
942
943 pSz = strtoll(pSize, (char **)NULL, 16);
944
945 fd = fileno(tmpfile());
946 if (fs_generator_generate(gen, fd, pSz)) {
947 close(fd);
948 fprintf(stderr, "Cannot generate image.\n");
949 return;
950 }
951
952 if (load_buf_fd(usb, fd, &buf)) {
953 fprintf(stderr, "Cannot read image.\n");
954 close(fd);
955 return;
956 }
957 flash_buf(partition, &buf);
958
959 return;
960
961
962failed:
963 if (skip_if_not_supported) {
964 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
965 if (errMsg)
966 fprintf(stderr, "%s", errMsg);
967 }
968 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
969}
970
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971int main(int argc, char **argv)
972{
973 int wants_wipe = 0;
974 int wants_reboot = 0;
975 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700976 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800977 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000978 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700979 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700980 int c;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800981
JP Abgrall7b8970c2013-03-07 17:06:41 -0800982 const struct option longopts[] = {
983 {"base", required_argument, 0, 'b'},
984 {"kernel_offset", required_argument, 0, 'k'},
985 {"page_size", required_argument, 0, 'n'},
986 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800987 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800988 {"help", 0, 0, 'h'},
989 {0, 0, 0, 0}
990 };
Colin Cross8879f982012-05-22 17:53:34 -0700991
992 serial = getenv("ANDROID_SERIAL");
993
994 while (1) {
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800995 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 -0700996 if (c < 0) {
997 break;
998 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800999 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001000 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001001 case 'b':
1002 base_addr = strtoul(optarg, 0, 16);
1003 break;
Colin Cross8879f982012-05-22 17:53:34 -07001004 case 'c':
1005 cmdline = optarg;
1006 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001007 case 'h':
1008 usage();
1009 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001010 case 'i': {
1011 char *endptr = NULL;
1012 unsigned long val;
1013
1014 val = strtoul(optarg, &endptr, 0);
1015 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1016 die("invalid vendor id '%s'", optarg);
1017 vendor_id = (unsigned short)val;
1018 break;
1019 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001020 case 'k':
1021 kernel_offset = strtoul(optarg, 0, 16);
1022 break;
1023 case 'l':
1024 long_listing = 1;
1025 break;
1026 case 'n':
1027 page_size = (unsigned)strtoul(optarg, NULL, 0);
1028 if (!page_size) die("invalid page size");
1029 break;
1030 case 'p':
1031 product = optarg;
1032 break;
1033 case 'r':
1034 ramdisk_offset = strtoul(optarg, 0, 16);
1035 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001036 case 't':
1037 tags_offset = strtoul(optarg, 0, 16);
1038 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001039 case 's':
1040 serial = optarg;
1041 break;
1042 case 'S':
1043 sparse_limit = parse_num(optarg);
1044 if (sparse_limit < 0) {
1045 die("invalid sparse limit");
1046 }
1047 break;
1048 case 'u':
1049 erase_first = 0;
1050 break;
1051 case 'w':
1052 wants_wipe = 1;
1053 break;
Colin Cross8879f982012-05-22 17:53:34 -07001054 case '?':
1055 return 1;
1056 default:
1057 abort();
1058 }
1059 }
1060
1061 argc -= optind;
1062 argv += optind;
1063
1064 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001065 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001066 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001067 }
1068
Colin Cross8fb6e062012-07-24 16:36:41 -07001069 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001070 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001072 return 0;
1073 }
1074
Colin Crossc7b75dc2012-08-29 18:17:06 -07001075 if (argc > 0 && !strcmp(*argv, "help")) {
1076 usage();
1077 return 0;
1078 }
1079
Colin Cross8879f982012-05-22 17:53:34 -07001080 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001081
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001083 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084 require(2);
1085 fb_queue_display(argv[1], argv[1]);
1086 skip(2);
1087 } else if(!strcmp(*argv, "erase")) {
1088 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001089
JP Abgrall7e859742014-05-06 15:14:15 -07001090 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001091 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1092 }
1093
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 fb_queue_erase(argv[1]);
1095 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001096 } else if(!strncmp(*argv, "format", strlen("format"))) {
1097 char *overrides;
1098 char *type_override = NULL;
1099 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001100 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001101 /*
1102 * Parsing for: "format[:[type][:[size]]]"
1103 * Some valid things:
1104 * - select ontly the size, and leave default fs type:
1105 * format::0x4000000 userdata
1106 * - default fs type and size:
1107 * format userdata
1108 * format:: userdata
1109 */
1110 overrides = strchr(*argv, ':');
1111 if (overrides) {
1112 overrides++;
1113 size_override = strchr(overrides, ':');
1114 if (size_override) {
1115 size_override[0] = '\0';
1116 size_override++;
1117 }
1118 type_override = overrides;
1119 }
1120 if (type_override && !type_override[0]) type_override = NULL;
1121 if (size_override && !size_override[0]) size_override = NULL;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001122 if (erase_first && needs_erase(argv[1])) {
1123 fb_queue_erase(argv[1]);
1124 }
JP Abgrall7e859742014-05-06 15:14:15 -07001125 fb_perform_format(argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001126 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001127 } else if(!strcmp(*argv, "signature")) {
1128 require(2);
1129 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001130 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131 if (sz != 256) die("signature must be 256 bytes");
1132 fb_queue_download("signature", data, sz);
1133 fb_queue_command("signature", "installing signature");
1134 skip(2);
1135 } else if(!strcmp(*argv, "reboot")) {
1136 wants_reboot = 1;
1137 skip(1);
1138 } else if(!strcmp(*argv, "reboot-bootloader")) {
1139 wants_reboot_bootloader = 1;
1140 skip(1);
1141 } else if (!strcmp(*argv, "continue")) {
1142 fb_queue_command("continue", "resuming boot");
1143 skip(1);
1144 } else if(!strcmp(*argv, "boot")) {
1145 char *kname = 0;
1146 char *rname = 0;
1147 skip(1);
1148 if (argc > 0) {
1149 kname = argv[0];
1150 skip(1);
1151 }
1152 if (argc > 0) {
1153 rname = argv[0];
1154 skip(1);
1155 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001156 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 if (data == 0) return 1;
1158 fb_queue_download("boot.img", data, sz);
1159 fb_queue_command("boot", "booting");
1160 } else if(!strcmp(*argv, "flash")) {
1161 char *pname = argv[1];
1162 char *fname = 0;
1163 require(2);
1164 if (argc > 2) {
1165 fname = argv[2];
1166 skip(3);
1167 } else {
1168 fname = find_item(pname, product);
1169 skip(2);
1170 }
1171 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001172 if (erase_first && needs_erase(pname)) {
1173 fb_queue_erase(pname);
1174 }
Colin Crossf8387882012-05-24 17:18:41 -07001175 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001176 } else if(!strcmp(*argv, "flash:raw")) {
1177 char *pname = argv[1];
1178 char *kname = argv[2];
1179 char *rname = 0;
1180 require(3);
1181 if(argc > 3) {
1182 rname = argv[3];
1183 skip(4);
1184 } else {
1185 skip(3);
1186 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001187 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001188 if (data == 0) die("cannot load bootable image");
1189 fb_queue_flash(pname, data, sz);
1190 } else if(!strcmp(*argv, "flashall")) {
1191 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001192 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 wants_reboot = 1;
1194 } else if(!strcmp(*argv, "update")) {
1195 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001196 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001197 skip(2);
1198 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001199 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001200 skip(1);
1201 }
1202 wants_reboot = 1;
1203 } else if(!strcmp(*argv, "oem")) {
1204 argc = do_oem_command(argc, argv);
1205 } else {
1206 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001207 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001208 }
1209 }
1210
1211 if (wants_wipe) {
1212 fb_queue_erase("userdata");
JP Abgrall7e859742014-05-06 15:14:15 -07001213 fb_perform_format("userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 fb_queue_erase("cache");
JP Abgrall7e859742014-05-06 15:14:15 -07001215 fb_perform_format("cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001216 }
1217 if (wants_reboot) {
1218 fb_queue_reboot();
1219 } else if (wants_reboot_bootloader) {
1220 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001221 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001222 }
1223
Scott Anderson13081c62012-04-06 12:39:30 -07001224 if (fb_queue_is_empty())
1225 return 0;
1226
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001227 status = fb_execute_queue(usb);
1228 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001229}