blob: 7ace5cd638014336b2e6217fa8195687798289d7 [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
Rom Lemarchand622810c2013-06-28 09:54:59 -070084enum fb_buffer_type {
85 FB_BUFFER,
86 FB_BUFFER_SPARSE,
87};
88
89struct fastboot_buffer {
90 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070091 void* data;
92 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070093};
94
95static struct {
96 char img_name[13];
97 char sig_name[13];
98 char part_name[9];
99 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700100} images[] = {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101 {"boot.img", "boot.sig", "boot", false},
102 {"recovery.img", "recovery.sig", "recovery", true},
103 {"system.img", "system.sig", "system", false},
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700104 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700106
Elliott Hughesfc797672015-04-07 20:12:50 -0700107static char* find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 char *dir;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700109 const char *fn;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 char path[PATH_MAX + 128];
111
112 if(!strcmp(item,"boot")) {
113 fn = "boot.img";
114 } else if(!strcmp(item,"recovery")) {
115 fn = "recovery.img";
116 } else if(!strcmp(item,"system")) {
117 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700118 } else if(!strcmp(item,"vendor")) {
119 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800120 } else if(!strcmp(item,"userdata")) {
121 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700122 } else if(!strcmp(item,"cache")) {
123 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124 } else if(!strcmp(item,"info")) {
125 fn = "android-info.txt";
126 } else {
127 fprintf(stderr,"unknown partition '%s'\n", item);
128 return 0;
129 }
130
131 if(product) {
132 get_my_path(path);
133 sprintf(path + strlen(path),
134 "../../../target/product/%s/%s", product, fn);
135 return strdup(path);
136 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800137
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 dir = getenv("ANDROID_PRODUCT_OUT");
139 if((dir == 0) || (dir[0] == 0)) {
140 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
141 return 0;
142 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800143
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 sprintf(path, "%s/%s", dir, fn);
145 return strdup(path);
146}
147
Elliott Hughesfc797672015-04-07 20:12:50 -0700148static int64_t get_file_size(int fd) {
149 struct stat sb;
150 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700151}
152
Elliott Hughesfc797672015-04-07 20:12:50 -0700153static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800154 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700155 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157 *sz = get_file_size(fd);
158 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700159 goto oops;
160 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162 data = (char*) malloc(*sz);
163 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 close(fd);
167
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 return data;
169
170oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800171 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 close(fd);
173 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800174 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 return 0;
176}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178static void* load_file(const char* fn, int64_t* sz) {
179 int fd = open(fn, O_RDONLY | O_BINARY);
180 if (fd == -1) return nullptr;
181 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700182}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700185 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700186 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700187 return -1;
188 }
189
190 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
191 return -1;
192 }
193
Scott Anderson13081c62012-04-06 12:39:30 -0700194 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700196 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
197 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 return 0;
199}
200
Elliott Hughesfc797672015-04-07 20:12:50 -0700201static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800202 return match_fastboot_with_serial(info, serial);
203}
204
Elliott Hughesfc797672015-04-07 20:12:50 -0700205static int list_devices_callback(usb_ifc_info* info) {
206 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700207 const char* serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700208 if (!info->writable) {
209 serial = "no permissions"; // like "adb devices"
210 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 if (!serial[0]) {
212 serial = "????????????";
213 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700214 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700215 if (!long_listing) {
Scott Anderson13081c62012-04-06 12:39:30 -0700216 printf("%s\tfastboot\n", serial);
Stephen Hines04f89532014-11-26 14:54:43 -0800217 } else if (strcmp("", info->device_path) == 0) {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700218 printf("%-22s fastboot\n", serial);
Scott Anderson13081c62012-04-06 12:39:30 -0700219 } else {
Scott Anderson866b1bd2012-06-04 20:29:11 -0700220 printf("%-22s fastboot %s\n", serial, info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 }
223
224 return -1;
225}
226
David Pursell0b156632015-10-30 11:22:01 -0700227static Transport* open_device() {
228 static Transport* transport = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229 int announce = 1;
230
David Pursell0b156632015-10-30 11:22:01 -0700231 if (transport) return transport;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800232
David Pursell0b156632015-10-30 11:22:01 -0700233 for (;;) {
234 transport = usb_open(match_fastboot);
235 if (transport) return transport;
236 if (announce) {
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800237 announce = 0;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700238 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700240 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 }
242}
243
Elliott Hughesfc797672015-04-07 20:12:50 -0700244static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800245 // We don't actually open a USB device here,
246 // just getting our callback called so we can
247 // list all the connected devices.
248 usb_open(list_devices_callback);
249}
250
Elliott Hughesfc797672015-04-07 20:12:50 -0700251static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 fprintf(stderr,
253/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
254 "usage: fastboot [ <option> ] <command>\n"
255 "\n"
256 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700257 " update <filename> Reflash device from update.zip.\n"
258 " flashall Flash boot, system, vendor, and --\n"
259 " if found -- recovery.\n"
260 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
261 " flashing lock Locks the device. Prevents flashing.\n"
262 " flashing unlock Unlocks the device. Allows flashing\n"
263 " any partition except\n"
264 " bootloader-related partitions.\n"
265 " flashing lock_critical Prevents flashing bootloader-related\n"
266 " partitions.\n"
267 " flashing unlock_critical Enables flashing bootloader-related\n"
268 " partitions.\n"
269 " flashing get_unlock_ability Queries bootloader to see if the\n"
270 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700271 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700272 " unlock nonce.\n"
273 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700274 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700275 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700276 " erase <partition> Erase a flash partition.\n"
277 " format[:[<fs type>][:[<size>]] <partition>\n"
278 " Format a flash partition. Can\n"
279 " override the fs type and/or size\n"
280 " the bootloader reports.\n"
281 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800282 " set_active <suffix> Sets the active slot. If slots are\n"
283 " not supported, this does nothing.\n"
284 " note: suffixes starting with a '-'\n"
285 " must use set_active -- <suffix>\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700286 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
287 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
288 " Create bootimage and flash it.\n"
289 " devices [-l] List all connected devices [with\n"
290 " device paths].\n"
291 " continue Continue with autoboot.\n"
292 " reboot [bootloader] Reboot device [into bootloader].\n"
293 " reboot-bootloader Reboot device into bootloader.\n"
294 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 "\n"
296 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700297 " -w Erase userdata and cache (and format\n"
298 " if supported by partition type).\n"
299 " -u Do not erase partition before\n"
300 " formatting.\n"
301 " -s <specific device> Specify device serial number\n"
302 " or path to device port.\n"
303 " -p <product> Specify product name.\n"
304 " -c <cmdline> Override kernel commandline.\n"
305 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800306 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700307 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800308 " --kernel-offset Specify a custom kernel offset.\n"
309 " (default: 0x00008000)\n"
310 " --ramdisk-offset Specify a custom ramdisk offset.\n"
311 " (default: 0x01000000)\n"
312 " --tags-offset Specify a custom tags offset.\n"
313 " (default: 0x00000100)\n"
314 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700315 " (default: 2048).\n"
316 " -S <size>[K|M|G] Automatically sparse files greater\n"
317 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700318 " --slot <suffix> Specify slot suffix to be used if the\n"
319 " device supports slots. This will be\n"
320 " added to all partition names that use\n"
321 " slots. 'all' can be given to refer\n"
322 " to all slots. If this is not given,\n"
323 " slotted partitions will default to\n"
324 " the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800325 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800326 " provided, this will default to the value\n"
327 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800328 " supported, this does nothing. This will\n"
329 " run after all non-reboot commands.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800330 " --unbuffered Do not buffer input or output.\n"
331 " --version Display version.\n"
332 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334}
Elliott Hughesfc797672015-04-07 20:12:50 -0700335static void* load_bootable_image(const char* kernel, const char* ramdisk,
336 const char* secondstage, int64_t* sz,
337 const char* cmdline) {
338 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800339 fprintf(stderr, "no image specified\n");
340 return 0;
341 }
342
Elliott Hughesfc797672015-04-07 20:12:50 -0700343 int64_t ksize;
344 void* kdata = load_file(kernel, &ksize);
345 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800346 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347 return 0;
348 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800349
Elliott Hughesfc797672015-04-07 20:12:50 -0700350 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700352 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800353
Elliott Hughesfc797672015-04-07 20:12:50 -0700354 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
356 return 0;
357 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800358
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359 *sz = ksize;
360 return kdata;
361 }
362
Elliott Hughesfc797672015-04-07 20:12:50 -0700363 void* rdata = nullptr;
364 int64_t rsize = 0;
365 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700367 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800368 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369 return 0;
370 }
371 }
372
Elliott Hughesfc797672015-04-07 20:12:50 -0700373 void* sdata = nullptr;
374 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200375 if (secondstage) {
376 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700377 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200378 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
379 return 0;
380 }
381 }
382
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700384 int64_t bsize = 0;
385 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800386 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200387 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800388 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700389 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 fprintf(stderr,"failed to create boot.img\n");
391 return 0;
392 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700393 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
394 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800396
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 return bdata;
398}
399
Elliott Hughesfc797672015-04-07 20:12:50 -0700400static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401{
Yusuke Sato07447542015-06-25 14:39:19 -0700402 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700403 ZipEntry zip_entry;
404 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700405 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000406 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800407 }
408
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700409 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700411 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700412 if (data == nullptr) {
413 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000414 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 }
416
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700417 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
418 if (error != 0) {
419 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000421 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000423
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 return data;
425}
426
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700427#if defined(_WIN32)
428
429// TODO: move this to somewhere it can be shared.
430
431#include <windows.h>
432
433// Windows' tmpfile(3) requires administrator rights because
434// it creates temporary files in the root directory.
435static FILE* win32_tmpfile() {
436 char temp_path[PATH_MAX];
437 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
438 if (nchars == 0 || nchars >= sizeof(temp_path)) {
439 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
440 return nullptr;
441 }
442
443 char filename[PATH_MAX];
444 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
445 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
446 return nullptr;
447 }
448
449 return fopen(filename, "w+bTD");
450}
451
452#define tmpfile win32_tmpfile
453
454#endif
455
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700456static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
457 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700459 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
460 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700461 return -1;
462 }
463
Yusuke Sato07447542015-06-25 14:39:19 -0700464 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700465 ZipEntry zip_entry;
466 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
467 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000468 return -1;
469 }
470
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700471 int fd = fileno(fp);
472 int error = ExtractEntryToFile(zip, &zip_entry, fd);
473 if (error != 0) {
474 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
475 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700476 }
477
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000478 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700479 return fd;
480}
481
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482static char *strip(char *s)
483{
484 int n;
485 while(*s && isspace(*s)) s++;
486 n = strlen(s);
487 while(n-- > 0) {
488 if(!isspace(s[n])) break;
489 s[n] = 0;
490 }
491 return s;
492}
493
494#define MAX_OPTIONS 32
495static int setup_requirement_line(char *name)
496{
497 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700498 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 unsigned n, count;
500 char *x;
501 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800502
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 if (!strncmp(name, "reject ", 7)) {
504 name += 7;
505 invert = 1;
506 } else if (!strncmp(name, "require ", 8)) {
507 name += 8;
508 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700509 } else if (!strncmp(name, "require-for-product:", 20)) {
510 // Get the product and point name past it
511 prod = name + 20;
512 name = strchr(name, ' ');
513 if (!name) return -1;
514 *name = 0;
515 name += 1;
516 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 }
518
519 x = strchr(name, '=');
520 if (x == 0) return 0;
521 *x = 0;
522 val[0] = x + 1;
523
524 for(count = 1; count < MAX_OPTIONS; count++) {
525 x = strchr(val[count - 1],'|');
526 if (x == 0) break;
527 *x = 0;
528 val[count] = x + 1;
529 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800530
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 name = strip(name);
532 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800533
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800534 name = strip(name);
535 if (name == 0) return -1;
536
Elliott Hughes253c18d2015-03-18 22:47:09 -0700537 const char* var = name;
538 // Work around an unfortunate name mismatch.
539 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540
Elliott Hughes253c18d2015-03-18 22:47:09 -0700541 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 if (out == 0) return -1;
543
544 for(n = 0; n < count; n++) {
545 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700546 if (out[n] == 0) {
547 for(size_t i = 0; i < n; ++i) {
548 free((char*) out[i]);
549 }
550 free(out);
551 return -1;
552 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800553 }
554
Elliott Hughes253c18d2015-03-18 22:47:09 -0700555 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 return 0;
557}
558
Elliott Hughesfc797672015-04-07 20:12:50 -0700559static void setup_requirements(char* data, int64_t sz) {
560 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800561 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700562 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800563 *s++ = 0;
564 if (setup_requirement_line(data)) {
565 die("out of memory");
566 }
567 data = s;
568 } else {
569 s++;
570 }
571 }
572}
573
Elliott Hughesfc797672015-04-07 20:12:50 -0700574static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800575 fb_queue_notice("--------------------------------------------");
576 fb_queue_display("version-bootloader", "Bootloader Version...");
577 fb_queue_display("version-baseband", "Baseband Version.....");
578 fb_queue_display("serialno", "Serial Number........");
579 fb_queue_notice("--------------------------------------------");
580}
581
Rom Lemarchand622810c2013-06-28 09:54:59 -0700582static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700583{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700584 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700585 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700586 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700587 }
588
Elliott Hughesfc797672015-04-07 20:12:50 -0700589 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700590 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700591 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700592 }
593
Elliott Hughes253c18d2015-03-18 22:47:09 -0700594 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700595 if (!out_s) {
596 die("Failed to allocate sparse file array\n");
597 }
598
599 files = sparse_file_resparse(s, max_size, out_s, files);
600 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700601 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700602 }
603
604 return out_s;
605}
606
David Pursell0b156632015-10-30 11:22:01 -0700607static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700608 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700609 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
610 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800611 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700612 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700613 }
614
Elliott Hughes77c0e662015-11-02 15:51:12 -0800615 // Some bootloaders (angler, for example) send spurious whitespace too.
616 max_download_size = android::base::Trim(max_download_size);
617
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700618 uint64_t limit;
619 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800620 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700621 return 0;
622 }
623 if (limit > 0) {
624 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
625 }
Colin Crossf8387882012-05-24 17:18:41 -0700626 return limit;
627}
628
David Pursell0b156632015-10-30 11:22:01 -0700629static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700630 int64_t limit;
631
632 if (sparse_limit == 0) {
633 return 0;
634 } else if (sparse_limit > 0) {
635 limit = sparse_limit;
636 } else {
637 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700638 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700639 }
640 if (target_sparse_limit > 0) {
641 limit = target_sparse_limit;
642 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700643 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700644 }
645 }
646
647 if (size > limit) {
648 return limit;
649 }
650
651 return 0;
652}
653
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700654// Until we get lazy inode table init working in make_ext4fs, we need to
655// erase partitions of type ext4 before flashing a filesystem so no stale
656// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700657static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800658 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700659 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800660 return false;
661 }
662 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700663}
664
David Pursell0b156632015-10-30 11:22:01 -0700665static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700666 int64_t sz = get_file_size(fd);
667 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000668 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700669 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700670
Elliott Hughesfc797672015-04-07 20:12:50 -0700671 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700672 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700673 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700674 sparse_file** s = load_sparse_files(fd, limit);
675 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700676 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700677 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700678 buf->type = FB_BUFFER_SPARSE;
679 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700680 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700681 void* data = load_fd(fd, &sz);
682 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700683 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700684 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000685 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700686 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700687
688 return 0;
689}
690
David Pursell0b156632015-10-30 11:22:01 -0700691static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700692{
693 int fd;
694
695 fd = open(fname, O_RDONLY | O_BINARY);
696 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700697 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700698 }
699
David Pursell0b156632015-10-30 11:22:01 -0700700 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700701}
702
703static void flash_buf(const char *pname, struct fastboot_buffer *buf)
704{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700705 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700706
707 switch (buf->type) {
708 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700709 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700710 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700711 int64_t sz = sparse_file_len(*s, true, false);
712 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700713 }
714 break;
715 case FB_BUFFER:
716 fb_queue_flash(pname, buf->data, buf->sz);
717 break;
718 default:
719 die("unknown buffer type: %d", buf->type);
720 }
721}
722
David Pursell0b156632015-10-30 11:22:01 -0700723static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700724 std::vector<std::string> suffixes;
725 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700726 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700727 die("Could not get suffixes.\n");
728 }
729 return android::base::Split(suffix_list, ",");
730}
731
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800732static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700733 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800734 if (allow_all) {
735 return "all";
736 } else {
737 std::vector<std::string> suffixes = get_suffixes(transport);
738 if (!suffixes.empty()) {
739 return suffixes[0];
740 } else {
741 fprintf(stderr, "No known slots.\n");
742 exit(1);
743 }
744 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700745 }
David Pursell0b156632015-10-30 11:22:01 -0700746 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700747 for (const std::string &suffix : suffixes) {
748 if (suffix == slot)
749 return slot;
750 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800751 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700752 for (const std::string &suffix : suffixes) {
753 fprintf(stderr, "%s\n", suffix.c_str());
754 }
755 exit(1);
756}
757
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800758static std::string verify_slot(Transport* transport, const char *slot) {
759 return verify_slot(transport, slot, true);
760}
761
David Pursell0b156632015-10-30 11:22:01 -0700762static void do_for_partition(Transport* transport, const char *part, const char *slot,
763 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800764 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700765 std::string current_slot;
766
David Pursell0b156632015-10-30 11:22:01 -0700767 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800768 /* If has-slot is not supported, the answer is no. */
769 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700770 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800771 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700772 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700773 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700774 die("Failed to identify current slot.\n");
775 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800776 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700777 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800778 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700779 }
780 } else {
781 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700782 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
783 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700784 }
785 func(part);
786 }
787}
788
David Pursell0b156632015-10-30 11:22:01 -0700789/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
790 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
791 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
792 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700793 */
David Pursell0b156632015-10-30 11:22:01 -0700794static void do_for_partitions(Transport* transport, const char *part, const char *slot,
795 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800796 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700797
798 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700799 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700800 die("Could not check if partition %s has slot.", part);
801 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800802 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700803 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700804 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700805 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700806 }
807 } else {
David Pursell0b156632015-10-30 11:22:01 -0700808 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700809 }
810 } else {
David Pursell0b156632015-10-30 11:22:01 -0700811 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700812 }
813}
814
David Pursell0b156632015-10-30 11:22:01 -0700815static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700816 struct fastboot_buffer buf;
817
David Pursell0b156632015-10-30 11:22:01 -0700818 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819 die("cannot load '%s'", fname);
820 }
821 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700822}
823
Elliott Hughesfc797672015-04-07 20:12:50 -0700824static void do_update_signature(ZipArchiveHandle zip, char* fn) {
825 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700826 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700827 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800828 fb_queue_download("signature", data, sz);
829 fb_queue_command("signature", "installing signature");
830}
831
David Pursell0b156632015-10-30 11:22:01 -0700832static 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 -0800833 queue_info_dump();
834
Wink Savilleb98762f2011-04-04 17:54:59 -0700835 fb_queue_query_save("product", cur_product, sizeof(cur_product));
836
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700837 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700838 int error = OpenArchive(filename, &zip);
839 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100840 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700841 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700842 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800843
Elliott Hughesfc797672015-04-07 20:12:50 -0700844 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700845 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700846 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100847 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700848 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800849 }
850
Elliott Hughes253c18d2015-03-18 22:47:09 -0700851 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800852
Elliott Hughesacdbe922015-06-02 21:37:05 -0700853 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700854 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700855 if (fd == -1) {
856 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700857 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700858 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100859 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700860 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700861 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700862 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700863 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700865
866 auto update = [&](const std::string &partition) {
867 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700868 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700869 fb_queue_erase(partition.c_str());
870 }
871 flash_buf(partition.c_str(), &buf);
872 /* not closing the fd here since the sparse code keeps the fd around
873 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
874 * program exits.
875 */
876 };
David Pursell0b156632015-10-30 11:22:01 -0700877 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800878 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700879
880 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881}
882
Elliott Hughesfc797672015-04-07 20:12:50 -0700883static void do_send_signature(char* fn) {
884 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800885 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700886
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800887 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800888
Elliott Hughesfc797672015-04-07 20:12:50 -0700889 strcpy(xtn, ".sig");
890
891 int64_t sz;
892 void* data = load_file(fn, &sz);
893 strcpy(xtn, ".img");
894 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800895 fb_queue_download("signature", data, sz);
896 fb_queue_command("signature", "installing signature");
897}
898
David Pursell0b156632015-10-30 11:22:01 -0700899static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800900 queue_info_dump();
901
Wink Savilleb98762f2011-04-04 17:54:59 -0700902 fb_queue_query_save("product", cur_product, sizeof(cur_product));
903
Elliott Hughes253c18d2015-03-18 22:47:09 -0700904 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700905 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800906
Elliott Hughesfc797672015-04-07 20:12:50 -0700907 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700908 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700909 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700910
911 setup_requirements(reinterpret_cast<char*>(data), sz);
912
913 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700914 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700915 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700916 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700917 if (images[i].is_optional)
918 continue;
919 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700920 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700921
922 auto flashall = [&](const std::string &partition) {
923 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -0700924 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700925 fb_queue_erase(partition.c_str());
926 }
927 flash_buf(partition.c_str(), &buf);
928 };
David Pursell0b156632015-10-30 11:22:01 -0700929 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800930 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800931}
932
933#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800934#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800935
Elliott Hughes45be42e2015-09-02 20:15:48 -0700936static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700937{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700938 if (argc <= 2) return 0;
939 skip(2);
940
941 /*
942 * Process unlock_bootloader, we have to load the message file
943 * and send that to the remote device.
944 */
945 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700946
947 int64_t sz;
948 void* data = load_file(*argv, &sz);
949 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700950 fb_queue_download("unlock_message", data, sz);
951 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
952 skip(1);
953 return 0;
954}
955
Elliott Hughes45be42e2015-09-02 20:15:48 -0700956static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800957{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800958 char command[256];
959 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800960
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800961 command[0] = 0;
962 while(1) {
963 strcat(command,*argv);
964 skip(1);
965 if(argc == 0) break;
966 strcat(command," ");
967 }
968
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800969 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800970 return 0;
971}
972
Colin Crossf8387882012-05-24 17:18:41 -0700973static int64_t parse_num(const char *arg)
974{
975 char *endptr;
976 unsigned long long num;
977
978 num = strtoull(arg, &endptr, 0);
979 if (endptr == arg) {
980 return -1;
981 }
982
983 if (*endptr == 'k' || *endptr == 'K') {
984 if (num >= (-1ULL) / 1024) {
985 return -1;
986 }
987 num *= 1024LL;
988 endptr++;
989 } else if (*endptr == 'm' || *endptr == 'M') {
990 if (num >= (-1ULL) / (1024 * 1024)) {
991 return -1;
992 }
993 num *= 1024LL * 1024LL;
994 endptr++;
995 } else if (*endptr == 'g' || *endptr == 'G') {
996 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
997 return -1;
998 }
999 num *= 1024LL * 1024LL * 1024LL;
1000 endptr++;
1001 }
1002
1003 if (*endptr != '\0') {
1004 return -1;
1005 }
1006
1007 if (num > INT64_MAX) {
1008 return -1;
1009 }
1010
1011 return num;
1012}
1013
David Pursell0b156632015-10-30 11:22:01 -07001014static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001015 const char* partition, int skip_if_not_supported,
1016 const char* type_override, const char* size_override) {
1017 std::string partition_type, partition_size;
1018
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001019 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001020 const char* errMsg = nullptr;
1021 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001022 int fd;
1023
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001024 unsigned int limit = INT_MAX;
1025 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001026 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001027 }
1028 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001029 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001030 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001031
David Pursell0b156632015-10-30 11:22:01 -07001032 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001033 errMsg = "Can't determine partition type.\n";
1034 goto failed;
1035 }
JP Abgrall7e859742014-05-06 15:14:15 -07001036 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001037 if (partition_type != type_override) {
1038 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1039 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001040 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001041 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001042 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001043
David Pursell0b156632015-10-30 11:22:01 -07001044 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001045 errMsg = "Unable to get partition size\n";
1046 goto failed;
1047 }
JP Abgrall7e859742014-05-06 15:14:15 -07001048 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001049 if (partition_size != size_override) {
1050 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1051 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001052 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001053 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001054 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001055 // Some bootloaders (angler, for example), send spurious leading whitespace.
1056 partition_size = android::base::Trim(partition_size);
1057 // Some bootloaders (hammerhead, for example) use implicit hex.
1058 // This code used to use strtol with base 16.
1059 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001060
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001061 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001062 if (!gen) {
1063 if (skip_if_not_supported) {
1064 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001065 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001066 return;
1067 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001068 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1069 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001070 return;
1071 }
1072
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001073 int64_t size;
1074 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1075 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1076 return;
1077 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001078
1079 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001080 if (fs_generator_generate(gen, fd, size)) {
1081 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001082 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001083 return;
1084 }
1085
David Pursell0b156632015-10-30 11:22:01 -07001086 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001087 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001088 close(fd);
1089 return;
1090 }
1091 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001092 return;
1093
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001094failed:
1095 if (skip_if_not_supported) {
1096 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001097 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001098 }
1099 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1100}
1101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001102int main(int argc, char **argv)
1103{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001104 bool wants_wipe = false;
1105 bool wants_reboot = false;
1106 bool wants_reboot_bootloader = false;
1107 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001108 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001109 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001110 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001111 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001112 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001113 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001114
JP Abgrall7b8970c2013-03-07 17:06:41 -08001115 const struct option longopts[] = {
1116 {"base", required_argument, 0, 'b'},
1117 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001118 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001119 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001120 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001121 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001122 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001123 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001124 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001125 {"help", no_argument, 0, 'h'},
1126 {"unbuffered", no_argument, 0, 0},
1127 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001128 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001129 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001130 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001131 {0, 0, 0, 0}
1132 };
Colin Cross8879f982012-05-22 17:53:34 -07001133
1134 serial = getenv("ANDROID_SERIAL");
1135
1136 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001137 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 -07001138 if (c < 0) {
1139 break;
1140 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001141 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001142 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001143 case 'a':
1144 wants_set_active = true;
1145 if (optarg)
1146 next_active = optarg;
1147 break;
Colin Cross8879f982012-05-22 17:53:34 -07001148 case 'b':
1149 base_addr = strtoul(optarg, 0, 16);
1150 break;
Colin Cross8879f982012-05-22 17:53:34 -07001151 case 'c':
1152 cmdline = optarg;
1153 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001154 case 'h':
1155 usage();
1156 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001157 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001158 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001159 unsigned long val;
1160
1161 val = strtoul(optarg, &endptr, 0);
1162 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1163 die("invalid vendor id '%s'", optarg);
1164 vendor_id = (unsigned short)val;
1165 break;
1166 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001167 case 'k':
1168 kernel_offset = strtoul(optarg, 0, 16);
1169 break;
1170 case 'l':
1171 long_listing = 1;
1172 break;
1173 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001174 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001175 if (!page_size) die("invalid page size");
1176 break;
1177 case 'p':
1178 product = optarg;
1179 break;
1180 case 'r':
1181 ramdisk_offset = strtoul(optarg, 0, 16);
1182 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001183 case 't':
1184 tags_offset = strtoul(optarg, 0, 16);
1185 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001186 case 's':
1187 serial = optarg;
1188 break;
1189 case 'S':
1190 sparse_limit = parse_num(optarg);
1191 if (sparse_limit < 0) {
1192 die("invalid sparse limit");
1193 }
1194 break;
1195 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001196 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001197 break;
1198 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001199 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001200 break;
Colin Cross8879f982012-05-22 17:53:34 -07001201 case '?':
1202 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001203 case 0:
1204 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001205 setvbuf(stdout, nullptr, _IONBF, 0);
1206 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001207 } else if (strcmp("version", longopts[longindex].name) == 0) {
1208 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1209 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001210 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1211 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001212 }
1213 break;
Colin Cross8879f982012-05-22 17:53:34 -07001214 default:
1215 abort();
1216 }
1217 }
1218
1219 argc -= optind;
1220 argv += optind;
1221
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001222 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001224 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001225 }
1226
Colin Cross8fb6e062012-07-24 16:36:41 -07001227 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001228 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001229 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001230 return 0;
1231 }
1232
Colin Crossc7b75dc2012-08-29 18:17:06 -07001233 if (argc > 0 && !strcmp(*argv, "help")) {
1234 usage();
1235 return 0;
1236 }
1237
David Pursell0b156632015-10-30 11:22:01 -07001238 Transport* transport = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001239 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001240 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001241 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001242 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001243
1244 if (wants_set_active) {
1245 if (next_active == "") {
1246 if (slot_override == "") {
1247 wants_set_active = false;
1248 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001249 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001250 }
1251 }
1252 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001253
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001255 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 require(2);
1257 fb_queue_display(argv[1], argv[1]);
1258 skip(2);
1259 } else if(!strcmp(*argv, "erase")) {
1260 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001261
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001262 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001263 std::string partition_type;
1264 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001265 fs_get_generator(partition_type) != nullptr) {
1266 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1267 partition_type.c_str());
1268 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001269
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001270 fb_queue_erase(partition.c_str());
1271 };
David Pursell0b156632015-10-30 11:22:01 -07001272 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001274 } else if(!strncmp(*argv, "format", strlen("format"))) {
1275 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001276 char *type_override = nullptr;
1277 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001278 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001279 /*
1280 * Parsing for: "format[:[type][:[size]]]"
1281 * Some valid things:
1282 * - select ontly the size, and leave default fs type:
1283 * format::0x4000000 userdata
1284 * - default fs type and size:
1285 * format userdata
1286 * format:: userdata
1287 */
1288 overrides = strchr(*argv, ':');
1289 if (overrides) {
1290 overrides++;
1291 size_override = strchr(overrides, ':');
1292 if (size_override) {
1293 size_override[0] = '\0';
1294 size_override++;
1295 }
1296 type_override = overrides;
1297 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001298 if (type_override && !type_override[0]) type_override = nullptr;
1299 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001300
1301 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001302 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001303 fb_queue_erase(partition.c_str());
1304 }
David Pursell0b156632015-10-30 11:22:01 -07001305 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001306 };
David Pursell0b156632015-10-30 11:22:01 -07001307 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001308 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001309 } else if(!strcmp(*argv, "signature")) {
1310 require(2);
1311 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001312 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001313 if (sz != 256) die("signature must be 256 bytes");
1314 fb_queue_download("signature", data, sz);
1315 fb_queue_command("signature", "installing signature");
1316 skip(2);
1317 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001318 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001319 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001320 if (argc > 0) {
1321 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001322 wants_reboot = false;
1323 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001324 skip(1);
1325 }
1326 }
1327 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001328 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001329 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001330 skip(1);
1331 } else if (!strcmp(*argv, "continue")) {
1332 fb_queue_command("continue", "resuming boot");
1333 skip(1);
1334 } else if(!strcmp(*argv, "boot")) {
1335 char *kname = 0;
1336 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001337 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001338 skip(1);
1339 if (argc > 0) {
1340 kname = argv[0];
1341 skip(1);
1342 }
1343 if (argc > 0) {
1344 rname = argv[0];
1345 skip(1);
1346 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001347 if (argc > 0) {
1348 sname = argv[0];
1349 skip(1);
1350 }
1351 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001352 if (data == 0) return 1;
1353 fb_queue_download("boot.img", data, sz);
1354 fb_queue_command("boot", "booting");
1355 } else if(!strcmp(*argv, "flash")) {
1356 char *pname = argv[1];
1357 char *fname = 0;
1358 require(2);
1359 if (argc > 2) {
1360 fname = argv[2];
1361 skip(3);
1362 } else {
1363 fname = find_item(pname, product);
1364 skip(2);
1365 }
1366 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001367
1368 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001369 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001370 fb_queue_erase(partition.c_str());
1371 }
David Pursell0b156632015-10-30 11:22:01 -07001372 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001373 };
David Pursell0b156632015-10-30 11:22:01 -07001374 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001375 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001376 char *kname = argv[2];
1377 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001378 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001379 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001380 skip(3);
1381 if (argc > 0) {
1382 rname = argv[0];
1383 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001384 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001385 if (argc > 0) {
1386 sname = argv[0];
1387 skip(1);
1388 }
1389 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001390 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001391 auto flashraw = [&](const std::string &partition) {
1392 fb_queue_flash(partition.c_str(), data, sz);
1393 };
David Pursell0b156632015-10-30 11:22:01 -07001394 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001395 } else if(!strcmp(*argv, "flashall")) {
1396 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001397 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001398 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001399 } else if(!strcmp(*argv, "update")) {
1400 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001401 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001402 skip(2);
1403 } else {
David Pursell0b156632015-10-30 11:22:01 -07001404 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001405 skip(1);
1406 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001407 wants_reboot = 1;
1408 } else if(!strcmp(*argv, "set_active")) {
1409 require(2);
1410 std::string slot = verify_slot(transport, argv[1], false);
1411 fb_set_active(slot.c_str());
1412 skip(2);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001413 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001414 } else if(!strcmp(*argv, "oem")) {
1415 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001416 } else if(!strcmp(*argv, "flashing")) {
1417 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1418 !strcmp(*(argv+1), "lock") ||
1419 !strcmp(*(argv+1), "unlock_critical") ||
1420 !strcmp(*(argv+1), "lock_critical") ||
1421 !strcmp(*(argv+1), "get_unlock_ability") ||
1422 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1423 !strcmp(*(argv+1), "lock_bootloader"))) {
1424 argc = do_oem_command(argc, argv);
1425 } else
1426 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1427 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001428 } else {
1429 usage();
1430 return 1;
1431 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001432 } else {
1433 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001434 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001435 }
1436 }
1437
1438 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001439 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001440 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001441 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001442
1443 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001444 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001445 fprintf(stderr, "wiping cache...\n");
1446 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001447 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001448 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001449 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001450 if (wants_set_active) {
1451 fb_set_active(next_active.c_str());
1452 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001453 if (wants_reboot) {
1454 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001455 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001456 } else if (wants_reboot_bootloader) {
1457 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001458 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001459 }
1460
David Pursell0b156632015-10-30 11:22:01 -07001461 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001462}