blob: 7bf4b3162fdc3c1cab36a712e37e001f21d9905b [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>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
Colin Crossf8387882012-05-24 17:18:41 -070045
Elliott Hughes2fd45a92015-10-30 11:49:47 -070046#include <base/parseint.h>
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
Elliott Hughesfc797672015-04-07 20:12:50 -070070static unsigned page_size = 2048;
71static unsigned base_addr = 0x10000000;
72static unsigned kernel_offset = 0x00008000;
73static unsigned ramdisk_offset = 0x01000000;
74static unsigned second_offset = 0x00f00000;
75static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080076
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;
Elliott Hughesfc797672015-04-07 20:12:50 -070084 void* data;
85 int64_t 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
Elliott Hughesfc797672015-04-07 20:12:50 -0700100static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700102 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 char path[PATH_MAX + 128];
104
105 if(!strcmp(item,"boot")) {
106 fn = "boot.img";
107 } else if(!strcmp(item,"recovery")) {
108 fn = "recovery.img";
109 } else if(!strcmp(item,"system")) {
110 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700111 } else if(!strcmp(item,"vendor")) {
112 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 } else if(!strcmp(item,"userdata")) {
114 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700115 } else if(!strcmp(item,"cache")) {
116 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 } else if(!strcmp(item,"info")) {
118 fn = "android-info.txt";
119 } else {
120 fprintf(stderr,"unknown partition '%s'\n", item);
121 return 0;
122 }
123
124 if(product) {
125 get_my_path(path);
126 sprintf(path + strlen(path),
127 "../../../target/product/%s/%s", product, fn);
128 return strdup(path);
129 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800130
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131 dir = getenv("ANDROID_PRODUCT_OUT");
132 if((dir == 0) || (dir[0] == 0)) {
133 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
134 return 0;
135 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 sprintf(path, "%s/%s", dir, fn);
138 return strdup(path);
139}
140
Elliott Hughesfc797672015-04-07 20:12:50 -0700141static int64_t get_file_size(int fd) {
142 struct stat sb;
143 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700144}
145
Elliott Hughesfc797672015-04-07 20:12:50 -0700146static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800147 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700148 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
Elliott Hughesfc797672015-04-07 20:12:50 -0700150 *sz = get_file_size(fd);
151 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700152 goto oops;
153 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155 data = (char*) malloc(*sz);
156 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 close(fd);
160
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 return data;
162
163oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800164 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 close(fd);
166 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800167 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return 0;
169}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171static void* load_file(const char* fn, int64_t* sz) {
172 int fd = open(fn, O_RDONLY | O_BINARY);
173 if (fd == -1) return nullptr;
174 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700175}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700178 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700179 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700180 return -1;
181 }
182
183 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
184 return -1;
185 }
186
Scott Anderson13081c62012-04-06 12:39:30 -0700187 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700189 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
190 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 return 0;
192}
193
Elliott Hughesfc797672015-04-07 20:12:50 -0700194static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800195 return match_fastboot_with_serial(info, serial);
196}
197
Elliott Hughesfc797672015-04-07 20:12:50 -0700198static int list_devices_callback(usb_ifc_info* info) {
199 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700200 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700201 if (!info->writable) {
202 serial = "no permissions"; // like "adb devices"
203 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 if (!serial[0]) {
205 serial = "????????????";
206 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700207 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700208 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700209 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800210 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700211 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700212 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700213 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700214 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 }
216
217 return -1;
218}
219
Elliott Hughesfc797672015-04-07 20:12:50 -0700220static usb_handle* open_device() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 static usb_handle *usb = 0;
222 int announce = 1;
223
224 if(usb) return usb;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800225
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226 for(;;) {
227 usb = usb_open(match_fastboot);
228 if(usb) return usb;
229 if(announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800230 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700231 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700233 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235}
236
Elliott Hughesfc797672015-04-07 20:12:50 -0700237static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 // We don't actually open a USB device here,
239 // just getting our callback called so we can
240 // list all the connected devices.
241 usb_open(list_devices_callback);
242}
243
Elliott Hughesfc797672015-04-07 20:12:50 -0700244static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 fprintf(stderr,
246/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
247 "usage: fastboot [ <option> ] <command>\n"
248 "\n"
249 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700250 " update <filename> Reflash device from update.zip.\n"
251 " flashall Flash boot, system, vendor, and --\n"
252 " if found -- recovery.\n"
253 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
254 " flashing lock Locks the device. Prevents flashing.\n"
255 " flashing unlock Unlocks the device. Allows flashing\n"
256 " any partition except\n"
257 " bootloader-related partitions.\n"
258 " flashing lock_critical Prevents flashing bootloader-related\n"
259 " partitions.\n"
260 " flashing unlock_critical Enables flashing bootloader-related\n"
261 " partitions.\n"
262 " flashing get_unlock_ability Queries bootloader to see if the\n"
263 " device is unlocked.\n"
264 " erase <partition> Erase a flash partition.\n"
265 " format[:[<fs type>][:[<size>]] <partition>\n"
266 " Format a flash partition. Can\n"
267 " override the fs type and/or size\n"
268 " the bootloader reports.\n"
269 " getvar <variable> Display a bootloader variable.\n"
270 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
271 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
272 " Create bootimage and flash it.\n"
273 " devices [-l] List all connected devices [with\n"
274 " device paths].\n"
275 " continue Continue with autoboot.\n"
276 " reboot [bootloader] Reboot device [into bootloader].\n"
277 " reboot-bootloader Reboot device into bootloader.\n"
278 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 "\n"
280 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700281 " -w Erase userdata and cache (and format\n"
282 " if supported by partition type).\n"
283 " -u Do not erase partition before\n"
284 " formatting.\n"
285 " -s <specific device> Specify device serial number\n"
286 " or path to device port.\n"
287 " -p <product> Specify product name.\n"
288 " -c <cmdline> Override kernel commandline.\n"
289 " -i <vendor id> Specify a custom USB vendor id.\n"
290 " -b <base_addr> Specify a custom kernel base\n"
291 " address (default: 0x10000000).\n"
292 " -n <page size> Specify the nand page size\n"
293 " (default: 2048).\n"
294 " -S <size>[K|M|G] Automatically sparse files greater\n"
295 " than 'size'. 0 to disable.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297}
298
Elliott Hughesfc797672015-04-07 20:12:50 -0700299static void* load_bootable_image(const char* kernel, const char* ramdisk,
300 const char* secondstage, int64_t* sz,
301 const char* cmdline) {
302 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 fprintf(stderr, "no image specified\n");
304 return 0;
305 }
306
Elliott Hughesfc797672015-04-07 20:12:50 -0700307 int64_t ksize;
308 void* kdata = load_file(kernel, &ksize);
309 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800310 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 return 0;
312 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800313
Elliott Hughesfc797672015-04-07 20:12:50 -0700314 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700316 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800317
Elliott Hughesfc797672015-04-07 20:12:50 -0700318 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
320 return 0;
321 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800322
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 *sz = ksize;
324 return kdata;
325 }
326
Elliott Hughesfc797672015-04-07 20:12:50 -0700327 void* rdata = nullptr;
328 int64_t rsize = 0;
329 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700331 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800332 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 return 0;
334 }
335 }
336
Elliott Hughesfc797672015-04-07 20:12:50 -0700337 void* sdata = nullptr;
338 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200339 if (secondstage) {
340 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700341 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200342 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
343 return 0;
344 }
345 }
346
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700348 int64_t bsize = 0;
349 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800350 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200351 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800352 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700353 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 fprintf(stderr,"failed to create boot.img\n");
355 return 0;
356 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700357 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
358 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 return bdata;
362}
363
Elliott Hughesfc797672015-04-07 20:12:50 -0700364static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365{
Yusuke Sato07447542015-06-25 14:39:19 -0700366 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700367 ZipEntry zip_entry;
368 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700369 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000370 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 }
372
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700373 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700375 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700376 if (data == nullptr) {
377 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000378 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 }
380
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700381 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
382 if (error != 0) {
383 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000385 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000387
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 return data;
389}
390
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700391#if defined(_WIN32)
392
393// TODO: move this to somewhere it can be shared.
394
395#include <windows.h>
396
397// Windows' tmpfile(3) requires administrator rights because
398// it creates temporary files in the root directory.
399static FILE* win32_tmpfile() {
400 char temp_path[PATH_MAX];
401 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
402 if (nchars == 0 || nchars >= sizeof(temp_path)) {
403 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
404 return nullptr;
405 }
406
407 char filename[PATH_MAX];
408 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
409 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
410 return nullptr;
411 }
412
413 return fopen(filename, "w+bTD");
414}
415
416#define tmpfile win32_tmpfile
417
418#endif
419
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700420static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
421 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700422 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700423 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
Yusuke Sato07447542015-06-25 14:39:19 -0700428 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700429 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];
Elliott Hughesfc797672015-04-07 20:12:50 -0700462 char *prod = nullptr;
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
Elliott Hughesfc797672015-04-07 20:12:50 -0700523static void setup_requirements(char* data, int64_t sz) {
524 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700526 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 *s++ = 0;
528 if (setup_requirement_line(data)) {
529 die("out of memory");
530 }
531 data = s;
532 } else {
533 s++;
534 }
535 }
536}
537
Elliott Hughesfc797672015-04-07 20:12:50 -0700538static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800539 fb_queue_notice("--------------------------------------------");
540 fb_queue_display("version-bootloader", "Bootloader Version...");
541 fb_queue_display("version-baseband", "Baseband Version.....");
542 fb_queue_display("serialno", "Serial Number........");
543 fb_queue_notice("--------------------------------------------");
544}
545
Rom Lemarchand622810c2013-06-28 09:54:59 -0700546static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700547{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700548 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700549 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700550 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700551 }
552
Elliott Hughesfc797672015-04-07 20:12:50 -0700553 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700554 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700555 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700556 }
557
Elliott Hughes253c18d2015-03-18 22:47:09 -0700558 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700559 if (!out_s) {
560 die("Failed to allocate sparse file array\n");
561 }
562
563 files = sparse_file_resparse(s, max_size, out_s, files);
564 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700566 }
567
568 return out_s;
569}
570
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700571static int64_t get_target_sparse_limit(usb_handle* usb) {
572 std::string max_download_size;
573 if (!fb_getvar(usb, "max-download-size", &max_download_size)) {
574 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700575 }
576
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700577 uint64_t limit;
578 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
579 return 0;
580 }
581 if (limit > 0) {
582 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
583 }
Colin Crossf8387882012-05-24 17:18:41 -0700584 return limit;
585}
586
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700587static int64_t get_sparse_limit(usb_handle* usb, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700588 int64_t limit;
589
590 if (sparse_limit == 0) {
591 return 0;
592 } else if (sparse_limit > 0) {
593 limit = sparse_limit;
594 } else {
595 if (target_sparse_limit == -1) {
596 target_sparse_limit = get_target_sparse_limit(usb);
597 }
598 if (target_sparse_limit > 0) {
599 limit = target_sparse_limit;
600 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700601 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700602 }
603 }
604
605 if (size > limit) {
606 return limit;
607 }
608
609 return 0;
610}
611
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700612// Until we get lazy inode table init working in make_ext4fs, we need to
613// erase partitions of type ext4 before flashing a filesystem so no stale
614// inodes are left lying around. Otherwise, e2fsck gets very upset.
615static bool needs_erase(usb_handle* usb, const char* part) {
616 return !fb_format_supported(usb, part, nullptr);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700617}
618
Elliott Hughesfc797672015-04-07 20:12:50 -0700619static int load_buf_fd(usb_handle* usb, int fd, struct fastboot_buffer* buf) {
620 int64_t sz = get_file_size(fd);
621 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000622 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700623 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700624
Elliott Hughesfc797672015-04-07 20:12:50 -0700625 lseek64(fd, 0, SEEK_SET);
626 int64_t limit = get_sparse_limit(usb, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700627 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700628 sparse_file** s = load_sparse_files(fd, limit);
629 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700630 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700631 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700632 buf->type = FB_BUFFER_SPARSE;
633 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700634 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700635 void* data = load_fd(fd, &sz);
636 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700638 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000639 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700640 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641
642 return 0;
643}
644
645static int load_buf(usb_handle *usb, const char *fname,
646 struct fastboot_buffer *buf)
647{
648 int fd;
649
650 fd = open(fname, O_RDONLY | O_BINARY);
651 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700652 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700653 }
654
655 return load_buf_fd(usb, fd, buf);
656}
657
658static void flash_buf(const char *pname, struct fastboot_buffer *buf)
659{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700660 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700661
662 switch (buf->type) {
663 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700664 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700665 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700666 int64_t sz = sparse_file_len(*s, true, false);
667 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700668 }
669 break;
670 case FB_BUFFER:
671 fb_queue_flash(pname, buf->data, buf->sz);
672 break;
673 default:
674 die("unknown buffer type: %d", buf->type);
675 }
676}
677
Elliott Hughesfc797672015-04-07 20:12:50 -0700678static void do_flash(usb_handle* usb, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700679 struct fastboot_buffer buf;
680
681 if (load_buf(usb, fname, &buf)) {
682 die("cannot load '%s'", fname);
683 }
684 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700685}
686
Elliott Hughesfc797672015-04-07 20:12:50 -0700687static void do_update_signature(ZipArchiveHandle zip, char* fn) {
688 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700689 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700690 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 fb_queue_download("signature", data, sz);
692 fb_queue_command("signature", "installing signature");
693}
694
Elliott Hughesfc797672015-04-07 20:12:50 -0700695static void do_update(usb_handle* usb, const char* filename, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 queue_info_dump();
697
Wink Savilleb98762f2011-04-04 17:54:59 -0700698 fb_queue_query_save("product", cur_product, sizeof(cur_product));
699
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700700 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700701 int error = OpenArchive(filename, &zip);
702 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100703 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700704 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700705 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706
Elliott Hughesfc797672015-04-07 20:12:50 -0700707 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700708 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700709 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100710 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700711 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 }
713
Elliott Hughes253c18d2015-03-18 22:47:09 -0700714 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715
Elliott Hughesacdbe922015-06-02 21:37:05 -0700716 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700717 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700718 if (fd == -1) {
719 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700720 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700721 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100722 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700723 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700724 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700725 fastboot_buffer buf;
726 int rc = load_buf_fd(usb, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700727 if (rc) die("cannot load %s from flash", images[i].img_name);
728 do_update_signature(zip, images[i].sig_name);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700729 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700730 fb_queue_erase(images[i].part_name);
731 }
732 flash_buf(images[i].part_name, &buf);
733 /* not closing the fd here since the sparse code keeps the fd around
734 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
735 * program exits.
736 */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800737 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700738
739 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740}
741
Elliott Hughesfc797672015-04-07 20:12:50 -0700742static void do_send_signature(char* fn) {
743 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800744 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700745
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800747
Elliott Hughesfc797672015-04-07 20:12:50 -0700748 strcpy(xtn, ".sig");
749
750 int64_t sz;
751 void* data = load_file(fn, &sz);
752 strcpy(xtn, ".img");
753 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 fb_queue_download("signature", data, sz);
755 fb_queue_command("signature", "installing signature");
756}
757
Elliott Hughesfc797672015-04-07 20:12:50 -0700758static void do_flashall(usb_handle* usb, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 queue_info_dump();
760
Wink Savilleb98762f2011-04-04 17:54:59 -0700761 fb_queue_query_save("product", cur_product, sizeof(cur_product));
762
Elliott Hughes253c18d2015-03-18 22:47:09 -0700763 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700764 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765
Elliott Hughesfc797672015-04-07 20:12:50 -0700766 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700767 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700768 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700769
770 setup_requirements(reinterpret_cast<char*>(data), sz);
771
772 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700773 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700774 fastboot_buffer buf;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700775 if (load_buf(usb, fname, &buf)) {
776 if (images[i].is_optional)
777 continue;
778 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700779 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700780 do_send_signature(fname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -0700781 if (erase_first && needs_erase(usb, images[i].part_name)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700782 fb_queue_erase(images[i].part_name);
783 }
784 flash_buf(images[i].part_name, &buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800785 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800786}
787
788#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800789#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800790
Elliott Hughesfc797672015-04-07 20:12:50 -0700791static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800792 char command[256];
793 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800794
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800795 command[0] = 0;
796 while(1) {
797 strcat(command,*argv);
798 skip(1);
799 if(argc == 0) break;
800 strcat(command," ");
801 }
802
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800803 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804 return 0;
805}
806
Colin Crossf8387882012-05-24 17:18:41 -0700807static int64_t parse_num(const char *arg)
808{
809 char *endptr;
810 unsigned long long num;
811
812 num = strtoull(arg, &endptr, 0);
813 if (endptr == arg) {
814 return -1;
815 }
816
817 if (*endptr == 'k' || *endptr == 'K') {
818 if (num >= (-1ULL) / 1024) {
819 return -1;
820 }
821 num *= 1024LL;
822 endptr++;
823 } else if (*endptr == 'm' || *endptr == 'M') {
824 if (num >= (-1ULL) / (1024 * 1024)) {
825 return -1;
826 }
827 num *= 1024LL * 1024LL;
828 endptr++;
829 } else if (*endptr == 'g' || *endptr == 'G') {
830 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
831 return -1;
832 }
833 num *= 1024LL * 1024LL * 1024LL;
834 endptr++;
835 }
836
837 if (*endptr != '\0') {
838 return -1;
839 }
840
841 if (num > INT64_MAX) {
842 return -1;
843 }
844
845 return num;
846}
847
Elliott Hughesfc797672015-04-07 20:12:50 -0700848static void fb_perform_format(usb_handle* usb,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700849 const char* partition, int skip_if_not_supported,
850 const char* type_override, const char* size_override) {
851 std::string partition_type, partition_size;
852
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700853 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700854 const char* errMsg = nullptr;
855 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700856 int fd;
857
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700858 unsigned int limit = INT_MAX;
859 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700860 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700861 }
862 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700863 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700864 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700865
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700866 if (!fb_getvar(usb, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700867 errMsg = "Can't determine partition type.\n";
868 goto failed;
869 }
JP Abgrall7e859742014-05-06 15:14:15 -0700870 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700871 if (partition_type != type_override) {
872 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
873 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700874 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700875 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700876 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700877
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700878 if (!fb_getvar(usb, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700879 errMsg = "Unable to get partition size\n";
880 goto failed;
881 }
JP Abgrall7e859742014-05-06 15:14:15 -0700882 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700883 if (partition_size != size_override) {
884 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
885 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -0700886 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700887 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -0700888 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700889
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700890 gen = fs_get_generator(partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700891 if (!gen) {
892 if (skip_if_not_supported) {
893 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700894 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700895 return;
896 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700897 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
898 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700899 return;
900 }
901
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700902 int64_t size;
903 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
904 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
905 return;
906 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700907
908 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700909 if (fs_generator_generate(gen, fd, size)) {
910 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700911 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700912 return;
913 }
914
915 if (load_buf_fd(usb, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700916 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700917 close(fd);
918 return;
919 }
920 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700921 return;
922
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700923failed:
924 if (skip_if_not_supported) {
925 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700926 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700927 }
928 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
929}
930
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800931int main(int argc, char **argv)
932{
933 int wants_wipe = 0;
934 int wants_reboot = 0;
935 int wants_reboot_bootloader = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -0700936 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800937 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -0700938 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +0100939 int longindex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800940
JP Abgrall7b8970c2013-03-07 17:06:41 -0800941 const struct option longopts[] = {
942 {"base", required_argument, 0, 'b'},
943 {"kernel_offset", required_argument, 0, 'k'},
944 {"page_size", required_argument, 0, 'n'},
945 {"ramdisk_offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800946 {"tags_offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -0700947 {"help", no_argument, 0, 'h'},
948 {"unbuffered", no_argument, 0, 0},
949 {"version", no_argument, 0, 0},
JP Abgrall7b8970c2013-03-07 17:06:41 -0800950 {0, 0, 0, 0}
951 };
Colin Cross8879f982012-05-22 17:53:34 -0700952
953 serial = getenv("ANDROID_SERIAL");
954
955 while (1) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700956 int 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 -0700957 if (c < 0) {
958 break;
959 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800960 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -0700961 switch (c) {
Colin Cross8879f982012-05-22 17:53:34 -0700962 case 'b':
963 base_addr = strtoul(optarg, 0, 16);
964 break;
Colin Cross8879f982012-05-22 17:53:34 -0700965 case 'c':
966 cmdline = optarg;
967 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -0800968 case 'h':
969 usage();
970 return 1;
Colin Cross8879f982012-05-22 17:53:34 -0700971 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -0700972 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -0700973 unsigned long val;
974
975 val = strtoul(optarg, &endptr, 0);
976 if (!endptr || *endptr != '\0' || (val & ~0xffff))
977 die("invalid vendor id '%s'", optarg);
978 vendor_id = (unsigned short)val;
979 break;
980 }
JP Abgrall7b8970c2013-03-07 17:06:41 -0800981 case 'k':
982 kernel_offset = strtoul(optarg, 0, 16);
983 break;
984 case 'l':
985 long_listing = 1;
986 break;
987 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -0700988 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -0800989 if (!page_size) die("invalid page size");
990 break;
991 case 'p':
992 product = optarg;
993 break;
994 case 'r':
995 ramdisk_offset = strtoul(optarg, 0, 16);
996 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -0800997 case 't':
998 tags_offset = strtoul(optarg, 0, 16);
999 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001000 case 's':
1001 serial = optarg;
1002 break;
1003 case 'S':
1004 sparse_limit = parse_num(optarg);
1005 if (sparse_limit < 0) {
1006 die("invalid sparse limit");
1007 }
1008 break;
1009 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001010 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001011 break;
1012 case 'w':
1013 wants_wipe = 1;
1014 break;
Colin Cross8879f982012-05-22 17:53:34 -07001015 case '?':
1016 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001017 case 0:
1018 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001019 setvbuf(stdout, nullptr, _IONBF, 0);
1020 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001021 } else if (strcmp("version", longopts[longindex].name) == 0) {
1022 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1023 return 0;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001024 }
1025 break;
Colin Cross8879f982012-05-22 17:53:34 -07001026 default:
1027 abort();
1028 }
1029 }
1030
1031 argc -= optind;
1032 argv += optind;
1033
1034 if (argc == 0 && !wants_wipe) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001035 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001036 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001037 }
1038
Colin Cross8fb6e062012-07-24 16:36:41 -07001039 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001040 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001041 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001042 return 0;
1043 }
1044
Colin Crossc7b75dc2012-08-29 18:17:06 -07001045 if (argc > 0 && !strcmp(*argv, "help")) {
1046 usage();
1047 return 0;
1048 }
1049
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001050 usb_handle* usb = open_device();
Elliott Hughes31dbed72009-10-07 15:38:53 -07001051
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001052 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001053 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001054 require(2);
1055 fb_queue_display(argv[1], argv[1]);
1056 skip(2);
1057 } else if(!strcmp(*argv, "erase")) {
1058 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001059
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001060 if (!fb_format_supported(usb, argv[1], nullptr)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001061 fprintf(stderr, "******** Did you mean to fastboot format this partition?\n");
1062 }
1063
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001064 fb_queue_erase(argv[1]);
1065 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001066 } else if(!strncmp(*argv, "format", strlen("format"))) {
1067 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001068 char *type_override = nullptr;
1069 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001070 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001071 /*
1072 * Parsing for: "format[:[type][:[size]]]"
1073 * Some valid things:
1074 * - select ontly the size, and leave default fs type:
1075 * format::0x4000000 userdata
1076 * - default fs type and size:
1077 * format userdata
1078 * format:: userdata
1079 */
1080 overrides = strchr(*argv, ':');
1081 if (overrides) {
1082 overrides++;
1083 size_override = strchr(overrides, ':');
1084 if (size_override) {
1085 size_override[0] = '\0';
1086 size_override++;
1087 }
1088 type_override = overrides;
1089 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001090 if (type_override && !type_override[0]) type_override = nullptr;
1091 if (size_override && !size_override[0]) size_override = nullptr;
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001092 if (erase_first && needs_erase(usb, argv[1])) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001093 fb_queue_erase(argv[1]);
1094 }
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001095 fb_perform_format(usb, argv[1], 0, type_override, size_override);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001096 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001097 } else if(!strcmp(*argv, "signature")) {
1098 require(2);
1099 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001100 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101 if (sz != 256) die("signature must be 256 bytes");
1102 fb_queue_download("signature", data, sz);
1103 fb_queue_command("signature", "installing signature");
1104 skip(2);
1105 } else if(!strcmp(*argv, "reboot")) {
1106 wants_reboot = 1;
1107 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001108 if (argc > 0) {
1109 if (!strcmp(*argv, "bootloader")) {
1110 wants_reboot = 0;
1111 wants_reboot_bootloader = 1;
1112 skip(1);
1113 }
1114 }
1115 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001116 } else if(!strcmp(*argv, "reboot-bootloader")) {
1117 wants_reboot_bootloader = 1;
1118 skip(1);
1119 } else if (!strcmp(*argv, "continue")) {
1120 fb_queue_command("continue", "resuming boot");
1121 skip(1);
1122 } else if(!strcmp(*argv, "boot")) {
1123 char *kname = 0;
1124 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001125 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001126 skip(1);
1127 if (argc > 0) {
1128 kname = argv[0];
1129 skip(1);
1130 }
1131 if (argc > 0) {
1132 rname = argv[0];
1133 skip(1);
1134 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001135 if (argc > 0) {
1136 sname = argv[0];
1137 skip(1);
1138 }
1139 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001140 if (data == 0) return 1;
1141 fb_queue_download("boot.img", data, sz);
1142 fb_queue_command("boot", "booting");
1143 } else if(!strcmp(*argv, "flash")) {
1144 char *pname = argv[1];
1145 char *fname = 0;
1146 require(2);
1147 if (argc > 2) {
1148 fname = argv[2];
1149 skip(3);
1150 } else {
1151 fname = find_item(pname, product);
1152 skip(2);
1153 }
1154 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Elliott Hughesc0ce65f2015-06-02 13:34:07 -07001155 if (erase_first && needs_erase(usb, pname)) {
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001156 fb_queue_erase(pname);
1157 }
Colin Crossf8387882012-05-24 17:18:41 -07001158 do_flash(usb, pname, fname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001159 } else if(!strcmp(*argv, "flash:raw")) {
1160 char *pname = argv[1];
1161 char *kname = argv[2];
1162 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001163 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001165 skip(3);
1166 if (argc > 0) {
1167 rname = argv[0];
1168 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001170 if (argc > 0) {
1171 sname = argv[0];
1172 skip(1);
1173 }
1174 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001175 if (data == 0) die("cannot load bootable image");
1176 fb_queue_flash(pname, data, sz);
1177 } else if(!strcmp(*argv, "flashall")) {
1178 skip(1);
Rom Lemarchand622810c2013-06-28 09:54:59 -07001179 do_flashall(usb, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001180 wants_reboot = 1;
1181 } else if(!strcmp(*argv, "update")) {
1182 if (argc > 1) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001183 do_update(usb, argv[1], erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 skip(2);
1185 } else {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001186 do_update(usb, "update.zip", erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187 skip(1);
1188 }
1189 wants_reboot = 1;
1190 } else if(!strcmp(*argv, "oem")) {
1191 argc = do_oem_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001192 } else if(!strcmp(*argv, "flashing") && argc == 2) {
1193 if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
1194 || !strcmp(*(argv+1), "unlock_critical")
1195 || !strcmp(*(argv+1), "lock_critical")
1196 || !strcmp(*(argv+1), "get_unlock_ability")) {
1197 argc = do_oem_command(argc, argv);
1198 } else {
1199 usage();
1200 return 1;
1201 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001202 } else {
1203 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001204 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205 }
1206 }
1207
1208 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001209 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210 fb_queue_erase("userdata");
Elliott Hughesfc797672015-04-07 20:12:50 -07001211 fb_perform_format(usb, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001212
1213 std::string cache_type;
1214 if (fb_getvar(usb, "partition-type:cache", &cache_type) && !cache_type.empty()) {
1215 fprintf(stderr, "wiping cache...\n");
1216 fb_queue_erase("cache");
1217 fb_perform_format(usb, "cache", 1, nullptr, nullptr);
1218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219 }
1220 if (wants_reboot) {
1221 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001222 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223 } else if (wants_reboot_bootloader) {
1224 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001225 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226 }
1227
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001228 return fb_execute_queue(usb) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001229}