blob: 7acfd6ee147ad0635e883311a75bb8da6a656356 [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>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070048#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
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"
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -0700283 " flashing lock locks the device. Prevents flashing"
284 " partitions\n"
285 " flashing unlock unlocks the device. Allows user to"
286 " flash any partition except the ones"
287 " that are related to bootloader\n"
288 " flashing lock_critical Prevents flashing bootloader related"
289 " partitions\n"
290 " flashing unlock_critical Enables flashing bootloader related"
291 " partitions\n"
292 " flashing get_unlock_ability Queries bootloader to see if the"
293 " device is unlocked\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800294 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700295 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
296 " Can override the fs type and/or\n"
297 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 " getvar <variable> display a bootloader variable\n"
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200299 " boot <kernel> [ <ramdisk> [ <second> ] ] download and boot kernel\n"
300 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ] create bootimage and \n"
301 " flash it\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700303 " continue continue with autoboot\n"
Elliott Hughesca85df02015-02-25 10:02:00 -0800304 " reboot [bootloader] reboot device, optionally into bootloader\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800306 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 "\n"
308 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700309 " -w erase userdata and cache (and format\n"
310 " if supported by partition type)\n"
311 " -u do not first erase partition before\n"
312 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700313 " -s <specific device> specify device serial number\n"
314 " or path to device port\n"
315 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 " -p <product> specify product name\n"
317 " -c <cmdline> override kernel commandline\n"
318 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700319 " -b <base_addr> specify a custom kernel base address.\n"
320 " default: 0x10000000\n"
321 " -n <page size> specify the nand page size.\n"
322 " default: 2048\n"
323 " -S <size>[K|M|G] automatically sparse files greater\n"
324 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326}
327
JP Abgrall7b8970c2013-03-07 17:06:41 -0800328void *load_bootable_image(const char *kernel, const char *ramdisk,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200329 const char *secondstage, unsigned *sz,
330 const char *cmdline)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331{
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200332 void *kdata = 0, *rdata = 0, *sdata = 0;
333 unsigned ksize = 0, rsize = 0, ssize = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 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
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200369 if (secondstage) {
370 sdata = load_file(secondstage, &ssize);
371 if(sdata == 0) {
372 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
373 return 0;
374 }
375 }
376
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800378 bdata = mkbootimg(kdata, ksize, kernel_offset,
379 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200380 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800381 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 if(bdata == 0) {
383 fprintf(stderr,"failed to create boot.img\n");
384 return 0;
385 }
386 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
387 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
388 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800389
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 return bdata;
391}
392
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700393static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394{
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700395 ZipEntryName zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700396 ZipEntry zip_entry;
397 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700398 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
401
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700402 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700404 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
405 if (data == NULL) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700406 fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000407 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408 }
409
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700410 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
411 if (error != 0) {
412 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000414 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000416
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 return data;
418}
419
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700420static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
421 FILE* fp = tmpfile();
422 if (fp == NULL) {
423 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
424 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700425 return -1;
426 }
427
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700428 ZipEntryName zip_entry_name(entry_name);
429 ZipEntry zip_entry;
430 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
431 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000432 return -1;
433 }
434
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700435 int fd = fileno(fp);
436 int error = ExtractEntryToFile(zip, &zip_entry, fd);
437 if (error != 0) {
438 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
439 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700440 }
441
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000442 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700443 return fd;
444}
445
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446static char *strip(char *s)
447{
448 int n;
449 while(*s && isspace(*s)) s++;
450 n = strlen(s);
451 while(n-- > 0) {
452 if(!isspace(s[n])) break;
453 s[n] = 0;
454 }
455 return s;
456}
457
458#define MAX_OPTIONS 32
459static int setup_requirement_line(char *name)
460{
461 char *val[MAX_OPTIONS];
Wink Savilleb98762f2011-04-04 17:54:59 -0700462 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 unsigned n, count;
464 char *x;
465 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 if (!strncmp(name, "reject ", 7)) {
468 name += 7;
469 invert = 1;
470 } else if (!strncmp(name, "require ", 8)) {
471 name += 8;
472 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700473 } else if (!strncmp(name, "require-for-product:", 20)) {
474 // Get the product and point name past it
475 prod = name + 20;
476 name = strchr(name, ' ');
477 if (!name) return -1;
478 *name = 0;
479 name += 1;
480 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 }
482
483 x = strchr(name, '=');
484 if (x == 0) return 0;
485 *x = 0;
486 val[0] = x + 1;
487
488 for(count = 1; count < MAX_OPTIONS; count++) {
489 x = strchr(val[count - 1],'|');
490 if (x == 0) break;
491 *x = 0;
492 val[count] = x + 1;
493 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800494
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 name = strip(name);
496 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800497
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 name = strip(name);
499 if (name == 0) return -1;
500
Elliott Hughes253c18d2015-03-18 22:47:09 -0700501 const char* var = name;
502 // Work around an unfortunate name mismatch.
503 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504
Elliott Hughes253c18d2015-03-18 22:47:09 -0700505 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 if (out == 0) return -1;
507
508 for(n = 0; n < count; n++) {
509 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700510 if (out[n] == 0) {
511 for(size_t i = 0; i < n; ++i) {
512 free((char*) out[i]);
513 }
514 free(out);
515 return -1;
516 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 }
518
Elliott Hughes253c18d2015-03-18 22:47:09 -0700519 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 return 0;
521}
522
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000523static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000525 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000527 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 while (sz-- > 0) {
529 if(*s == '\n') {
530 *s++ = 0;
531 if (setup_requirement_line(data)) {
532 die("out of memory");
533 }
534 data = s;
535 } else {
536 s++;
537 }
538 }
539}
540
541void queue_info_dump(void)
542{
543 fb_queue_notice("--------------------------------------------");
544 fb_queue_display("version-bootloader", "Bootloader Version...");
545 fb_queue_display("version-baseband", "Baseband Version.....");
546 fb_queue_display("serialno", "Serial Number........");
547 fb_queue_notice("--------------------------------------------");
548}
549
Rom Lemarchand622810c2013-06-28 09:54:59 -0700550static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700551{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700552 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700553 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700554 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700555 }
556
Elliott Hughes253c18d2015-03-18 22:47:09 -0700557 int files = sparse_file_resparse(s, max_size, NULL, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700558 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700559 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700560 }
561
Elliott Hughes253c18d2015-03-18 22:47:09 -0700562 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700563 if (!out_s) {
564 die("Failed to allocate sparse file array\n");
565 }
566
567 files = sparse_file_resparse(s, max_size, out_s, files);
568 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700569 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700570 }
571
572 return out_s;
573}
574
575static int64_t get_target_sparse_limit(struct usb_handle *usb)
576{
577 int64_t limit = 0;
578 char response[FB_RESPONSE_SZ + 1];
579 int status = fb_getvar(usb, response, "max-download-size");
580
581 if (!status) {
582 limit = strtoul(response, NULL, 0);
583 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700584 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700585 limit);
586 }
587 }
588
589 return limit;
590}
591
592static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
593{
594 int64_t limit;
595
596 if (sparse_limit == 0) {
597 return 0;
598 } else if (sparse_limit > 0) {
599 limit = sparse_limit;
600 } else {
601 if (target_sparse_limit == -1) {
602 target_sparse_limit = get_target_sparse_limit(usb);
603 }
604 if (target_sparse_limit > 0) {
605 limit = target_sparse_limit;
606 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700607 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700608 }
609 }
610
611 if (size > limit) {
612 return limit;
613 }
614
615 return 0;
616}
617
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700618/* Until we get lazy inode table init working in make_ext4fs, we need to
619 * erase partitions of type ext4 before flashing a filesystem so no stale
620 * inodes are left lying around. Otherwise, e2fsck gets very upset.
621 */
622static int needs_erase(const char *part)
623{
624 /* The function fb_format_supported() currently returns the value
625 * we want, so just call it.
626 */
JP Abgrall7e859742014-05-06 15:14:15 -0700627 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700628}
629
Rom Lemarchand622810c2013-06-28 09:54:59 -0700630static int load_buf_fd(usb_handle *usb, int fd,
631 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700632{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700633 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000634 void *data;
635 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700636
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000637
638 sz64 = file_size(fd);
639 if (sz64 < 0) {
640 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700642
643 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700644 limit = get_sparse_limit(usb, sz64);
645 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700647 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700648 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700649 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700650 buf->type = FB_BUFFER_SPARSE;
651 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700652 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000653 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700654 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000655 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700656 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700657 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000658 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700659 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660
661 return 0;
662}
663
664static int load_buf(usb_handle *usb, const char *fname,
665 struct fastboot_buffer *buf)
666{
667 int fd;
668
669 fd = open(fname, O_RDONLY | O_BINARY);
670 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700671 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700672 }
673
674 return load_buf_fd(usb, fd, buf);
675}
676
677static void flash_buf(const char *pname, struct fastboot_buffer *buf)
678{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700679 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700680
681 switch (buf->type) {
682 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700683 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700684 while (*s) {
685 int64_t sz64 = sparse_file_len(*s, true, false);
686 fb_queue_flash_sparse(pname, *s++, sz64);
687 }
688 break;
689 case FB_BUFFER:
690 fb_queue_flash(pname, buf->data, buf->sz);
691 break;
692 default:
693 die("unknown buffer type: %d", buf->type);
694 }
695}
696
697void do_flash(usb_handle *usb, const char *pname, const char *fname)
698{
699 struct fastboot_buffer buf;
700
701 if (load_buf(usb, fname, &buf)) {
702 die("cannot load '%s'", fname);
703 }
704 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700705}
706
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700707void do_update_signature(ZipArchiveHandle zip, char *fn)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000709 unsigned sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700710 void* data = unzip_file(zip, fn, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000711 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
Elliott Hughes253c18d2015-03-18 22:47:09 -0700716void do_update(usb_handle *usb, const char *filename, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718 queue_info_dump();
719
Wink Savilleb98762f2011-04-04 17:54:59 -0700720 fb_queue_query_save("product", cur_product, sizeof(cur_product));
721
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700722 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700723 int error = OpenArchive(filename, &zip);
724 if (error != 0) {
725 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700726 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800727
Elliott Hughes253c18d2015-03-18 22:47:09 -0700728 unsigned sz;
729 void* data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000730 if (data == 0) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700731 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732 }
733
Elliott Hughes253c18d2015-03-18 22:47:09 -0700734 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800735
Elliott Hughes253c18d2015-03-18 22:47:09 -0700736 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
737 int fd = unzip_to_file(zip, images[i].img_name);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700738 if (fd < 0) {
739 if (images[i].is_optional)
740 continue;
741 die("update package missing %s", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700742 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700743 fastboot_buffer buf;
744 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 if (rc) die("cannot load %s from flash", images[i].img_name);
746 do_update_signature(zip, images[i].sig_name);
747 if (erase_first && needs_erase(images[i].part_name)) {
748 fb_queue_erase(images[i].part_name);
749 }
750 flash_buf(images[i].part_name, &buf);
751 /* not closing the fd here since the sparse code keeps the fd around
752 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
753 * program exits.
754 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700756
757 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758}
759
760void do_send_signature(char *fn)
761{
762 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000763 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800764 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800765
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800766 xtn = strrchr(fn, '.');
767 if (!xtn) return;
768 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800769
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800770 strcpy(xtn,".sig");
771 data = load_file(fn, &sz);
772 strcpy(xtn,".img");
773 if (data == 0) return;
774 fb_queue_download("signature", data, sz);
775 fb_queue_command("signature", "installing signature");
776}
777
Rom Lemarchand622810c2013-06-28 09:54:59 -0700778void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800779{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 queue_info_dump();
781
Wink Savilleb98762f2011-04-04 17:54:59 -0700782 fb_queue_query_save("product", cur_product, sizeof(cur_product));
783
Elliott Hughes253c18d2015-03-18 22:47:09 -0700784 char* fname = find_item("info", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 if (fname == 0) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786
Elliott Hughes253c18d2015-03-18 22:47:09 -0700787 unsigned sz;
788 void* data = load_file(fname, &sz);
789 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
790
791 setup_requirements(reinterpret_cast<char*>(data), sz);
792
793 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700794 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700795 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796 if (load_buf(usb, fname, &buf)) {
797 if (images[i].is_optional)
798 continue;
799 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700800 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700801 do_send_signature(fname);
802 if (erase_first && needs_erase(images[i].part_name)) {
803 fb_queue_erase(images[i].part_name);
804 }
805 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800806 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800807}
808
809#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800810#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800811
812int do_oem_command(int argc, char **argv)
813{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800814 char command[256];
815 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800816
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800817 command[0] = 0;
818 while(1) {
819 strcat(command,*argv);
820 skip(1);
821 if(argc == 0) break;
822 strcat(command," ");
823 }
824
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800825 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800826 return 0;
827}
828
Colin Crossf8387882012-05-24 17:18:41 -0700829static int64_t parse_num(const char *arg)
830{
831 char *endptr;
832 unsigned long long num;
833
834 num = strtoull(arg, &endptr, 0);
835 if (endptr == arg) {
836 return -1;
837 }
838
839 if (*endptr == 'k' || *endptr == 'K') {
840 if (num >= (-1ULL) / 1024) {
841 return -1;
842 }
843 num *= 1024LL;
844 endptr++;
845 } else if (*endptr == 'm' || *endptr == 'M') {
846 if (num >= (-1ULL) / (1024 * 1024)) {
847 return -1;
848 }
849 num *= 1024LL * 1024LL;
850 endptr++;
851 } else if (*endptr == 'g' || *endptr == 'G') {
852 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
853 return -1;
854 }
855 num *= 1024LL * 1024LL * 1024LL;
856 endptr++;
857 }
858
859 if (*endptr != '\0') {
860 return -1;
861 }
862
863 if (num > INT64_MAX) {
864 return -1;
865 }
866
867 return num;
868}
869
JP Abgrall7e859742014-05-06 15:14:15 -0700870void fb_perform_format(const char *partition, int skip_if_not_supported,
871 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700872{
JP Abgrall7e859742014-05-06 15:14:15 -0700873 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
874 char *pType = pTypeBuff;
875 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700876 unsigned int limit = INT_MAX;
877 struct fastboot_buffer buf;
878 const char *errMsg = NULL;
879 const struct fs_generator *gen;
880 uint64_t pSz;
881 int status;
882 int fd;
883
884 if (target_sparse_limit > 0 && target_sparse_limit < limit)
885 limit = target_sparse_limit;
886 if (sparse_limit > 0 && sparse_limit < limit)
887 limit = sparse_limit;
888
889 status = fb_getvar(usb, pType, "partition-type:%s", partition);
890 if (status) {
891 errMsg = "Can't determine partition type.\n";
892 goto failed;
893 }
JP Abgrall7e859742014-05-06 15:14:15 -0700894 if (type_override) {
895 if (strcmp(type_override, pType)) {
896 fprintf(stderr,
897 "Warning: %s type is %s, but %s was requested for formating.\n",
898 partition, pType, type_override);
899 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700900 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700901 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700902
903 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
904 if (status) {
905 errMsg = "Unable to get partition size\n";
906 goto failed;
907 }
JP Abgrall7e859742014-05-06 15:14:15 -0700908 if (size_override) {
909 if (strcmp(size_override, pSize)) {
910 fprintf(stderr,
911 "Warning: %s size is %s, but %s was requested for formating.\n",
912 partition, pSize, size_override);
913 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700914 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700915 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700916
917 gen = fs_get_generator(pType);
918 if (!gen) {
919 if (skip_if_not_supported) {
920 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
921 fprintf(stderr, "File system type %s not supported.\n", pType);
922 return;
923 }
924 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
925 return;
926 }
927
928 pSz = strtoll(pSize, (char **)NULL, 16);
929
930 fd = fileno(tmpfile());
931 if (fs_generator_generate(gen, fd, pSz)) {
932 close(fd);
933 fprintf(stderr, "Cannot generate image.\n");
934 return;
935 }
936
937 if (load_buf_fd(usb, fd, &buf)) {
938 fprintf(stderr, "Cannot read image.\n");
939 close(fd);
940 return;
941 }
942 flash_buf(partition, &buf);
943
944 return;
945
946
947failed:
948 if (skip_if_not_supported) {
949 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
950 if (errMsg)
951 fprintf(stderr, "%s", errMsg);
952 }
953 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
954}
955
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800956int main(int argc, char **argv)
957{
958 int wants_wipe = 0;
959 int wants_reboot = 0;
960 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700961 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000963 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700964 int status;
Colin Cross8879f982012-05-22 17:53:34 -0700965 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100966 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800967
JP Abgrall7b8970c2013-03-07 17:06:41 -0800968 const struct option longopts[] = {
969 {"base", required_argument, 0, 'b'},
970 {"kernel_offset", required_argument, 0, 'k'},
971 {"page_size", required_argument, 0, 'n'},
972 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800973 {"tags_offset", required_argument, 0, 't'},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800974 {"help", 0, 0, 'h'},
Florian Bäuerle27ded482014-11-24 11:29:34 +0100975 {"unbuffered", 0, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800976 {0, 0, 0, 0}
977 };
Colin Cross8879f982012-05-22 17:53:34 -0700978
979 serial = getenv("ANDROID_SERIAL");
980
981 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +0100982 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 -0700983 if (c < 0) {
984 break;
985 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800986 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700987 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700988 case 'b':
989 base_addr = strtoul(optarg, 0, 16);
990 break;
Colin Cross8879f982012-05-22 17:53:34 -0700991 case 'c':
992 cmdline = optarg;
993 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800994 case 'h':
995 usage();
996 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700997 case 'i': {
998 char *endptr = NULL;
999 unsigned long val;
1000
1001 val = strtoul(optarg, &endptr, 0);
1002 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1003 die("invalid vendor id '%s'", optarg);
1004 vendor_id = (unsigned short)val;
1005 break;
1006 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001007 case 'k':
1008 kernel_offset = strtoul(optarg, 0, 16);
1009 break;
1010 case 'l':
1011 long_listing = 1;
1012 break;
1013 case 'n':
1014 page_size = (unsigned)strtoul(optarg, NULL, 0);
1015 if (!page_size) die("invalid page size");
1016 break;
1017 case 'p':
1018 product = optarg;
1019 break;
1020 case 'r':
1021 ramdisk_offset = strtoul(optarg, 0, 16);
1022 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001023 case 't':
1024 tags_offset = strtoul(optarg, 0, 16);
1025 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001026 case 's':
1027 serial = optarg;
1028 break;
1029 case 'S':
1030 sparse_limit = parse_num(optarg);
1031 if (sparse_limit < 0) {
1032 die("invalid sparse limit");
1033 }
1034 break;
1035 case 'u':
1036 erase_first = 0;
1037 break;
1038 case 'w':
1039 wants_wipe = 1;
1040 break;
Colin Cross8879f982012-05-22 17:53:34 -07001041 case '?':
1042 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001043 case 0:
1044 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
1045 setvbuf(stdout, NULL, _IONBF, 0);
1046 setvbuf(stderr, NULL, _IONBF, 0);
1047 }
1048 break;
Colin Cross8879f982012-05-22 17:53:34 -07001049 default:
1050 abort();
1051 }
1052 }
1053
1054 argc -= optind;
1055 argv += optind;
1056
1057 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001058 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001059 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060 }
1061
Colin Cross8fb6e062012-07-24 16:36:41 -07001062 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001063 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001065 return 0;
1066 }
1067
Colin Crossc7b75dc2012-08-29 18:17:06 -07001068 if (argc > 0 && !strcmp(*argv, "help")) {
1069 usage();
1070 return 0;
1071 }
1072
Colin Cross8879f982012-05-22 17:53:34 -07001073 usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001074
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001076 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001077 require(2);
1078 fb_queue_display(argv[1], argv[1]);
1079 skip(2);
1080 } else if(!strcmp(*argv, "erase")) {
1081 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001082
JP Abgrall7e859742014-05-06 15:14:15 -07001083 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001084 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1085 }
1086
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087 fb_queue_erase(argv[1]);
1088 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001089 } else if(!strncmp(*argv, "format", strlen("format"))) {
1090 char *overrides;
1091 char *type_override = NULL;
1092 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001093 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001094 /*
1095 * Parsing for: "format[:[type][:[size]]]"
1096 * Some valid things:
1097 * - select ontly the size, and leave default fs type:
1098 * format::0x4000000 userdata
1099 * - default fs type and size:
1100 * format userdata
1101 * format:: userdata
1102 */
1103 overrides = strchr(*argv, ':');
1104 if (overrides) {
1105 overrides++;
1106 size_override = strchr(overrides, ':');
1107 if (size_override) {
1108 size_override[0] = '\0';
1109 size_override++;
1110 }
1111 type_override = overrides;
1112 }
1113 if (type_override && !type_override[0]) type_override = NULL;
1114 if (size_override && !size_override[0]) size_override = NULL;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001115 if (erase_first && needs_erase(argv[1])) {
1116 fb_queue_erase(argv[1]);
1117 }
JP Abgrall7e859742014-05-06 15:14:15 -07001118 fb_perform_format(argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001119 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001120 } else if(!strcmp(*argv, "signature")) {
1121 require(2);
1122 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001123 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001124 if (sz != 256) die("signature must be 256 bytes");
1125 fb_queue_download("signature", data, sz);
1126 fb_queue_command("signature", "installing signature");
1127 skip(2);
1128 } else if(!strcmp(*argv, "reboot")) {
1129 wants_reboot = 1;
1130 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001131 if (argc > 0) {
1132 if (!strcmp(*argv, "bootloader")) {
1133 wants_reboot = 0;
1134 wants_reboot_bootloader = 1;
1135 skip(1);
1136 }
1137 }
1138 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001139 } else if(!strcmp(*argv, "reboot-bootloader")) {
1140 wants_reboot_bootloader = 1;
1141 skip(1);
1142 } else if (!strcmp(*argv, "continue")) {
1143 fb_queue_command("continue", "resuming boot");
1144 skip(1);
1145 } else if(!strcmp(*argv, "boot")) {
1146 char *kname = 0;
1147 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001148 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 skip(1);
1150 if (argc > 0) {
1151 kname = argv[0];
1152 skip(1);
1153 }
1154 if (argc > 0) {
1155 rname = argv[0];
1156 skip(1);
1157 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001158 if (argc > 0) {
1159 sname = argv[0];
1160 skip(1);
1161 }
1162 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163 if (data == 0) return 1;
1164 fb_queue_download("boot.img", data, sz);
1165 fb_queue_command("boot", "booting");
1166 } else if(!strcmp(*argv, "flash")) {
1167 char *pname = argv[1];
1168 char *fname = 0;
1169 require(2);
1170 if (argc > 2) {
1171 fname = argv[2];
1172 skip(3);
1173 } else {
1174 fname = find_item(pname, product);
1175 skip(2);
1176 }
1177 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001178 if (erase_first && needs_erase(pname)) {
1179 fb_queue_erase(pname);
1180 }
Colin Crossf8387882012-05-24 17:18:41 -07001181 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001182 } else if(!strcmp(*argv, "flash:raw")) {
1183 char *pname = argv[1];
1184 char *kname = argv[2];
1185 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001186 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001188 skip(3);
1189 if (argc > 0) {
1190 rname = argv[0];
1191 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001193 if (argc > 0) {
1194 sname = argv[0];
1195 skip(1);
1196 }
1197 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198 if (data == 0) die("cannot load bootable image");
1199 fb_queue_flash(pname, data, sz);
1200 } else if(!strcmp(*argv, "flashall")) {
1201 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001202 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001203 wants_reboot = 1;
1204 } else if(!strcmp(*argv, "update")) {
1205 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001206 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001207 skip(2);
1208 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001209 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210 skip(1);
1211 }
1212 wants_reboot = 1;
1213 } else if(!strcmp(*argv, "oem")) {
1214 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001215 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1216 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1217 || !strcmp(*(argv+1), "unlock_critical")
1218 || !strcmp(*(argv+1), "lock_critical")
1219 || !strcmp(*(argv+1), "get_unlock_ability")) {
1220 argc = do_oem_command(argc, argv);
1221 } else {
1222 usage();
1223 return 1;
1224 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001225 } else {
1226 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001227 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001228 }
1229 }
1230
1231 if (wants_wipe) {
1232 fb_queue_erase("userdata");
JP Abgrall7e859742014-05-06 15:14:15 -07001233 fb_perform_format("userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001234 fb_queue_erase("cache");
JP Abgrall7e859742014-05-06 15:14:15 -07001235 fb_perform_format("cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001236 }
1237 if (wants_reboot) {
1238 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001239 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001240 } else if (wants_reboot_bootloader) {
1241 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001242 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001243 }
1244
Scott Anderson13081c62012-04-06 12:39:30 -07001245 if (fb_queue_is_empty())
1246 return 0;
1247
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001248 status = fb_execute_queue(usb);
1249 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001250}