blob: afdfd0ebcdca24f6d51e50bb49bc519ad592bd39 [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>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Colin Crossf8387882012-05-24 17:18:41 -070046
Elliott Hughes2fd45a92015-10-30 11:49:47 -070047#include <base/parseint.h>
Elliott Hughes77c0e662015-11-02 15:51:12 -080048#include <base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070049#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070050#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070052#include <base/strings.h>
53#include <base/parseint.h>
54
Elliott Hughes253c18d2015-03-18 22:47:09 -070055#include "bootimg_utils.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070057#include "fs.h"
David Pursell0b156632015-10-30 11:22:01 -070058#include "transport.h"
59#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Colin Crossf8387882012-05-24 17:18:41 -070061#ifndef O_BINARY
62#define O_BINARY 0
63#endif
64
Rom Lemarchand622810c2013-06-28 09:54:59 -070065#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
66
Wink Savilleb98762f2011-04-04 17:54:59 -070067char cur_product[FB_RESPONSE_SZ + 1];
68
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069static const char *serial = 0;
70static const char *product = 0;
71static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070073static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070074static int64_t sparse_limit = -1;
75static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Elliott Hughesfc797672015-04-07 20:12:50 -070077static unsigned page_size = 2048;
78static unsigned base_addr = 0x10000000;
79static unsigned kernel_offset = 0x00008000;
80static unsigned ramdisk_offset = 0x01000000;
81static unsigned second_offset = 0x00f00000;
82static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080083
Paul Crowley8f7f56e2015-11-27 09:29:37 +000084static const std::string convert_fbe_marker_filename("convert_fbe");
85
Rom Lemarchand622810c2013-06-28 09:54:59 -070086enum fb_buffer_type {
87 FB_BUFFER,
88 FB_BUFFER_SPARSE,
89};
90
91struct fastboot_buffer {
92 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070093 void* data;
94 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070095};
96
97static struct {
98 char img_name[13];
99 char sig_name[13];
100 char part_name[9];
101 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700102} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103 {"boot.img", "boot.sig", "boot", false},
104 {"recovery.img", "recovery.sig", "recovery", true},
105 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700106 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700108
Elliott Hughesfc797672015-04-07 20:12:50 -0700109static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700111 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 char path[PATH_MAX + 128];
113
114 if(!strcmp(item,"boot")) {
115 fn = "boot.img";
116 } else if(!strcmp(item,"recovery")) {
117 fn = "recovery.img";
118 } else if(!strcmp(item,"system")) {
119 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700120 } else if(!strcmp(item,"vendor")) {
121 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122 } else if(!strcmp(item,"userdata")) {
123 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700124 } else if(!strcmp(item,"cache")) {
125 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 } else if(!strcmp(item,"info")) {
127 fn = "android-info.txt";
128 } else {
129 fprintf(stderr,"unknown partition '%s'\n", item);
130 return 0;
131 }
132
133 if(product) {
134 get_my_path(path);
135 sprintf(path + strlen(path),
136 "../../../target/product/%s/%s", product, fn);
137 return strdup(path);
138 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800139
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 dir = getenv("ANDROID_PRODUCT_OUT");
141 if((dir == 0) || (dir[0] == 0)) {
142 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
143 return 0;
144 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800145
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146 sprintf(path, "%s/%s", dir, fn);
147 return strdup(path);
148}
149
Elliott Hughesfc797672015-04-07 20:12:50 -0700150static int64_t get_file_size(int fd) {
151 struct stat sb;
152 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700153}
154
Elliott Hughesfc797672015-04-07 20:12:50 -0700155static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800156 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700157 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159 *sz = get_file_size(fd);
160 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161 goto oops;
162 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164 data = (char*) malloc(*sz);
165 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 close(fd);
169
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 return data;
171
172oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800173 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 close(fd);
175 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800176 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 return 0;
178}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
Elliott Hughesfc797672015-04-07 20:12:50 -0700180static void* load_file(const char* fn, int64_t* sz) {
181 int fd = open(fn, O_RDONLY | O_BINARY);
182 if (fd == -1) return nullptr;
183 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700184}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughesfc797672015-04-07 20:12:50 -0700186static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700187 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700188 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700189 return -1;
190 }
191
192 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
193 return -1;
194 }
195
Scott Anderson13081c62012-04-06 12:39:30 -0700196 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700198 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
199 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 return 0;
201}
202
Elliott Hughesfc797672015-04-07 20:12:50 -0700203static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800204 return match_fastboot_with_serial(info, serial);
205}
206
Elliott Hughesfc797672015-04-07 20:12:50 -0700207static int list_devices_callback(usb_ifc_info* info) {
208 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700209 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700210 if (!info->writable) {
211 serial = "no permissions"; // like "adb devices"
212 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213 if (!serial[0]) {
214 serial = "????????????";
215 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700216 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700217 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700218 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800219 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700220 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700221 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700222 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 }
225
226 return -1;
227}
228
David Pursell0b156632015-10-30 11:22:01 -0700229static Transport* open_device() {
230 static Transport* transport = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 int announce = 1;
232
David Pursell0b156632015-10-30 11:22:01 -0700233 if (transport) return transport;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800234
David Pursell0b156632015-10-30 11:22:01 -0700235 for (;;) {
236 transport = usb_open(match_fastboot);
237 if (transport) return transport;
238 if (announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800239 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700240 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700242 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
244}
245
Elliott Hughesfc797672015-04-07 20:12:50 -0700246static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 // We don't actually open a USB device here,
248 // just getting our callback called so we can
249 // list all the connected devices.
250 usb_open(list_devices_callback);
251}
252
Elliott Hughesfc797672015-04-07 20:12:50 -0700253static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 fprintf(stderr,
255/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
256 "usage: fastboot [ <option> ] <command>\n"
257 "\n"
258 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700259 " update <filename> Reflash device from update.zip.\n"
260 " flashall Flash boot, system, vendor, and --\n"
261 " if found -- recovery.\n"
262 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
263 " flashing lock Locks the device. Prevents flashing.\n"
264 " flashing unlock Unlocks the device. Allows flashing\n"
265 " any partition except\n"
266 " bootloader-related partitions.\n"
267 " flashing lock_critical Prevents flashing bootloader-related\n"
268 " partitions.\n"
269 " flashing unlock_critical Enables flashing bootloader-related\n"
270 " partitions.\n"
271 " flashing get_unlock_ability Queries bootloader to see if the\n"
272 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700273 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700274 " unlock nonce.\n"
275 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700276 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700277 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700278 " erase <partition> Erase a flash partition.\n"
279 " format[:[<fs type>][:[<size>]] <partition>\n"
280 " Format a flash partition. Can\n"
281 " override the fs type and/or size\n"
282 " the bootloader reports.\n"
283 " getvar <variable> Display a bootloader variable.\n"
284 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
285 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
286 " Create bootimage and flash it.\n"
287 " devices [-l] List all connected devices [with\n"
288 " device paths].\n"
289 " continue Continue with autoboot.\n"
290 " reboot [bootloader] Reboot device [into bootloader].\n"
291 " reboot-bootloader Reboot device into bootloader.\n"
292 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 "\n"
294 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700295 " -w Erase userdata and cache (and format\n"
296 " if supported by partition type).\n"
297 " -u Do not erase partition before\n"
298 " formatting.\n"
299 " -s <specific device> Specify device serial number\n"
300 " or path to device port.\n"
301 " -p <product> Specify product name.\n"
302 " -c <cmdline> Override kernel commandline.\n"
303 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800304 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700305 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800306 " --kernel-offset Specify a custom kernel offset.\n"
307 " (default: 0x00008000)\n"
308 " --ramdisk-offset Specify a custom ramdisk offset.\n"
309 " (default: 0x01000000)\n"
310 " --tags-offset Specify a custom tags offset.\n"
311 " (default: 0x00000100)\n"
312 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700313 " (default: 2048).\n"
314 " -S <size>[K|M|G] Automatically sparse files greater\n"
315 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700316 " --slot <suffix> Specify slot suffix to be used if the\n"
317 " device supports slots. This will be\n"
318 " added to all partition names that use\n"
319 " slots. 'all' can be given to refer\n"
320 " to all slots. If this is not given,\n"
321 " slotted partitions will default to\n"
322 " the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800323 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800324 " provided, this will default to the value\n"
325 " given by --slot. If slots are not\n"
326 " supported, this does nothing.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000327#if !defined(_WIN32)
328 " --wipe-and-use-fbe On devices which support it,\n"
329 " erase userdata and cache, and\n"
330 " enable file-based encryption\n"
331#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800332 " --unbuffered Do not buffer input or output.\n"
333 " --version Display version.\n"
334 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000337
Elliott Hughesfc797672015-04-07 20:12:50 -0700338static void* load_bootable_image(const char* kernel, const char* ramdisk,
339 const char* secondstage, int64_t* sz,
340 const char* cmdline) {
341 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 fprintf(stderr, "no image specified\n");
343 return 0;
344 }
345
Elliott Hughesfc797672015-04-07 20:12:50 -0700346 int64_t ksize;
347 void* kdata = load_file(kernel, &ksize);
348 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800349 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 return 0;
351 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800352
Elliott Hughesfc797672015-04-07 20:12:50 -0700353 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700355 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800356
Elliott Hughesfc797672015-04-07 20:12:50 -0700357 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
359 return 0;
360 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800361
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 *sz = ksize;
363 return kdata;
364 }
365
Elliott Hughesfc797672015-04-07 20:12:50 -0700366 void* rdata = nullptr;
367 int64_t rsize = 0;
368 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700370 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800371 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 return 0;
373 }
374 }
375
Elliott Hughesfc797672015-04-07 20:12:50 -0700376 void* sdata = nullptr;
377 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200378 if (secondstage) {
379 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700380 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200381 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
382 return 0;
383 }
384 }
385
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700387 int64_t bsize = 0;
388 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800389 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200390 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800391 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700392 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 fprintf(stderr,"failed to create boot.img\n");
394 return 0;
395 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700396 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
397 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800399
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400 return bdata;
401}
402
Elliott Hughesfc797672015-04-07 20:12:50 -0700403static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404{
Yusuke Sato07447542015-06-25 14:39:19 -0700405 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700406 ZipEntry zip_entry;
407 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700408 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000409 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
411
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700412 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700414 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700415 if (data == nullptr) {
416 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000417 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 }
419
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700420 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
421 if (error != 0) {
422 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000424 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000426
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 return data;
428}
429
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700430#if defined(_WIN32)
431
432// TODO: move this to somewhere it can be shared.
433
434#include <windows.h>
435
436// Windows' tmpfile(3) requires administrator rights because
437// it creates temporary files in the root directory.
438static FILE* win32_tmpfile() {
439 char temp_path[PATH_MAX];
440 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
441 if (nchars == 0 || nchars >= sizeof(temp_path)) {
442 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
443 return nullptr;
444 }
445
446 char filename[PATH_MAX];
447 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
448 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
449 return nullptr;
450 }
451
452 return fopen(filename, "w+bTD");
453}
454
455#define tmpfile win32_tmpfile
456
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000457static std::string make_temporary_directory() {
458 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
459 return "";
460}
461
462#else
463
464static std::string make_temporary_directory() {
465 const char *tmpdir = getenv("TMPDIR");
466 if (tmpdir == nullptr) {
467 tmpdir = P_tmpdir;
468 }
469 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
470 if (mkdtemp(&result[0]) == NULL) {
471 fprintf(stderr, "Unable to create temporary directory: %s\n",
472 strerror(errno));
473 return "";
474 }
475 return result;
476}
477
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700478#endif
479
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000480static std::string create_fbemarker_tmpdir() {
481 std::string dir = make_temporary_directory();
482 if (dir.empty()) {
483 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
484 return "";
485 }
486 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
487 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
488 if (fd == -1) {
489 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
490 marker_file.c_str(), errno, strerror(errno));
491 return "";
492 }
493 close(fd);
494 return dir;
495}
496
497static void delete_fbemarker_tmpdir(const std::string& dir) {
498 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
499 if (unlink(marker_file.c_str()) == -1) {
500 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
501 marker_file.c_str(), errno, strerror(errno));
502 return;
503 }
504 if (rmdir(dir.c_str()) == -1) {
505 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
506 dir.c_str(), errno, strerror(errno));
507 return;
508 }
509}
510
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700511static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
512 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700513 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700514 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
515 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700516 return -1;
517 }
518
Yusuke Sato07447542015-06-25 14:39:19 -0700519 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700520 ZipEntry zip_entry;
521 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
522 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000523 return -1;
524 }
525
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700526 int fd = fileno(fp);
527 int error = ExtractEntryToFile(zip, &zip_entry, fd);
528 if (error != 0) {
529 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
530 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700531 }
532
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000533 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700534 return fd;
535}
536
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537static char *strip(char *s)
538{
539 int n;
540 while(*s && isspace(*s)) s++;
541 n = strlen(s);
542 while(n-- > 0) {
543 if(!isspace(s[n])) break;
544 s[n] = 0;
545 }
546 return s;
547}
548
549#define MAX_OPTIONS 32
550static int setup_requirement_line(char *name)
551{
552 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700553 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800554 unsigned n, count;
555 char *x;
556 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800557
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 if (!strncmp(name, "reject ", 7)) {
559 name += 7;
560 invert = 1;
561 } else if (!strncmp(name, "require ", 8)) {
562 name += 8;
563 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700564 } else if (!strncmp(name, "require-for-product:", 20)) {
565 // Get the product and point name past it
566 prod = name + 20;
567 name = strchr(name, ' ');
568 if (!name) return -1;
569 *name = 0;
570 name += 1;
571 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800572 }
573
574 x = strchr(name, '=');
575 if (x == 0) return 0;
576 *x = 0;
577 val[0] = x + 1;
578
579 for(count = 1; count < MAX_OPTIONS; count++) {
580 x = strchr(val[count - 1],'|');
581 if (x == 0) break;
582 *x = 0;
583 val[count] = x + 1;
584 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800585
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800586 name = strip(name);
587 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800588
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 name = strip(name);
590 if (name == 0) return -1;
591
Elliott Hughes253c18d2015-03-18 22:47:09 -0700592 const char* var = name;
593 // Work around an unfortunate name mismatch.
594 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800595
Elliott Hughes253c18d2015-03-18 22:47:09 -0700596 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 if (out == 0) return -1;
598
599 for(n = 0; n < count; n++) {
600 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700601 if (out[n] == 0) {
602 for(size_t i = 0; i < n; ++i) {
603 free((char*) out[i]);
604 }
605 free(out);
606 return -1;
607 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800608 }
609
Elliott Hughes253c18d2015-03-18 22:47:09 -0700610 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 return 0;
612}
613
Elliott Hughesfc797672015-04-07 20:12:50 -0700614static void setup_requirements(char* data, int64_t sz) {
615 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700617 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 *s++ = 0;
619 if (setup_requirement_line(data)) {
620 die("out of memory");
621 }
622 data = s;
623 } else {
624 s++;
625 }
626 }
627}
628
Elliott Hughesfc797672015-04-07 20:12:50 -0700629static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 fb_queue_notice("--------------------------------------------");
631 fb_queue_display("version-bootloader", "Bootloader Version...");
632 fb_queue_display("version-baseband", "Baseband Version.....");
633 fb_queue_display("serialno", "Serial Number........");
634 fb_queue_notice("--------------------------------------------");
635}
636
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700638{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700639 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700640 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700641 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700642 }
643
Elliott Hughesfc797672015-04-07 20:12:50 -0700644 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700645 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700646 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700647 }
648
Elliott Hughes253c18d2015-03-18 22:47:09 -0700649 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700650 if (!out_s) {
651 die("Failed to allocate sparse file array\n");
652 }
653
654 files = sparse_file_resparse(s, max_size, out_s, files);
655 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700656 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700657 }
658
659 return out_s;
660}
661
David Pursell0b156632015-10-30 11:22:01 -0700662static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700663 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700664 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
665 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800666 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700667 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700668 }
669
Elliott Hughes77c0e662015-11-02 15:51:12 -0800670 // Some bootloaders (angler, for example) send spurious whitespace too.
671 max_download_size = android::base::Trim(max_download_size);
672
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700673 uint64_t limit;
674 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800675 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700676 return 0;
677 }
678 if (limit > 0) {
679 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
680 }
Colin Crossf8387882012-05-24 17:18:41 -0700681 return limit;
682}
683
David Pursell0b156632015-10-30 11:22:01 -0700684static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700685 int64_t limit;
686
687 if (sparse_limit == 0) {
688 return 0;
689 } else if (sparse_limit > 0) {
690 limit = sparse_limit;
691 } else {
692 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700693 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700694 }
695 if (target_sparse_limit > 0) {
696 limit = target_sparse_limit;
697 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700698 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700699 }
700 }
701
702 if (size > limit) {
703 return limit;
704 }
705
706 return 0;
707}
708
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700709// Until we get lazy inode table init working in make_ext4fs, we need to
710// erase partitions of type ext4 before flashing a filesystem so no stale
711// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700712static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800713 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700714 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800715 return false;
716 }
717 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700718}
719
David Pursell0b156632015-10-30 11:22:01 -0700720static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700721 int64_t sz = get_file_size(fd);
722 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000723 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700724 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700725
Elliott Hughesfc797672015-04-07 20:12:50 -0700726 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700727 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700728 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700729 sparse_file** s = load_sparse_files(fd, limit);
730 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700731 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700732 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700733 buf->type = FB_BUFFER_SPARSE;
734 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700735 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700736 void* data = load_fd(fd, &sz);
737 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700738 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700739 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000740 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700741 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742
743 return 0;
744}
745
David Pursell0b156632015-10-30 11:22:01 -0700746static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700747{
748 int fd;
749
750 fd = open(fname, O_RDONLY | O_BINARY);
751 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700752 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700753 }
754
David Pursell0b156632015-10-30 11:22:01 -0700755 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700756}
757
758static void flash_buf(const char *pname, struct fastboot_buffer *buf)
759{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700760 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700761
762 switch (buf->type) {
763 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700764 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700765 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700766 int64_t sz = sparse_file_len(*s, true, false);
767 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700768 }
769 break;
770 case FB_BUFFER:
771 fb_queue_flash(pname, buf->data, buf->sz);
772 break;
773 default:
774 die("unknown buffer type: %d", buf->type);
775 }
776}
777
David Pursell0b156632015-10-30 11:22:01 -0700778static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700779 std::vector<std::string> suffixes;
780 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700781 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700782 die("Could not get suffixes.\n");
783 }
784 return android::base::Split(suffix_list, ",");
785}
786
David Pursell0b156632015-10-30 11:22:01 -0700787static std::string verify_slot(Transport* transport, const char *slot) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700788 if (strcmp(slot, "all") == 0) {
789 return "all";
790 }
David Pursell0b156632015-10-30 11:22:01 -0700791 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700792 for (const std::string &suffix : suffixes) {
793 if (suffix == slot)
794 return slot;
795 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800796 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700797 for (const std::string &suffix : suffixes) {
798 fprintf(stderr, "%s\n", suffix.c_str());
799 }
800 exit(1);
801}
802
David Pursell0b156632015-10-30 11:22:01 -0700803static void do_for_partition(Transport* transport, const char *part, const char *slot,
804 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800805 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700806 std::string current_slot;
807
David Pursell0b156632015-10-30 11:22:01 -0700808 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800809 /* If has-slot is not supported, the answer is no. */
810 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700811 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800812 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700813 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700814 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700815 die("Failed to identify current slot.\n");
816 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800817 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700818 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800819 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700820 }
821 } else {
822 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700823 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
824 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700825 }
826 func(part);
827 }
828}
829
David Pursell0b156632015-10-30 11:22:01 -0700830/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
831 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
832 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
833 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700834 */
David Pursell0b156632015-10-30 11:22:01 -0700835static void do_for_partitions(Transport* transport, const char *part, const char *slot,
836 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800837 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700838
839 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700840 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700841 die("Could not check if partition %s has slot.", part);
842 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800843 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700844 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700845 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700846 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700847 }
848 } else {
David Pursell0b156632015-10-30 11:22:01 -0700849 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700850 }
851 } else {
David Pursell0b156632015-10-30 11:22:01 -0700852 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700853 }
854}
855
David Pursell0b156632015-10-30 11:22:01 -0700856static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700857 struct fastboot_buffer buf;
858
David Pursell0b156632015-10-30 11:22:01 -0700859 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860 die("cannot load '%s'", fname);
861 }
862 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700863}
864
Elliott Hughesfc797672015-04-07 20:12:50 -0700865static void do_update_signature(ZipArchiveHandle zip, char* fn) {
866 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700867 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700868 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800869 fb_queue_download("signature", data, sz);
870 fb_queue_command("signature", "installing signature");
871}
872
David Pursell0b156632015-10-30 11:22:01 -0700873static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800874 queue_info_dump();
875
Wink Savilleb98762f2011-04-04 17:54:59 -0700876 fb_queue_query_save("product", cur_product, sizeof(cur_product));
877
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700878 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700879 int error = OpenArchive(filename, &zip);
880 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100881 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700882 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700883 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800884
Elliott Hughesfc797672015-04-07 20:12:50 -0700885 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700886 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700887 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100888 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700889 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800890 }
891
Elliott Hughes253c18d2015-03-18 22:47:09 -0700892 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800893
Elliott Hughesacdbe922015-06-02 21:37:05 -0700894 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700895 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700896 if (fd == -1) {
897 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700898 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700899 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100900 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700901 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700902 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700903 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700904 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700905 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700906
907 auto update = [&](const std::string &partition) {
908 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700909 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700910 fb_queue_erase(partition.c_str());
911 }
912 flash_buf(partition.c_str(), &buf);
913 /* not closing the fd here since the sparse code keeps the fd around
914 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
915 * program exits.
916 */
917 };
David Pursell0b156632015-10-30 11:22:01 -0700918 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800919 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700920
921 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922}
923
Elliott Hughesfc797672015-04-07 20:12:50 -0700924static void do_send_signature(char* fn) {
925 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800926 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700927
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800928 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800929
Elliott Hughesfc797672015-04-07 20:12:50 -0700930 strcpy(xtn, ".sig");
931
932 int64_t sz;
933 void* data = load_file(fn, &sz);
934 strcpy(xtn, ".img");
935 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800936 fb_queue_download("signature", data, sz);
937 fb_queue_command("signature", "installing signature");
938}
939
David Pursell0b156632015-10-30 11:22:01 -0700940static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800941 queue_info_dump();
942
Wink Savilleb98762f2011-04-04 17:54:59 -0700943 fb_queue_query_save("product", cur_product, sizeof(cur_product));
944
Elliott Hughes253c18d2015-03-18 22:47:09 -0700945 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700946 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800947
Elliott Hughesfc797672015-04-07 20:12:50 -0700948 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700949 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700950 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700951
952 setup_requirements(reinterpret_cast<char*>(data), sz);
953
954 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700955 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700956 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700957 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700958 if (images[i].is_optional)
959 continue;
960 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700961 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962
963 auto flashall = [&](const std::string &partition) {
964 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -0700965 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966 fb_queue_erase(partition.c_str());
967 }
968 flash_buf(partition.c_str(), &buf);
969 };
David Pursell0b156632015-10-30 11:22:01 -0700970 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800971 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800972}
973
974#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800975#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800976
Elliott Hughes45be42e2015-09-02 20:15:48 -0700977static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700978{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700979 if (argc <= 2) return 0;
980 skip(2);
981
982 /*
983 * Process unlock_bootloader, we have to load the message file
984 * and send that to the remote device.
985 */
986 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700987
988 int64_t sz;
989 void* data = load_file(*argv, &sz);
990 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700991 fb_queue_download("unlock_message", data, sz);
992 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
993 skip(1);
994 return 0;
995}
996
Elliott Hughes45be42e2015-09-02 20:15:48 -0700997static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800998{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800999 char command[256];
1000 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001001
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001002 command[0] = 0;
1003 while(1) {
1004 strcat(command,*argv);
1005 skip(1);
1006 if(argc == 0) break;
1007 strcat(command," ");
1008 }
1009
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001010 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001011 return 0;
1012}
1013
Colin Crossf8387882012-05-24 17:18:41 -07001014static int64_t parse_num(const char *arg)
1015{
1016 char *endptr;
1017 unsigned long long num;
1018
1019 num = strtoull(arg, &endptr, 0);
1020 if (endptr == arg) {
1021 return -1;
1022 }
1023
1024 if (*endptr == 'k' || *endptr == 'K') {
1025 if (num >= (-1ULL) / 1024) {
1026 return -1;
1027 }
1028 num *= 1024LL;
1029 endptr++;
1030 } else if (*endptr == 'm' || *endptr == 'M') {
1031 if (num >= (-1ULL) / (1024 * 1024)) {
1032 return -1;
1033 }
1034 num *= 1024LL * 1024LL;
1035 endptr++;
1036 } else if (*endptr == 'g' || *endptr == 'G') {
1037 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1038 return -1;
1039 }
1040 num *= 1024LL * 1024LL * 1024LL;
1041 endptr++;
1042 }
1043
1044 if (*endptr != '\0') {
1045 return -1;
1046 }
1047
1048 if (num > INT64_MAX) {
1049 return -1;
1050 }
1051
1052 return num;
1053}
1054
David Pursell0b156632015-10-30 11:22:01 -07001055static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001056 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001057 const char* type_override, const char* size_override,
1058 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001059 std::string partition_type, partition_size;
1060
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001061 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001062 const char* errMsg = nullptr;
1063 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001064 int fd;
1065
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001066 unsigned int limit = INT_MAX;
1067 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001068 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001069 }
1070 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001071 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001072 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001073
David Pursell0b156632015-10-30 11:22:01 -07001074 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001075 errMsg = "Can't determine partition type.\n";
1076 goto failed;
1077 }
JP Abgrall7e859742014-05-06 15:14:15 -07001078 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001079 if (partition_type != type_override) {
1080 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1081 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001082 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001083 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001084 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001085
David Pursell0b156632015-10-30 11:22:01 -07001086 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001087 errMsg = "Unable to get partition size\n";
1088 goto failed;
1089 }
JP Abgrall7e859742014-05-06 15:14:15 -07001090 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001091 if (partition_size != size_override) {
1092 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1093 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001094 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001095 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001096 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001097 // Some bootloaders (angler, for example), send spurious leading whitespace.
1098 partition_size = android::base::Trim(partition_size);
1099 // Some bootloaders (hammerhead, for example) use implicit hex.
1100 // This code used to use strtol with base 16.
1101 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001102
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001103 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001104 if (!gen) {
1105 if (skip_if_not_supported) {
1106 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001107 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001108 return;
1109 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001110 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1111 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001112 return;
1113 }
1114
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001115 int64_t size;
1116 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1117 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1118 return;
1119 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001120
1121 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001122 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001123 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001124 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001125 return;
1126 }
1127
David Pursell0b156632015-10-30 11:22:01 -07001128 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001129 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001130 close(fd);
1131 return;
1132 }
1133 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001134 return;
1135
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001136failed:
1137 if (skip_if_not_supported) {
1138 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001139 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001140 }
1141 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1142}
1143
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001144int main(int argc, char **argv)
1145{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001146 bool wants_wipe = false;
1147 bool wants_reboot = false;
1148 bool wants_reboot_bootloader = false;
1149 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001150 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001151 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001152 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001153 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001154 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001155 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001156 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157
JP Abgrall7b8970c2013-03-07 17:06:41 -08001158 const struct option longopts[] = {
1159 {"base", required_argument, 0, 'b'},
1160 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001161 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001162 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001163 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001164 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001165 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001166 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001167 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001168 {"help", no_argument, 0, 'h'},
1169 {"unbuffered", no_argument, 0, 0},
1170 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001171 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001172 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001173 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001174#if !defined(_WIN32)
1175 {"wipe-and-use-fbe", no_argument, 0, 0},
1176#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001177 {0, 0, 0, 0}
1178 };
Colin Cross8879f982012-05-22 17:53:34 -07001179
1180 serial = getenv("ANDROID_SERIAL");
1181
1182 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001183 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001184 if (c < 0) {
1185 break;
1186 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001187 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001188 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001189 case 'a':
1190 wants_set_active = true;
1191 if (optarg)
1192 next_active = optarg;
1193 break;
Colin Cross8879f982012-05-22 17:53:34 -07001194 case 'b':
1195 base_addr = strtoul(optarg, 0, 16);
1196 break;
Colin Cross8879f982012-05-22 17:53:34 -07001197 case 'c':
1198 cmdline = optarg;
1199 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001200 case 'h':
1201 usage();
1202 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001203 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001204 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001205 unsigned long val;
1206
1207 val = strtoul(optarg, &endptr, 0);
1208 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1209 die("invalid vendor id '%s'", optarg);
1210 vendor_id = (unsigned short)val;
1211 break;
1212 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001213 case 'k':
1214 kernel_offset = strtoul(optarg, 0, 16);
1215 break;
1216 case 'l':
1217 long_listing = 1;
1218 break;
1219 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001220 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001221 if (!page_size) die("invalid page size");
1222 break;
1223 case 'p':
1224 product = optarg;
1225 break;
1226 case 'r':
1227 ramdisk_offset = strtoul(optarg, 0, 16);
1228 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001229 case 't':
1230 tags_offset = strtoul(optarg, 0, 16);
1231 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001232 case 's':
1233 serial = optarg;
1234 break;
1235 case 'S':
1236 sparse_limit = parse_num(optarg);
1237 if (sparse_limit < 0) {
1238 die("invalid sparse limit");
1239 }
1240 break;
1241 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001242 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001243 break;
1244 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001245 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001246 break;
Colin Cross8879f982012-05-22 17:53:34 -07001247 case '?':
1248 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001249 case 0:
1250 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001251 setvbuf(stdout, nullptr, _IONBF, 0);
1252 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001253 } else if (strcmp("version", longopts[longindex].name) == 0) {
1254 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1255 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001256 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1257 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001258#if !defined(_WIN32)
1259 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1260 wants_wipe = true;
1261 set_fbe_marker = true;
1262#endif
1263 } else {
1264 fprintf(stderr, "Internal error in options processing for %s\n",
1265 longopts[longindex].name);
1266 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001267 }
1268 break;
Colin Cross8879f982012-05-22 17:53:34 -07001269 default:
1270 abort();
1271 }
1272 }
1273
1274 argc -= optind;
1275 argv += optind;
1276
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001277 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001278 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001279 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001280 }
1281
Colin Cross8fb6e062012-07-24 16:36:41 -07001282 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001283 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001285 return 0;
1286 }
1287
Colin Crossc7b75dc2012-08-29 18:17:06 -07001288 if (argc > 0 && !strcmp(*argv, "help")) {
1289 usage();
1290 return 0;
1291 }
1292
David Pursell0b156632015-10-30 11:22:01 -07001293 Transport* transport = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001294 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001295 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001296 if (next_active != "")
David Pursell0b156632015-10-30 11:22:01 -07001297 next_active = verify_slot(transport, next_active.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001298
1299 if (wants_set_active) {
1300 if (next_active == "") {
1301 if (slot_override == "") {
1302 wants_set_active = false;
1303 } else {
1304 next_active = slot_override;
1305 }
1306 }
1307 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001308
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001309 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001310 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001311 require(2);
1312 fb_queue_display(argv[1], argv[1]);
1313 skip(2);
1314 } else if(!strcmp(*argv, "erase")) {
1315 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001316
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001317 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001318 std::string partition_type;
1319 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001320 fs_get_generator(partition_type) != nullptr) {
1321 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1322 partition_type.c_str());
1323 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001324
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001325 fb_queue_erase(partition.c_str());
1326 };
David Pursell0b156632015-10-30 11:22:01 -07001327 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001328 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001329 } else if(!strncmp(*argv, "format", strlen("format"))) {
1330 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001331 char *type_override = nullptr;
1332 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001333 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001334 /*
1335 * Parsing for: "format[:[type][:[size]]]"
1336 * Some valid things:
1337 * - select ontly the size, and leave default fs type:
1338 * format::0x4000000 userdata
1339 * - default fs type and size:
1340 * format userdata
1341 * format:: userdata
1342 */
1343 overrides = strchr(*argv, ':');
1344 if (overrides) {
1345 overrides++;
1346 size_override = strchr(overrides, ':');
1347 if (size_override) {
1348 size_override[0] = '\0';
1349 size_override++;
1350 }
1351 type_override = overrides;
1352 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001353 if (type_override && !type_override[0]) type_override = nullptr;
1354 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001355
1356 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001357 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001358 fb_queue_erase(partition.c_str());
1359 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001360 fb_perform_format(transport, partition.c_str(), 0,
1361 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001362 };
David Pursell0b156632015-10-30 11:22:01 -07001363 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001364 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001365 } else if(!strcmp(*argv, "signature")) {
1366 require(2);
1367 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001368 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001369 if (sz != 256) die("signature must be 256 bytes");
1370 fb_queue_download("signature", data, sz);
1371 fb_queue_command("signature", "installing signature");
1372 skip(2);
1373 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001374 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001375 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001376 if (argc > 0) {
1377 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001378 wants_reboot = false;
1379 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001380 skip(1);
1381 }
1382 }
1383 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001384 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001385 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001386 skip(1);
1387 } else if (!strcmp(*argv, "continue")) {
1388 fb_queue_command("continue", "resuming boot");
1389 skip(1);
1390 } else if(!strcmp(*argv, "boot")) {
1391 char *kname = 0;
1392 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001393 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001394 skip(1);
1395 if (argc > 0) {
1396 kname = argv[0];
1397 skip(1);
1398 }
1399 if (argc > 0) {
1400 rname = argv[0];
1401 skip(1);
1402 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001403 if (argc > 0) {
1404 sname = argv[0];
1405 skip(1);
1406 }
1407 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001408 if (data == 0) return 1;
1409 fb_queue_download("boot.img", data, sz);
1410 fb_queue_command("boot", "booting");
1411 } else if(!strcmp(*argv, "flash")) {
1412 char *pname = argv[1];
1413 char *fname = 0;
1414 require(2);
1415 if (argc > 2) {
1416 fname = argv[2];
1417 skip(3);
1418 } else {
1419 fname = find_item(pname, product);
1420 skip(2);
1421 }
1422 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001423
1424 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001425 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001426 fb_queue_erase(partition.c_str());
1427 }
David Pursell0b156632015-10-30 11:22:01 -07001428 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001429 };
David Pursell0b156632015-10-30 11:22:01 -07001430 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001431 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001432 char *kname = argv[2];
1433 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001434 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001435 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001436 skip(3);
1437 if (argc > 0) {
1438 rname = argv[0];
1439 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001440 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001441 if (argc > 0) {
1442 sname = argv[0];
1443 skip(1);
1444 }
1445 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001446 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001447 auto flashraw = [&](const std::string &partition) {
1448 fb_queue_flash(partition.c_str(), data, sz);
1449 };
David Pursell0b156632015-10-30 11:22:01 -07001450 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001451 } else if(!strcmp(*argv, "flashall")) {
1452 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001453 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001454 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001455 } else if(!strcmp(*argv, "update")) {
1456 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001457 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001458 skip(2);
1459 } else {
David Pursell0b156632015-10-30 11:22:01 -07001460 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 skip(1);
1462 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001463 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001464 } else if(!strcmp(*argv, "oem")) {
1465 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001466 } else if(!strcmp(*argv, "flashing")) {
1467 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1468 !strcmp(*(argv+1), "lock") ||
1469 !strcmp(*(argv+1), "unlock_critical") ||
1470 !strcmp(*(argv+1), "lock_critical") ||
1471 !strcmp(*(argv+1), "get_unlock_ability") ||
1472 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1473 !strcmp(*(argv+1), "lock_bootloader"))) {
1474 argc = do_oem_command(argc, argv);
1475 } else
1476 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1477 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001478 } else {
1479 usage();
1480 return 1;
1481 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001482 } else {
1483 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001484 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001485 }
1486 }
1487
1488 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001489 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001490 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001491 if (set_fbe_marker) {
1492 fprintf(stderr, "setting FBE marker...\n");
1493 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1494 if (initial_userdata_dir.empty()) {
1495 return 1;
1496 }
1497 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1498 delete_fbemarker_tmpdir(initial_userdata_dir);
1499 } else {
1500 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1501 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001502
1503 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001504 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001505 fprintf(stderr, "wiping cache...\n");
1506 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001507 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001508 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001509 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001510 if (wants_set_active) {
1511 fb_set_active(next_active.c_str());
1512 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001513 if (wants_reboot) {
1514 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001515 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001516 } else if (wants_reboot_bootloader) {
1517 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001518 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001519 }
1520
David Pursell0b156632015-10-30 11:22:01 -07001521 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522}