blob: 64a52b5713d0b2a3ce93fd644b74ef985cc4404e [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 Rosenbergf530c932014-05-28 14:10:01 -0700102} images[] = {
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 Rosenbergf530c932014-05-28 14:10:01 -0700106 {"vendor.img", "vendor.sig", "vendor", true},
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700107 {"tos.img", "tos.sig", "tos", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700109
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110void get_my_path(char *path);
111
112char *find_item(const char *item, const char *product)
113{
114 char *dir;
115 char *fn;
116 char path[PATH_MAX + 128];
117
118 if(!strcmp(item,"boot")) {
119 fn = "boot.img";
120 } else if(!strcmp(item,"recovery")) {
121 fn = "recovery.img";
122 } else if(!strcmp(item,"system")) {
123 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700124 } else if(!strcmp(item,"vendor")) {
125 fn = "vendor.img";
Daniel Rosenberg73a4ad22014-04-29 13:54:19 -0700126 } else if(!strcmp(item,"tos")) {
127 fn = "tos.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128 } else if(!strcmp(item,"userdata")) {
129 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700130 } else if(!strcmp(item,"cache")) {
131 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 } else if(!strcmp(item,"info")) {
133 fn = "android-info.txt";
134 } else {
135 fprintf(stderr,"unknown partition '%s'\n", item);
136 return 0;
137 }
138
139 if(product) {
140 get_my_path(path);
141 sprintf(path + strlen(path),
142 "../../../target/product/%s/%s", product, fn);
143 return strdup(path);
144 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 dir = getenv("ANDROID_PRODUCT_OUT");
147 if((dir == 0) || (dir[0] == 0)) {
148 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
149 return 0;
150 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800151
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 sprintf(path, "%s/%s", dir, fn);
153 return strdup(path);
154}
155
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000156static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700157{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700158 struct stat st;
159 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700160
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700162
Rom Lemarchand622810c2013-06-28 09:54:59 -0700163 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700164}
165
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000166static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000168 char *data;
169 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800170 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000172 data = 0;
173
174 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700175 if (sz < 0) {
176 goto oops;
177 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
179 data = (char*) malloc(sz);
180 if(data == 0) goto oops;
181
182 if(read(fd, data, sz) != sz) goto oops;
183 close(fd);
184
185 if(_sz) *_sz = sz;
186 return data;
187
188oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800189 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 close(fd);
191 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800192 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 return 0;
194}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000196static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700197{
198 int fd;
199
200 fd = open(fn, O_RDONLY | O_BINARY);
201 if(fd < 0) return 0;
202
203 return load_fd(fd, _sz);
204}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205
JP Abgralla032ded2012-06-06 11:53:33 -0700206int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
207{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400209 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800210 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700211 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800212 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700213 (info->dev_vendor != 0x0fce) && // Sony Ericsson
214 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400215 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800216 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800217 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800218 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800219 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400220 (info->dev_vendor != 0x0bb4)) // HTC
221 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if(info->ifc_class != 0xff) return -1;
223 if(info->ifc_subclass != 0x42) return -1;
224 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700225 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700227 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
228 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 return 0;
230}
231
JP Abgrall7b8970c2013-03-07 17:06:41 -0800232int match_fastboot(usb_ifc_info *info)
233{
234 return match_fastboot_with_serial(info, serial);
235}
236
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237int list_devices_callback(usb_ifc_info *info)
238{
JP Abgralla032ded2012-06-06 11:53:33 -0700239 if (match_fastboot_with_serial(info, NULL) == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700241 if (!info->writable) {
242 serial = "no permissions"; // like "adb devices"
243 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 if (!serial[0]) {
245 serial = "????????????";
246 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700247 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700248 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700249 printf("%s\tfastboot\n", serial);
Scott Anderson866b1bd2012-06-04 20:29:11 -0700250 } else if (!info->device_path) {
251 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700252 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700253 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700254 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 }
256
257 return -1;
258}
259
260usb_handle *open_device(void)
261{
262 static usb_handle *usb = 0;
263 int announce = 1;
264
265 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800266
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 for(;;) {
268 usb = usb_open(match_fastboot);
269 if(usb) return usb;
270 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800271 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 fprintf(stderr,"< waiting for device >\n");
273 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700274 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275 }
276}
277
278void list_devices(void) {
279 // We don't actually open a USB device here,
280 // just getting our callback called so we can
281 // list all the connected devices.
282 usb_open(list_devices_callback);
283}
284
285void usage(void)
286{
287 fprintf(stderr,
288/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
289 "usage: fastboot [ <option> ] <command>\n"
290 "\n"
291 "commands:\n"
292 " update <filename> reflash device from update.zip\n"
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700293 " flashall flash boot, system, vendor and if found,\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700294 " recovery, tos\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 " flash <partition> [ <filename> ] write a file to a flash partition\n"
296 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700297 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
298 " Can override the fs type and/or\n"
299 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 " getvar <variable> display a bootloader variable\n"
301 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
302 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
303 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700304 " continue continue with autoboot\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 " reboot reboot device normally\n"
306 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800307 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 "\n"
309 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700310 " -w erase userdata and cache (and format\n"
311 " if supported by partition type)\n"
312 " -u do not first erase partition before\n"
313 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700314 " -s <specific device> specify device serial number\n"
315 " or path to device port\n"
316 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317 " -p <product> specify product name\n"
318 " -c <cmdline> override kernel commandline\n"
319 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700320 " -b <base_addr> specify a custom kernel base address.\n"
321 " default: 0x10000000\n"
322 " -n <page size> specify the nand page size.\n"
323 " default: 2048\n"
324 " -S <size>[K|M|G] automatically sparse files greater\n"
325 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327}
328
JP Abgrall7b8970c2013-03-07 17:06:41 -0800329void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 unsigned *sz, const char *cmdline)
331{
332 void *kdata = 0, *rdata = 0;
333 unsigned ksize = 0, rsize = 0;
334 void *bdata;
335 unsigned bsize;
336
337 if(kernel == 0) {
338 fprintf(stderr, "no image specified\n");
339 return 0;
340 }
341
342 kdata = load_file(kernel, &ksize);
343 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800344 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 return 0;
346 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800347
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348 /* is this actually a boot image? */
349 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
350 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800351
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 if(ramdisk) {
353 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
354 return 0;
355 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800356
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 *sz = ksize;
358 return kdata;
359 }
360
361 if(ramdisk) {
362 rdata = load_file(ramdisk, &rsize);
363 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800364 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 return 0;
366 }
367 }
368
369 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800370 bdata = mkbootimg(kdata, ksize, kernel_offset,
371 rdata, rsize, ramdisk_offset,
372 0, 0, second_offset,
373 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 if(bdata == 0) {
375 fprintf(stderr,"failed to create boot.img\n");
376 return 0;
377 }
378 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
379 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
380 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800381
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 return bdata;
383}
384
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000385void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386{
387 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000388 zipentry_t entry;
389 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800390
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000391 entry = lookup_zipentry(zip, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 if (entry == NULL) {
393 fprintf(stderr, "archive does not contain '%s'\n", name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000394 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 }
396
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000397 *sz = get_zipentry_size(entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 datasz = *sz * 1.001;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 data = malloc(datasz);
401
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000402 if(data == 0) {
403 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 }
406
407 if (decompress_zipentry(entry, data, datasz)) {
408 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
409 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000410 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000412
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 return data;
414}
415
Rom Lemarchand622810c2013-06-28 09:54:59 -0700416static int unzip_to_file(zipfile_t zip, char *name)
417{
418 int fd;
419 char *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000420 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700421
422 fd = fileno(tmpfile());
423 if (fd < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700424 return -1;
425 }
426
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000427 data = unzip_file(zip, name, &sz);
428 if (data == 0) {
429 return -1;
430 }
431
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700432 if (write(fd, data, sz) != (ssize_t)sz) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700433 fd = -1;
434 }
435
436 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000437 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700438 return fd;
439}
440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441static char *strip(char *s)
442{
443 int n;
444 while(*s && isspace(*s)) s++;
445 n = strlen(s);
446 while(n-- > 0) {
447 if(!isspace(s[n])) break;
448 s[n] = 0;
449 }
450 return s;
451}
452
453#define MAX_OPTIONS 32
454static int setup_requirement_line(char *name)
455{
456 char *val[MAX_OPTIONS];
457 const char **out;
Wink Savilleb98762f2011-04-04 17:54:59 -0700458 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 unsigned n, count;
460 char *x;
461 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 if (!strncmp(name, "reject ", 7)) {
464 name += 7;
465 invert = 1;
466 } else if (!strncmp(name, "require ", 8)) {
467 name += 8;
468 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700469 } else if (!strncmp(name, "require-for-product:", 20)) {
470 // Get the product and point name past it
471 prod = name + 20;
472 name = strchr(name, ' ');
473 if (!name) return -1;
474 *name = 0;
475 name += 1;
476 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 }
478
479 x = strchr(name, '=');
480 if (x == 0) return 0;
481 *x = 0;
482 val[0] = x + 1;
483
484 for(count = 1; count < MAX_OPTIONS; count++) {
485 x = strchr(val[count - 1],'|');
486 if (x == 0) break;
487 *x = 0;
488 val[count] = x + 1;
489 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 name = strip(name);
492 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800493
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 name = strip(name);
495 if (name == 0) return -1;
496
497 /* work around an unfortunate name mismatch */
498 if (!strcmp(name,"board")) name = "product";
499
500 out = malloc(sizeof(char*) * count);
501 if (out == 0) return -1;
502
503 for(n = 0; n < count; n++) {
504 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700505 if (out[n] == 0) {
506 for(size_t i = 0; i < n; ++i) {
507 free((char*) out[i]);
508 }
509 free(out);
510 return -1;
511 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 }
513
Wink Savilleb98762f2011-04-04 17:54:59 -0700514 fb_queue_require(prod, name, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 return 0;
516}
517
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000518static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000520 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000522 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800523 while (sz-- > 0) {
524 if(*s == '\n') {
525 *s++ = 0;
526 if (setup_requirement_line(data)) {
527 die("out of memory");
528 }
529 data = s;
530 } else {
531 s++;
532 }
533 }
534}
535
536void queue_info_dump(void)
537{
538 fb_queue_notice("--------------------------------------------");
539 fb_queue_display("version-bootloader", "Bootloader Version...");
540 fb_queue_display("version-baseband", "Baseband Version.....");
541 fb_queue_display("serialno", "Serial Number........");
542 fb_queue_notice("--------------------------------------------");
543}
544
Rom Lemarchand622810c2013-06-28 09:54:59 -0700545static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700546{
Colin Crossf8387882012-05-24 17:18:41 -0700547 struct sparse_file *s;
548 int files;
549 struct sparse_file **out_s;
550
Colin Crossf8387882012-05-24 17:18:41 -0700551 s = sparse_file_import_auto(fd, false);
552 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700553 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700554 }
555
556 files = sparse_file_resparse(s, max_size, NULL, 0);
557 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700558 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700559 }
560
561 out_s = calloc(sizeof(struct sparse_file *), files + 1);
562 if (!out_s) {
563 die("Failed to allocate sparse file array\n");
564 }
565
566 files = sparse_file_resparse(s, max_size, out_s, files);
567 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700568 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700569 }
570
571 return out_s;
572}
573
574static int64_t get_target_sparse_limit(struct usb_handle *usb)
575{
576 int64_t limit = 0;
577 char response[FB_RESPONSE_SZ + 1];
578 int status = fb_getvar(usb, response, "max-download-size");
579
580 if (!status) {
581 limit = strtoul(response, NULL, 0);
582 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700583 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700584 limit);
585 }
586 }
587
588 return limit;
589}
590
591static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
592{
593 int64_t limit;
594
595 if (sparse_limit == 0) {
596 return 0;
597 } else if (sparse_limit > 0) {
598 limit = sparse_limit;
599 } else {
600 if (target_sparse_limit == -1) {
601 target_sparse_limit = get_target_sparse_limit(usb);
602 }
603 if (target_sparse_limit > 0) {
604 limit = target_sparse_limit;
605 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700606 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700607 }
608 }
609
610 if (size > limit) {
611 return limit;
612 }
613
614 return 0;
615}
616
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700617/* Until we get lazy inode table init working in make_ext4fs, we need to
618 * erase partitions of type ext4 before flashing a filesystem so no stale
619 * inodes are left lying around. Otherwise, e2fsck gets very upset.
620 */
621static int needs_erase(const char *part)
622{
623 /* The function fb_format_supported() currently returns the value
624 * we want, so just call it.
625 */
JP Abgrall7e859742014-05-06 15:14:15 -0700626 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700627}
628
Rom Lemarchand622810c2013-06-28 09:54:59 -0700629static int load_buf_fd(usb_handle *usb, int fd,
630 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700631{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700632 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000633 void *data;
634 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700635
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000636
637 sz64 = file_size(fd);
638 if (sz64 < 0) {
639 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700640 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700641
642 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700643 limit = get_sparse_limit(usb, sz64);
644 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700646 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700647 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700648 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700649 buf->type = FB_BUFFER_SPARSE;
650 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700651 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000652 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700653 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000654 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700655 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700656 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000657 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700658 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700659
660 return 0;
661}
662
663static int load_buf(usb_handle *usb, const char *fname,
664 struct fastboot_buffer *buf)
665{
666 int fd;
667
668 fd = open(fname, O_RDONLY | O_BINARY);
669 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700670 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700671 }
672
673 return load_buf_fd(usb, fd, buf);
674}
675
676static void flash_buf(const char *pname, struct fastboot_buffer *buf)
677{
678 struct sparse_file **s;
679
680 switch (buf->type) {
681 case FB_BUFFER_SPARSE:
682 s = buf->data;
683 while (*s) {
684 int64_t sz64 = sparse_file_len(*s, true, false);
685 fb_queue_flash_sparse(pname, *s++, sz64);
686 }
687 break;
688 case FB_BUFFER:
689 fb_queue_flash(pname, buf->data, buf->sz);
690 break;
691 default:
692 die("unknown buffer type: %d", buf->type);
693 }
694}
695
696void do_flash(usb_handle *usb, const char *pname, const char *fname)
697{
698 struct fastboot_buffer buf;
699
700 if (load_buf(usb, fname, &buf)) {
701 die("cannot load '%s'", fname);
702 }
703 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700704}
705
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706void do_update_signature(zipfile_t zip, char *fn)
707{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000708 void *data;
709 unsigned sz;
710 data = unzip_file(zip, fn, &sz);
711 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 fb_queue_download("signature", data, sz);
713 fb_queue_command("signature", "installing signature");
714}
715
Rom Lemarchand622810c2013-06-28 09:54:59 -0700716void do_update(usb_handle *usb, char *fn, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717{
718 void *zdata;
719 unsigned zsize;
720 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000721 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 zipfile_t zip;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700723 int fd;
724 int rc;
725 struct fastboot_buffer buf;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700726 size_t i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727
728 queue_info_dump();
729
Wink Savilleb98762f2011-04-04 17:54:59 -0700730 fb_queue_query_save("product", cur_product, sizeof(cur_product));
731
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732 zdata = load_file(fn, &zsize);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800733 if (zdata == 0) die("failed to load '%s': %s", fn, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734
735 zip = init_zipfile(zdata, zsize);
736 if(zip == 0) die("failed to access zipdata in '%s'");
737
738 data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000739 if (data == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 char *tmp;
741 /* fallback for older zipfiles */
742 data = unzip_file(zip, "android-product.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000743 if ((data == 0) || (sz < 1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 die("update package has no android-info.txt or android-product.txt");
745 }
746 tmp = malloc(sz + 128);
747 if (tmp == 0) die("out of memory");
748 sprintf(tmp,"board=%sversion-baseband=0.66.04.19\n",(char*)data);
749 data = tmp;
750 sz = strlen(tmp);
751 }
752
753 setup_requirements(data, sz);
754
Rom Lemarchand622810c2013-06-28 09:54:59 -0700755 for (i = 0; i < ARRAY_SIZE(images); i++) {
756 fd = unzip_to_file(zip, images[i].img_name);
757 if (fd < 0) {
758 if (images[i].is_optional)
759 continue;
760 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700761 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700762 rc = load_buf_fd(usb, fd, &buf);
763 if (rc) die("cannot load %s from flash", images[i].img_name);
764 do_update_signature(zip, images[i].sig_name);
765 if (erase_first && needs_erase(images[i].part_name)) {
766 fb_queue_erase(images[i].part_name);
767 }
768 flash_buf(images[i].part_name, &buf);
769 /* not closing the fd here since the sparse code keeps the fd around
770 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
771 * program exits.
772 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774}
775
776void do_send_signature(char *fn)
777{
778 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000779 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800781
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 xtn = strrchr(fn, '.');
783 if (!xtn) return;
784 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800785
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786 strcpy(xtn,".sig");
787 data = load_file(fn, &sz);
788 strcpy(xtn,".img");
789 if (data == 0) return;
790 fb_queue_download("signature", data, sz);
791 fb_queue_command("signature", "installing signature");
792}
793
Rom Lemarchand622810c2013-06-28 09:54:59 -0700794void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795{
796 char *fname;
797 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000798 unsigned sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700799 struct fastboot_buffer buf;
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700800 size_t i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801
802 queue_info_dump();
803
Wink Savilleb98762f2011-04-04 17:54:59 -0700804 fb_queue_query_save("product", cur_product, sizeof(cur_product));
805
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 fname = find_item("info", product);
807 if (fname == 0) die("cannot find android-info.txt");
808 data = load_file(fname, &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800809 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800810 setup_requirements(data, sz);
811
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812 for (i = 0; i < ARRAY_SIZE(images); i++) {
813 fname = find_item(images[i].part_name, product);
814 if (load_buf(usb, fname, &buf)) {
815 if (images[i].is_optional)
816 continue;
817 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700818 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819 do_send_signature(fname);
820 if (erase_first && needs_erase(images[i].part_name)) {
821 fb_queue_erase(images[i].part_name);
822 }
823 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825}
826
827#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800828#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829
830int do_oem_command(int argc, char **argv)
831{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800832 char command[256];
833 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800834
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800835 command[0] = 0;
836 while(1) {
837 strcat(command,*argv);
838 skip(1);
839 if(argc == 0) break;
840 strcat(command," ");
841 }
842
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800843 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800844 return 0;
845}
846
Colin Crossf8387882012-05-24 17:18:41 -0700847static int64_t parse_num(const char *arg)
848{
849 char *endptr;
850 unsigned long long num;
851
852 num = strtoull(arg, &endptr, 0);
853 if (endptr == arg) {
854 return -1;
855 }
856
857 if (*endptr == 'k' || *endptr == 'K') {
858 if (num >= (-1ULL) / 1024) {
859 return -1;
860 }
861 num *= 1024LL;
862 endptr++;
863 } else if (*endptr == 'm' || *endptr == 'M') {
864 if (num >= (-1ULL) / (1024 * 1024)) {
865 return -1;
866 }
867 num *= 1024LL * 1024LL;
868 endptr++;
869 } else if (*endptr == 'g' || *endptr == 'G') {
870 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
871 return -1;
872 }
873 num *= 1024LL * 1024LL * 1024LL;
874 endptr++;
875 }
876
877 if (*endptr != '\0') {
878 return -1;
879 }
880
881 if (num > INT64_MAX) {
882 return -1;
883 }
884
885 return num;
886}
887
JP Abgrall7e859742014-05-06 15:14:15 -0700888void fb_perform_format(const char *partition, int skip_if_not_supported,
889 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700890{
JP Abgrall7e859742014-05-06 15:14:15 -0700891 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
892 char *pType = pTypeBuff;
893 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700894 unsigned int limit = INT_MAX;
895 struct fastboot_buffer buf;
896 const char *errMsg = NULL;
897 const struct fs_generator *gen;
898 uint64_t pSz;
899 int status;
900 int fd;
901
902 if (target_sparse_limit > 0 && target_sparse_limit < limit)
903 limit = target_sparse_limit;
904 if (sparse_limit > 0 && sparse_limit < limit)
905 limit = sparse_limit;
906
907 status = fb_getvar(usb, pType, "partition-type:%s", partition);
908 if (status) {
909 errMsg = "Can't determine partition type.\n";
910 goto failed;
911 }
JP Abgrall7e859742014-05-06 15:14:15 -0700912 if (type_override) {
913 if (strcmp(type_override, pType)) {
914 fprintf(stderr,
915 "Warning: %s type is %s, but %s was requested for formating.\n",
916 partition, pType, type_override);
917 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700918 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700919 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700920
921 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
922 if (status) {
923 errMsg = "Unable to get partition size\n";
924 goto failed;
925 }
JP Abgrall7e859742014-05-06 15:14:15 -0700926 if (size_override) {
927 if (strcmp(size_override, pSize)) {
928 fprintf(stderr,
929 "Warning: %s size is %s, but %s was requested for formating.\n",
930 partition, pSize, size_override);
931 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700932 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700933 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700934
935 gen = fs_get_generator(pType);
936 if (!gen) {
937 if (skip_if_not_supported) {
938 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
939 fprintf(stderr, "File system type %s not supported.\n", pType);
940 return;
941 }
942 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
943 return;
944 }
945
946 pSz = strtoll(pSize, (char **)NULL, 16);
947
948 fd = fileno(tmpfile());
949 if (fs_generator_generate(gen, fd, pSz)) {
950 close(fd);
951 fprintf(stderr, "Cannot generate image.\n");
952 return;
953 }
954
955 if (load_buf_fd(usb, fd, &buf)) {
956 fprintf(stderr, "Cannot read image.\n");
957 close(fd);
958 return;
959 }
960 flash_buf(partition, &buf);
961
962 return;
963
964
965failed:
966 if (skip_if_not_supported) {
967 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
968 if (errMsg)
969 fprintf(stderr, "%s", errMsg);
970 }
971 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
972}
973
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800974int main(int argc, char **argv)
975{
976 int wants_wipe = 0;
977 int wants_reboot = 0;
978 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700979 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800980 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000981 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700982 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700983 int c;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800984
JP Abgrall7b8970c2013-03-07 17:06:41 -0800985 const struct option longopts[] = {
986 {"base", required_argument, 0, 'b'},
987 {"kernel_offset", required_argument, 0, 'k'},
988 {"page_size", required_argument, 0, 'n'},
989 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800990 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800991 {"help", 0, 0, 'h'},
992 {0, 0, 0, 0}
993 };
Colin Cross8879f982012-05-22 17:53:34 -0700994
995 serial = getenv("ANDROID_SERIAL");
996
997 while (1) {
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800998 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 -0700999 if (c < 0) {
1000 break;
1001 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001002 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001003 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001004 case 'b':
1005 base_addr = strtoul(optarg, 0, 16);
1006 break;
Colin Cross8879f982012-05-22 17:53:34 -07001007 case 'c':
1008 cmdline = optarg;
1009 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001010 case 'h':
1011 usage();
1012 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001013 case 'i': {
1014 char *endptr = NULL;
1015 unsigned long val;
1016
1017 val = strtoul(optarg, &endptr, 0);
1018 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1019 die("invalid vendor id '%s'", optarg);
1020 vendor_id = (unsigned short)val;
1021 break;
1022 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001023 case 'k':
1024 kernel_offset = strtoul(optarg, 0, 16);
1025 break;
1026 case 'l':
1027 long_listing = 1;
1028 break;
1029 case 'n':
1030 page_size = (unsigned)strtoul(optarg, NULL, 0);
1031 if (!page_size) die("invalid page size");
1032 break;
1033 case 'p':
1034 product = optarg;
1035 break;
1036 case 'r':
1037 ramdisk_offset = strtoul(optarg, 0, 16);
1038 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001039 case 't':
1040 tags_offset = strtoul(optarg, 0, 16);
1041 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001042 case 's':
1043 serial = optarg;
1044 break;
1045 case 'S':
1046 sparse_limit = parse_num(optarg);
1047 if (sparse_limit < 0) {
1048 die("invalid sparse limit");
1049 }
1050 break;
1051 case 'u':
1052 erase_first = 0;
1053 break;
1054 case 'w':
1055 wants_wipe = 1;
1056 break;
Colin Cross8879f982012-05-22 17:53:34 -07001057 case '?':
1058 return 1;
1059 default:
1060 abort();
1061 }
1062 }
1063
1064 argc -= optind;
1065 argv += optind;
1066
1067 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001069 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001070 }
1071
Colin Cross8fb6e062012-07-24 16:36:41 -07001072 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001073 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001074 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001075 return 0;
1076 }
1077
Colin Crossc7b75dc2012-08-29 18:17:06 -07001078 if (argc > 0 && !strcmp(*argv, "help")) {
1079 usage();
1080 return 0;
1081 }
1082
Colin Cross8879f982012-05-22 17:53:34 -07001083 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001084
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001085 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001086 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 require(2);
1088 fb_queue_display(argv[1], argv[1]);
1089 skip(2);
1090 } else if(!strcmp(*argv, "erase")) {
1091 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001092
JP Abgrall7e859742014-05-06 15:14:15 -07001093 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001094 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1095 }
1096
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001097 fb_queue_erase(argv[1]);
1098 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001099 } else if(!strncmp(*argv, "format", strlen("format"))) {
1100 char *overrides;
1101 char *type_override = NULL;
1102 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001103 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001104 /*
1105 * Parsing for: "format[:[type][:[size]]]"
1106 * Some valid things:
1107 * - select ontly the size, and leave default fs type:
1108 * format::0x4000000 userdata
1109 * - default fs type and size:
1110 * format userdata
1111 * format:: userdata
1112 */
1113 overrides = strchr(*argv, ':');
1114 if (overrides) {
1115 overrides++;
1116 size_override = strchr(overrides, ':');
1117 if (size_override) {
1118 size_override[0] = '\0';
1119 size_override++;
1120 }
1121 type_override = overrides;
1122 }
1123 if (type_override && !type_override[0]) type_override = NULL;
1124 if (size_override && !size_override[0]) size_override = NULL;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001125 if (erase_first && needs_erase(argv[1])) {
1126 fb_queue_erase(argv[1]);
1127 }
JP Abgrall7e859742014-05-06 15:14:15 -07001128 fb_perform_format(argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001129 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001130 } else if(!strcmp(*argv, "signature")) {
1131 require(2);
1132 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001133 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134 if (sz != 256) die("signature must be 256 bytes");
1135 fb_queue_download("signature", data, sz);
1136 fb_queue_command("signature", "installing signature");
1137 skip(2);
1138 } else if(!strcmp(*argv, "reboot")) {
1139 wants_reboot = 1;
1140 skip(1);
1141 } else if(!strcmp(*argv, "reboot-bootloader")) {
1142 wants_reboot_bootloader = 1;
1143 skip(1);
1144 } else if (!strcmp(*argv, "continue")) {
1145 fb_queue_command("continue", "resuming boot");
1146 skip(1);
1147 } else if(!strcmp(*argv, "boot")) {
1148 char *kname = 0;
1149 char *rname = 0;
1150 skip(1);
1151 if (argc > 0) {
1152 kname = argv[0];
1153 skip(1);
1154 }
1155 if (argc > 0) {
1156 rname = argv[0];
1157 skip(1);
1158 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001159 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001160 if (data == 0) return 1;
1161 fb_queue_download("boot.img", data, sz);
1162 fb_queue_command("boot", "booting");
1163 } else if(!strcmp(*argv, "flash")) {
1164 char *pname = argv[1];
1165 char *fname = 0;
1166 require(2);
1167 if (argc > 2) {
1168 fname = argv[2];
1169 skip(3);
1170 } else {
1171 fname = find_item(pname, product);
1172 skip(2);
1173 }
1174 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001175 if (erase_first && needs_erase(pname)) {
1176 fb_queue_erase(pname);
1177 }
Colin Crossf8387882012-05-24 17:18:41 -07001178 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001179 } else if(!strcmp(*argv, "flash:raw")) {
1180 char *pname = argv[1];
1181 char *kname = argv[2];
1182 char *rname = 0;
1183 require(3);
1184 if(argc > 3) {
1185 rname = argv[3];
1186 skip(4);
1187 } else {
1188 skip(3);
1189 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001190 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191 if (data == 0) die("cannot load bootable image");
1192 fb_queue_flash(pname, data, sz);
1193 } else if(!strcmp(*argv, "flashall")) {
1194 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001195 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001196 wants_reboot = 1;
1197 } else if(!strcmp(*argv, "update")) {
1198 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001199 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001200 skip(2);
1201 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001202 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001203 skip(1);
1204 }
1205 wants_reboot = 1;
1206 } else if(!strcmp(*argv, "oem")) {
1207 argc = do_oem_command(argc, argv);
1208 } else {
1209 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001210 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001211 }
1212 }
1213
1214 if (wants_wipe) {
1215 fb_queue_erase("userdata");
JP Abgrall7e859742014-05-06 15:14:15 -07001216 fb_perform_format("userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001217 fb_queue_erase("cache");
JP Abgrall7e859742014-05-06 15:14:15 -07001218 fb_perform_format("cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 }
1220 if (wants_reboot) {
1221 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001222 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223 } else if (wants_reboot_bootloader) {
1224 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001225 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226 }
1227
Scott Anderson13081c62012-04-06 12:39:30 -07001228 if (fb_queue_is_empty())
1229 return 0;
1230
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001231 status = fb_execute_queue(usb);
1232 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001233}