blob: 3a140abbdff92fac0994a5f2d3d92c22f3cd77fc [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>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <limits.h>
36#include <stdbool.h>
37#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
Colin Crossf8387882012-05-24 17:18:41 -070045
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#include <bootimg.h>
Colin Crossf8387882012-05-24 17:18:41 -070047#include <sparse/sparse.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#include <zipfile/zipfile.h>
49
50#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070051#include "fs.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052
Colin Crossf8387882012-05-24 17:18:41 -070053#ifndef O_BINARY
54#define O_BINARY 0
55#endif
56
Rom Lemarchand622810c2013-06-28 09:54:59 -070057#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
58
Wink Savilleb98762f2011-04-04 17:54:59 -070059char cur_product[FB_RESPONSE_SZ + 1];
60
Brian Swetland2a63bb72009-04-28 16:05:07 -070061void bootimg_set_cmdline(boot_img_hdr *h, const char *cmdline);
62
JP Abgrall7b8970c2013-03-07 17:06:41 -080063boot_img_hdr *mkbootimg(void *kernel, unsigned kernel_size, unsigned kernel_offset,
64 void *ramdisk, unsigned ramdisk_size, unsigned ramdisk_offset,
65 void *second, unsigned second_size, unsigned second_offset,
66 unsigned page_size, unsigned base, unsigned tags_offset,
Brian Swetland2a63bb72009-04-28 16:05:07 -070067 unsigned *bootimg_size);
68
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069static usb_handle *usb = 0;
70static const char *serial = 0;
71static const char *product = 0;
72static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070074static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070075static int64_t sparse_limit = -1;
76static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
JP Abgrall7b8970c2013-03-07 17:06:41 -080078unsigned page_size = 2048;
79unsigned base_addr = 0x10000000;
80unsigned kernel_offset = 0x00008000;
81unsigned ramdisk_offset = 0x01000000;
82unsigned second_offset = 0x00f00000;
83unsigned tags_offset = 0x00000100;
84
Rom Lemarchand622810c2013-06-28 09:54:59 -070085enum fb_buffer_type {
86 FB_BUFFER,
87 FB_BUFFER_SPARSE,
88};
89
90struct fastboot_buffer {
91 enum fb_buffer_type type;
92 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000093 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070094};
95
96static struct {
97 char img_name[13];
98 char sig_name[13];
99 char part_name[9];
100 bool is_optional;
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700101} images[4] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 {"boot.img", "boot.sig", "boot", false},
103 {"recovery.img", "recovery.sig", "recovery", true},
104 {"system.img", "system.sig", "system", false},
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700105 {"tos.img", "tos.sig", "tos", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700107
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108void get_my_path(char *path);
109
110char *find_item(const char *item, const char *product)
111{
112 char *dir;
113 char *fn;
114 char path[PATH_MAX + 128];
115
116 if(!strcmp(item,"boot")) {
117 fn = "boot.img";
118 } else if(!strcmp(item,"recovery")) {
119 fn = "recovery.img";
120 } else if(!strcmp(item,"system")) {
121 fn = "system.img";
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700122 } else if(!strcmp(item,"tos")) {
123 fn = "tos.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 } else if(!strcmp(item,"userdata")) {
125 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700126 } else if(!strcmp(item,"cache")) {
127 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 } else if(!strcmp(item,"info")) {
129 fn = "android-info.txt";
130 } else {
131 fprintf(stderr,"unknown partition '%s'\n", item);
132 return 0;
133 }
134
135 if(product) {
136 get_my_path(path);
137 sprintf(path + strlen(path),
138 "../../../target/product/%s/%s", product, fn);
139 return strdup(path);
140 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 dir = getenv("ANDROID_PRODUCT_OUT");
143 if((dir == 0) || (dir[0] == 0)) {
144 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
145 return 0;
146 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800147
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 sprintf(path, "%s/%s", dir, fn);
149 return strdup(path);
150}
151
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000152static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700153{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700154 struct stat st;
155 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700156
Rom Lemarchand622810c2013-06-28 09:54:59 -0700157 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700158
Rom Lemarchand622810c2013-06-28 09:54:59 -0700159 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700160}
161
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000162static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000164 char *data;
165 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800166 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000168 data = 0;
169
170 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700171 if (sz < 0) {
172 goto oops;
173 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
175 data = (char*) malloc(sz);
176 if(data == 0) goto oops;
177
178 if(read(fd, data, sz) != sz) goto oops;
179 close(fd);
180
181 if(_sz) *_sz = sz;
182 return data;
183
184oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800185 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 close(fd);
187 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800188 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 return 0;
190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000192static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700193{
194 int fd;
195
196 fd = open(fn, O_RDONLY | O_BINARY);
197 if(fd < 0) return 0;
198
199 return load_fd(fd, _sz);
200}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
JP Abgralla032ded2012-06-06 11:53:33 -0700202int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
203{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400205 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800206 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700207 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800208 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700209 (info->dev_vendor != 0x0fce) && // Sony Ericsson
210 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400211 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800212 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800213 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800214 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800215 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400216 (info->dev_vendor != 0x0bb4)) // HTC
217 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 if(info->ifc_class != 0xff) return -1;
219 if(info->ifc_subclass != 0x42) return -1;
220 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700221 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700223 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
224 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 return 0;
226}
227
JP Abgrall7b8970c2013-03-07 17:06:41 -0800228int match_fastboot(usb_ifc_info *info)
229{
230 return match_fastboot_with_serial(info, serial);
231}
232
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233int list_devices_callback(usb_ifc_info *info)
234{
JP Abgralla032ded2012-06-06 11:53:33 -0700235 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700237 if (!info->writable) {
238 serial = "no permissions"; // like "adb devices"
239 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 if (!serial[0]) {
241 serial = "????????????";
242 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700243 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700244 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700245 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700246 } else if (!info->device_path) {
247 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700248 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700249 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700250 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 }
252
253 return -1;
254}
255
256usb_handle *open_device(void)
257{
258 static usb_handle *usb = 0;
259 int announce = 1;
260
261 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800262
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 for(;;) {
264 usb = usb_open(match_fastboot);
265 if(usb) return usb;
266 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800267 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 fprintf(stderr,"< waiting for device >\n");
269 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700270 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271 }
272}
273
274void list_devices(void) {
275 // We don't actually open a USB device here,
276 // just getting our callback called so we can
277 // list all the connected devices.
278 usb_open(list_devices_callback);
279}
280
281void usage(void)
282{
283 fprintf(stderr,
284/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
285 "usage: fastboot [ <option> ] <command>\n"
286 "\n"
287 "commands:\n"
288 " update <filename> reflash device from update.zip\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700289 " flashall flash boot, system, and if found,\n"
290 " recovery, tos\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " flash <partition> [ <filename> ] write a file to a flash partition\n"
292 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700293 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
294 " Can override the fs type and/or\n"
295 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 " getvar <variable> display a bootloader variable\n"
297 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
298 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
299 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700300 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 " reboot reboot device normally\n"
302 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800303 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 "\n"
305 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700306 " -w erase userdata and cache (and format\n"
307 " if supported by partition type)\n"
308 " -u do not first erase partition before\n"
309 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700310 " -s <specific device> specify device serial number\n"
311 " or path to device port\n"
312 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 " -p <product> specify product name\n"
314 " -c <cmdline> override kernel commandline\n"
315 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700316 " -b <base_addr> specify a custom kernel base address.\n"
317 " default: 0x10000000\n"
318 " -n <page size> specify the nand page size.\n"
319 " default: 2048\n"
320 " -S <size>[K|M|G] automatically sparse files greater\n"
321 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323}
324
JP Abgrall7b8970c2013-03-07 17:06:41 -0800325void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 unsigned *sz, const char *cmdline)
327{
328 void *kdata = 0, *rdata = 0;
329 unsigned ksize = 0, rsize = 0;
330 void *bdata;
331 unsigned bsize;
332
333 if(kernel == 0) {
334 fprintf(stderr, "no image specified\n");
335 return 0;
336 }
337
338 kdata = load_file(kernel, &ksize);
339 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800340 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 return 0;
342 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800343
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 /* is this actually a boot image? */
345 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
346 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800347
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 if(ramdisk) {
349 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
350 return 0;
351 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800352
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 *sz = ksize;
354 return kdata;
355 }
356
357 if(ramdisk) {
358 rdata = load_file(ramdisk, &rsize);
359 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800360 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 return 0;
362 }
363 }
364
365 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800366 bdata = mkbootimg(kdata, ksize, kernel_offset,
367 rdata, rsize, ramdisk_offset,
368 0, 0, second_offset,
369 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 if(bdata == 0) {
371 fprintf(stderr,"failed to create boot.img\n");
372 return 0;
373 }
374 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
375 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
376 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800377
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 return bdata;
379}
380
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000381void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382{
383 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000384 zipentry_t entry;
385 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800386
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000387 entry = lookup_zipentry(zip, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 if (entry == NULL) {
389 fprintf(stderr, "archive does not contain '%s'\n", name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000390 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 }
392
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000393 *sz = get_zipentry_size(entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000395 datasz = *sz * 1.001;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 data = malloc(datasz);
397
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000398 if(data == 0) {
399 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
400 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 }
402
403 if (decompress_zipentry(entry, data, datasz)) {
404 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
405 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000406 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000408
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 return data;
410}
411
Rom Lemarchand622810c2013-06-28 09:54:59 -0700412static int unzip_to_file(zipfile_t zip, char *name)
413{
414 int fd;
415 char *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000416 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700417
418 fd = fileno(tmpfile());
419 if (fd < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700420 return -1;
421 }
422
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000423 data = unzip_file(zip, name, &sz);
424 if (data == 0) {
425 return -1;
426 }
427
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700428 if (write(fd, data, sz) != (ssize_t)sz) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700429 fd = -1;
430 }
431
432 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000433 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700434 return fd;
435}
436
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437static char *strip(char *s)
438{
439 int n;
440 while(*s && isspace(*s)) s++;
441 n = strlen(s);
442 while(n-- > 0) {
443 if(!isspace(s[n])) break;
444 s[n] = 0;
445 }
446 return s;
447}
448
449#define MAX_OPTIONS 32
450static int setup_requirement_line(char *name)
451{
452 char *val[MAX_OPTIONS];
453 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700454 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 unsigned n, count;
456 char *x;
457 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800458
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 if (!strncmp(name, "reject ", 7)) {
460 name += 7;
461 invert = 1;
462 } else if (!strncmp(name, "require ", 8)) {
463 name += 8;
464 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700465 } else if (!strncmp(name, "require-for-product:", 20)) {
466 // Get the product and point name past it
467 prod = name + 20;
468 name = strchr(name, ' ');
469 if (!name) return -1;
470 *name = 0;
471 name += 1;
472 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 }
474
475 x = strchr(name, '=');
476 if (x == 0) return 0;
477 *x = 0;
478 val[0] = x + 1;
479
480 for(count = 1; count < MAX_OPTIONS; count++) {
481 x = strchr(val[count - 1],'|');
482 if (x == 0) break;
483 *x = 0;
484 val[count] = x + 1;
485 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 name = strip(name);
488 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800489
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 name = strip(name);
491 if (name == 0) return -1;
492
493 /* work around an unfortunate name mismatch */
494 if (!strcmp(name,"board")) name = "product";
495
496 out = malloc(sizeof(char*) * count);
497 if (out == 0) return -1;
498
499 for(n = 0; n < count; n++) {
500 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700501 if (out[n] == 0) {
502 for(size_t i = 0; i < n; ++i) {
503 free((char*) out[i]);
504 }
505 free(out);
506 return -1;
507 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 }
509
Wink Savilleb98762f2011-04-04 17:54:59 -0700510 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 return 0;
512}
513
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000514static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000516 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000518 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 while (sz-- > 0) {
520 if(*s == '\n') {
521 *s++ = 0;
522 if (setup_requirement_line(data)) {
523 die("out of memory");
524 }
525 data = s;
526 } else {
527 s++;
528 }
529 }
530}
531
532void queue_info_dump(void)
533{
534 fb_queue_notice("--------------------------------------------");
535 fb_queue_display("version-bootloader", "Bootloader Version...");
536 fb_queue_display("version-baseband", "Baseband Version.....");
537 fb_queue_display("serialno", "Serial Number........");
538 fb_queue_notice("--------------------------------------------");
539}
540
Rom Lemarchand622810c2013-06-28 09:54:59 -0700541static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700542{
Colin Crossf8387882012-05-24 17:18:41 -0700543 struct sparse_file *s;
544 int files;
545 struct sparse_file **out_s;
546
Colin Crossf8387882012-05-24 17:18:41 -0700547 s = sparse_file_import_auto(fd, false);
548 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700549 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700550 }
551
552 files = sparse_file_resparse(s, max_size, NULL, 0);
553 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700554 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700555 }
556
557 out_s = calloc(sizeof(struct sparse_file *), files + 1);
558 if (!out_s) {
559 die("Failed to allocate sparse file array\n");
560 }
561
562 files = sparse_file_resparse(s, max_size, out_s, files);
563 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700564 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700565 }
566
567 return out_s;
568}
569
570static int64_t get_target_sparse_limit(struct usb_handle *usb)
571{
572 int64_t limit = 0;
573 char response[FB_RESPONSE_SZ + 1];
574 int status = fb_getvar(usb, response, "max-download-size");
575
576 if (!status) {
577 limit = strtoul(response, NULL, 0);
578 if (limit > 0) {
579 fprintf(stderr, "target reported max download size of %lld bytes\n",
580 limit);
581 }
582 }
583
584 return limit;
585}
586
587static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
588{
589 int64_t limit;
590
591 if (sparse_limit == 0) {
592 return 0;
593 } else if (sparse_limit > 0) {
594 limit = sparse_limit;
595 } else {
596 if (target_sparse_limit == -1) {
597 target_sparse_limit = get_target_sparse_limit(usb);
598 }
599 if (target_sparse_limit > 0) {
600 limit = target_sparse_limit;
601 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700602 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700603 }
604 }
605
606 if (size > limit) {
607 return limit;
608 }
609
610 return 0;
611}
612
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700613/* Until we get lazy inode table init working in make_ext4fs, we need to
614 * erase partitions of type ext4 before flashing a filesystem so no stale
615 * inodes are left lying around. Otherwise, e2fsck gets very upset.
616 */
617static int needs_erase(const char *part)
618{
619 /* The function fb_format_supported() currently returns the value
620 * we want, so just call it.
621 */
JP Abgrall7e859742014-05-06 15:14:15 -0700622 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700623}
624
Rom Lemarchand622810c2013-06-28 09:54:59 -0700625static int load_buf_fd(usb_handle *usb, int fd,
626 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700627{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700628 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000629 void *data;
630 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700631
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000632
633 sz64 = file_size(fd);
634 if (sz64 < 0) {
635 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700636 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700637
638 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700639 limit = get_sparse_limit(usb, sz64);
640 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700642 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700644 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645 buf->type = FB_BUFFER_SPARSE;
646 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700647 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000648 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700649 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000650 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700651 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700652 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000653 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700654 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700655
656 return 0;
657}
658
659static int load_buf(usb_handle *usb, const char *fname,
660 struct fastboot_buffer *buf)
661{
662 int fd;
663
664 fd = open(fname, O_RDONLY | O_BINARY);
665 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700666 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700667 }
668
669 return load_buf_fd(usb, fd, buf);
670}
671
672static void flash_buf(const char *pname, struct fastboot_buffer *buf)
673{
674 struct sparse_file **s;
675
676 switch (buf->type) {
677 case FB_BUFFER_SPARSE:
678 s = buf->data;
679 while (*s) {
680 int64_t sz64 = sparse_file_len(*s, true, false);
681 fb_queue_flash_sparse(pname, *s++, sz64);
682 }
683 break;
684 case FB_BUFFER:
685 fb_queue_flash(pname, buf->data, buf->sz);
686 break;
687 default:
688 die("unknown buffer type: %d", buf->type);
689 }
690}
691
692void do_flash(usb_handle *usb, const char *pname, const char *fname)
693{
694 struct fastboot_buffer buf;
695
696 if (load_buf(usb, fname, &buf)) {
697 die("cannot load '%s'", fname);
698 }
699 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700700}
701
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702void do_update_signature(zipfile_t zip, char *fn)
703{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000704 void *data;
705 unsigned sz;
706 data = unzip_file(zip, fn, &sz);
707 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 fb_queue_download("signature", data, sz);
709 fb_queue_command("signature", "installing signature");
710}
711
Rom Lemarchand622810c2013-06-28 09:54:59 -0700712void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713{
714 void *zdata;
715 unsigned zsize;
716 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000717 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700719 int fd;
720 int rc;
721 struct fastboot_buffer buf;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700722 size_t i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723
724 queue_info_dump();
725
Wink Savilleb98762f2011-04-04 17:54:59 -0700726 fb_queue_query_save("product", cur_product, sizeof(cur_product));
727
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800728 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800729 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730
731 zip = init_zipfile(zdata, zsize);
732 if(zip == 0) die("failed to access zipdata in '%s'");
733
734 data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000735 if (data == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 char *tmp;
737 /* fallback for older zipfiles */
738 data = unzip_file(zip, "android-product.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000739 if ((data == 0) || (sz < 1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 die("update package has no android-info.txt or android-product.txt");
741 }
742 tmp = malloc(sz + 128);
743 if (tmp == 0) die("out of memory");
744 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
745 data = tmp;
746 sz = strlen(tmp);
747 }
748
749 setup_requirements(data, sz);
750
Rom Lemarchand622810c2013-06-28 09:54:59 -0700751 for (i = 0; i < ARRAY_SIZE(images); i++) {
752 fd = unzip_to_file(zip, images[i].img_name);
753 if (fd < 0) {
754 if (images[i].is_optional)
755 continue;
756 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700757 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700758 rc = load_buf_fd(usb, fd, &buf);
759 if (rc) die("cannot load %s from flash", images[i].img_name);
760 do_update_signature(zip, images[i].sig_name);
761 if (erase_first && needs_erase(images[i].part_name)) {
762 fb_queue_erase(images[i].part_name);
763 }
764 flash_buf(images[i].part_name, &buf);
765 /* not closing the fd here since the sparse code keeps the fd around
766 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
767 * program exits.
768 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800769 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770}
771
772void do_send_signature(char *fn)
773{
774 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000775 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800777
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 xtn = strrchr(fn, '.');
779 if (!xtn) return;
780 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800781
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 strcpy(xtn,".sig");
783 data = load_file(fn, &sz);
784 strcpy(xtn,".img");
785 if (data == 0) return;
786 fb_queue_download("signature", data, sz);
787 fb_queue_command("signature", "installing signature");
788}
789
Rom Lemarchand622810c2013-06-28 09:54:59 -0700790void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800791{
792 char *fname;
793 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000794 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700795 struct fastboot_buffer buf;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700796 size_t i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797
798 queue_info_dump();
799
Wink Savilleb98762f2011-04-04 17:54:59 -0700800 fb_queue_query_save("product", cur_product, sizeof(cur_product));
801
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802 fname = find_item("info", product);
803 if (fname == 0) die("cannot find android-info.txt");
804 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800805 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 setup_requirements(data, sz);
807
Rom Lemarchand622810c2013-06-28 09:54:59 -0700808 for (i = 0; i < ARRAY_SIZE(images); i++) {
809 fname = find_item(images[i].part_name, product);
810 if (load_buf(usb, fname, &buf)) {
811 if (images[i].is_optional)
812 continue;
813 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700814 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700815 do_send_signature(fname);
816 if (erase_first && needs_erase(images[i].part_name)) {
817 fb_queue_erase(images[i].part_name);
818 }
819 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800820 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821}
822
823#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800824#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825
826int do_oem_command(int argc, char **argv)
827{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 char command[256];
829 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800830
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800831 command[0] = 0;
832 while(1) {
833 strcat(command,*argv);
834 skip(1);
835 if(argc == 0) break;
836 strcat(command," ");
837 }
838
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800839 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800840 return 0;
841}
842
Colin Crossf8387882012-05-24 17:18:41 -0700843static int64_t parse_num(const char *arg)
844{
845 char *endptr;
846 unsigned long long num;
847
848 num = strtoull(arg, &endptr, 0);
849 if (endptr == arg) {
850 return -1;
851 }
852
853 if (*endptr == 'k' || *endptr == 'K') {
854 if (num >= (-1ULL) / 1024) {
855 return -1;
856 }
857 num *= 1024LL;
858 endptr++;
859 } else if (*endptr == 'm' || *endptr == 'M') {
860 if (num >= (-1ULL) / (1024 * 1024)) {
861 return -1;
862 }
863 num *= 1024LL * 1024LL;
864 endptr++;
865 } else if (*endptr == 'g' || *endptr == 'G') {
866 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
867 return -1;
868 }
869 num *= 1024LL * 1024LL * 1024LL;
870 endptr++;
871 }
872
873 if (*endptr != '\0') {
874 return -1;
875 }
876
877 if (num > INT64_MAX) {
878 return -1;
879 }
880
881 return num;
882}
883
JP Abgrall7e859742014-05-06 15:14:15 -0700884void fb_perform_format(const char *partition, int skip_if_not_supported,
885 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700886{
JP Abgrall7e859742014-05-06 15:14:15 -0700887 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
888 char *pType = pTypeBuff;
889 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700890 unsigned int limit = INT_MAX;
891 struct fastboot_buffer buf;
892 const char *errMsg = NULL;
893 const struct fs_generator *gen;
894 uint64_t pSz;
895 int status;
896 int fd;
897
898 if (target_sparse_limit > 0 && target_sparse_limit < limit)
899 limit = target_sparse_limit;
900 if (sparse_limit > 0 && sparse_limit < limit)
901 limit = sparse_limit;
902
903 status = fb_getvar(usb, pType, "partition-type:%s", partition);
904 if (status) {
905 errMsg = "Can't determine partition type.\n";
906 goto failed;
907 }
JP Abgrall7e859742014-05-06 15:14:15 -0700908 if (type_override) {
909 if (strcmp(type_override, pType)) {
910 fprintf(stderr,
911 "Warning: %s type is %s, but %s was requested for formating.\n",
912 partition, pType, type_override);
913 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700914 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700915 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700916
917 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
918 if (status) {
919 errMsg = "Unable to get partition size\n";
920 goto failed;
921 }
JP Abgrall7e859742014-05-06 15:14:15 -0700922 if (size_override) {
923 if (strcmp(size_override, pSize)) {
924 fprintf(stderr,
925 "Warning: %s size is %s, but %s was requested for formating.\n",
926 partition, pSize, size_override);
927 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700928 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700929 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700930
931 gen = fs_get_generator(pType);
932 if (!gen) {
933 if (skip_if_not_supported) {
934 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
935 fprintf(stderr, "File system type %s not supported.\n", pType);
936 return;
937 }
938 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
939 return;
940 }
941
942 pSz = strtoll(pSize, (char **)NULL, 16);
943
944 fd = fileno(tmpfile());
945 if (fs_generator_generate(gen, fd, pSz)) {
946 close(fd);
947 fprintf(stderr, "Cannot generate image.\n");
948 return;
949 }
950
951 if (load_buf_fd(usb, fd, &buf)) {
952 fprintf(stderr, "Cannot read image.\n");
953 close(fd);
954 return;
955 }
956 flash_buf(partition, &buf);
957
958 return;
959
960
961failed:
962 if (skip_if_not_supported) {
963 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
964 if (errMsg)
965 fprintf(stderr, "%s", errMsg);
966 }
967 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
968}
969
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800970int main(int argc, char **argv)
971{
972 int wants_wipe = 0;
973 int wants_reboot = 0;
974 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700975 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800976 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000977 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700978 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700979 int c;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980
JP Abgrall7b8970c2013-03-07 17:06:41 -0800981 const struct option longopts[] = {
982 {"base", required_argument, 0, 'b'},
983 {"kernel_offset", required_argument, 0, 'k'},
984 {"page_size", required_argument, 0, 'n'},
985 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800986 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800987 {"help", 0, 0, 'h'},
988 {0, 0, 0, 0}
989 };
Colin Cross8879f982012-05-22 17:53:34 -0700990
991 serial = getenv("ANDROID_SERIAL");
992
993 while (1) {
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800994 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 -0700995 if (c < 0) {
996 break;
997 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800998 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700999 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001000 case 'b':
1001 base_addr = strtoul(optarg, 0, 16);
1002 break;
Colin Cross8879f982012-05-22 17:53:34 -07001003 case 'c':
1004 cmdline = optarg;
1005 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001006 case 'h':
1007 usage();
1008 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001009 case 'i': {
1010 char *endptr = NULL;
1011 unsigned long val;
1012
1013 val = strtoul(optarg, &endptr, 0);
1014 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1015 die("invalid vendor id '%s'", optarg);
1016 vendor_id = (unsigned short)val;
1017 break;
1018 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001019 case 'k':
1020 kernel_offset = strtoul(optarg, 0, 16);
1021 break;
1022 case 'l':
1023 long_listing = 1;
1024 break;
1025 case 'n':
1026 page_size = (unsigned)strtoul(optarg, NULL, 0);
1027 if (!page_size) die("invalid page size");
1028 break;
1029 case 'p':
1030 product = optarg;
1031 break;
1032 case 'r':
1033 ramdisk_offset = strtoul(optarg, 0, 16);
1034 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001035 case 't':
1036 tags_offset = strtoul(optarg, 0, 16);
1037 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001038 case 's':
1039 serial = optarg;
1040 break;
1041 case 'S':
1042 sparse_limit = parse_num(optarg);
1043 if (sparse_limit < 0) {
1044 die("invalid sparse limit");
1045 }
1046 break;
1047 case 'u':
1048 erase_first = 0;
1049 break;
1050 case 'w':
1051 wants_wipe = 1;
1052 break;
Colin Cross8879f982012-05-22 17:53:34 -07001053 case '?':
1054 return 1;
1055 default:
1056 abort();
1057 }
1058 }
1059
1060 argc -= optind;
1061 argv += optind;
1062
1063 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001065 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001066 }
1067
Colin Cross8fb6e062012-07-24 16:36:41 -07001068 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001069 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001070 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001071 return 0;
1072 }
1073
Colin Crossc7b75dc2012-08-29 18:17:06 -07001074 if (argc > 0 && !strcmp(*argv, "help")) {
1075 usage();
1076 return 0;
1077 }
1078
Colin Cross8879f982012-05-22 17:53:34 -07001079 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001080
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001082 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083 require(2);
1084 fb_queue_display(argv[1], argv[1]);
1085 skip(2);
1086 } else if(!strcmp(*argv, "erase")) {
1087 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001088
JP Abgrall7e859742014-05-06 15:14:15 -07001089 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001090 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1091 }
1092
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001093 fb_queue_erase(argv[1]);
1094 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001095 } else if(!strncmp(*argv, "format", strlen("format"))) {
1096 char *overrides;
1097 char *type_override = NULL;
1098 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001099 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001100 /*
1101 * Parsing for: "format[:[type][:[size]]]"
1102 * Some valid things:
1103 * - select ontly the size, and leave default fs type:
1104 * format::0x4000000 userdata
1105 * - default fs type and size:
1106 * format userdata
1107 * format:: userdata
1108 */
1109 overrides = strchr(*argv, ':');
1110 if (overrides) {
1111 overrides++;
1112 size_override = strchr(overrides, ':');
1113 if (size_override) {
1114 size_override[0] = '\0';
1115 size_override++;
1116 }
1117 type_override = overrides;
1118 }
1119 if (type_override && !type_override[0]) type_override = NULL;
1120 if (size_override && !size_override[0]) size_override = NULL;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001121 if (erase_first && needs_erase(argv[1])) {
1122 fb_queue_erase(argv[1]);
1123 }
JP Abgrall7e859742014-05-06 15:14:15 -07001124 fb_perform_format(argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001125 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001126 } else if(!strcmp(*argv, "signature")) {
1127 require(2);
1128 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001129 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 if (sz != 256) die("signature must be 256 bytes");
1131 fb_queue_download("signature", data, sz);
1132 fb_queue_command("signature", "installing signature");
1133 skip(2);
1134 } else if(!strcmp(*argv, "reboot")) {
1135 wants_reboot = 1;
1136 skip(1);
1137 } else if(!strcmp(*argv, "reboot-bootloader")) {
1138 wants_reboot_bootloader = 1;
1139 skip(1);
1140 } else if (!strcmp(*argv, "continue")) {
1141 fb_queue_command("continue", "resuming boot");
1142 skip(1);
1143 } else if(!strcmp(*argv, "boot")) {
1144 char *kname = 0;
1145 char *rname = 0;
1146 skip(1);
1147 if (argc > 0) {
1148 kname = argv[0];
1149 skip(1);
1150 }
1151 if (argc > 0) {
1152 rname = argv[0];
1153 skip(1);
1154 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001155 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001156 if (data == 0) return 1;
1157 fb_queue_download("boot.img", data, sz);
1158 fb_queue_command("boot", "booting");
1159 } else if(!strcmp(*argv, "flash")) {
1160 char *pname = argv[1];
1161 char *fname = 0;
1162 require(2);
1163 if (argc > 2) {
1164 fname = argv[2];
1165 skip(3);
1166 } else {
1167 fname = find_item(pname, product);
1168 skip(2);
1169 }
1170 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001171 if (erase_first && needs_erase(pname)) {
1172 fb_queue_erase(pname);
1173 }
Colin Crossf8387882012-05-24 17:18:41 -07001174 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001175 } else if(!strcmp(*argv, "flash:raw")) {
1176 char *pname = argv[1];
1177 char *kname = argv[2];
1178 char *rname = 0;
1179 require(3);
1180 if(argc > 3) {
1181 rname = argv[3];
1182 skip(4);
1183 } else {
1184 skip(3);
1185 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001186 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187 if (data == 0) die("cannot load bootable image");
1188 fb_queue_flash(pname, data, sz);
1189 } else if(!strcmp(*argv, "flashall")) {
1190 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001191 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 wants_reboot = 1;
1193 } else if(!strcmp(*argv, "update")) {
1194 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001195 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196 skip(2);
1197 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001198 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001199 skip(1);
1200 }
1201 wants_reboot = 1;
1202 } else if(!strcmp(*argv, "oem")) {
1203 argc = do_oem_command(argc, argv);
1204 } else {
1205 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001206 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207 }
1208 }
1209
1210 if (wants_wipe) {
1211 fb_queue_erase("userdata");
JP Abgrall7e859742014-05-06 15:14:15 -07001212 fb_perform_format("userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213 fb_queue_erase("cache");
JP Abgrall7e859742014-05-06 15:14:15 -07001214 fb_perform_format("cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215 }
1216 if (wants_reboot) {
1217 fb_queue_reboot();
1218 } else if (wants_reboot_bootloader) {
1219 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001220 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 }
1222
Scott Anderson13081c62012-04-06 12:39:30 -07001223 if (fb_queue_is_empty())
1224 return 0;
1225
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001226 status = fb_execute_queue(usb);
1227 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228}