blob: 04f3166f85e6bf8a3da7595bc6be7626781e8ea8 [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
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
Elliott Hughes253c18d2015-03-18 22:47:09 -070050#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062static usb_handle *usb = 0;
63static const char *serial = 0;
64static const char *product = 0;
65static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070067static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070068static int64_t sparse_limit = -1;
69static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
JP Abgrall7b8970c2013-03-07 17:06:41 -080071unsigned page_size = 2048;
72unsigned base_addr = 0x10000000;
73unsigned kernel_offset = 0x00008000;
74unsigned ramdisk_offset = 0x01000000;
75unsigned second_offset = 0x00f00000;
76unsigned tags_offset = 0x00000100;
77
Rom Lemarchand622810c2013-06-28 09:54:59 -070078enum fb_buffer_type {
79 FB_BUFFER,
80 FB_BUFFER_SPARSE,
81};
82
83struct fastboot_buffer {
84 enum fb_buffer_type type;
85 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000086 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070087};
88
89static struct {
90 char img_name[13];
91 char sig_name[13];
92 char part_name[9];
93 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070094} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070095 {"boot.img", "boot.sig", "boot", false},
96 {"recovery.img", "recovery.sig", "recovery", true},
97 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070098 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070099};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700100
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101char *find_item(const char *item, const char *product)
102{
103 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700104 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 char path[PATH_MAX + 128];
106
107 if(!strcmp(item,"boot")) {
108 fn = "boot.img";
109 } else if(!strcmp(item,"recovery")) {
110 fn = "recovery.img";
111 } else if(!strcmp(item,"system")) {
112 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700113 } else if(!strcmp(item,"vendor")) {
114 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 } else if(!strcmp(item,"userdata")) {
116 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700117 } else if(!strcmp(item,"cache")) {
118 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 } else if(!strcmp(item,"info")) {
120 fn = "android-info.txt";
121 } else {
122 fprintf(stderr,"unknown partition '%s'\n", item);
123 return 0;
124 }
125
126 if(product) {
127 get_my_path(path);
128 sprintf(path + strlen(path),
129 "../../../target/product/%s/%s", product, fn);
130 return strdup(path);
131 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800132
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 dir = getenv("ANDROID_PRODUCT_OUT");
134 if((dir == 0) || (dir[0] == 0)) {
135 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
136 return 0;
137 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800138
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 sprintf(path, "%s/%s", dir, fn);
140 return strdup(path);
141}
142
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000143static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700144{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700145 struct stat st;
146 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700147
Rom Lemarchand622810c2013-06-28 09:54:59 -0700148 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700149
Rom Lemarchand622810c2013-06-28 09:54:59 -0700150 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700151}
152
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000153static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000155 char *data;
156 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800157 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000159 data = 0;
160
161 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700162 if (sz < 0) {
163 goto oops;
164 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
166 data = (char*) malloc(sz);
167 if(data == 0) goto oops;
168
169 if(read(fd, data, sz) != sz) goto oops;
170 close(fd);
171
172 if(_sz) *_sz = sz;
173 return data;
174
175oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800176 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 close(fd);
178 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return 0;
181}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000183static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700184{
185 int fd;
186
187 fd = open(fn, O_RDONLY | O_BINARY);
188 if(fd < 0) return 0;
189
190 return load_fd(fd, _sz);
191}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
JP Abgralla032ded2012-06-06 11:53:33 -0700193int match_fastboot_with_serial(usb_ifc_info *info, const char *local_serial)
194{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 if(!(vendor_id && (info->dev_vendor == vendor_id)) &&
Mike Lockwood09070d92009-08-05 17:04:36 -0400196 (info->dev_vendor != 0x18d1) && // Google
Wu, Haof60e8632012-01-17 12:04:11 -0800197 (info->dev_vendor != 0x8087) && // Intel
The Android Open Source Projectf614d642009-03-18 17:39:49 -0700198 (info->dev_vendor != 0x0451) &&
Robert CH Choue25ff1c2009-09-21 09:51:35 +0800199 (info->dev_vendor != 0x0502) &&
Dima Zavin509f7392010-05-14 14:48:30 -0700200 (info->dev_vendor != 0x0fce) && // Sony Ericsson
201 (info->dev_vendor != 0x05c6) && // Qualcomm
Mike Lockwood09070d92009-08-05 17:04:36 -0400202 (info->dev_vendor != 0x22b8) && // Motorola
Erik Gilling37e9e902010-01-20 17:40:05 -0800203 (info->dev_vendor != 0x0955) && // Nvidia
Xavier Ducrohetaf82f212010-01-21 17:39:25 -0800204 (info->dev_vendor != 0x413c) && // DELL
Xavier Ducrohet746f3242012-01-13 16:03:37 -0800205 (info->dev_vendor != 0x2314) && // INQ Mobile
Ramanan Rajeswaran73c019b2012-02-24 13:00:34 -0800206 (info->dev_vendor != 0x0b05) && // Asus
Mike Lockwood09070d92009-08-05 17:04:36 -0400207 (info->dev_vendor != 0x0bb4)) // HTC
208 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 if(info->ifc_class != 0xff) return -1;
210 if(info->ifc_subclass != 0x42) return -1;
211 if(info->ifc_protocol != 0x03) return -1;
Scott Anderson13081c62012-04-06 12:39:30 -0700212 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700214 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
215 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 return 0;
217}
218
JP Abgrall7b8970c2013-03-07 17:06:41 -0800219int match_fastboot(usb_ifc_info *info)
220{
221 return match_fastboot_with_serial(info, serial);
222}
223
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224int list_devices_callback(usb_ifc_info *info)
225{
JP Abgralla032ded2012-06-06 11:53:33 -0700226 if (match_fastboot_with_serial(info, NULL) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700227 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700228 if (!info->writable) {
229 serial = "no permissions"; // like "adb devices"
230 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if (!serial[0]) {
232 serial = "????????????";
233 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700234 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700235 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700236 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800237 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700238 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700239 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700240 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
244 return -1;
245}
246
247usb_handle *open_device(void)
248{
249 static usb_handle *usb = 0;
250 int announce = 1;
251
252 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 for(;;) {
255 usb = usb_open(match_fastboot);
256 if(usb) return usb;
257 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800258 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 fprintf(stderr,"< waiting for device >\n");
260 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700261 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263}
264
265void list_devices(void) {
266 // We don't actually open a USB device here,
267 // just getting our callback called so we can
268 // list all the connected devices.
269 usb_open(list_devices_callback);
270}
271
272void usage(void)
273{
274 fprintf(stderr,
275/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
276 "usage: fastboot [ <option> ] <command>\n"
277 "\n"
278 "commands:\n"
279 " update <filename> reflash device from update.zip\n"
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700280 " flashall flash boot, system, vendor and if found,\n"
Daniel Rosenberg015d73f2014-09-15 13:44:07 -0700281 " recovery\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 " flash <partition> [ <filename> ] write a file to a flash partition\n"
283 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700284 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
285 " Can override the fs type and/or\n"
286 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 " getvar <variable> display a bootloader variable\n"
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200288 " boot <kernel> [ <ramdisk> [ <second> ] ] download and boot kernel\n"
289 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ] create bootimage and \n"
290 " flash it\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700292 " continue continue with autoboot\n"
Elliott Hughesca85df02015-02-25 10:02:00 -0800293 " reboot [bootloader] reboot device, optionally into bootloader\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800295 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 "\n"
297 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700298 " -w erase userdata and cache (and format\n"
299 " if supported by partition type)\n"
300 " -u do not first erase partition before\n"
301 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700302 " -s <specific device> specify device serial number\n"
303 " or path to device port\n"
304 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 " -p <product> specify product name\n"
306 " -c <cmdline> override kernel commandline\n"
307 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700308 " -b <base_addr> specify a custom kernel base address.\n"
309 " default: 0x10000000\n"
310 " -n <page size> specify the nand page size.\n"
311 " default: 2048\n"
312 " -S <size>[K|M|G] automatically sparse files greater\n"
313 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315}
316
JP Abgrall7b8970c2013-03-07 17:06:41 -0800317void *load_bootable_image(const char *kernel, const char *ramdisk,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200318 const char *secondstage, unsigned *sz,
319 const char *cmdline)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320{
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200321 void *kdata = 0, *rdata = 0, *sdata = 0;
322 unsigned ksize = 0, rsize = 0, ssize = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 void *bdata;
324 unsigned bsize;
325
326 if(kernel == 0) {
327 fprintf(stderr, "no image specified\n");
328 return 0;
329 }
330
331 kdata = load_file(kernel, &ksize);
332 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800333 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 return 0;
335 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800336
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 /* is this actually a boot image? */
338 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
339 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 if(ramdisk) {
342 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
343 return 0;
344 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800345
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 *sz = ksize;
347 return kdata;
348 }
349
350 if(ramdisk) {
351 rdata = load_file(ramdisk, &rsize);
352 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800353 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 return 0;
355 }
356 }
357
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200358 if (secondstage) {
359 sdata = load_file(secondstage, &ssize);
360 if(sdata == 0) {
361 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
362 return 0;
363 }
364 }
365
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800367 bdata = mkbootimg(kdata, ksize, kernel_offset,
368 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200369 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800370 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 if(bdata == 0) {
372 fprintf(stderr,"failed to create boot.img\n");
373 return 0;
374 }
375 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
376 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
377 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800378
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 return bdata;
380}
381
Elliott Hughes253c18d2015-03-18 22:47:09 -0700382static void *unzip_file(zipfile_t zip, const char *name, unsigned *sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383{
384 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000385 zipentry_t entry;
386 unsigned datasz;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800387
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000388 entry = lookup_zipentry(zip, name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 if (entry == NULL) {
390 fprintf(stderr, "archive does not contain '%s'\n", name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000391 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 }
393
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000394 *sz = get_zipentry_size(entry);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000396 datasz = *sz * 1.001;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 data = malloc(datasz);
398
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 if(data == 0) {
400 fprintf(stderr, "failed to allocate %d bytes\n", *sz);
401 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 }
403
404 if (decompress_zipentry(entry, data, datasz)) {
405 fprintf(stderr, "failed to unzip '%s' from archive\n", name);
406 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000407 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000409
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 return data;
411}
412
Rom Lemarchand622810c2013-06-28 09:54:59 -0700413static int unzip_to_file(zipfile_t zip, char *name)
414{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700415 int fd = fileno(tmpfile());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700416 if (fd < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700417 return -1;
418 }
419
Elliott Hughes253c18d2015-03-18 22:47:09 -0700420 unsigned sz;
421 void* data = unzip_file(zip, name, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000422 if (data == 0) {
423 return -1;
424 }
425
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700426 if (write(fd, data, sz) != (ssize_t)sz) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700427 fd = -1;
428 }
429
430 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000431 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700432 return fd;
433}
434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435static char *strip(char *s)
436{
437 int n;
438 while(*s && isspace(*s)) s++;
439 n = strlen(s);
440 while(n-- > 0) {
441 if(!isspace(s[n])) break;
442 s[n] = 0;
443 }
444 return s;
445}
446
447#define MAX_OPTIONS 32
448static int setup_requirement_line(char *name)
449{
450 char *val[MAX_OPTIONS];
Wink Savilleb98762f2011-04-04 17:54:59 -0700451 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 unsigned n, count;
453 char *x;
454 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 if (!strncmp(name, "reject ", 7)) {
457 name += 7;
458 invert = 1;
459 } else if (!strncmp(name, "require ", 8)) {
460 name += 8;
461 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700462 } else if (!strncmp(name, "require-for-product:", 20)) {
463 // Get the product and point name past it
464 prod = name + 20;
465 name = strchr(name, ' ');
466 if (!name) return -1;
467 *name = 0;
468 name += 1;
469 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 }
471
472 x = strchr(name, '=');
473 if (x == 0) return 0;
474 *x = 0;
475 val[0] = x + 1;
476
477 for(count = 1; count < MAX_OPTIONS; count++) {
478 x = strchr(val[count - 1],'|');
479 if (x == 0) break;
480 *x = 0;
481 val[count] = x + 1;
482 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800483
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 name = strip(name);
485 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 name = strip(name);
488 if (name == 0) return -1;
489
Elliott Hughes253c18d2015-03-18 22:47:09 -0700490 const char* var = name;
491 // Work around an unfortunate name mismatch.
492 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493
Elliott Hughes253c18d2015-03-18 22:47:09 -0700494 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 if (out == 0) return -1;
496
497 for(n = 0; n < count; n++) {
498 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700499 if (out[n] == 0) {
500 for(size_t i = 0; i < n; ++i) {
501 free((char*) out[i]);
502 }
503 free(out);
504 return -1;
505 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 }
507
Elliott Hughes253c18d2015-03-18 22:47:09 -0700508 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 return 0;
510}
511
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000512static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000514 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000516 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 while (sz-- > 0) {
518 if(*s == '\n') {
519 *s++ = 0;
520 if (setup_requirement_line(data)) {
521 die("out of memory");
522 }
523 data = s;
524 } else {
525 s++;
526 }
527 }
528}
529
530void queue_info_dump(void)
531{
532 fb_queue_notice("--------------------------------------------");
533 fb_queue_display("version-bootloader", "Bootloader Version...");
534 fb_queue_display("version-baseband", "Baseband Version.....");
535 fb_queue_display("serialno", "Serial Number........");
536 fb_queue_notice("--------------------------------------------");
537}
538
Rom Lemarchand622810c2013-06-28 09:54:59 -0700539static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700540{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700541 struct sparse_file* s = sparse_file_import_auto(fd, false);
Colin Crossf8387882012-05-24 17:18:41 -0700542 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700543 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700544 }
545
Elliott Hughes253c18d2015-03-18 22:47:09 -0700546 int files = sparse_file_resparse(s, max_size, NULL, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700547 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700548 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700549 }
550
Elliott Hughes253c18d2015-03-18 22:47:09 -0700551 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700552 if (!out_s) {
553 die("Failed to allocate sparse file array\n");
554 }
555
556 files = sparse_file_resparse(s, max_size, out_s, files);
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 return out_s;
562}
563
564static int64_t get_target_sparse_limit(struct usb_handle *usb)
565{
566 int64_t limit = 0;
567 char response[FB_RESPONSE_SZ + 1];
568 int status = fb_getvar(usb, response, "max-download-size");
569
570 if (!status) {
571 limit = strtoul(response, NULL, 0);
572 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700573 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700574 limit);
575 }
576 }
577
578 return limit;
579}
580
581static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
582{
583 int64_t limit;
584
585 if (sparse_limit == 0) {
586 return 0;
587 } else if (sparse_limit > 0) {
588 limit = sparse_limit;
589 } else {
590 if (target_sparse_limit == -1) {
591 target_sparse_limit = get_target_sparse_limit(usb);
592 }
593 if (target_sparse_limit > 0) {
594 limit = target_sparse_limit;
595 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700596 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700597 }
598 }
599
600 if (size > limit) {
601 return limit;
602 }
603
604 return 0;
605}
606
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700607/* Until we get lazy inode table init working in make_ext4fs, we need to
608 * erase partitions of type ext4 before flashing a filesystem so no stale
609 * inodes are left lying around. Otherwise, e2fsck gets very upset.
610 */
611static int needs_erase(const char *part)
612{
613 /* The function fb_format_supported() currently returns the value
614 * we want, so just call it.
615 */
JP Abgrall7e859742014-05-06 15:14:15 -0700616 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700617}
618
Rom Lemarchand622810c2013-06-28 09:54:59 -0700619static int load_buf_fd(usb_handle *usb, int fd,
620 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700621{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700622 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000623 void *data;
624 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700625
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000626
627 sz64 = file_size(fd);
628 if (sz64 < 0) {
629 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700630 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700631
632 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700633 limit = get_sparse_limit(usb, sz64);
634 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700635 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700636 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700638 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639 buf->type = FB_BUFFER_SPARSE;
640 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700641 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000642 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700643 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000644 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700646 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000647 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700648 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700649
650 return 0;
651}
652
653static int load_buf(usb_handle *usb, const char *fname,
654 struct fastboot_buffer *buf)
655{
656 int fd;
657
658 fd = open(fname, O_RDONLY | O_BINARY);
659 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700660 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700661 }
662
663 return load_buf_fd(usb, fd, buf);
664}
665
666static void flash_buf(const char *pname, struct fastboot_buffer *buf)
667{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700668 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700669
670 switch (buf->type) {
671 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700673 while (*s) {
674 int64_t sz64 = sparse_file_len(*s, true, false);
675 fb_queue_flash_sparse(pname, *s++, sz64);
676 }
677 break;
678 case FB_BUFFER:
679 fb_queue_flash(pname, buf->data, buf->sz);
680 break;
681 default:
682 die("unknown buffer type: %d", buf->type);
683 }
684}
685
686void do_flash(usb_handle *usb, const char *pname, const char *fname)
687{
688 struct fastboot_buffer buf;
689
690 if (load_buf(usb, fname, &buf)) {
691 die("cannot load '%s'", fname);
692 }
693 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700694}
695
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696void do_update_signature(zipfile_t zip, char *fn)
697{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000698 void *data;
699 unsigned sz;
700 data = unzip_file(zip, fn, &sz);
701 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 fb_queue_download("signature", data, sz);
703 fb_queue_command("signature", "installing signature");
704}
705
Elliott Hughes253c18d2015-03-18 22:47:09 -0700706void do_update(usb_handle *usb, const char *filename, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708 queue_info_dump();
709
Wink Savilleb98762f2011-04-04 17:54:59 -0700710 fb_queue_query_save("product", cur_product, sizeof(cur_product));
711
Elliott Hughes253c18d2015-03-18 22:47:09 -0700712 unsigned zsize;
713 void* zdata = load_file(filename, &zsize);
714 if (zdata == 0) die("failed to load '%s': %s", filename, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715
Elliott Hughes253c18d2015-03-18 22:47:09 -0700716 zipfile_t zip = init_zipfile(zdata, zsize);
717 if (zip == 0) die("failed to access zipdata in '%s'");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718
Elliott Hughes253c18d2015-03-18 22:47:09 -0700719 unsigned sz;
720 void* data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000721 if (data == 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 /* fallback for older zipfiles */
723 data = unzip_file(zip, "android-product.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000724 if ((data == 0) || (sz < 1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800725 die("update package has no android-info.txt or android-product.txt");
726 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700727 data = mkmsg("board=%sversion-baseband=0.66.04.19\n", reinterpret_cast<char*>(data));
728 sz = strlen(reinterpret_cast<char*>(data));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 }
730
Elliott Hughes253c18d2015-03-18 22:47:09 -0700731 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732
Elliott Hughes253c18d2015-03-18 22:47:09 -0700733 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
734 int fd = unzip_to_file(zip, images[i].img_name);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700735 if (fd < 0) {
736 if (images[i].is_optional)
737 continue;
738 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700739 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700740 fastboot_buffer buf;
741 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742 if (rc) die("cannot load %s from flash", images[i].img_name);
743 do_update_signature(zip, images[i].sig_name);
744 if (erase_first && needs_erase(images[i].part_name)) {
745 fb_queue_erase(images[i].part_name);
746 }
747 flash_buf(images[i].part_name, &buf);
748 /* not closing the fd here since the sparse code keeps the fd around
749 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
750 * program exits.
751 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800752 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753}
754
755void do_send_signature(char *fn)
756{
757 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000758 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800760
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800761 xtn = strrchr(fn, '.');
762 if (!xtn) return;
763 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800764
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 strcpy(xtn,".sig");
766 data = load_file(fn, &sz);
767 strcpy(xtn,".img");
768 if (data == 0) return;
769 fb_queue_download("signature", data, sz);
770 fb_queue_command("signature", "installing signature");
771}
772
Rom Lemarchand622810c2013-06-28 09:54:59 -0700773void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800774{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775 queue_info_dump();
776
Wink Savilleb98762f2011-04-04 17:54:59 -0700777 fb_queue_query_save("product", cur_product, sizeof(cur_product));
778
Elliott Hughes253c18d2015-03-18 22:47:09 -0700779 char* fname = find_item("info", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 if (fname == 0) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781
Elliott Hughes253c18d2015-03-18 22:47:09 -0700782 unsigned sz;
783 void* data = load_file(fname, &sz);
784 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
785
786 setup_requirements(reinterpret_cast<char*>(data), sz);
787
788 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700789 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700790 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 if (load_buf(usb, fname, &buf)) {
792 if (images[i].is_optional)
793 continue;
794 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700795 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 do_send_signature(fname);
797 if (erase_first && needs_erase(images[i].part_name)) {
798 fb_queue_erase(images[i].part_name);
799 }
800 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800801 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802}
803
804#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800805#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806
807int do_oem_command(int argc, char **argv)
808{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 char command[256];
810 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800811
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800812 command[0] = 0;
813 while(1) {
814 strcat(command,*argv);
815 skip(1);
816 if(argc == 0) break;
817 strcat(command," ");
818 }
819
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800820 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800821 return 0;
822}
823
Colin Crossf8387882012-05-24 17:18:41 -0700824static int64_t parse_num(const char *arg)
825{
826 char *endptr;
827 unsigned long long num;
828
829 num = strtoull(arg, &endptr, 0);
830 if (endptr == arg) {
831 return -1;
832 }
833
834 if (*endptr == 'k' || *endptr == 'K') {
835 if (num >= (-1ULL) / 1024) {
836 return -1;
837 }
838 num *= 1024LL;
839 endptr++;
840 } else if (*endptr == 'm' || *endptr == 'M') {
841 if (num >= (-1ULL) / (1024 * 1024)) {
842 return -1;
843 }
844 num *= 1024LL * 1024LL;
845 endptr++;
846 } else if (*endptr == 'g' || *endptr == 'G') {
847 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
848 return -1;
849 }
850 num *= 1024LL * 1024LL * 1024LL;
851 endptr++;
852 }
853
854 if (*endptr != '\0') {
855 return -1;
856 }
857
858 if (num > INT64_MAX) {
859 return -1;
860 }
861
862 return num;
863}
864
JP Abgrall7e859742014-05-06 15:14:15 -0700865void fb_perform_format(const char *partition, int skip_if_not_supported,
866 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700867{
JP Abgrall7e859742014-05-06 15:14:15 -0700868 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
869 char *pType = pTypeBuff;
870 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700871 unsigned int limit = INT_MAX;
872 struct fastboot_buffer buf;
873 const char *errMsg = NULL;
874 const struct fs_generator *gen;
875 uint64_t pSz;
876 int status;
877 int fd;
878
879 if (target_sparse_limit > 0 && target_sparse_limit < limit)
880 limit = target_sparse_limit;
881 if (sparse_limit > 0 && sparse_limit < limit)
882 limit = sparse_limit;
883
884 status = fb_getvar(usb, pType, "partition-type:%s", partition);
885 if (status) {
886 errMsg = "Can't determine partition type.\n";
887 goto failed;
888 }
JP Abgrall7e859742014-05-06 15:14:15 -0700889 if (type_override) {
890 if (strcmp(type_override, pType)) {
891 fprintf(stderr,
892 "Warning: %s type is %s, but %s was requested for formating.\n",
893 partition, pType, type_override);
894 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700895 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700896 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700897
898 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
899 if (status) {
900 errMsg = "Unable to get partition size\n";
901 goto failed;
902 }
JP Abgrall7e859742014-05-06 15:14:15 -0700903 if (size_override) {
904 if (strcmp(size_override, pSize)) {
905 fprintf(stderr,
906 "Warning: %s size is %s, but %s was requested for formating.\n",
907 partition, pSize, size_override);
908 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700909 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700910 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700911
912 gen = fs_get_generator(pType);
913 if (!gen) {
914 if (skip_if_not_supported) {
915 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
916 fprintf(stderr, "File system type %s not supported.\n", pType);
917 return;
918 }
919 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
920 return;
921 }
922
923 pSz = strtoll(pSize, (char **)NULL, 16);
924
925 fd = fileno(tmpfile());
926 if (fs_generator_generate(gen, fd, pSz)) {
927 close(fd);
928 fprintf(stderr, "Cannot generate image.\n");
929 return;
930 }
931
932 if (load_buf_fd(usb, fd, &buf)) {
933 fprintf(stderr, "Cannot read image.\n");
934 close(fd);
935 return;
936 }
937 flash_buf(partition, &buf);
938
939 return;
940
941
942failed:
943 if (skip_if_not_supported) {
944 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
945 if (errMsg)
946 fprintf(stderr, "%s", errMsg);
947 }
948 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
949}
950
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951int main(int argc, char **argv)
952{
953 int wants_wipe = 0;
954 int wants_reboot = 0;
955 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700956 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000958 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700959 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700960 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100961 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962
JP Abgrall7b8970c2013-03-07 17:06:41 -0800963 const struct option longopts[] = {
964 {"base", required_argument, 0, 'b'},
965 {"kernel_offset", required_argument, 0, 'k'},
966 {"page_size", required_argument, 0, 'n'},
967 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800968 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800969 {"help", 0, 0, 'h'},
Florian Bäuerle27ded482014-11-24 11:29:34 +0100970 {"unbuffered", 0, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800971 {0, 0, 0, 0}
972 };
Colin Cross8879f982012-05-22 17:53:34 -0700973
974 serial = getenv("ANDROID_SERIAL");
975
976 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +0100977 c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:h", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -0700978 if (c < 0) {
979 break;
980 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800981 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700982 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700983 case 'b':
984 base_addr = strtoul(optarg, 0, 16);
985 break;
Colin Cross8879f982012-05-22 17:53:34 -0700986 case 'c':
987 cmdline = optarg;
988 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800989 case 'h':
990 usage();
991 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700992 case 'i': {
993 char *endptr = NULL;
994 unsigned long val;
995
996 val = strtoul(optarg, &endptr, 0);
997 if (!endptr || *endptr != '\0' || (val & ~0xffff))
998 die("invalid vendor id '%s'", optarg);
999 vendor_id = (unsigned short)val;
1000 break;
1001 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001002 case 'k':
1003 kernel_offset = strtoul(optarg, 0, 16);
1004 break;
1005 case 'l':
1006 long_listing = 1;
1007 break;
1008 case 'n':
1009 page_size = (unsigned)strtoul(optarg, NULL, 0);
1010 if (!page_size) die("invalid page size");
1011 break;
1012 case 'p':
1013 product = optarg;
1014 break;
1015 case 'r':
1016 ramdisk_offset = strtoul(optarg, 0, 16);
1017 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001018 case 't':
1019 tags_offset = strtoul(optarg, 0, 16);
1020 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001021 case 's':
1022 serial = optarg;
1023 break;
1024 case 'S':
1025 sparse_limit = parse_num(optarg);
1026 if (sparse_limit < 0) {
1027 die("invalid sparse limit");
1028 }
1029 break;
1030 case 'u':
1031 erase_first = 0;
1032 break;
1033 case 'w':
1034 wants_wipe = 1;
1035 break;
Colin Cross8879f982012-05-22 17:53:34 -07001036 case '?':
1037 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001038 case 0:
1039 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
1040 setvbuf(stdout, NULL, _IONBF, 0);
1041 setvbuf(stderr, NULL, _IONBF, 0);
1042 }
1043 break;
Colin Cross8879f982012-05-22 17:53:34 -07001044 default:
1045 abort();
1046 }
1047 }
1048
1049 argc -= optind;
1050 argv += optind;
1051
1052 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001054 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001055 }
1056
Colin Cross8fb6e062012-07-24 16:36:41 -07001057 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001058 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001059 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001060 return 0;
1061 }
1062
Colin Crossc7b75dc2012-08-29 18:17:06 -07001063 if (argc > 0 && !strcmp(*argv, "help")) {
1064 usage();
1065 return 0;
1066 }
1067
Colin Cross8879f982012-05-22 17:53:34 -07001068 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001069
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001070 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001071 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001072 require(2);
1073 fb_queue_display(argv[1], argv[1]);
1074 skip(2);
1075 } else if(!strcmp(*argv, "erase")) {
1076 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001077
JP Abgrall7e859742014-05-06 15:14:15 -07001078 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001079 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1080 }
1081
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001082 fb_queue_erase(argv[1]);
1083 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001084 } else if(!strncmp(*argv, "format", strlen("format"))) {
1085 char *overrides;
1086 char *type_override = NULL;
1087 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001088 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001089 /*
1090 * Parsing for: "format[:[type][:[size]]]"
1091 * Some valid things:
1092 * - select ontly the size, and leave default fs type:
1093 * format::0x4000000 userdata
1094 * - default fs type and size:
1095 * format userdata
1096 * format:: userdata
1097 */
1098 overrides = strchr(*argv, ':');
1099 if (overrides) {
1100 overrides++;
1101 size_override = strchr(overrides, ':');
1102 if (size_override) {
1103 size_override[0] = '\0';
1104 size_override++;
1105 }
1106 type_override = overrides;
1107 }
1108 if (type_override && !type_override[0]) type_override = NULL;
1109 if (size_override && !size_override[0]) size_override = NULL;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001110 if (erase_first && needs_erase(argv[1])) {
1111 fb_queue_erase(argv[1]);
1112 }
JP Abgrall7e859742014-05-06 15:14:15 -07001113 fb_perform_format(argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001114 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001115 } else if(!strcmp(*argv, "signature")) {
1116 require(2);
1117 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001118 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001119 if (sz != 256) die("signature must be 256 bytes");
1120 fb_queue_download("signature", data, sz);
1121 fb_queue_command("signature", "installing signature");
1122 skip(2);
1123 } else if(!strcmp(*argv, "reboot")) {
1124 wants_reboot = 1;
1125 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001126 if (argc > 0) {
1127 if (!strcmp(*argv, "bootloader")) {
1128 wants_reboot = 0;
1129 wants_reboot_bootloader = 1;
1130 skip(1);
1131 }
1132 }
1133 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134 } else if(!strcmp(*argv, "reboot-bootloader")) {
1135 wants_reboot_bootloader = 1;
1136 skip(1);
1137 } else if (!strcmp(*argv, "continue")) {
1138 fb_queue_command("continue", "resuming boot");
1139 skip(1);
1140 } else if(!strcmp(*argv, "boot")) {
1141 char *kname = 0;
1142 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001143 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144 skip(1);
1145 if (argc > 0) {
1146 kname = argv[0];
1147 skip(1);
1148 }
1149 if (argc > 0) {
1150 rname = argv[0];
1151 skip(1);
1152 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001153 if (argc > 0) {
1154 sname = argv[0];
1155 skip(1);
1156 }
1157 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158 if (data == 0) return 1;
1159 fb_queue_download("boot.img", data, sz);
1160 fb_queue_command("boot", "booting");
1161 } else if(!strcmp(*argv, "flash")) {
1162 char *pname = argv[1];
1163 char *fname = 0;
1164 require(2);
1165 if (argc > 2) {
1166 fname = argv[2];
1167 skip(3);
1168 } else {
1169 fname = find_item(pname, product);
1170 skip(2);
1171 }
1172 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001173 if (erase_first && needs_erase(pname)) {
1174 fb_queue_erase(pname);
1175 }
Colin Crossf8387882012-05-24 17:18:41 -07001176 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001177 } else if(!strcmp(*argv, "flash:raw")) {
1178 char *pname = argv[1];
1179 char *kname = argv[2];
1180 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001181 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001183 skip(3);
1184 if (argc > 0) {
1185 rname = argv[0];
1186 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001188 if (argc > 0) {
1189 sname = argv[0];
1190 skip(1);
1191 }
1192 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 if (data == 0) die("cannot load bootable image");
1194 fb_queue_flash(pname, data, sz);
1195 } else if(!strcmp(*argv, "flashall")) {
1196 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001197 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198 wants_reboot = 1;
1199 } else if(!strcmp(*argv, "update")) {
1200 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001201 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 skip(2);
1203 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001204 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205 skip(1);
1206 }
1207 wants_reboot = 1;
1208 } else if(!strcmp(*argv, "oem")) {
1209 argc = do_oem_command(argc, argv);
1210 } else {
1211 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001212 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213 }
1214 }
1215
1216 if (wants_wipe) {
1217 fb_queue_erase("userdata");
JP Abgrall7e859742014-05-06 15:14:15 -07001218 fb_perform_format("userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 fb_queue_erase("cache");
JP Abgrall7e859742014-05-06 15:14:15 -07001220 fb_perform_format("cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 }
1222 if (wants_reboot) {
1223 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001224 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001225 } else if (wants_reboot_bootloader) {
1226 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001227 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228 }
1229
Scott Anderson13081c62012-04-06 12:39:30 -07001230 if (fb_queue_is_empty())
1231 return 0;
1232
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001233 status = fb_execute_queue(usb);
1234 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235}