blob: ec7c4aeeaf7763735443727c808a561bc546987c [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 const char *serial = 0;
63static const char *product = 0;
64static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070066static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070067static int64_t sparse_limit = -1;
68static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
JP Abgrall7b8970c2013-03-07 17:06:41 -080070unsigned page_size = 2048;
71unsigned base_addr = 0x10000000;
72unsigned kernel_offset = 0x00008000;
73unsigned ramdisk_offset = 0x01000000;
74unsigned second_offset = 0x00f00000;
75unsigned tags_offset = 0x00000100;
76
Rom Lemarchand622810c2013-06-28 09:54:59 -070077enum fb_buffer_type {
78 FB_BUFFER,
79 FB_BUFFER_SPARSE,
80};
81
82struct fastboot_buffer {
83 enum fb_buffer_type type;
84 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +000085 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070086};
87
88static struct {
89 char img_name[13];
90 char sig_name[13];
91 char part_name[9];
92 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -070093} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -070094 {"boot.img", "boot.sig", "boot", false},
95 {"recovery.img", "recovery.sig", "recovery", true},
96 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -070097 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -070098};
Brian Swetland2a63bb72009-04-28 16:05:07 -070099
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100char *find_item(const char *item, const char *product)
101{
102 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700103 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 char path[PATH_MAX + 128];
105
106 if(!strcmp(item,"boot")) {
107 fn = "boot.img";
108 } else if(!strcmp(item,"recovery")) {
109 fn = "recovery.img";
110 } else if(!strcmp(item,"system")) {
111 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700112 } else if(!strcmp(item,"vendor")) {
113 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 } else if(!strcmp(item,"userdata")) {
115 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700116 } else if(!strcmp(item,"cache")) {
117 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 } else if(!strcmp(item,"info")) {
119 fn = "android-info.txt";
120 } else {
121 fprintf(stderr,"unknown partition '%s'\n", item);
122 return 0;
123 }
124
125 if(product) {
126 get_my_path(path);
127 sprintf(path + strlen(path),
128 "../../../target/product/%s/%s", product, fn);
129 return strdup(path);
130 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 dir = getenv("ANDROID_PRODUCT_OUT");
133 if((dir == 0) || (dir[0] == 0)) {
134 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
135 return 0;
136 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 sprintf(path, "%s/%s", dir, fn);
139 return strdup(path);
140}
141
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000142static int64_t file_size(int fd)
Colin Crossf8387882012-05-24 17:18:41 -0700143{
Rom Lemarchand622810c2013-06-28 09:54:59 -0700144 struct stat st;
145 int ret;
Colin Crossf8387882012-05-24 17:18:41 -0700146
Rom Lemarchand622810c2013-06-28 09:54:59 -0700147 ret = fstat(fd, &st);
Colin Crossf8387882012-05-24 17:18:41 -0700148
Rom Lemarchand622810c2013-06-28 09:54:59 -0700149 return ret ? -1 : st.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700150}
151
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000152static void *load_fd(int fd, unsigned *_sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000154 char *data;
155 int sz;
Matt Gumbel64ba2582011-12-08 11:59:38 -0800156 int errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000158 data = 0;
159
160 sz = file_size(fd);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 if (sz < 0) {
162 goto oops;
163 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
165 data = (char*) malloc(sz);
166 if(data == 0) goto oops;
167
168 if(read(fd, data, sz) != sz) goto oops;
169 close(fd);
170
171 if(_sz) *_sz = sz;
172 return data;
173
174oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return 0;
180}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000182static void *load_file(const char *fn, unsigned *_sz)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183{
184 int fd;
185
186 fd = open(fn, O_RDONLY | O_BINARY);
187 if(fd < 0) return 0;
188
189 return load_fd(fd, _sz);
190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughesa463d242015-08-10 15:30:54 -0700192int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
193 // Require a matching vendor id if the user specified one with -i.
194 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
195 return -1;
196 }
197
198 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
199 return -1;
200 }
201
Scott Anderson13081c62012-04-06 12:39:30 -0700202 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700204 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
205 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 return 0;
207}
208
JP Abgrall7b8970c2013-03-07 17:06:41 -0800209int match_fastboot(usb_ifc_info *info)
210{
211 return match_fastboot_with_serial(info, serial);
212}
213
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214int list_devices_callback(usb_ifc_info *info)
215{
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (match_fastboot_with_serial(info, NULL) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700217 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 if (!info->writable) {
219 serial = "no permissions"; // like "adb devices"
220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 if (!serial[0]) {
222 serial = "????????????";
223 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700224 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700225 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700226 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800227 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700228 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700229 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700230 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
233
234 return -1;
235}
236
237usb_handle *open_device(void)
238{
239 static usb_handle *usb = 0;
240 int announce = 1;
241
242 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800243
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 for(;;) {
245 usb = usb_open(match_fastboot);
246 if(usb) return usb;
247 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248 announce = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 fprintf(stderr,"< waiting for device >\n");
250 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700251 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 }
253}
254
255void list_devices(void) {
256 // We don't actually open a USB device here,
257 // just getting our callback called so we can
258 // list all the connected devices.
259 usb_open(list_devices_callback);
260}
261
262void usage(void)
263{
264 fprintf(stderr,
265/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
266 "usage: fastboot [ <option> ] <command>\n"
267 "\n"
268 "commands:\n"
269 " update <filename> reflash device from update.zip\n"
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700270 " flashall flash boot, system, vendor and if found,\n"
Daniel Rosenberg015d73f2014-09-15 13:44:07 -0700271 " recovery\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272 " flash <partition> [ <filename> ] write a file to a flash partition\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700273 " flashing lock locks the device. Prevents flashing\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700274 " partitions\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700275 " flashing unlock unlocks the device. Allows user to\n"
276 " flash any partition except the ones\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700277 " that are related to bootloader\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700278 " flashing lock_critical Prevents flashing bootloader related\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700279 " partitions\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700280 " flashing unlock_critical Enables flashing bootloader related\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700281 " partitions\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700282 " flashing get_unlock_ability Queries bootloader to see if the\n"
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -0700283 " device is unlocked\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700284 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
285 " unlock nonce\n"
286 " flashing unlock_bootloader <request> Issue unlock bootloader using request\n"
287 " flashing lock_bootloader Locks the bootloader to prevent\n"
288 " bootloader version rollback\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800289 " erase <partition> erase a flash partition\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700290 " format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
291 " Can override the fs type and/or\n"
292 " size the bootloader reports.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 " getvar <variable> display a bootloader variable\n"
294 " boot <kernel> [ <ramdisk> ] download and boot kernel\n"
295 " flash:raw boot <kernel> [ <ramdisk> ] create bootimage and flash it\n"
296 " devices list all connected devices\n"
Bruce Beare24ce4bc2010-10-14 09:43:26 -0700297 " continue continue with autoboot\n"
Elliott Hughesca85df02015-02-25 10:02:00 -0800298 " reboot [bootloader] reboot device, optionally into bootloader\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 " reboot-bootloader reboot device into bootloader\n"
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800300 " help show this help message\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 "\n"
302 "options:\n"
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700303 " -w erase userdata and cache (and format\n"
304 " if supported by partition type)\n"
305 " -u do not first erase partition before\n"
306 " formatting\n"
Scott Anderson13081c62012-04-06 12:39:30 -0700307 " -s <specific device> specify device serial number\n"
308 " or path to device port\n"
309 " -l with \"devices\", lists device paths\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 " -p <product> specify product name\n"
311 " -c <cmdline> override kernel commandline\n"
312 " -i <vendor id> specify a custom USB vendor id\n"
JP Abgrall7e859742014-05-06 15:14:15 -0700313 " -b <base_addr> specify a custom kernel base address.\n"
314 " default: 0x10000000\n"
315 " -n <page size> specify the nand page size.\n"
316 " default: 2048\n"
317 " -S <size>[K|M|G] automatically sparse files greater\n"
318 " than size. 0 to disable\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320}
321
JP Abgrall7b8970c2013-03-07 17:06:41 -0800322void *load_bootable_image(const char *kernel, const char *ramdisk,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 unsigned *sz, const char *cmdline)
324{
325 void *kdata = 0, *rdata = 0;
326 unsigned ksize = 0, rsize = 0;
327 void *bdata;
328 unsigned bsize;
329
330 if(kernel == 0) {
331 fprintf(stderr, "no image specified\n");
332 return 0;
333 }
334
335 kdata = load_file(kernel, &ksize);
336 if(kdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800337 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 return 0;
339 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800340
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 /* is this actually a boot image? */
342 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
343 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800344
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800345 if(ramdisk) {
346 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
347 return 0;
348 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800349
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 *sz = ksize;
351 return kdata;
352 }
353
354 if(ramdisk) {
355 rdata = load_file(ramdisk, &rsize);
356 if(rdata == 0) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800357 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 return 0;
359 }
360 }
361
362 fprintf(stderr,"creating boot image...\n");
JP Abgrall7b8970c2013-03-07 17:06:41 -0800363 bdata = mkbootimg(kdata, ksize, kernel_offset,
364 rdata, rsize, ramdisk_offset,
365 0, 0, second_offset,
366 page_size, base_addr, tags_offset, &bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 if(bdata == 0) {
368 fprintf(stderr,"failed to create boot.img\n");
369 return 0;
370 }
371 if(cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
372 fprintf(stderr,"creating boot image - %d bytes\n", bsize);
373 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800374
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 return bdata;
376}
377
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700378static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, unsigned* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379{
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700380 ZipEntryName zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700381 ZipEntry zip_entry;
382 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700383 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000384 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 }
386
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700387 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700389 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
390 if (data == NULL) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700391 fprintf(stderr, "failed to allocate %u bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000392 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 }
394
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700395 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
396 if (error != 0) {
397 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000399 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000401
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 return data;
403}
404
Elliott Hughes8bdd4bb2015-06-03 15:22:11 -0700405#if defined(_WIN32)
406
407// TODO: move this to somewhere it can be shared.
408
409#include <windows.h>
410
411// Windows' tmpfile(3) requires administrator rights because
412// it creates temporary files in the root directory.
413static FILE* win32_tmpfile() {
414 char temp_path[PATH_MAX];
415 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
416 if (nchars == 0 || nchars >= sizeof(temp_path)) {
417 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
418 return nullptr;
419 }
420
421 char filename[PATH_MAX];
422 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
423 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
424 return nullptr;
425 }
426
427 return fopen(filename, "w+bTD");
428}
429
430#define tmpfile win32_tmpfile
431
432#endif
433
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700434static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
435 FILE* fp = tmpfile();
436 if (fp == NULL) {
437 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
438 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700439 return -1;
440 }
441
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700442 ZipEntryName zip_entry_name(entry_name);
443 ZipEntry zip_entry;
444 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
445 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000446 return -1;
447 }
448
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700449 int fd = fileno(fp);
450 int error = ExtractEntryToFile(zip, &zip_entry, fd);
451 if (error != 0) {
452 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
453 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700454 }
455
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000456 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700457 return fd;
458}
459
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460static char *strip(char *s)
461{
462 int n;
463 while(*s && isspace(*s)) s++;
464 n = strlen(s);
465 while(n-- > 0) {
466 if(!isspace(s[n])) break;
467 s[n] = 0;
468 }
469 return s;
470}
471
472#define MAX_OPTIONS 32
473static int setup_requirement_line(char *name)
474{
475 char *val[MAX_OPTIONS];
Wink Savilleb98762f2011-04-04 17:54:59 -0700476 char *prod = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 unsigned n, count;
478 char *x;
479 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800480
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 if (!strncmp(name, "reject ", 7)) {
482 name += 7;
483 invert = 1;
484 } else if (!strncmp(name, "require ", 8)) {
485 name += 8;
486 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700487 } else if (!strncmp(name, "require-for-product:", 20)) {
488 // Get the product and point name past it
489 prod = name + 20;
490 name = strchr(name, ' ');
491 if (!name) return -1;
492 *name = 0;
493 name += 1;
494 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
496
497 x = strchr(name, '=');
498 if (x == 0) return 0;
499 *x = 0;
500 val[0] = x + 1;
501
502 for(count = 1; count < MAX_OPTIONS; count++) {
503 x = strchr(val[count - 1],'|');
504 if (x == 0) break;
505 *x = 0;
506 val[count] = x + 1;
507 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800508
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 name = strip(name);
510 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800511
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 name = strip(name);
513 if (name == 0) return -1;
514
Elliott Hughes253c18d2015-03-18 22:47:09 -0700515 const char* var = name;
516 // Work around an unfortunate name mismatch.
517 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518
Elliott Hughes253c18d2015-03-18 22:47:09 -0700519 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 if (out == 0) return -1;
521
522 for(n = 0; n < count; n++) {
523 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700524 if (out[n] == 0) {
525 for(size_t i = 0; i < n; ++i) {
526 free((char*) out[i]);
527 }
528 free(out);
529 return -1;
530 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 }
532
Elliott Hughes253c18d2015-03-18 22:47:09 -0700533 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 return 0;
535}
536
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000537static void setup_requirements(char *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800538{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000539 char *s;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000541 s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 while (sz-- > 0) {
543 if(*s == '\n') {
544 *s++ = 0;
545 if (setup_requirement_line(data)) {
546 die("out of memory");
547 }
548 data = s;
549 } else {
550 s++;
551 }
552 }
553}
554
555void queue_info_dump(void)
556{
557 fb_queue_notice("--------------------------------------------");
558 fb_queue_display("version-bootloader", "Bootloader Version...");
559 fb_queue_display("version-baseband", "Baseband Version.....");
560 fb_queue_display("serialno", "Serial Number........");
561 fb_queue_notice("--------------------------------------------");
562}
563
Rom Lemarchand622810c2013-06-28 09:54:59 -0700564static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700565{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700566 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700567 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700568 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700569 }
570
Elliott Hughes253c18d2015-03-18 22:47:09 -0700571 int files = sparse_file_resparse(s, max_size, NULL, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700572 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700573 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700574 }
575
Elliott Hughes253c18d2015-03-18 22:47:09 -0700576 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700577 if (!out_s) {
578 die("Failed to allocate sparse file array\n");
579 }
580
581 files = sparse_file_resparse(s, max_size, out_s, files);
582 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700583 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700584 }
585
586 return out_s;
587}
588
589static int64_t get_target_sparse_limit(struct usb_handle *usb)
590{
591 int64_t limit = 0;
592 char response[FB_RESPONSE_SZ + 1];
593 int status = fb_getvar(usb, response, "max-download-size");
594
595 if (!status) {
596 limit = strtoul(response, NULL, 0);
597 if (limit > 0) {
Ying Wangcf86e2f2014-05-15 20:06:40 -0700598 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n",
Colin Crossf8387882012-05-24 17:18:41 -0700599 limit);
600 }
601 }
602
603 return limit;
604}
605
606static int64_t get_sparse_limit(struct usb_handle *usb, int64_t size)
607{
608 int64_t limit;
609
610 if (sparse_limit == 0) {
611 return 0;
612 } else if (sparse_limit > 0) {
613 limit = sparse_limit;
614 } else {
615 if (target_sparse_limit == -1) {
616 target_sparse_limit = get_target_sparse_limit(usb);
617 }
618 if (target_sparse_limit > 0) {
619 limit = target_sparse_limit;
620 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700621 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700622 }
623 }
624
625 if (size > limit) {
626 return limit;
627 }
628
629 return 0;
630}
631
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700632/* Until we get lazy inode table init working in make_ext4fs, we need to
633 * erase partitions of type ext4 before flashing a filesystem so no stale
634 * inodes are left lying around. Otherwise, e2fsck gets very upset.
635 */
Elliott Hughesc688c232015-06-02 13:34:07 -0700636static int needs_erase(usb_handle* usb, const char *part)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700637{
638 /* The function fb_format_supported() currently returns the value
639 * we want, so just call it.
640 */
JP Abgrall7e859742014-05-06 15:14:15 -0700641 return fb_format_supported(usb, part, NULL);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700642}
643
Rom Lemarchand622810c2013-06-28 09:54:59 -0700644static int load_buf_fd(usb_handle *usb, int fd,
645 struct fastboot_buffer *buf)
Colin Crossf8387882012-05-24 17:18:41 -0700646{
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700647 int64_t sz64;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000648 void *data;
649 int64_t limit;
Colin Crossf8387882012-05-24 17:18:41 -0700650
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000651
652 sz64 = file_size(fd);
653 if (sz64 < 0) {
654 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700655 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700656
657 lseek(fd, 0, SEEK_SET);
Colin Crossf8387882012-05-24 17:18:41 -0700658 limit = get_sparse_limit(usb, sz64);
659 if (limit) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700660 struct sparse_file **s = load_sparse_files(fd, limit);
Colin Crossf8387882012-05-24 17:18:41 -0700661 if (s == NULL) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700662 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700663 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700664 buf->type = FB_BUFFER_SPARSE;
665 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700666 } else {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000667 unsigned int sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700668 data = load_fd(fd, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000669 if (data == 0) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700670 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700671 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000672 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700673 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700674
675 return 0;
676}
677
678static int load_buf(usb_handle *usb, const char *fname,
679 struct fastboot_buffer *buf)
680{
681 int fd;
682
683 fd = open(fname, O_RDONLY | O_BINARY);
684 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700685 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700686 }
687
688 return load_buf_fd(usb, fd, buf);
689}
690
691static void flash_buf(const char *pname, struct fastboot_buffer *buf)
692{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700693 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700694
695 switch (buf->type) {
696 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700697 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700698 while (*s) {
699 int64_t sz64 = sparse_file_len(*s, true, false);
700 fb_queue_flash_sparse(pname, *s++, sz64);
701 }
702 break;
703 case FB_BUFFER:
704 fb_queue_flash(pname, buf->data, buf->sz);
705 break;
706 default:
707 die("unknown buffer type: %d", buf->type);
708 }
709}
710
711void do_flash(usb_handle *usb, const char *pname, const char *fname)
712{
713 struct fastboot_buffer buf;
714
715 if (load_buf(usb, fname, &buf)) {
716 die("cannot load '%s'", fname);
717 }
718 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700719}
720
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700721void do_update_signature(ZipArchiveHandle zip, char *fn)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722{
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000723 unsigned sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700724 void* data = unzip_file(zip, fn, &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000725 if (data == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 fb_queue_download("signature", data, sz);
727 fb_queue_command("signature", "installing signature");
728}
729
Elliott Hughes253c18d2015-03-18 22:47:09 -0700730void do_update(usb_handle *usb, const char *filename, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732 queue_info_dump();
733
Wink Savilleb98762f2011-04-04 17:54:59 -0700734 fb_queue_query_save("product", cur_product, sizeof(cur_product));
735
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700736 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700737 int error = OpenArchive(filename, &zip);
738 if (error != 0) {
Narayan Kamath241bcf02015-05-11 16:59:46 +0100739 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700740 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700741 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800742
Elliott Hughes253c18d2015-03-18 22:47:09 -0700743 unsigned sz;
744 void* data = unzip_file(zip, "android-info.txt", &sz);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000745 if (data == 0) {
Narayan Kamath241bcf02015-05-11 16:59:46 +0100746 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700747 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800748 }
749
Elliott Hughes253c18d2015-03-18 22:47:09 -0700750 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800751
Elliott Hughes966339b2015-06-02 21:37:05 -0700752 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700753 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughes966339b2015-06-02 21:37:05 -0700754 if (fd == -1) {
755 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700756 continue;
Elliott Hughes966339b2015-06-02 21:37:05 -0700757 }
Narayan Kamath241bcf02015-05-11 16:59:46 +0100758 CloseArchive(zip);
Elliott Hughes966339b2015-06-02 21:37:05 -0700759 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700760 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700761 fastboot_buffer buf;
762 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700763 if (rc) die("cannot load %s from flash", images[i].img_name);
764 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc688c232015-06-02 13:34:07 -0700765 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700766 fb_queue_erase(images[i].part_name);
767 }
768 flash_buf(images[i].part_name, &buf);
769 /* not closing the fd here since the sparse code keeps the fd around
770 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
771 * program exits.
772 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800773 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700774
775 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800776}
777
778void do_send_signature(char *fn)
779{
780 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000781 unsigned sz;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 char *xtn;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800783
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800784 xtn = strrchr(fn, '.');
785 if (!xtn) return;
786 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800787
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800788 strcpy(xtn,".sig");
789 data = load_file(fn, &sz);
790 strcpy(xtn,".img");
791 if (data == 0) return;
792 fb_queue_download("signature", data, sz);
793 fb_queue_command("signature", "installing signature");
794}
795
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796void do_flashall(usb_handle *usb, int erase_first)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800797{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800798 queue_info_dump();
799
Wink Savilleb98762f2011-04-04 17:54:59 -0700800 fb_queue_query_save("product", cur_product, sizeof(cur_product));
801
Elliott Hughes253c18d2015-03-18 22:47:09 -0700802 char* fname = find_item("info", product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800803 if (fname == 0) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804
Elliott Hughes253c18d2015-03-18 22:47:09 -0700805 unsigned sz;
806 void* data = load_file(fname, &sz);
807 if (data == 0) die("could not load android-info.txt: %s", strerror(errno));
808
809 setup_requirements(reinterpret_cast<char*>(data), sz);
810
811 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700813 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700814 if (load_buf(usb, fname, &buf)) {
815 if (images[i].is_optional)
816 continue;
817 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700818 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819 do_send_signature(fname);
Elliott Hughesc688c232015-06-02 13:34:07 -0700820 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700821 fb_queue_erase(images[i].part_name);
822 }
823 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800825}
826
827#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800828#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800829
Patrick Tjin51e8b032015-09-01 08:15:23 -0700830int do_bypass_unlock_command(int argc, char **argv)
831{
832 unsigned sz;
833 void *data;
834
835 if (argc <= 2) return 0;
836 skip(2);
837
838 /*
839 * Process unlock_bootloader, we have to load the message file
840 * and send that to the remote device.
841 */
842 require(1);
843 data = load_file(*argv, &sz);
844 if (data == 0) die("could not load '%s': %s", *argv, strerror(errno));
845 fb_queue_download("unlock_message", data, sz);
846 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
847 skip(1);
848 return 0;
849}
850
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800851int do_oem_command(int argc, char **argv)
852{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800853 char command[256];
854 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800855
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800856 command[0] = 0;
857 while(1) {
858 strcat(command,*argv);
859 skip(1);
860 if(argc == 0) break;
861 strcat(command," ");
862 }
863
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800864 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800865 return 0;
866}
867
Colin Crossf8387882012-05-24 17:18:41 -0700868static int64_t parse_num(const char *arg)
869{
870 char *endptr;
871 unsigned long long num;
872
873 num = strtoull(arg, &endptr, 0);
874 if (endptr == arg) {
875 return -1;
876 }
877
878 if (*endptr == 'k' || *endptr == 'K') {
879 if (num >= (-1ULL) / 1024) {
880 return -1;
881 }
882 num *= 1024LL;
883 endptr++;
884 } else if (*endptr == 'm' || *endptr == 'M') {
885 if (num >= (-1ULL) / (1024 * 1024)) {
886 return -1;
887 }
888 num *= 1024LL * 1024LL;
889 endptr++;
890 } else if (*endptr == 'g' || *endptr == 'G') {
891 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
892 return -1;
893 }
894 num *= 1024LL * 1024LL * 1024LL;
895 endptr++;
896 }
897
898 if (*endptr != '\0') {
899 return -1;
900 }
901
902 if (num > INT64_MAX) {
903 return -1;
904 }
905
906 return num;
907}
908
Elliott Hughesc688c232015-06-02 13:34:07 -0700909void fb_perform_format(usb_handle* usb,
910 const char *partition, int skip_if_not_supported,
JP Abgrall7e859742014-05-06 15:14:15 -0700911 const char *type_override, const char *size_override)
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700912{
JP Abgrall7e859742014-05-06 15:14:15 -0700913 char pTypeBuff[FB_RESPONSE_SZ + 1], pSizeBuff[FB_RESPONSE_SZ + 1];
914 char *pType = pTypeBuff;
915 char *pSize = pSizeBuff;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700916 unsigned int limit = INT_MAX;
917 struct fastboot_buffer buf;
918 const char *errMsg = NULL;
919 const struct fs_generator *gen;
920 uint64_t pSz;
921 int status;
922 int fd;
923
924 if (target_sparse_limit > 0 && target_sparse_limit < limit)
925 limit = target_sparse_limit;
926 if (sparse_limit > 0 && sparse_limit < limit)
927 limit = sparse_limit;
928
929 status = fb_getvar(usb, pType, "partition-type:%s", partition);
930 if (status) {
931 errMsg = "Can't determine partition type.\n";
932 goto failed;
933 }
JP Abgrall7e859742014-05-06 15:14:15 -0700934 if (type_override) {
935 if (strcmp(type_override, pType)) {
936 fprintf(stderr,
937 "Warning: %s type is %s, but %s was requested for formating.\n",
938 partition, pType, type_override);
939 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700940 pType = (char *)type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700941 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700942
943 status = fb_getvar(usb, pSize, "partition-size:%s", partition);
944 if (status) {
945 errMsg = "Unable to get partition size\n";
946 goto failed;
947 }
JP Abgrall7e859742014-05-06 15:14:15 -0700948 if (size_override) {
949 if (strcmp(size_override, pSize)) {
950 fprintf(stderr,
951 "Warning: %s size is %s, but %s was requested for formating.\n",
952 partition, pSize, size_override);
953 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700954 pSize = (char *)size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700955 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700956
957 gen = fs_get_generator(pType);
958 if (!gen) {
959 if (skip_if_not_supported) {
960 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
961 fprintf(stderr, "File system type %s not supported.\n", pType);
962 return;
963 }
964 fprintf(stderr, "Formatting is not supported for filesystem with type '%s'.\n", pType);
965 return;
966 }
967
968 pSz = strtoll(pSize, (char **)NULL, 16);
969
970 fd = fileno(tmpfile());
971 if (fs_generator_generate(gen, fd, pSz)) {
972 close(fd);
973 fprintf(stderr, "Cannot generate image.\n");
974 return;
975 }
976
977 if (load_buf_fd(usb, fd, &buf)) {
978 fprintf(stderr, "Cannot read image.\n");
979 close(fd);
980 return;
981 }
982 flash_buf(partition, &buf);
983
984 return;
985
986
987failed:
988 if (skip_if_not_supported) {
989 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
990 if (errMsg)
991 fprintf(stderr, "%s", errMsg);
992 }
993 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
994}
995
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800996int main(int argc, char **argv)
997{
998 int wants_wipe = 0;
999 int wants_reboot = 0;
1000 int wants_reboot_bootloader = 0;
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001001 int erase_first = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001002 void *data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +00001003 unsigned sz;
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001004 int status;
Colin Cross8879f982012-05-22 17:53:34 -07001005 int c;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001006 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001007
JP Abgrall7b8970c2013-03-07 17:06:41 -08001008 const struct option longopts[] = {
1009 {"base", required_argument, 0, 'b'},
1010 {"kernel_offset", required_argument, 0, 'k'},
1011 {"page_size", required_argument, 0, 'n'},
1012 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001013 {"tags_offset", required_argument, 0, 't'},
Elliott Hughese283ca22015-06-02 13:50:00 -07001014 {"help", no_argument, 0, 'h'},
1015 {"unbuffered", no_argument, 0, 0},
1016 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001017 {0, 0, 0, 0}
1018 };
Colin Cross8879f982012-05-22 17:53:34 -07001019
1020 serial = getenv("ANDROID_SERIAL");
1021
1022 while (1) {
Florian Bäuerle27ded482014-11-24 11:29:34 +01001023 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 -07001024 if (c < 0) {
1025 break;
1026 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001027 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001028 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -07001029 case 'b':
1030 base_addr = strtoul(optarg, 0, 16);
1031 break;
Colin Cross8879f982012-05-22 17:53:34 -07001032 case 'c':
1033 cmdline = optarg;
1034 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001035 case 'h':
1036 usage();
1037 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001038 case 'i': {
1039 char *endptr = NULL;
1040 unsigned long val;
1041
1042 val = strtoul(optarg, &endptr, 0);
1043 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1044 die("invalid vendor id '%s'", optarg);
1045 vendor_id = (unsigned short)val;
1046 break;
1047 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001048 case 'k':
1049 kernel_offset = strtoul(optarg, 0, 16);
1050 break;
1051 case 'l':
1052 long_listing = 1;
1053 break;
1054 case 'n':
1055 page_size = (unsigned)strtoul(optarg, NULL, 0);
1056 if (!page_size) die("invalid page size");
1057 break;
1058 case 'p':
1059 product = optarg;
1060 break;
1061 case 'r':
1062 ramdisk_offset = strtoul(optarg, 0, 16);
1063 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001064 case 't':
1065 tags_offset = strtoul(optarg, 0, 16);
1066 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001067 case 's':
1068 serial = optarg;
1069 break;
1070 case 'S':
1071 sparse_limit = parse_num(optarg);
1072 if (sparse_limit < 0) {
1073 die("invalid sparse limit");
1074 }
1075 break;
1076 case 'u':
1077 erase_first = 0;
1078 break;
1079 case 'w':
1080 wants_wipe = 1;
1081 break;
Colin Cross8879f982012-05-22 17:53:34 -07001082 case '?':
1083 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001084 case 0:
1085 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
1086 setvbuf(stdout, NULL, _IONBF, 0);
1087 setvbuf(stderr, NULL, _IONBF, 0);
Elliott Hughese283ca22015-06-02 13:50:00 -07001088 } else if (strcmp("version", longopts[longindex].name) == 0) {
1089 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1090 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001091 }
1092 break;
Colin Cross8879f982012-05-22 17:53:34 -07001093 default:
1094 abort();
1095 }
1096 }
1097
1098 argc -= optind;
1099 argv += optind;
1100
1101 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001102 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001103 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104 }
1105
Colin Cross8fb6e062012-07-24 16:36:41 -07001106 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001107 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001108 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001109 return 0;
1110 }
1111
Colin Crossc7b75dc2012-08-29 18:17:06 -07001112 if (argc > 0 && !strcmp(*argv, "help")) {
1113 usage();
1114 return 0;
1115 }
1116
Elliott Hughesc688c232015-06-02 13:34:07 -07001117 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001119 while (argc > 0) {
Colin Cross8879f982012-05-22 17:53:34 -07001120 if(!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001121 require(2);
1122 fb_queue_display(argv[1], argv[1]);
1123 skip(2);
1124 } else if(!strcmp(*argv, "erase")) {
1125 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001126
JP Abgrall7e859742014-05-06 15:14:15 -07001127 if (fb_format_supported(usb, argv[1], NULL)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001128 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1129 }
1130
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001131 fb_queue_erase(argv[1]);
1132 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001133 } else if(!strncmp(*argv, "format", strlen("format"))) {
1134 char *overrides;
1135 char *type_override = NULL;
1136 char *size_override = NULL;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001137 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001138 /*
1139 * Parsing for: "format[:[type][:[size]]]"
1140 * Some valid things:
1141 * - select ontly the size, and leave default fs type:
1142 * format::0x4000000 userdata
1143 * - default fs type and size:
1144 * format userdata
1145 * format:: userdata
1146 */
1147 overrides = strchr(*argv, ':');
1148 if (overrides) {
1149 overrides++;
1150 size_override = strchr(overrides, ':');
1151 if (size_override) {
1152 size_override[0] = '\0';
1153 size_override++;
1154 }
1155 type_override = overrides;
1156 }
1157 if (type_override && !type_override[0]) type_override = NULL;
1158 if (size_override && !size_override[0]) size_override = NULL;
Elliott Hughesc688c232015-06-02 13:34:07 -07001159 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001160 fb_queue_erase(argv[1]);
1161 }
Elliott Hughesc688c232015-06-02 13:34:07 -07001162 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001163 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164 } else if(!strcmp(*argv, "signature")) {
1165 require(2);
1166 data = load_file(argv[1], &sz);
Matt Gumbel64ba2582011-12-08 11:59:38 -08001167 if (data == 0) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 if (sz != 256) die("signature must be 256 bytes");
1169 fb_queue_download("signature", data, sz);
1170 fb_queue_command("signature", "installing signature");
1171 skip(2);
1172 } else if(!strcmp(*argv, "reboot")) {
1173 wants_reboot = 1;
1174 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001175 if (argc > 0) {
1176 if (!strcmp(*argv, "bootloader")) {
1177 wants_reboot = 0;
1178 wants_reboot_bootloader = 1;
1179 skip(1);
1180 }
1181 }
1182 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 } else if(!strcmp(*argv, "reboot-bootloader")) {
1184 wants_reboot_bootloader = 1;
1185 skip(1);
1186 } else if (!strcmp(*argv, "continue")) {
1187 fb_queue_command("continue", "resuming boot");
1188 skip(1);
1189 } else if(!strcmp(*argv, "boot")) {
1190 char *kname = 0;
1191 char *rname = 0;
1192 skip(1);
1193 if (argc > 0) {
1194 kname = argv[0];
1195 skip(1);
1196 }
1197 if (argc > 0) {
1198 rname = argv[0];
1199 skip(1);
1200 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001201 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 if (data == 0) return 1;
1203 fb_queue_download("boot.img", data, sz);
1204 fb_queue_command("boot", "booting");
1205 } else if(!strcmp(*argv, "flash")) {
1206 char *pname = argv[1];
1207 char *fname = 0;
1208 require(2);
1209 if (argc > 2) {
1210 fname = argv[2];
1211 skip(3);
1212 } else {
1213 fname = find_item(pname, product);
1214 skip(2);
1215 }
1216 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc688c232015-06-02 13:34:07 -07001217 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001218 fb_queue_erase(pname);
1219 }
Colin Crossf8387882012-05-24 17:18:41 -07001220 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 } else if(!strcmp(*argv, "flash:raw")) {
1222 char *pname = argv[1];
1223 char *kname = argv[2];
1224 char *rname = 0;
1225 require(3);
1226 if(argc > 3) {
1227 rname = argv[3];
1228 skip(4);
1229 } else {
1230 skip(3);
1231 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001232 data = load_bootable_image(kname, rname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001233 if (data == 0) die("cannot load bootable image");
1234 fb_queue_flash(pname, data, sz);
1235 } else if(!strcmp(*argv, "flashall")) {
1236 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001237 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001238 wants_reboot = 1;
1239 } else if(!strcmp(*argv, "update")) {
1240 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001241 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001242 skip(2);
1243 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001244 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001245 skip(1);
1246 }
1247 wants_reboot = 1;
1248 } else if(!strcmp(*argv, "oem")) {
1249 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001250 } else if(!strcmp(*argv, "flashing")) {
1251 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1252 !strcmp(*(argv+1), "lock") ||
1253 !strcmp(*(argv+1), "unlock_critical") ||
1254 !strcmp(*(argv+1), "lock_critical") ||
1255 !strcmp(*(argv+1), "get_unlock_ability") ||
1256 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1257 !strcmp(*(argv+1), "lock_bootloader"))) {
1258 argc = do_oem_command(argc, argv);
1259 } else
1260 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1261 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharana873e1d2015-05-15 16:43:47 -07001262 } else {
1263 usage();
1264 return 1;
1265 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001266 } else {
1267 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001268 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 }
1270 }
1271
1272 if (wants_wipe) {
1273 fb_queue_erase("userdata");
Elliott Hughesc688c232015-06-02 13:34:07 -07001274 fb_perform_format(usb, "userdata", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001275 fb_queue_erase("cache");
Elliott Hughesc688c232015-06-02 13:34:07 -07001276 fb_perform_format(usb, "cache", 1, NULL, NULL);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001277 }
1278 if (wants_reboot) {
1279 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001280 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001281 } else if (wants_reboot_bootloader) {
1282 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001283 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284 }
1285
Scott Anderson13081c62012-04-06 12:39:30 -07001286 if (fb_queue_is_empty())
1287 return 0;
1288
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001289 status = fb_execute_queue(usb);
1290 return (status) ? 1 : 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001291}