blob: 4573da0529530be257d80f87915fc005d080537d [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>
Josh Gao9da9ac52016-01-19 14:50:18 -080046#include <utility>
47#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070048
Elliott Hughes4f713192015-12-04 22:00:26 -080049#include <android-base/parseint.h>
50#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070051#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070052#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/strings.h>
55#include <android-base/parseint.h>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070056
Elliott Hughes253c18d2015-03-18 22:47:09 -070057#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080058#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070060#include "fs.h"
David Pursell0b156632015-10-30 11:22:01 -070061#include "transport.h"
62#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Colin Crossf8387882012-05-24 17:18:41 -070064#ifndef O_BINARY
65#define O_BINARY 0
66#endif
67
Rom Lemarchand622810c2013-06-28 09:54:59 -070068#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a)))
69
Wink Savilleb98762f2011-04-04 17:54:59 -070070char cur_product[FB_RESPONSE_SZ + 1];
71
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072static const char *serial = 0;
73static const char *product = 0;
74static const char *cmdline = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070076static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070077static int64_t sparse_limit = -1;
78static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
Elliott Hughesfc797672015-04-07 20:12:50 -070080static unsigned page_size = 2048;
81static unsigned base_addr = 0x10000000;
82static unsigned kernel_offset = 0x00008000;
83static unsigned ramdisk_offset = 0x01000000;
84static unsigned second_offset = 0x00f00000;
85static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080086
Rom Lemarchand622810c2013-06-28 09:54:59 -070087enum fb_buffer_type {
88 FB_BUFFER,
89 FB_BUFFER_SPARSE,
90};
91
92struct fastboot_buffer {
93 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070094 void* data;
95 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070096};
97
98static struct {
99 char img_name[13];
100 char sig_name[13];
101 char part_name[9];
102 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700103} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 {"boot.img", "boot.sig", "boot", false},
105 {"recovery.img", "recovery.sig", "recovery", true},
106 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700107 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700109
Elliott Hughesfc797672015-04-07 20:12:50 -0700110static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700112 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113 char path[PATH_MAX + 128];
114
115 if(!strcmp(item,"boot")) {
116 fn = "boot.img";
117 } else if(!strcmp(item,"recovery")) {
118 fn = "recovery.img";
119 } else if(!strcmp(item,"system")) {
120 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700121 } else if(!strcmp(item,"vendor")) {
122 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 } else if(!strcmp(item,"userdata")) {
124 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700125 } else if(!strcmp(item,"cache")) {
126 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 } else if(!strcmp(item,"info")) {
128 fn = "android-info.txt";
129 } else {
130 fprintf(stderr,"unknown partition '%s'\n", item);
131 return 0;
132 }
133
134 if(product) {
135 get_my_path(path);
136 sprintf(path + strlen(path),
137 "../../../target/product/%s/%s", product, fn);
138 return strdup(path);
139 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800140
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 dir = getenv("ANDROID_PRODUCT_OUT");
142 if((dir == 0) || (dir[0] == 0)) {
143 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
144 return 0;
145 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800146
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 sprintf(path, "%s/%s", dir, fn);
148 return strdup(path);
149}
150
Elliott Hughesfc797672015-04-07 20:12:50 -0700151static int64_t get_file_size(int fd) {
152 struct stat sb;
153 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700154}
155
Elliott Hughesfc797672015-04-07 20:12:50 -0700156static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800157 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700158 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
Elliott Hughesfc797672015-04-07 20:12:50 -0700160 *sz = get_file_size(fd);
161 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700162 goto oops;
163 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 data = (char*) malloc(*sz);
166 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 close(fd);
170
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 return data;
172
173oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800174 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 close(fd);
176 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800177 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 return 0;
179}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181static void* load_file(const char* fn, int64_t* sz) {
182 int fd = open(fn, O_RDONLY | O_BINARY);
183 if (fd == -1) return nullptr;
184 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700185}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Elliott Hughesfc797672015-04-07 20:12:50 -0700187static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700188 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700189 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700190 return -1;
191 }
192
193 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
194 return -1;
195 }
196
Scott Anderson13081c62012-04-06 12:39:30 -0700197 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700199 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
200 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 return 0;
202}
203
Elliott Hughesfc797672015-04-07 20:12:50 -0700204static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800205 return match_fastboot_with_serial(info, serial);
206}
207
Elliott Hughesfc797672015-04-07 20:12:50 -0700208static int list_devices_callback(usb_ifc_info* info) {
209 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800210 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700211 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800212 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700213 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800214 if (!serial[0]) {
215 serial = "????????????";
216 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700217 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700218 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700220 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 printf("%-22s fastboot", serial.c_str());
222 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700223 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 }
226
227 return -1;
228}
229
David Pursell0b156632015-10-30 11:22:01 -0700230static Transport* open_device() {
231 static Transport* transport = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 int announce = 1;
233
David Pursell0b156632015-10-30 11:22:01 -0700234 if (transport) return transport;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800235
David Pursell0b156632015-10-30 11:22:01 -0700236 for (;;) {
237 transport = usb_open(match_fastboot);
238 if (transport) return transport;
239 if (announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800240 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700241 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700243 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 }
245}
246
Elliott Hughesfc797672015-04-07 20:12:50 -0700247static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 // We don't actually open a USB device here,
249 // just getting our callback called so we can
250 // list all the connected devices.
251 usb_open(list_devices_callback);
252}
253
Elliott Hughesfc797672015-04-07 20:12:50 -0700254static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800255 fprintf(stderr,
256/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
257 "usage: fastboot [ <option> ] <command>\n"
258 "\n"
259 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700260 " update <filename> Reflash device from update.zip.\n"
261 " flashall Flash boot, system, vendor, and --\n"
262 " if found -- recovery.\n"
263 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
264 " flashing lock Locks the device. Prevents flashing.\n"
265 " flashing unlock Unlocks the device. Allows flashing\n"
266 " any partition except\n"
267 " bootloader-related partitions.\n"
268 " flashing lock_critical Prevents flashing bootloader-related\n"
269 " partitions.\n"
270 " flashing unlock_critical Enables flashing bootloader-related\n"
271 " partitions.\n"
272 " flashing get_unlock_ability Queries bootloader to see if the\n"
273 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700274 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700275 " unlock nonce.\n"
276 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700277 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700278 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700279 " erase <partition> Erase a flash partition.\n"
280 " format[:[<fs type>][:[<size>]] <partition>\n"
281 " Format a flash partition. Can\n"
282 " override the fs type and/or size\n"
283 " the bootloader reports.\n"
284 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800285 " set_active <suffix> Sets the active slot. If slots are\n"
286 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700287 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
288 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
289 " Create bootimage and flash it.\n"
290 " devices [-l] List all connected devices [with\n"
291 " device paths].\n"
292 " continue Continue with autoboot.\n"
293 " reboot [bootloader] Reboot device [into bootloader].\n"
294 " reboot-bootloader Reboot device into bootloader.\n"
295 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296 "\n"
297 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700298 " -w Erase userdata and cache (and format\n"
299 " if supported by partition type).\n"
300 " -u Do not erase partition before\n"
301 " formatting.\n"
302 " -s <specific device> Specify device serial number\n"
303 " or path to device port.\n"
304 " -p <product> Specify product name.\n"
305 " -c <cmdline> Override kernel commandline.\n"
306 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800307 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700308 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800309 " --kernel-offset Specify a custom kernel offset.\n"
310 " (default: 0x00008000)\n"
311 " --ramdisk-offset Specify a custom ramdisk offset.\n"
312 " (default: 0x01000000)\n"
313 " --tags-offset Specify a custom tags offset.\n"
314 " (default: 0x00000100)\n"
315 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700316 " (default: 2048).\n"
317 " -S <size>[K|M|G] Automatically sparse files greater\n"
318 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700319 " --slot <suffix> Specify slot suffix to be used if the\n"
320 " device supports slots. This will be\n"
321 " added to all partition names that use\n"
322 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800323 " to all slots. 'other' can be given to\n"
324 " refer to a non-current slot. If this\n"
325 " flag is not used, slotted partitions\n"
326 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800327 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800328 " provided, this will default to the value\n"
329 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800330 " supported, this does nothing. This will\n"
331 " run after all non-reboot commands.\n"
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}
Elliott Hughesfc797672015-04-07 20:12:50 -0700337static void* load_bootable_image(const char* kernel, const char* ramdisk,
338 const char* secondstage, int64_t* sz,
339 const char* cmdline) {
340 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 fprintf(stderr, "no image specified\n");
342 return 0;
343 }
344
Elliott Hughesfc797672015-04-07 20:12:50 -0700345 int64_t ksize;
346 void* kdata = load_file(kernel, &ksize);
347 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800348 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800349 return 0;
350 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800351
Elliott Hughesfc797672015-04-07 20:12:50 -0700352 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700354 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800355
Elliott Hughesfc797672015-04-07 20:12:50 -0700356 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
358 return 0;
359 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 *sz = ksize;
362 return kdata;
363 }
364
Elliott Hughesfc797672015-04-07 20:12:50 -0700365 void* rdata = nullptr;
366 int64_t rsize = 0;
367 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700369 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800370 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 return 0;
372 }
373 }
374
Elliott Hughesfc797672015-04-07 20:12:50 -0700375 void* sdata = nullptr;
376 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200377 if (secondstage) {
378 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700379 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200380 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
381 return 0;
382 }
383 }
384
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700386 int64_t bsize = 0;
387 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800388 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200389 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800390 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700391 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 fprintf(stderr,"failed to create boot.img\n");
393 return 0;
394 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700395 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
396 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800398
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 return bdata;
400}
401
Elliott Hughesfc797672015-04-07 20:12:50 -0700402static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403{
Yusuke Sato07447542015-06-25 14:39:19 -0700404 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700405 ZipEntry zip_entry;
406 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700407 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000408 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 }
410
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700411 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700413 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700414 if (data == nullptr) {
415 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000416 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 }
418
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700419 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
420 if (error != 0) {
421 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000423 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000425
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 return data;
427}
428
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700429#if defined(_WIN32)
430
431// TODO: move this to somewhere it can be shared.
432
433#include <windows.h>
434
435// Windows' tmpfile(3) requires administrator rights because
436// it creates temporary files in the root directory.
437static FILE* win32_tmpfile() {
438 char temp_path[PATH_MAX];
439 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
440 if (nchars == 0 || nchars >= sizeof(temp_path)) {
441 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
442 return nullptr;
443 }
444
445 char filename[PATH_MAX];
446 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
447 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
448 return nullptr;
449 }
450
451 return fopen(filename, "w+bTD");
452}
453
454#define tmpfile win32_tmpfile
455
456#endif
457
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700458static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
459 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700460 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700461 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
462 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700463 return -1;
464 }
465
Yusuke Sato07447542015-06-25 14:39:19 -0700466 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700467 ZipEntry zip_entry;
468 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
469 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000470 return -1;
471 }
472
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700473 int fd = fileno(fp);
474 int error = ExtractEntryToFile(zip, &zip_entry, fd);
475 if (error != 0) {
476 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
477 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700478 }
479
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000480 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700481 return fd;
482}
483
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484static char *strip(char *s)
485{
486 int n;
487 while(*s && isspace(*s)) s++;
488 n = strlen(s);
489 while(n-- > 0) {
490 if(!isspace(s[n])) break;
491 s[n] = 0;
492 }
493 return s;
494}
495
496#define MAX_OPTIONS 32
497static int setup_requirement_line(char *name)
498{
499 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700500 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 unsigned n, count;
502 char *x;
503 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800504
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 if (!strncmp(name, "reject ", 7)) {
506 name += 7;
507 invert = 1;
508 } else if (!strncmp(name, "require ", 8)) {
509 name += 8;
510 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700511 } else if (!strncmp(name, "require-for-product:", 20)) {
512 // Get the product and point name past it
513 prod = name + 20;
514 name = strchr(name, ' ');
515 if (!name) return -1;
516 *name = 0;
517 name += 1;
518 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
520
521 x = strchr(name, '=');
522 if (x == 0) return 0;
523 *x = 0;
524 val[0] = x + 1;
525
526 for(count = 1; count < MAX_OPTIONS; count++) {
527 x = strchr(val[count - 1],'|');
528 if (x == 0) break;
529 *x = 0;
530 val[count] = x + 1;
531 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800532
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533 name = strip(name);
534 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800535
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800536 name = strip(name);
537 if (name == 0) return -1;
538
Elliott Hughes253c18d2015-03-18 22:47:09 -0700539 const char* var = name;
540 // Work around an unfortunate name mismatch.
541 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542
Elliott Hughes253c18d2015-03-18 22:47:09 -0700543 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 if (out == 0) return -1;
545
546 for(n = 0; n < count; n++) {
547 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700548 if (out[n] == 0) {
549 for(size_t i = 0; i < n; ++i) {
550 free((char*) out[i]);
551 }
552 free(out);
553 return -1;
554 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800555 }
556
Elliott Hughes253c18d2015-03-18 22:47:09 -0700557 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 return 0;
559}
560
Elliott Hughesfc797672015-04-07 20:12:50 -0700561static void setup_requirements(char* data, int64_t sz) {
562 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700564 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565 *s++ = 0;
566 if (setup_requirement_line(data)) {
567 die("out of memory");
568 }
569 data = s;
570 } else {
571 s++;
572 }
573 }
574}
575
Elliott Hughesfc797672015-04-07 20:12:50 -0700576static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 fb_queue_notice("--------------------------------------------");
578 fb_queue_display("version-bootloader", "Bootloader Version...");
579 fb_queue_display("version-baseband", "Baseband Version.....");
580 fb_queue_display("serialno", "Serial Number........");
581 fb_queue_notice("--------------------------------------------");
582}
583
Rom Lemarchand622810c2013-06-28 09:54:59 -0700584static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700585{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700586 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700587 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700588 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700589 }
590
Elliott Hughesfc797672015-04-07 20:12:50 -0700591 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700592 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700593 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700594 }
595
Elliott Hughes253c18d2015-03-18 22:47:09 -0700596 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700597 if (!out_s) {
598 die("Failed to allocate sparse file array\n");
599 }
600
601 files = sparse_file_resparse(s, max_size, out_s, files);
602 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700603 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700604 }
605
606 return out_s;
607}
608
David Pursell0b156632015-10-30 11:22:01 -0700609static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700610 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700611 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
612 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800613 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700614 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700615 }
616
Elliott Hughes77c0e662015-11-02 15:51:12 -0800617 // Some bootloaders (angler, for example) send spurious whitespace too.
618 max_download_size = android::base::Trim(max_download_size);
619
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700620 uint64_t limit;
621 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800622 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700623 return 0;
624 }
625 if (limit > 0) {
626 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
627 }
Colin Crossf8387882012-05-24 17:18:41 -0700628 return limit;
629}
630
David Pursell0b156632015-10-30 11:22:01 -0700631static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700632 int64_t limit;
633
634 if (sparse_limit == 0) {
635 return 0;
636 } else if (sparse_limit > 0) {
637 limit = sparse_limit;
638 } else {
639 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700640 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700641 }
642 if (target_sparse_limit > 0) {
643 limit = target_sparse_limit;
644 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700645 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700646 }
647 }
648
649 if (size > limit) {
650 return limit;
651 }
652
653 return 0;
654}
655
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700656// Until we get lazy inode table init working in make_ext4fs, we need to
657// erase partitions of type ext4 before flashing a filesystem so no stale
658// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700659static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800660 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700661 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800662 return false;
663 }
664 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700665}
666
David Pursell0b156632015-10-30 11:22:01 -0700667static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700668 int64_t sz = get_file_size(fd);
669 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000670 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700671 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700672
Elliott Hughesfc797672015-04-07 20:12:50 -0700673 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700674 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700675 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700676 sparse_file** s = load_sparse_files(fd, limit);
677 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700678 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700679 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700680 buf->type = FB_BUFFER_SPARSE;
681 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700682 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700683 void* data = load_fd(fd, &sz);
684 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700685 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700686 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000687 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700688 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700689
690 return 0;
691}
692
David Pursell0b156632015-10-30 11:22:01 -0700693static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700694{
695 int fd;
696
697 fd = open(fname, O_RDONLY | O_BINARY);
698 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700699 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700700 }
701
David Pursell0b156632015-10-30 11:22:01 -0700702 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700703}
704
705static void flash_buf(const char *pname, struct fastboot_buffer *buf)
706{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700707 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700708
709 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800710 case FB_BUFFER_SPARSE: {
711 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700712 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700713 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700714 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800715 sparse_files.emplace_back(*s, sz);
716 ++s;
717 }
718
719 for (size_t i = 0; i < sparse_files.size(); ++i) {
720 const auto& pair = sparse_files[i];
721 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700722 }
723 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800724 }
725
Rom Lemarchand622810c2013-06-28 09:54:59 -0700726 case FB_BUFFER:
727 fb_queue_flash(pname, buf->data, buf->sz);
728 break;
729 default:
730 die("unknown buffer type: %d", buf->type);
731 }
732}
733
David Pursell0b156632015-10-30 11:22:01 -0700734static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700735 std::vector<std::string> suffixes;
736 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700737 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700738 die("Could not get suffixes.\n");
739 }
740 return android::base::Split(suffix_list, ",");
741}
742
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800743static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700744 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800745 if (allow_all) {
746 return "all";
747 } else {
748 std::vector<std::string> suffixes = get_suffixes(transport);
749 if (!suffixes.empty()) {
750 return suffixes[0];
751 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800752 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800753 }
754 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700755 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800756
David Pursell0b156632015-10-30 11:22:01 -0700757 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800758
759 if (strcmp(slot, "other") == 0) {
760 std::string current_slot;
761 if (!fb_getvar(transport, "current-slot", &current_slot)) {
762 die("Failed to identify current slot.");
763 }
764 if (!suffixes.empty()) {
765 for (size_t i = 0; i < suffixes.size(); i++) {
766 if (current_slot == suffixes[i])
767 return suffixes[(i+1)%suffixes.size()];
768 }
769 } else {
770 die("No known slots.");
771 }
772 }
773
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700774 for (const std::string &suffix : suffixes) {
775 if (suffix == slot)
776 return slot;
777 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800778 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700779 for (const std::string &suffix : suffixes) {
780 fprintf(stderr, "%s\n", suffix.c_str());
781 }
782 exit(1);
783}
784
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800785static std::string verify_slot(Transport* transport, const char *slot) {
786 return verify_slot(transport, slot, true);
787}
788
David Pursell0b156632015-10-30 11:22:01 -0700789static void do_for_partition(Transport* transport, const char *part, const char *slot,
790 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800791 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700792 std::string current_slot;
793
David Pursell0b156632015-10-30 11:22:01 -0700794 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800795 /* If has-slot is not supported, the answer is no. */
796 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700797 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800798 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700799 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700800 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700801 die("Failed to identify current slot.\n");
802 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800803 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700804 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800805 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700806 }
807 } else {
808 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700809 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
810 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700811 }
812 func(part);
813 }
814}
815
David Pursell0b156632015-10-30 11:22:01 -0700816/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
817 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
818 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
819 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700820 */
David Pursell0b156632015-10-30 11:22:01 -0700821static void do_for_partitions(Transport* transport, const char *part, const char *slot,
822 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800823 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700824
825 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700826 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700827 die("Could not check if partition %s has slot.", part);
828 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800829 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700830 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700831 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700832 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700833 }
834 } else {
David Pursell0b156632015-10-30 11:22:01 -0700835 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700836 }
837 } else {
David Pursell0b156632015-10-30 11:22:01 -0700838 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700839 }
840}
841
David Pursell0b156632015-10-30 11:22:01 -0700842static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700843 struct fastboot_buffer buf;
844
David Pursell0b156632015-10-30 11:22:01 -0700845 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846 die("cannot load '%s'", fname);
847 }
848 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700849}
850
Elliott Hughesfc797672015-04-07 20:12:50 -0700851static void do_update_signature(ZipArchiveHandle zip, char* fn) {
852 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700853 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700854 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800855 fb_queue_download("signature", data, sz);
856 fb_queue_command("signature", "installing signature");
857}
858
David Pursell0b156632015-10-30 11:22:01 -0700859static 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 -0800860 queue_info_dump();
861
Wink Savilleb98762f2011-04-04 17:54:59 -0700862 fb_queue_query_save("product", cur_product, sizeof(cur_product));
863
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700864 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700865 int error = OpenArchive(filename, &zip);
866 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100867 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700868 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700869 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800870
Elliott Hughesfc797672015-04-07 20:12:50 -0700871 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700872 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700873 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100874 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700875 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 }
877
Elliott Hughes253c18d2015-03-18 22:47:09 -0700878 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800879
Elliott Hughesacdbe922015-06-02 21:37:05 -0700880 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700881 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700882 if (fd == -1) {
883 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700884 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700885 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100886 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700887 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700888 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700889 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700890 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700891 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700892
893 auto update = [&](const std::string &partition) {
894 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700895 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700896 fb_queue_erase(partition.c_str());
897 }
898 flash_buf(partition.c_str(), &buf);
899 /* not closing the fd here since the sparse code keeps the fd around
900 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
901 * program exits.
902 */
903 };
David Pursell0b156632015-10-30 11:22:01 -0700904 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800905 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700906
907 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800908}
909
Elliott Hughesfc797672015-04-07 20:12:50 -0700910static void do_send_signature(char* fn) {
911 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700913
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800914 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800915
Elliott Hughesfc797672015-04-07 20:12:50 -0700916 strcpy(xtn, ".sig");
917
918 int64_t sz;
919 void* data = load_file(fn, &sz);
920 strcpy(xtn, ".img");
921 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800922 fb_queue_download("signature", data, sz);
923 fb_queue_command("signature", "installing signature");
924}
925
David Pursell0b156632015-10-30 11:22:01 -0700926static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800927 queue_info_dump();
928
Wink Savilleb98762f2011-04-04 17:54:59 -0700929 fb_queue_query_save("product", cur_product, sizeof(cur_product));
930
Elliott Hughes253c18d2015-03-18 22:47:09 -0700931 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700932 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800933
Elliott Hughesfc797672015-04-07 20:12:50 -0700934 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700935 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700936 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700937
938 setup_requirements(reinterpret_cast<char*>(data), sz);
939
940 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700941 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700942 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700943 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700944 if (images[i].is_optional)
945 continue;
946 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700947 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700948
949 auto flashall = [&](const std::string &partition) {
950 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -0700951 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700952 fb_queue_erase(partition.c_str());
953 }
954 flash_buf(partition.c_str(), &buf);
955 };
David Pursell0b156632015-10-30 11:22:01 -0700956 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958}
959
960#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800961#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800962
Elliott Hughes45be42e2015-09-02 20:15:48 -0700963static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700964{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700965 if (argc <= 2) return 0;
966 skip(2);
967
968 /*
969 * Process unlock_bootloader, we have to load the message file
970 * and send that to the remote device.
971 */
972 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700973
974 int64_t sz;
975 void* data = load_file(*argv, &sz);
976 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700977 fb_queue_download("unlock_message", data, sz);
978 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
979 skip(1);
980 return 0;
981}
982
Elliott Hughes45be42e2015-09-02 20:15:48 -0700983static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800984{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800985 char command[256];
986 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800987
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800988 command[0] = 0;
989 while(1) {
990 strcat(command,*argv);
991 skip(1);
992 if(argc == 0) break;
993 strcat(command," ");
994 }
995
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800996 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800997 return 0;
998}
999
Colin Crossf8387882012-05-24 17:18:41 -07001000static int64_t parse_num(const char *arg)
1001{
1002 char *endptr;
1003 unsigned long long num;
1004
1005 num = strtoull(arg, &endptr, 0);
1006 if (endptr == arg) {
1007 return -1;
1008 }
1009
1010 if (*endptr == 'k' || *endptr == 'K') {
1011 if (num >= (-1ULL) / 1024) {
1012 return -1;
1013 }
1014 num *= 1024LL;
1015 endptr++;
1016 } else if (*endptr == 'm' || *endptr == 'M') {
1017 if (num >= (-1ULL) / (1024 * 1024)) {
1018 return -1;
1019 }
1020 num *= 1024LL * 1024LL;
1021 endptr++;
1022 } else if (*endptr == 'g' || *endptr == 'G') {
1023 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1024 return -1;
1025 }
1026 num *= 1024LL * 1024LL * 1024LL;
1027 endptr++;
1028 }
1029
1030 if (*endptr != '\0') {
1031 return -1;
1032 }
1033
1034 if (num > INT64_MAX) {
1035 return -1;
1036 }
1037
1038 return num;
1039}
1040
David Pursell0b156632015-10-30 11:22:01 -07001041static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001042 const char* partition, int skip_if_not_supported,
1043 const char* type_override, const char* size_override) {
1044 std::string partition_type, partition_size;
1045
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001046 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001047 const char* errMsg = nullptr;
1048 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001049 int fd;
1050
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001051 unsigned int limit = INT_MAX;
1052 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001053 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001054 }
1055 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001056 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001057 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001058
David Pursell0b156632015-10-30 11:22:01 -07001059 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001060 errMsg = "Can't determine partition type.\n";
1061 goto failed;
1062 }
JP Abgrall7e859742014-05-06 15:14:15 -07001063 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001064 if (partition_type != type_override) {
1065 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1066 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001067 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001068 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001069 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001070
David Pursell0b156632015-10-30 11:22:01 -07001071 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001072 errMsg = "Unable to get partition size\n";
1073 goto failed;
1074 }
JP Abgrall7e859742014-05-06 15:14:15 -07001075 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001076 if (partition_size != size_override) {
1077 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1078 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001079 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001080 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001081 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001082 // Some bootloaders (angler, for example), send spurious leading whitespace.
1083 partition_size = android::base::Trim(partition_size);
1084 // Some bootloaders (hammerhead, for example) use implicit hex.
1085 // This code used to use strtol with base 16.
1086 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001087
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001088 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001089 if (!gen) {
1090 if (skip_if_not_supported) {
1091 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001092 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001093 return;
1094 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001095 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1096 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001097 return;
1098 }
1099
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001100 int64_t size;
1101 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1102 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1103 return;
1104 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001105
1106 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001107 if (fs_generator_generate(gen, fd, size)) {
1108 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001109 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001110 return;
1111 }
1112
David Pursell0b156632015-10-30 11:22:01 -07001113 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001114 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001115 close(fd);
1116 return;
1117 }
1118 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001119 return;
1120
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001121failed:
1122 if (skip_if_not_supported) {
1123 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001124 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001125 }
1126 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1127}
1128
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001129int main(int argc, char **argv)
1130{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001131 bool wants_wipe = false;
1132 bool wants_reboot = false;
1133 bool wants_reboot_bootloader = false;
1134 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001135 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001136 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001137 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001138 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001139 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001140 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001141
JP Abgrall7b8970c2013-03-07 17:06:41 -08001142 const struct option longopts[] = {
1143 {"base", required_argument, 0, 'b'},
1144 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001145 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001146 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001147 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001148 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001149 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001150 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001151 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001152 {"help", no_argument, 0, 'h'},
1153 {"unbuffered", no_argument, 0, 0},
1154 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001155 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001156 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001157 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001158 {0, 0, 0, 0}
1159 };
Colin Cross8879f982012-05-22 17:53:34 -07001160
1161 serial = getenv("ANDROID_SERIAL");
1162
1163 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001164 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 -07001165 if (c < 0) {
1166 break;
1167 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001168 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001169 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001170 case 'a':
1171 wants_set_active = true;
1172 if (optarg)
1173 next_active = optarg;
1174 break;
Colin Cross8879f982012-05-22 17:53:34 -07001175 case 'b':
1176 base_addr = strtoul(optarg, 0, 16);
1177 break;
Colin Cross8879f982012-05-22 17:53:34 -07001178 case 'c':
1179 cmdline = optarg;
1180 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001181 case 'h':
1182 usage();
1183 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001184 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001185 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001186 unsigned long val;
1187
1188 val = strtoul(optarg, &endptr, 0);
1189 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1190 die("invalid vendor id '%s'", optarg);
1191 vendor_id = (unsigned short)val;
1192 break;
1193 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001194 case 'k':
1195 kernel_offset = strtoul(optarg, 0, 16);
1196 break;
1197 case 'l':
1198 long_listing = 1;
1199 break;
1200 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001201 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001202 if (!page_size) die("invalid page size");
1203 break;
1204 case 'p':
1205 product = optarg;
1206 break;
1207 case 'r':
1208 ramdisk_offset = strtoul(optarg, 0, 16);
1209 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001210 case 't':
1211 tags_offset = strtoul(optarg, 0, 16);
1212 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001213 case 's':
1214 serial = optarg;
1215 break;
1216 case 'S':
1217 sparse_limit = parse_num(optarg);
1218 if (sparse_limit < 0) {
1219 die("invalid sparse limit");
1220 }
1221 break;
1222 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001223 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001224 break;
1225 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001226 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001227 break;
Colin Cross8879f982012-05-22 17:53:34 -07001228 case '?':
1229 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001230 case 0:
1231 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001232 setvbuf(stdout, nullptr, _IONBF, 0);
1233 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001234 } else if (strcmp("version", longopts[longindex].name) == 0) {
1235 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1236 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001237 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1238 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001239 }
1240 break;
Colin Cross8879f982012-05-22 17:53:34 -07001241 default:
1242 abort();
1243 }
1244 }
1245
1246 argc -= optind;
1247 argv += optind;
1248
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001249 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001250 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001251 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252 }
1253
Colin Cross8fb6e062012-07-24 16:36:41 -07001254 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001255 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001257 return 0;
1258 }
1259
Colin Crossc7b75dc2012-08-29 18:17:06 -07001260 if (argc > 0 && !strcmp(*argv, "help")) {
1261 usage();
1262 return 0;
1263 }
1264
David Pursell0b156632015-10-30 11:22:01 -07001265 Transport* transport = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001266 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001267 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001268 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001269 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001270
1271 if (wants_set_active) {
1272 if (next_active == "") {
1273 if (slot_override == "") {
1274 wants_set_active = false;
1275 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001276 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001277 }
1278 }
1279 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001280
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001281 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001282 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 require(2);
1284 fb_queue_display(argv[1], argv[1]);
1285 skip(2);
1286 } else if(!strcmp(*argv, "erase")) {
1287 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001288
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001289 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001290 std::string partition_type;
1291 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001292 fs_get_generator(partition_type) != nullptr) {
1293 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1294 partition_type.c_str());
1295 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001296
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001297 fb_queue_erase(partition.c_str());
1298 };
David Pursell0b156632015-10-30 11:22:01 -07001299 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001300 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001301 } else if(!strncmp(*argv, "format", strlen("format"))) {
1302 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001303 char *type_override = nullptr;
1304 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001305 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001306 /*
1307 * Parsing for: "format[:[type][:[size]]]"
1308 * Some valid things:
1309 * - select ontly the size, and leave default fs type:
1310 * format::0x4000000 userdata
1311 * - default fs type and size:
1312 * format userdata
1313 * format:: userdata
1314 */
1315 overrides = strchr(*argv, ':');
1316 if (overrides) {
1317 overrides++;
1318 size_override = strchr(overrides, ':');
1319 if (size_override) {
1320 size_override[0] = '\0';
1321 size_override++;
1322 }
1323 type_override = overrides;
1324 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001325 if (type_override && !type_override[0]) type_override = nullptr;
1326 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001327
1328 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001329 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001330 fb_queue_erase(partition.c_str());
1331 }
David Pursell0b156632015-10-30 11:22:01 -07001332 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001333 };
David Pursell0b156632015-10-30 11:22:01 -07001334 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001335 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001336 } else if(!strcmp(*argv, "signature")) {
1337 require(2);
1338 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001339 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001340 if (sz != 256) die("signature must be 256 bytes");
1341 fb_queue_download("signature", data, sz);
1342 fb_queue_command("signature", "installing signature");
1343 skip(2);
1344 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001345 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001346 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001347 if (argc > 0) {
1348 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001349 wants_reboot = false;
1350 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001351 skip(1);
1352 }
1353 }
1354 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001355 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001356 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001357 skip(1);
1358 } else if (!strcmp(*argv, "continue")) {
1359 fb_queue_command("continue", "resuming boot");
1360 skip(1);
1361 } else if(!strcmp(*argv, "boot")) {
1362 char *kname = 0;
1363 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001364 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001365 skip(1);
1366 if (argc > 0) {
1367 kname = argv[0];
1368 skip(1);
1369 }
1370 if (argc > 0) {
1371 rname = argv[0];
1372 skip(1);
1373 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001374 if (argc > 0) {
1375 sname = argv[0];
1376 skip(1);
1377 }
1378 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001379 if (data == 0) return 1;
1380 fb_queue_download("boot.img", data, sz);
1381 fb_queue_command("boot", "booting");
1382 } else if(!strcmp(*argv, "flash")) {
1383 char *pname = argv[1];
1384 char *fname = 0;
1385 require(2);
1386 if (argc > 2) {
1387 fname = argv[2];
1388 skip(3);
1389 } else {
1390 fname = find_item(pname, product);
1391 skip(2);
1392 }
1393 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001394
1395 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001396 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001397 fb_queue_erase(partition.c_str());
1398 }
David Pursell0b156632015-10-30 11:22:01 -07001399 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001400 };
David Pursell0b156632015-10-30 11:22:01 -07001401 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001402 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403 char *kname = argv[2];
1404 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001405 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001406 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001407 skip(3);
1408 if (argc > 0) {
1409 rname = argv[0];
1410 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001411 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001412 if (argc > 0) {
1413 sname = argv[0];
1414 skip(1);
1415 }
1416 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001417 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001418 auto flashraw = [&](const std::string &partition) {
1419 fb_queue_flash(partition.c_str(), data, sz);
1420 };
David Pursell0b156632015-10-30 11:22:01 -07001421 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001422 } else if(!strcmp(*argv, "flashall")) {
1423 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001424 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001425 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001426 } else if(!strcmp(*argv, "update")) {
1427 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001428 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001429 skip(2);
1430 } else {
David Pursell0b156632015-10-30 11:22:01 -07001431 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001432 skip(1);
1433 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001434 wants_reboot = 1;
1435 } else if(!strcmp(*argv, "set_active")) {
1436 require(2);
1437 std::string slot = verify_slot(transport, argv[1], false);
1438 fb_set_active(slot.c_str());
1439 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001440 } else if(!strcmp(*argv, "oem")) {
1441 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001442 } else if(!strcmp(*argv, "flashing")) {
1443 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1444 !strcmp(*(argv+1), "lock") ||
1445 !strcmp(*(argv+1), "unlock_critical") ||
1446 !strcmp(*(argv+1), "lock_critical") ||
1447 !strcmp(*(argv+1), "get_unlock_ability") ||
1448 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1449 !strcmp(*(argv+1), "lock_bootloader"))) {
1450 argc = do_oem_command(argc, argv);
1451 } else
1452 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1453 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001454 } else {
1455 usage();
1456 return 1;
1457 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001458 } else {
1459 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001460 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 }
1462 }
1463
1464 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001465 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001466 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001467 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001468
1469 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001470 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001471 fprintf(stderr, "wiping cache...\n");
1472 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001473 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001474 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001475 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001476 if (wants_set_active) {
1477 fb_set_active(next_active.c_str());
1478 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001479 if (wants_reboot) {
1480 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001481 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001482 } else if (wants_reboot_bootloader) {
1483 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001484 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001485 }
1486
David Pursell0b156632015-10-30 11:22:01 -07001487 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001488}