blob: cc9f06c8cde40a6496a81dc03c8658fc8afe227a [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 Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/parseint.h>
48#include <android-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
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/strings.h>
53#include <android-base/parseint.h>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070054
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"
282 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
283 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
284 " Create bootimage and flash it.\n"
285 " devices [-l] List all connected devices [with\n"
286 " device paths].\n"
287 " continue Continue with autoboot.\n"
288 " reboot [bootloader] Reboot device [into bootloader].\n"
289 " reboot-bootloader Reboot device into bootloader.\n"
290 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 "\n"
292 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700293 " -w Erase userdata and cache (and format\n"
294 " if supported by partition type).\n"
295 " -u Do not erase partition before\n"
296 " formatting.\n"
297 " -s <specific device> Specify device serial number\n"
298 " or path to device port.\n"
299 " -p <product> Specify product name.\n"
300 " -c <cmdline> Override kernel commandline.\n"
301 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800302 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700303 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800304 " --kernel-offset Specify a custom kernel offset.\n"
305 " (default: 0x00008000)\n"
306 " --ramdisk-offset Specify a custom ramdisk offset.\n"
307 " (default: 0x01000000)\n"
308 " --tags-offset Specify a custom tags offset.\n"
309 " (default: 0x00000100)\n"
310 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700311 " (default: 2048).\n"
312 " -S <size>[K|M|G] Automatically sparse files greater\n"
313 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700314 " --slot <suffix> Specify slot suffix to be used if the\n"
315 " device supports slots. This will be\n"
316 " added to all partition names that use\n"
317 " slots. 'all' can be given to refer\n"
318 " to all slots. If this is not given,\n"
319 " slotted partitions will default to\n"
320 " the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800321 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800322 " provided, this will default to the value\n"
323 " given by --slot. If slots are not\n"
324 " supported, this does nothing.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800325 " --unbuffered Do not buffer input or output.\n"
326 " --version Display version.\n"
327 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329}
Elliott Hughesfc797672015-04-07 20:12:50 -0700330static void* load_bootable_image(const char* kernel, const char* ramdisk,
331 const char* secondstage, int64_t* sz,
332 const char* cmdline) {
333 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 fprintf(stderr, "no image specified\n");
335 return 0;
336 }
337
Elliott Hughesfc797672015-04-07 20:12:50 -0700338 int64_t ksize;
339 void* kdata = load_file(kernel, &ksize);
340 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800341 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 return 0;
343 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800344
Elliott Hughesfc797672015-04-07 20:12:50 -0700345 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800346 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700347 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800348
Elliott Hughesfc797672015-04-07 20:12:50 -0700349 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
351 return 0;
352 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800353
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354 *sz = ksize;
355 return kdata;
356 }
357
Elliott Hughesfc797672015-04-07 20:12:50 -0700358 void* rdata = nullptr;
359 int64_t rsize = 0;
360 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700362 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800363 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 return 0;
365 }
366 }
367
Elliott Hughesfc797672015-04-07 20:12:50 -0700368 void* sdata = nullptr;
369 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200370 if (secondstage) {
371 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700372 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200373 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
374 return 0;
375 }
376 }
377
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700379 int64_t bsize = 0;
380 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800381 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200382 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800383 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700384 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 fprintf(stderr,"failed to create boot.img\n");
386 return 0;
387 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700388 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
389 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800391
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 return bdata;
393}
394
Elliott Hughesfc797672015-04-07 20:12:50 -0700395static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396{
Yusuke Sato07447542015-06-25 14:39:19 -0700397 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700398 ZipEntry zip_entry;
399 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700400 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000401 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 }
403
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700404 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700406 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700407 if (data == nullptr) {
408 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000409 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410 }
411
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700412 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
413 if (error != 0) {
414 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000416 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000418
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 return data;
420}
421
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700422#if defined(_WIN32)
423
424// TODO: move this to somewhere it can be shared.
425
426#include <windows.h>
427
428// Windows' tmpfile(3) requires administrator rights because
429// it creates temporary files in the root directory.
430static FILE* win32_tmpfile() {
431 char temp_path[PATH_MAX];
432 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
433 if (nchars == 0 || nchars >= sizeof(temp_path)) {
434 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
435 return nullptr;
436 }
437
438 char filename[PATH_MAX];
439 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
440 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
441 return nullptr;
442 }
443
444 return fopen(filename, "w+bTD");
445}
446
447#define tmpfile win32_tmpfile
448
449#endif
450
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700451static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
452 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700453 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700454 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
455 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700456 return -1;
457 }
458
Yusuke Sato07447542015-06-25 14:39:19 -0700459 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700460 ZipEntry zip_entry;
461 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
462 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000463 return -1;
464 }
465
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700466 int fd = fileno(fp);
467 int error = ExtractEntryToFile(zip, &zip_entry, fd);
468 if (error != 0) {
469 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
470 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700471 }
472
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000473 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700474 return fd;
475}
476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477static char *strip(char *s)
478{
479 int n;
480 while(*s && isspace(*s)) s++;
481 n = strlen(s);
482 while(n-- > 0) {
483 if(!isspace(s[n])) break;
484 s[n] = 0;
485 }
486 return s;
487}
488
489#define MAX_OPTIONS 32
490static int setup_requirement_line(char *name)
491{
492 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700493 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 unsigned n, count;
495 char *x;
496 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800497
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800498 if (!strncmp(name, "reject ", 7)) {
499 name += 7;
500 invert = 1;
501 } else if (!strncmp(name, "require ", 8)) {
502 name += 8;
503 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700504 } else if (!strncmp(name, "require-for-product:", 20)) {
505 // Get the product and point name past it
506 prod = name + 20;
507 name = strchr(name, ' ');
508 if (!name) return -1;
509 *name = 0;
510 name += 1;
511 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 }
513
514 x = strchr(name, '=');
515 if (x == 0) return 0;
516 *x = 0;
517 val[0] = x + 1;
518
519 for(count = 1; count < MAX_OPTIONS; count++) {
520 x = strchr(val[count - 1],'|');
521 if (x == 0) break;
522 *x = 0;
523 val[count] = x + 1;
524 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800525
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 name = strip(name);
527 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800528
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 name = strip(name);
530 if (name == 0) return -1;
531
Elliott Hughes253c18d2015-03-18 22:47:09 -0700532 const char* var = name;
533 // Work around an unfortunate name mismatch.
534 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535
Elliott Hughes253c18d2015-03-18 22:47:09 -0700536 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800537 if (out == 0) return -1;
538
539 for(n = 0; n < count; n++) {
540 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700541 if (out[n] == 0) {
542 for(size_t i = 0; i < n; ++i) {
543 free((char*) out[i]);
544 }
545 free(out);
546 return -1;
547 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548 }
549
Elliott Hughes253c18d2015-03-18 22:47:09 -0700550 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800551 return 0;
552}
553
Elliott Hughesfc797672015-04-07 20:12:50 -0700554static void setup_requirements(char* data, int64_t sz) {
555 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800556 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700557 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800558 *s++ = 0;
559 if (setup_requirement_line(data)) {
560 die("out of memory");
561 }
562 data = s;
563 } else {
564 s++;
565 }
566 }
567}
568
Elliott Hughesfc797672015-04-07 20:12:50 -0700569static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800570 fb_queue_notice("--------------------------------------------");
571 fb_queue_display("version-bootloader", "Bootloader Version...");
572 fb_queue_display("version-baseband", "Baseband Version.....");
573 fb_queue_display("serialno", "Serial Number........");
574 fb_queue_notice("--------------------------------------------");
575}
576
Rom Lemarchand622810c2013-06-28 09:54:59 -0700577static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700578{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700579 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700580 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700581 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700582 }
583
Elliott Hughesfc797672015-04-07 20:12:50 -0700584 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700585 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700586 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700587 }
588
Elliott Hughes253c18d2015-03-18 22:47:09 -0700589 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700590 if (!out_s) {
591 die("Failed to allocate sparse file array\n");
592 }
593
594 files = sparse_file_resparse(s, max_size, out_s, files);
595 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700596 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700597 }
598
599 return out_s;
600}
601
David Pursell0b156632015-10-30 11:22:01 -0700602static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700603 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700604 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
605 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800606 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700607 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700608 }
609
Elliott Hughes77c0e662015-11-02 15:51:12 -0800610 // Some bootloaders (angler, for example) send spurious whitespace too.
611 max_download_size = android::base::Trim(max_download_size);
612
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700613 uint64_t limit;
614 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800615 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700616 return 0;
617 }
618 if (limit > 0) {
619 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
620 }
Colin Crossf8387882012-05-24 17:18:41 -0700621 return limit;
622}
623
David Pursell0b156632015-10-30 11:22:01 -0700624static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700625 int64_t limit;
626
627 if (sparse_limit == 0) {
628 return 0;
629 } else if (sparse_limit > 0) {
630 limit = sparse_limit;
631 } else {
632 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700633 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700634 }
635 if (target_sparse_limit > 0) {
636 limit = target_sparse_limit;
637 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700638 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700639 }
640 }
641
642 if (size > limit) {
643 return limit;
644 }
645
646 return 0;
647}
648
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700649// Until we get lazy inode table init working in make_ext4fs, we need to
650// erase partitions of type ext4 before flashing a filesystem so no stale
651// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700652static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800653 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700654 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800655 return false;
656 }
657 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700658}
659
David Pursell0b156632015-10-30 11:22:01 -0700660static int load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700661 int64_t sz = get_file_size(fd);
662 if (sz == -1) {
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000663 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700664 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700665
Elliott Hughesfc797672015-04-07 20:12:50 -0700666 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700667 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700668 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700669 sparse_file** s = load_sparse_files(fd, limit);
670 if (s == nullptr) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700671 return -1;
Colin Crossf8387882012-05-24 17:18:41 -0700672 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700673 buf->type = FB_BUFFER_SPARSE;
674 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700675 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700676 void* data = load_fd(fd, &sz);
677 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700678 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700679 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000680 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700681 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700682
683 return 0;
684}
685
David Pursell0b156632015-10-30 11:22:01 -0700686static int load_buf(Transport* transport, const char *fname, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700687{
688 int fd;
689
690 fd = open(fname, O_RDONLY | O_BINARY);
691 if (fd < 0) {
Daniel Rosenberg82280592014-04-29 13:45:05 -0700692 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700693 }
694
David Pursell0b156632015-10-30 11:22:01 -0700695 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700696}
697
698static void flash_buf(const char *pname, struct fastboot_buffer *buf)
699{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700700 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700701
702 switch (buf->type) {
703 case FB_BUFFER_SPARSE:
Elliott Hughes253c18d2015-03-18 22:47:09 -0700704 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700705 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700706 int64_t sz = sparse_file_len(*s, true, false);
707 fb_queue_flash_sparse(pname, *s++, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700708 }
709 break;
710 case FB_BUFFER:
711 fb_queue_flash(pname, buf->data, buf->sz);
712 break;
713 default:
714 die("unknown buffer type: %d", buf->type);
715 }
716}
717
David Pursell0b156632015-10-30 11:22:01 -0700718static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700719 std::vector<std::string> suffixes;
720 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700721 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700722 die("Could not get suffixes.\n");
723 }
724 return android::base::Split(suffix_list, ",");
725}
726
David Pursell0b156632015-10-30 11:22:01 -0700727static std::string verify_slot(Transport* transport, const char *slot) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700728 if (strcmp(slot, "all") == 0) {
729 return "all";
730 }
David Pursell0b156632015-10-30 11:22:01 -0700731 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700732 for (const std::string &suffix : suffixes) {
733 if (suffix == slot)
734 return slot;
735 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800736 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700737 for (const std::string &suffix : suffixes) {
738 fprintf(stderr, "%s\n", suffix.c_str());
739 }
740 exit(1);
741}
742
David Pursell0b156632015-10-30 11:22:01 -0700743static void do_for_partition(Transport* transport, const char *part, const char *slot,
744 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800745 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700746 std::string current_slot;
747
David Pursell0b156632015-10-30 11:22:01 -0700748 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800749 /* If has-slot is not supported, the answer is no. */
750 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700751 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800752 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700753 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700754 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700755 die("Failed to identify current slot.\n");
756 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800757 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700758 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800759 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700760 }
761 } else {
762 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700763 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
764 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700765 }
766 func(part);
767 }
768}
769
David Pursell0b156632015-10-30 11:22:01 -0700770/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
771 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
772 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
773 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700774 */
David Pursell0b156632015-10-30 11:22:01 -0700775static void do_for_partitions(Transport* transport, const char *part, const char *slot,
776 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800777 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700778
779 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700780 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700781 die("Could not check if partition %s has slot.", part);
782 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800783 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700784 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700785 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700786 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700787 }
788 } else {
David Pursell0b156632015-10-30 11:22:01 -0700789 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700790 }
791 } else {
David Pursell0b156632015-10-30 11:22:01 -0700792 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700793 }
794}
795
David Pursell0b156632015-10-30 11:22:01 -0700796static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700797 struct fastboot_buffer buf;
798
David Pursell0b156632015-10-30 11:22:01 -0700799 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800 die("cannot load '%s'", fname);
801 }
802 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700803}
804
Elliott Hughesfc797672015-04-07 20:12:50 -0700805static void do_update_signature(ZipArchiveHandle zip, char* fn) {
806 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700807 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700808 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800809 fb_queue_download("signature", data, sz);
810 fb_queue_command("signature", "installing signature");
811}
812
David Pursell0b156632015-10-30 11:22:01 -0700813static 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 -0800814 queue_info_dump();
815
Wink Savilleb98762f2011-04-04 17:54:59 -0700816 fb_queue_query_save("product", cur_product, sizeof(cur_product));
817
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700818 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700819 int error = OpenArchive(filename, &zip);
820 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100821 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700822 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700823 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800824
Elliott Hughesfc797672015-04-07 20:12:50 -0700825 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700826 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700827 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100828 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -0700829 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800830 }
831
Elliott Hughes253c18d2015-03-18 22:47:09 -0700832 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800833
Elliott Hughesacdbe922015-06-02 21:37:05 -0700834 for (size_t i = 0; i < ARRAY_SIZE(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700835 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700836 if (fd == -1) {
837 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700838 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -0700839 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +0100840 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -0700841 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700842 }
Elliott Hughes253c18d2015-03-18 22:47:09 -0700843 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700844 int rc = load_buf_fd(transport, fd, &buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700845 if (rc) die("cannot load %s from flash", images[i].img_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700846
847 auto update = [&](const std::string &partition) {
848 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -0700849 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700850 fb_queue_erase(partition.c_str());
851 }
852 flash_buf(partition.c_str(), &buf);
853 /* not closing the fd here since the sparse code keeps the fd around
854 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
855 * program exits.
856 */
857 };
David Pursell0b156632015-10-30 11:22:01 -0700858 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800859 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700860
861 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800862}
863
Elliott Hughesfc797672015-04-07 20:12:50 -0700864static void do_send_signature(char* fn) {
865 char* xtn = strrchr(fn, '.');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800866 if (!xtn) return;
Elliott Hughesfc797672015-04-07 20:12:50 -0700867
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800868 if (strcmp(xtn, ".img")) return;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800869
Elliott Hughesfc797672015-04-07 20:12:50 -0700870 strcpy(xtn, ".sig");
871
872 int64_t sz;
873 void* data = load_file(fn, &sz);
874 strcpy(xtn, ".img");
875 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800876 fb_queue_download("signature", data, sz);
877 fb_queue_command("signature", "installing signature");
878}
879
David Pursell0b156632015-10-30 11:22:01 -0700880static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800881 queue_info_dump();
882
Wink Savilleb98762f2011-04-04 17:54:59 -0700883 fb_queue_query_save("product", cur_product, sizeof(cur_product));
884
Elliott Hughes253c18d2015-03-18 22:47:09 -0700885 char* fname = find_item("info", product);
Elliott Hughesfc797672015-04-07 20:12:50 -0700886 if (fname == nullptr) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800887
Elliott Hughesfc797672015-04-07 20:12:50 -0700888 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700889 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700890 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -0700891
892 setup_requirements(reinterpret_cast<char*>(data), sz);
893
894 for (size_t i = 0; i < ARRAY_SIZE(images); i++) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700895 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -0700896 fastboot_buffer buf;
David Pursell0b156632015-10-30 11:22:01 -0700897 if (load_buf(transport, fname, &buf)) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700898 if (images[i].is_optional)
899 continue;
900 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700901 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700902
903 auto flashall = [&](const std::string &partition) {
904 do_send_signature(fname);
David Pursell0b156632015-10-30 11:22:01 -0700905 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700906 fb_queue_erase(partition.c_str());
907 }
908 flash_buf(partition.c_str(), &buf);
909 };
David Pursell0b156632015-10-30 11:22:01 -0700910 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800911 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800912}
913
914#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -0800915#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800916
Elliott Hughes45be42e2015-09-02 20:15:48 -0700917static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -0700918{
Patrick Tjin51e8b032015-09-01 08:15:23 -0700919 if (argc <= 2) return 0;
920 skip(2);
921
922 /*
923 * Process unlock_bootloader, we have to load the message file
924 * and send that to the remote device.
925 */
926 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -0700927
928 int64_t sz;
929 void* data = load_file(*argv, &sz);
930 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -0700931 fb_queue_download("unlock_message", data, sz);
932 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
933 skip(1);
934 return 0;
935}
936
Elliott Hughes45be42e2015-09-02 20:15:48 -0700937static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800938{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800939 char command[256];
940 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800941
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800942 command[0] = 0;
943 while(1) {
944 strcat(command,*argv);
945 skip(1);
946 if(argc == 0) break;
947 strcat(command," ");
948 }
949
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800950 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800951 return 0;
952}
953
Colin Crossf8387882012-05-24 17:18:41 -0700954static int64_t parse_num(const char *arg)
955{
956 char *endptr;
957 unsigned long long num;
958
959 num = strtoull(arg, &endptr, 0);
960 if (endptr == arg) {
961 return -1;
962 }
963
964 if (*endptr == 'k' || *endptr == 'K') {
965 if (num >= (-1ULL) / 1024) {
966 return -1;
967 }
968 num *= 1024LL;
969 endptr++;
970 } else if (*endptr == 'm' || *endptr == 'M') {
971 if (num >= (-1ULL) / (1024 * 1024)) {
972 return -1;
973 }
974 num *= 1024LL * 1024LL;
975 endptr++;
976 } else if (*endptr == 'g' || *endptr == 'G') {
977 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
978 return -1;
979 }
980 num *= 1024LL * 1024LL * 1024LL;
981 endptr++;
982 }
983
984 if (*endptr != '\0') {
985 return -1;
986 }
987
988 if (num > INT64_MAX) {
989 return -1;
990 }
991
992 return num;
993}
994
David Pursell0b156632015-10-30 11:22:01 -0700995static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700996 const char* partition, int skip_if_not_supported,
997 const char* type_override, const char* size_override) {
998 std::string partition_type, partition_size;
999
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001000 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001001 const char* errMsg = nullptr;
1002 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001003 int fd;
1004
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001005 unsigned int limit = INT_MAX;
1006 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001007 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001008 }
1009 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001010 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001011 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001012
David Pursell0b156632015-10-30 11:22:01 -07001013 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001014 errMsg = "Can't determine partition type.\n";
1015 goto failed;
1016 }
JP Abgrall7e859742014-05-06 15:14:15 -07001017 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001018 if (partition_type != type_override) {
1019 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1020 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001021 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001022 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001023 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001024
David Pursell0b156632015-10-30 11:22:01 -07001025 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001026 errMsg = "Unable to get partition size\n";
1027 goto failed;
1028 }
JP Abgrall7e859742014-05-06 15:14:15 -07001029 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001030 if (partition_size != size_override) {
1031 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1032 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001033 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001034 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001035 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001036 // Some bootloaders (angler, for example), send spurious leading whitespace.
1037 partition_size = android::base::Trim(partition_size);
1038 // Some bootloaders (hammerhead, for example) use implicit hex.
1039 // This code used to use strtol with base 16.
1040 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001041
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001042 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001043 if (!gen) {
1044 if (skip_if_not_supported) {
1045 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001046 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001047 return;
1048 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001049 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1050 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001051 return;
1052 }
1053
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001054 int64_t size;
1055 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1056 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1057 return;
1058 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001059
1060 fd = fileno(tmpfile());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001061 if (fs_generator_generate(gen, fd, size)) {
1062 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001063 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001064 return;
1065 }
1066
David Pursell0b156632015-10-30 11:22:01 -07001067 if (load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001068 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001069 close(fd);
1070 return;
1071 }
1072 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001073 return;
1074
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001075failed:
1076 if (skip_if_not_supported) {
1077 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001078 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001079 }
1080 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
1081}
1082
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001083int main(int argc, char **argv)
1084{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001085 bool wants_wipe = false;
1086 bool wants_reboot = false;
1087 bool wants_reboot_bootloader = false;
1088 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001089 bool erase_first = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001090 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001091 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001092 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001093 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001094 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001095
JP Abgrall7b8970c2013-03-07 17:06:41 -08001096 const struct option longopts[] = {
1097 {"base", required_argument, 0, 'b'},
1098 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001099 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001100 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001101 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001102 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001103 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001104 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001105 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001106 {"help", no_argument, 0, 'h'},
1107 {"unbuffered", no_argument, 0, 0},
1108 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001109 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001110 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001111 {"set-active", optional_argument, 0, 'a'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001112 {0, 0, 0, 0}
1113 };
Colin Cross8879f982012-05-22 17:53:34 -07001114
1115 serial = getenv("ANDROID_SERIAL");
1116
1117 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001118 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 -07001119 if (c < 0) {
1120 break;
1121 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001122 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001123 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001124 case 'a':
1125 wants_set_active = true;
1126 if (optarg)
1127 next_active = optarg;
1128 break;
Colin Cross8879f982012-05-22 17:53:34 -07001129 case 'b':
1130 base_addr = strtoul(optarg, 0, 16);
1131 break;
Colin Cross8879f982012-05-22 17:53:34 -07001132 case 'c':
1133 cmdline = optarg;
1134 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001135 case 'h':
1136 usage();
1137 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001138 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001139 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001140 unsigned long val;
1141
1142 val = strtoul(optarg, &endptr, 0);
1143 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1144 die("invalid vendor id '%s'", optarg);
1145 vendor_id = (unsigned short)val;
1146 break;
1147 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001148 case 'k':
1149 kernel_offset = strtoul(optarg, 0, 16);
1150 break;
1151 case 'l':
1152 long_listing = 1;
1153 break;
1154 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001155 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001156 if (!page_size) die("invalid page size");
1157 break;
1158 case 'p':
1159 product = optarg;
1160 break;
1161 case 'r':
1162 ramdisk_offset = strtoul(optarg, 0, 16);
1163 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001164 case 't':
1165 tags_offset = strtoul(optarg, 0, 16);
1166 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001167 case 's':
1168 serial = optarg;
1169 break;
1170 case 'S':
1171 sparse_limit = parse_num(optarg);
1172 if (sparse_limit < 0) {
1173 die("invalid sparse limit");
1174 }
1175 break;
1176 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001177 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001178 break;
1179 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001180 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001181 break;
Colin Cross8879f982012-05-22 17:53:34 -07001182 case '?':
1183 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001184 case 0:
1185 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001186 setvbuf(stdout, nullptr, _IONBF, 0);
1187 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001188 } else if (strcmp("version", longopts[longindex].name) == 0) {
1189 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1190 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001191 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1192 slot_override = std::string(optarg);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001193 }
1194 break;
Colin Cross8879f982012-05-22 17:53:34 -07001195 default:
1196 abort();
1197 }
1198 }
1199
1200 argc -= optind;
1201 argv += optind;
1202
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001203 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001204 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001205 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001206 }
1207
Colin Cross8fb6e062012-07-24 16:36:41 -07001208 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001209 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001210 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001211 return 0;
1212 }
1213
Colin Crossc7b75dc2012-08-29 18:17:06 -07001214 if (argc > 0 && !strcmp(*argv, "help")) {
1215 usage();
1216 return 0;
1217 }
1218
David Pursell0b156632015-10-30 11:22:01 -07001219 Transport* transport = open_device();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001220 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001221 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001222 if (next_active != "")
David Pursell0b156632015-10-30 11:22:01 -07001223 next_active = verify_slot(transport, next_active.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001224
1225 if (wants_set_active) {
1226 if (next_active == "") {
1227 if (slot_override == "") {
1228 wants_set_active = false;
1229 } else {
1230 next_active = slot_override;
1231 }
1232 }
1233 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001234
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001236 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001237 require(2);
1238 fb_queue_display(argv[1], argv[1]);
1239 skip(2);
1240 } else if(!strcmp(*argv, "erase")) {
1241 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001242
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001243 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001244 std::string partition_type;
1245 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001246 fs_get_generator(partition_type) != nullptr) {
1247 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1248 partition_type.c_str());
1249 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001250
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001251 fb_queue_erase(partition.c_str());
1252 };
David Pursell0b156632015-10-30 11:22:01 -07001253 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001255 } else if(!strncmp(*argv, "format", strlen("format"))) {
1256 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001257 char *type_override = nullptr;
1258 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001259 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001260 /*
1261 * Parsing for: "format[:[type][:[size]]]"
1262 * Some valid things:
1263 * - select ontly the size, and leave default fs type:
1264 * format::0x4000000 userdata
1265 * - default fs type and size:
1266 * format userdata
1267 * format:: userdata
1268 */
1269 overrides = strchr(*argv, ':');
1270 if (overrides) {
1271 overrides++;
1272 size_override = strchr(overrides, ':');
1273 if (size_override) {
1274 size_override[0] = '\0';
1275 size_override++;
1276 }
1277 type_override = overrides;
1278 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001279 if (type_override && !type_override[0]) type_override = nullptr;
1280 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001281
1282 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001283 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001284 fb_queue_erase(partition.c_str());
1285 }
David Pursell0b156632015-10-30 11:22:01 -07001286 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001287 };
David Pursell0b156632015-10-30 11:22:01 -07001288 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001289 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001290 } else if(!strcmp(*argv, "signature")) {
1291 require(2);
1292 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001293 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001294 if (sz != 256) die("signature must be 256 bytes");
1295 fb_queue_download("signature", data, sz);
1296 fb_queue_command("signature", "installing signature");
1297 skip(2);
1298 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001299 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001300 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001301 if (argc > 0) {
1302 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001303 wants_reboot = false;
1304 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001305 skip(1);
1306 }
1307 }
1308 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001309 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001310 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001311 skip(1);
1312 } else if (!strcmp(*argv, "continue")) {
1313 fb_queue_command("continue", "resuming boot");
1314 skip(1);
1315 } else if(!strcmp(*argv, "boot")) {
1316 char *kname = 0;
1317 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001318 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001319 skip(1);
1320 if (argc > 0) {
1321 kname = argv[0];
1322 skip(1);
1323 }
1324 if (argc > 0) {
1325 rname = argv[0];
1326 skip(1);
1327 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001328 if (argc > 0) {
1329 sname = argv[0];
1330 skip(1);
1331 }
1332 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001333 if (data == 0) return 1;
1334 fb_queue_download("boot.img", data, sz);
1335 fb_queue_command("boot", "booting");
1336 } else if(!strcmp(*argv, "flash")) {
1337 char *pname = argv[1];
1338 char *fname = 0;
1339 require(2);
1340 if (argc > 2) {
1341 fname = argv[2];
1342 skip(3);
1343 } else {
1344 fname = find_item(pname, product);
1345 skip(2);
1346 }
1347 if (fname == 0) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001348
1349 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001350 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001351 fb_queue_erase(partition.c_str());
1352 }
David Pursell0b156632015-10-30 11:22:01 -07001353 do_flash(transport, partition.c_str(), fname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001354 };
David Pursell0b156632015-10-30 11:22:01 -07001355 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001356 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001357 char *kname = argv[2];
1358 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001359 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001360 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001361 skip(3);
1362 if (argc > 0) {
1363 rname = argv[0];
1364 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001365 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001366 if (argc > 0) {
1367 sname = argv[0];
1368 skip(1);
1369 }
1370 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001371 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001372 auto flashraw = [&](const std::string &partition) {
1373 fb_queue_flash(partition.c_str(), data, sz);
1374 };
David Pursell0b156632015-10-30 11:22:01 -07001375 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001376 } else if(!strcmp(*argv, "flashall")) {
1377 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001378 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001379 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001380 } else if(!strcmp(*argv, "update")) {
1381 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001382 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001383 skip(2);
1384 } else {
David Pursell0b156632015-10-30 11:22:01 -07001385 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001386 skip(1);
1387 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001388 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001389 } else if(!strcmp(*argv, "oem")) {
1390 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001391 } else if(!strcmp(*argv, "flashing")) {
1392 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1393 !strcmp(*(argv+1), "lock") ||
1394 !strcmp(*(argv+1), "unlock_critical") ||
1395 !strcmp(*(argv+1), "lock_critical") ||
1396 !strcmp(*(argv+1), "get_unlock_ability") ||
1397 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1398 !strcmp(*(argv+1), "lock_bootloader"))) {
1399 argc = do_oem_command(argc, argv);
1400 } else
1401 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1402 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001403 } else {
1404 usage();
1405 return 1;
1406 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001407 } else {
1408 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001409 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001410 }
1411 }
1412
1413 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001415 fb_queue_erase("userdata");
David Pursell0b156632015-10-30 11:22:01 -07001416 fb_perform_format(transport, "userdata", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001417
1418 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001419 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001420 fprintf(stderr, "wiping cache...\n");
1421 fb_queue_erase("cache");
David Pursell0b156632015-10-30 11:22:01 -07001422 fb_perform_format(transport, "cache", 1, nullptr, nullptr);
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001423 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001424 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001425 if (wants_set_active) {
1426 fb_set_active(next_active.c_str());
1427 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001428 if (wants_reboot) {
1429 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001430 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001431 } else if (wants_reboot_bootloader) {
1432 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001433 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001434 }
1435
David Pursell0b156632015-10-30 11:22:01 -07001436 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001437}