blob: 5f2267c6330143c32796851946c378fea9868919 [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
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070081static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050082// Don't resparse files in too-big chunks.
83// libsparse will support INT_MAX, but this results in large allocations, so
84// let's keep it at 1GB to avoid memory pressure on the host.
85static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t sparse_limit = -1;
87static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughesfc797672015-04-07 20:12:50 -070089static unsigned page_size = 2048;
90static unsigned base_addr = 0x10000000;
91static unsigned kernel_offset = 0x00008000;
92static unsigned ramdisk_offset = 0x01000000;
93static unsigned second_offset = 0x00f00000;
94static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080095
David Zeuthenb6ea4352017-08-07 14:29:26 -040096static bool g_disable_verity = false;
97static bool g_disable_verification = false;
98
Paul Crowley8f7f56e2015-11-27 09:29:37 +000099static const std::string convert_fbe_marker_filename("convert_fbe");
100
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500102 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103 FB_BUFFER_SPARSE,
104};
105
106struct fastboot_buffer {
107 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700108 void* data;
109 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500110 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
112
113static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700114 const char* nickname;
115 const char* img_name;
116 const char* sig_name;
117 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700118 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700119 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700120} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700121 // clang-format off
122 { "boot", "boot.img", "boot.sig", "boot", false, false },
123 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
124 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700125 { "dts", "dt.img", "dt.sig", "dts", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700126 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
127 { "system", "system.img", "system.sig", "system", false, false },
128 { nullptr, "system_other.img", "system.sig", "system", true, true },
129 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
130 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
131 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
132 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700133};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700134
Elliott Hughes45964a82017-05-03 22:43:23 -0700135static std::string find_item_given_name(const char* img_name) {
136 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700137 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700138 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700139 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700140 return android::base::StringPrintf("%s/%s", dir, img_name);
141}
142
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700143static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700144 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700145 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700146 return find_item_given_name(images[i].img_name);
147 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 }
149
Elliott Hughesd6365a72017-05-08 18:04:49 -0700150 if (item == "userdata") return find_item_given_name("userdata.img");
151 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700152
Elliott Hughesd6365a72017-05-08 18:04:49 -0700153 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700154 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155}
156
Elliott Hughesfc797672015-04-07 20:12:50 -0700157static int64_t get_file_size(int fd) {
158 struct stat sb;
159 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700160}
161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800163 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700164 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 *sz = get_file_size(fd);
167 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700168 goto oops;
169 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 data = (char*) malloc(*sz);
172 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173
Elliott Hughesfc797672015-04-07 20:12:50 -0700174 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 close(fd);
176
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 return data;
178
179oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800180 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 close(fd);
182 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800183 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184 return 0;
185}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Elliott Hughes2810d002016-04-25 14:31:18 -0700187static void* load_file(const std::string& path, int64_t* sz) {
188 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 if (fd == -1) return nullptr;
190 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700191}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Elliott Hughesfc797672015-04-07 20:12:50 -0700193static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700194 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700195 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700196 return -1;
197 }
198
199 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
200 return -1;
201 }
202
Scott Anderson13081c62012-04-06 12:39:30 -0700203 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700205 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
206 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 return 0;
208}
209
Elliott Hughesfc797672015-04-07 20:12:50 -0700210static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800211 return match_fastboot_with_serial(info, serial);
212}
213
Elliott Hughesfc797672015-04-07 20:12:50 -0700214static int list_devices_callback(usb_ifc_info* info) {
215 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800216 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700217 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if (!serial[0]) {
221 serial = "????????????";
222 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700223 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700224 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700226 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%-22s fastboot", serial.c_str());
228 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700229 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800230 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
232
233 return -1;
234}
235
David Pursell2ec418a2016-01-20 08:32:08 -0800236// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
237// a specific device, otherwise the first USB device found will be used.
238//
239// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
240// Otherwise it blocks until the target is available.
241//
242// The returned Transport is a singleton, so multiple calls to this function will return the same
243// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700244static Transport* open_device() {
245 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800246 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247
David Pursell2ec418a2016-01-20 08:32:08 -0800248 if (transport != nullptr) {
249 return transport;
250 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800251
David Pursell4601c972016-02-05 15:35:09 -0800252 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800253 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800254 int port = 0;
255 if (serial != nullptr) {
256 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800257
David Pursell4601c972016-02-05 15:35:09 -0800258 if (android::base::StartsWith(serial, "tcp:")) {
259 protocol = Socket::Protocol::kTcp;
260 port = tcp::kDefaultPort;
261 net_address = serial + strlen("tcp:");
262 } else if (android::base::StartsWith(serial, "udp:")) {
263 protocol = Socket::Protocol::kUdp;
264 port = udp::kDefaultPort;
265 net_address = serial + strlen("udp:");
266 }
267
268 if (net_address != nullptr) {
269 std::string error;
270 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
271 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
272 error.c_str());
273 return nullptr;
274 }
David Pursell2ec418a2016-01-20 08:32:08 -0800275 }
276 }
277
278 while (true) {
279 if (!host.empty()) {
280 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800281 if (protocol == Socket::Protocol::kTcp) {
282 transport = tcp::Connect(host, port, &error).release();
283 } else if (protocol == Socket::Protocol::kUdp) {
284 transport = udp::Connect(host, port, &error).release();
285 }
286
David Pursell2ec418a2016-01-20 08:32:08 -0800287 if (transport == nullptr && announce) {
288 fprintf(stderr, "error: %s\n", error.c_str());
289 }
290 } else {
291 transport = usb_open(match_fastboot);
292 }
293
294 if (transport != nullptr) {
295 return transport;
296 }
297
David Pursell0b156632015-10-30 11:22:01 -0700298 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800299 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700300 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800302 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
304}
305
Elliott Hughesfc797672015-04-07 20:12:50 -0700306static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 // We don't actually open a USB device here,
308 // just getting our callback called so we can
309 // list all the connected devices.
310 usb_open(list_devices_callback);
311}
312
Elliott Hughesd6365a72017-05-08 18:04:49 -0700313static void syntax_error(const char* fmt, ...) {
314 fprintf(stderr, "fastboot: usage: ");
315
316 va_list ap;
317 va_start(ap, fmt);
318 vfprintf(stderr, fmt, ap);
319 va_end(ap);
320
321 fprintf(stderr, "\n");
322 exit(1);
323}
324
325static int show_help() {
326 // clang-format off
327 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
329 "usage: fastboot [ <option> ] <command>\n"
330 "\n"
331 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700332 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700333 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700334 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700335 " if found -- recovery. If the device\n"
336 " supports slots, the slot that has\n"
337 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700338 " Secondary images may be flashed to\n"
339 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700340 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
341 " flashing lock Locks the device. Prevents flashing.\n"
342 " flashing unlock Unlocks the device. Allows flashing\n"
343 " any partition except\n"
344 " bootloader-related partitions.\n"
345 " flashing lock_critical Prevents flashing bootloader-related\n"
346 " partitions.\n"
347 " flashing unlock_critical Enables flashing bootloader-related\n"
348 " partitions.\n"
349 " flashing get_unlock_ability Queries bootloader to see if the\n"
350 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700351 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700352 " unlock nonce.\n"
353 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700354 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700355 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700356 " erase <partition> Erase a flash partition.\n"
357 " format[:[<fs type>][:[<size>]] <partition>\n"
358 " Format a flash partition. Can\n"
359 " override the fs type and/or size\n"
360 " the bootloader reports.\n"
361 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700362 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800363 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700364 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
Elliott Hughesd6365a72017-05-08 18:04:49 -0700365 " flash:raw <bootable-partition> <kernel> [ <ramdisk> [ <second> ] ]\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " Create bootimage and flash it.\n"
367 " devices [-l] List all connected devices [with\n"
368 " device paths].\n"
369 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800370 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700371 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800372 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
373 " stage <infile> Sends contents of <infile> to stage for\n"
374 " the next command. Supported only on\n"
375 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800376 " get_staged <outfile> Receives data to <outfile> staged by the\n"
377 " last command. Supported only on Android\n"
378 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700379 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 "\n"
381 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700382 " -w Erase userdata and cache (and format\n"
383 " if supported by partition type).\n"
384 " -u Do not erase partition before\n"
385 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800386 " -s <specific device> Specify a device. For USB, provide either\n"
387 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700388 " For ethernet, provide an address in the\n"
389 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800390 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700391 " -c <cmdline> Override kernel commandline.\n"
392 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800393 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700394 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800395 " --kernel-offset Specify a custom kernel offset.\n"
396 " (default: 0x00008000)\n"
397 " --ramdisk-offset Specify a custom ramdisk offset.\n"
398 " (default: 0x01000000)\n"
399 " --tags-offset Specify a custom tags offset.\n"
400 " (default: 0x00000100)\n"
401 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700402 " (default: 2048).\n"
403 " -S <size>[K|M|G] Automatically sparse files greater\n"
404 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700405 " --slot <slot> Specify slot name to be used if the\n"
406 " device supports slots. All operations\n"
407 " on partitions that support slots will\n"
408 " be done on the slot specified.\n"
409 " 'all' can be given to refer to all slots.\n"
410 " 'other' can be given to refer to a\n"
411 " non-current slot. If this flag is not\n"
412 " used, slotted partitions will default\n"
413 " to the current active slot.\n"
414 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800415 " provided, this will default to the value\n"
416 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800417 " supported, this does nothing. This will\n"
418 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700419 " --skip-secondary Will not flash secondary slots when\n"
420 " performing a flashall or update. This\n"
421 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700422 " --skip-reboot Will not reboot the device when\n"
423 " performing commands that normally\n"
424 " trigger a reboot.\n"
David Zeuthenb6ea4352017-08-07 14:29:26 -0400425 " --disable-verity Set the disable-verity flag in the\n"
426 " the vbmeta image being flashed.\n"
427 " --disable-verification Set the disable-verification flag in"
428 " the vbmeta image being flashed.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000429#if !defined(_WIN32)
430 " --wipe-and-use-fbe On devices which support it,\n"
431 " erase userdata and cache, and\n"
432 " enable file-based encryption\n"
433#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800434 " --unbuffered Do not buffer input or output.\n"
435 " --version Display version.\n"
436 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700438 // clang-format off
439 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000441
Elliott Hughesd6365a72017-05-08 18:04:49 -0700442static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
443 const std::string& second_stage, int64_t* sz,
Elliott Hughesfc797672015-04-07 20:12:50 -0700444 const char* cmdline) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700445 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700446 void* kdata = load_file(kernel.c_str(), &ksize);
447 if (kdata == nullptr) die("cannot load '%s': %s\n", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800448
Elliott Hughesfc797672015-04-07 20:12:50 -0700449 // Is this actually a boot image?
Elliott Hughesd6365a72017-05-08 18:04:49 -0700450 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700451 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800452
Elliott Hughesd6365a72017-05-08 18:04:49 -0700453 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk\n");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800454
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 *sz = ksize;
456 return kdata;
457 }
458
Elliott Hughesfc797672015-04-07 20:12:50 -0700459 void* rdata = nullptr;
460 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700461 if (!ramdisk.empty()) {
462 rdata = load_file(ramdisk.c_str(), &rsize);
463 if (rdata == nullptr) die("cannot load '%s': %s\n", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 }
465
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 void* sdata = nullptr;
467 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700468 if (!second_stage.empty()) {
469 sdata = load_file(second_stage.c_str(), &ssize);
470 if (sdata == nullptr) die("cannot load '%s': %s\n", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200471 }
472
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700474 int64_t bsize = 0;
475 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800476 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200477 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800478 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesd6365a72017-05-08 18:04:49 -0700479 if (bdata == nullptr) die("failed to create boot.img\n");
480
Elliott Hughesfc797672015-04-07 20:12:50 -0700481 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
482 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800484
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 return bdata;
486}
487
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700488static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700489 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700490 ZipEntry zip_entry;
491 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700492 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000493 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 }
495
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700496 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700498 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700499 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700500 if (data == nullptr) {
501 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000502 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 }
504
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700505 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
506 if (error != 0) {
507 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000509 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000511
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 return data;
513}
514
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700515#if defined(_WIN32)
516
517// TODO: move this to somewhere it can be shared.
518
519#include <windows.h>
520
521// Windows' tmpfile(3) requires administrator rights because
522// it creates temporary files in the root directory.
523static FILE* win32_tmpfile() {
524 char temp_path[PATH_MAX];
525 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
526 if (nchars == 0 || nchars >= sizeof(temp_path)) {
527 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
528 return nullptr;
529 }
530
531 char filename[PATH_MAX];
532 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
533 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
534 return nullptr;
535 }
536
537 return fopen(filename, "w+bTD");
538}
539
540#define tmpfile win32_tmpfile
541
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000542static std::string make_temporary_directory() {
543 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
544 return "";
545}
546
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700547static int make_temporary_fd() {
548 // TODO: reimplement to avoid leaking a FILE*.
549 return fileno(tmpfile());
550}
551
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000552#else
553
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700554static std::string make_temporary_template() {
555 const char* tmpdir = getenv("TMPDIR");
556 if (tmpdir == nullptr) tmpdir = P_tmpdir;
557 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
558}
559
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000560static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700561 std::string result(make_temporary_template());
562 if (mkdtemp(&result[0]) == nullptr) {
563 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000564 return "";
565 }
566 return result;
567}
568
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700569static int make_temporary_fd() {
570 std::string path_template(make_temporary_template());
571 int fd = mkstemp(&path_template[0]);
572 if (fd == -1) {
573 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
574 return -1;
575 }
576 unlink(path_template.c_str());
577 return fd;
578}
579
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700580#endif
581
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000582static std::string create_fbemarker_tmpdir() {
583 std::string dir = make_temporary_directory();
584 if (dir.empty()) {
585 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
586 return "";
587 }
588 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
589 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
590 if (fd == -1) {
591 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
592 marker_file.c_str(), errno, strerror(errno));
593 return "";
594 }
595 close(fd);
596 return dir;
597}
598
599static void delete_fbemarker_tmpdir(const std::string& dir) {
600 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
601 if (unlink(marker_file.c_str()) == -1) {
602 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
603 marker_file.c_str(), errno, strerror(errno));
604 return;
605 }
606 if (rmdir(dir.c_str()) == -1) {
607 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
608 dir.c_str(), errno, strerror(errno));
609 return;
610 }
611}
612
Elliott Hughes45964a82017-05-03 22:43:23 -0700613static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700614 unique_fd fd(make_temporary_fd());
615 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700616 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
617 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700618 return -1;
619 }
620
Yusuke Sato07447542015-06-25 14:39:19 -0700621 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700622 ZipEntry zip_entry;
623 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
624 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000625 return -1;
626 }
627
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700628 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
629 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700630 int error = ExtractEntryToFile(zip, &zip_entry, fd);
631 if (error != 0) {
632 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
633 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700634 }
635
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000636 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700637 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700638 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700639}
640
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641static char *strip(char *s)
642{
643 int n;
644 while(*s && isspace(*s)) s++;
645 n = strlen(s);
646 while(n-- > 0) {
647 if(!isspace(s[n])) break;
648 s[n] = 0;
649 }
650 return s;
651}
652
653#define MAX_OPTIONS 32
654static int setup_requirement_line(char *name)
655{
656 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700657 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 unsigned n, count;
659 char *x;
660 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800661
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 if (!strncmp(name, "reject ", 7)) {
663 name += 7;
664 invert = 1;
665 } else if (!strncmp(name, "require ", 8)) {
666 name += 8;
667 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700668 } else if (!strncmp(name, "require-for-product:", 20)) {
669 // Get the product and point name past it
670 prod = name + 20;
671 name = strchr(name, ' ');
672 if (!name) return -1;
673 *name = 0;
674 name += 1;
675 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 }
677
678 x = strchr(name, '=');
679 if (x == 0) return 0;
680 *x = 0;
681 val[0] = x + 1;
682
683 for(count = 1; count < MAX_OPTIONS; count++) {
684 x = strchr(val[count - 1],'|');
685 if (x == 0) break;
686 *x = 0;
687 val[count] = x + 1;
688 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800689
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 name = strip(name);
691 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800692
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 name = strip(name);
694 if (name == 0) return -1;
695
Elliott Hughes253c18d2015-03-18 22:47:09 -0700696 const char* var = name;
697 // Work around an unfortunate name mismatch.
698 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699
Elliott Hughes253c18d2015-03-18 22:47:09 -0700700 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 if (out == 0) return -1;
702
703 for(n = 0; n < count; n++) {
704 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700705 if (out[n] == 0) {
706 for(size_t i = 0; i < n; ++i) {
707 free((char*) out[i]);
708 }
709 free(out);
710 return -1;
711 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 }
713
Elliott Hughes253c18d2015-03-18 22:47:09 -0700714 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715 return 0;
716}
717
Elliott Hughesfc797672015-04-07 20:12:50 -0700718static void setup_requirements(char* data, int64_t sz) {
719 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700721 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 *s++ = 0;
723 if (setup_requirement_line(data)) {
724 die("out of memory");
725 }
726 data = s;
727 } else {
728 s++;
729 }
730 }
731}
732
Elliott Hughesfc797672015-04-07 20:12:50 -0700733static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734 fb_queue_notice("--------------------------------------------");
735 fb_queue_display("version-bootloader", "Bootloader Version...");
736 fb_queue_display("version-baseband", "Baseband Version.....");
737 fb_queue_display("serialno", "Serial Number........");
738 fb_queue_notice("--------------------------------------------");
739}
740
Rom Lemarchand622810c2013-06-28 09:54:59 -0700741static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700742{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700743 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700744 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700746 }
747
Elliott Hughesfc797672015-04-07 20:12:50 -0700748 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700749 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700750 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700751 }
752
Elliott Hughes253c18d2015-03-18 22:47:09 -0700753 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700754 if (!out_s) {
755 die("Failed to allocate sparse file array\n");
756 }
757
758 files = sparse_file_resparse(s, max_size, out_s, files);
759 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700760 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762
763 return out_s;
764}
765
David Pursell0b156632015-10-30 11:22:01 -0700766static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700767 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700768 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
769 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800770 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700771 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700772 }
773
Elliott Hughes77c0e662015-11-02 15:51:12 -0800774 // Some bootloaders (angler, for example) send spurious whitespace too.
775 max_download_size = android::base::Trim(max_download_size);
776
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700777 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700778 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800779 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700780 return 0;
781 }
782 if (limit > 0) {
783 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
784 }
Colin Crossf8387882012-05-24 17:18:41 -0700785 return limit;
786}
787
David Pursell0b156632015-10-30 11:22:01 -0700788static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700789 int64_t limit;
790
791 if (sparse_limit == 0) {
792 return 0;
793 } else if (sparse_limit > 0) {
794 limit = sparse_limit;
795 } else {
796 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700797 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700798 }
799 if (target_sparse_limit > 0) {
800 limit = target_sparse_limit;
801 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700802 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700803 }
804 }
805
806 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500807 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700808 }
809
810 return 0;
811}
812
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700813// Until we get lazy inode table init working in make_ext4fs, we need to
814// erase partitions of type ext4 before flashing a filesystem so no stale
815// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700816static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800817 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700818 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800819 return false;
820 }
821 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700822}
823
Elliott Hughes53ec4952016-05-11 12:39:27 -0700824static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700825 int64_t sz = get_file_size(fd);
826 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700827 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700828 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700829
Elliott Hughesfc797672015-04-07 20:12:50 -0700830 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700831 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700832 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700833 sparse_file** s = load_sparse_files(fd, limit);
834 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700835 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700836 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837 buf->type = FB_BUFFER_SPARSE;
838 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700839 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500840 buf->type = FB_BUFFER_FD;
841 buf->data = nullptr;
842 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000843 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700844 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700845
Elliott Hughes53ec4952016-05-11 12:39:27 -0700846 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700847}
848
Elliott Hughes53ec4952016-05-11 12:39:27 -0700849static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500850 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
851
Elliott Hughes53ec4952016-05-11 12:39:27 -0700852 if (fd == -1) {
853 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700854 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500855
856 struct stat s;
857 if (fstat(fd, &s)) {
858 return false;
859 }
860 if (!S_ISREG(s.st_mode)) {
861 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
862 return false;
863 }
864
865 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700866}
867
David Zeuthenb6ea4352017-08-07 14:29:26 -0400868static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
869 // Buffer needs to be at least the size of the VBMeta struct which
870 // is 256 bytes.
871 if (buf->sz < 256) {
872 return;
873 }
874
875 int fd = make_temporary_fd();
876 if (fd == -1) {
877 die("Failed to create temporary file for vbmeta rewriting");
878 }
879
880 std::string data;
881 if (!android::base::ReadFdToString(buf->fd, &data)) {
882 die("Failed reading from vbmeta");
883 }
884
885 // There's a 32-bit big endian |flags| field at offset 120 where
886 // bit 0 corresponds to disable-verity and bit 1 corresponds to
887 // disable-verification.
888 //
889 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
890 // the VBMeta struct.
891 if (g_disable_verity) {
892 data[123] |= 0x01;
893 }
894 if (g_disable_verification) {
895 data[123] |= 0x02;
896 }
897
898 if (!android::base::WriteStringToFd(data, fd)) {
899 die("Failed writing to modified vbmeta");
900 }
901 close(buf->fd);
902 buf->fd = fd;
903 lseek(fd, 0, SEEK_SET);
904}
905
Rom Lemarchand622810c2013-06-28 09:54:59 -0700906static void flash_buf(const char *pname, struct fastboot_buffer *buf)
907{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700908 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700909
David Zeuthenb6ea4352017-08-07 14:29:26 -0400910 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
911 if ((g_disable_verity || g_disable_verification) &&
912 (strcmp(pname, "vbmeta") == 0 || strcmp(pname, "vbmeta_a") == 0 ||
913 strcmp(pname, "vbmeta_b") == 0)) {
914 rewrite_vbmeta_buffer(buf);
915 }
916
Rom Lemarchand622810c2013-06-28 09:54:59 -0700917 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800918 case FB_BUFFER_SPARSE: {
919 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700920 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700921 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700922 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800923 sparse_files.emplace_back(*s, sz);
924 ++s;
925 }
926
927 for (size_t i = 0; i < sparse_files.size(); ++i) {
928 const auto& pair = sparse_files[i];
929 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700930 }
931 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800932 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500933 case FB_BUFFER_FD:
934 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700935 break;
936 default:
937 die("unknown buffer type: %d", buf->type);
938 }
939}
940
Daniel Rosenberg80919472016-06-30 19:25:31 -0700941static std::string get_current_slot(Transport* transport)
942{
943 std::string current_slot;
944 if (fb_getvar(transport, "current-slot", &current_slot)) {
945 if (current_slot == "_a") return "a"; // Legacy support
946 if (current_slot == "_b") return "b"; // Legacy support
947 return current_slot;
948 }
949 return "";
950}
951
952// Legacy support
953static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700954 std::vector<std::string> suffixes;
955 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700956 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700957 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700958 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700959 suffixes = android::base::Split(suffix_list, ",");
960 // Unfortunately some devices will return an error message in the
961 // guise of a valid value. If we only see only one suffix, it's probably
962 // not real.
963 if (suffixes.size() == 1) {
964 suffixes.clear();
965 }
966 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967}
968
Daniel Rosenberg80919472016-06-30 19:25:31 -0700969// Legacy support
970static bool supports_AB_obsolete(Transport* transport) {
971 return !get_suffixes_obsolete(transport).empty();
972}
973
974static int get_slot_count(Transport* transport) {
975 std::string var;
976 int count;
977 if (!fb_getvar(transport, "slot-count", &var)) {
978 if (supports_AB_obsolete(transport)) return 2; // Legacy support
979 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700980 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981 return count;
982}
983
Alex Lightbb9b8a52016-06-29 09:26:44 -0700984static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700986}
987
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700988// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989static std::string get_other_slot(const std::string& current_slot, int count) {
990 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700991
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992 char next = (current_slot[0] - 'a' + 1)%count + 'a';
993 return std::string(1, next);
994}
995
996static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
997 return get_other_slot(current_slot, get_slot_count(transport));
998}
999
1000static std::string get_other_slot(Transport* transport, int count) {
1001 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001002}
1003
Alex Lightbb9b8a52016-06-29 09:26:44 -07001004static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001005 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -07001006}
1007
Daniel Rosenberg80919472016-06-30 19:25:31 -07001008static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
1009 std::string slot = slot_name;
1010 if (slot == "_a") slot = "a"; // Legacy support
1011 if (slot == "_b") slot = "b"; // Legacy support
1012 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001013 if (allow_all) {
1014 return "all";
1015 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001016 int count = get_slot_count(transport);
1017 if (count > 0) {
1018 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001019 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001020 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001021 }
1022 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001023 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001024
Daniel Rosenberg80919472016-06-30 19:25:31 -07001025 int count = get_slot_count(transport);
1026 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001027
Daniel Rosenberg80919472016-06-30 19:25:31 -07001028 if (slot == "other") {
1029 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001030 if (other == "") {
1031 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001032 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001033 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001034 }
1035
Daniel Rosenberg80919472016-06-30 19:25:31 -07001036 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1037
1038 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1039 for (int i=0; i<count; i++) {
1040 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001041 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001042
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001043 exit(1);
1044}
1045
Daniel Rosenberg80919472016-06-30 19:25:31 -07001046static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001047 return verify_slot(transport, slot, true);
1048}
1049
Daniel Rosenberg80919472016-06-30 19:25:31 -07001050static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001051 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001052 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001053 std::string current_slot;
1054
Daniel Rosenberg80919472016-06-30 19:25:31 -07001055 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001056 /* If has-slot is not supported, the answer is no. */
1057 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001058 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001059 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001060 if (slot == "") {
1061 current_slot = get_current_slot(transport);
1062 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001063 die("Failed to identify current slot.\n");
1064 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001065 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001066 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001067 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001068 }
1069 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001070 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001071 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001072 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001073 }
1074 func(part);
1075 }
1076}
1077
David Pursell0b156632015-10-30 11:22:01 -07001078/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1079 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1080 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1081 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001082 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001083static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001084 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001085 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086
Daniel Rosenberg80919472016-06-30 19:25:31 -07001087 if (slot == "all") {
1088 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1089 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001090 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001091 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001092 for (int i=0; i < get_slot_count(transport); i++) {
1093 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001094 }
1095 } else {
David Pursell0b156632015-10-30 11:22:01 -07001096 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001097 }
1098 } else {
David Pursell0b156632015-10-30 11:22:01 -07001099 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001100 }
1101}
1102
David Pursell0b156632015-10-30 11:22:01 -07001103static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001104 struct fastboot_buffer buf;
1105
Elliott Hughes53ec4952016-05-11 12:39:27 -07001106 if (!load_buf(transport, fname, &buf)) {
1107 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001108 }
1109 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001110}
1111
Elliott Hughes45964a82017-05-03 22:43:23 -07001112static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001113 int64_t sz;
Elliott Hughes45964a82017-05-03 22:43:23 -07001114 void* data = unzip_file(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001115 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001116 fb_queue_download("signature", data, sz);
1117 fb_queue_command("signature", "installing signature");
1118}
1119
Daniel Rosenberg13454092016-06-27 19:43:11 -07001120// Sets slot_override as the active slot. If slot_override is blank,
1121// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001122static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001123 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001124 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001125 if (supports_AB_obsolete(transport)) {
1126 separator = "_"; // Legacy support
1127 } else {
1128 return;
1129 }
1130 }
1131 if (slot_override != "") {
1132 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001133 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001134 std::string current_slot = get_current_slot(transport);
1135 if (current_slot != "") {
1136 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001137 }
1138 }
1139}
1140
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001141static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142 queue_info_dump();
1143
Wink Savilleb98762f2011-04-04 17:54:59 -07001144 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1145
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001146 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001147 int error = OpenArchive(filename, &zip);
1148 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001149 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001150 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001152
Elliott Hughesfc797672015-04-07 20:12:50 -07001153 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001154 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001155 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001156 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001157 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158 }
1159
Elliott Hughes253c18d2015-03-18 22:47:09 -07001160 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161
Alex Lightbb9b8a52016-06-29 09:26:44 -07001162 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001163 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001164 if (slot_override != "") {
1165 secondary = get_other_slot(transport, slot_override);
1166 } else {
1167 secondary = get_other_slot(transport);
1168 }
1169 if (secondary == "") {
1170 if (supports_AB(transport)) {
1171 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1172 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001173 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001174 }
1175 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001176 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001177 const char* slot = slot_override.c_str();
1178 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001179 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001180 slot = secondary.c_str();
1181 } else {
1182 continue;
1183 }
1184 }
1185
Elliott Hughes253c18d2015-03-18 22:47:09 -07001186 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001187 if (fd == -1) {
1188 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001189 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001190 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001191 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001192 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001193 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001194 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001195 if (!load_buf_fd(transport, fd, &buf)) {
1196 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1197 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001198
1199 auto update = [&](const std::string &partition) {
1200 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001201 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001202 fb_queue_erase(partition.c_str());
1203 }
1204 flash_buf(partition.c_str(), &buf);
1205 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001206 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001207 * program exits.
1208 */
1209 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001210 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001211 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001212
1213 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001214 if (slot_override == "all") {
1215 set_active(transport, "a");
1216 } else {
1217 set_active(transport, slot_override);
1218 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001219}
1220
Alex Lightbb9b8a52016-06-29 09:26:44 -07001221static void do_send_signature(const std::string& fn) {
1222 std::size_t extension_loc = fn.find(".img");
1223 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001224
Alex Lightbb9b8a52016-06-29 09:26:44 -07001225 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001226
1227 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001228 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001229 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001231 fb_queue_download("signature", data, sz);
1232 fb_queue_command("signature", "installing signature");
1233}
1234
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001235static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001236 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001237 queue_info_dump();
1238
Wink Savilleb98762f2011-04-04 17:54:59 -07001239 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1240
Elliott Hughes45964a82017-05-03 22:43:23 -07001241 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001242 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001243
Elliott Hughesfc797672015-04-07 20:12:50 -07001244 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001245 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001246 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001247
1248 setup_requirements(reinterpret_cast<char*>(data), sz);
1249
Alex Lightbb9b8a52016-06-29 09:26:44 -07001250 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001251 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001252 if (slot_override != "") {
1253 secondary = get_other_slot(transport, slot_override);
1254 } else {
1255 secondary = get_other_slot(transport);
1256 }
1257 if (secondary == "") {
1258 if (supports_AB(transport)) {
1259 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1260 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001261 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001262 }
1263 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001264
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001265 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001266 const char* slot = NULL;
1267 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001268 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001269 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001270 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001271 }
1272 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001273 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001274 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001275 if (!load_buf(transport, fname.c_str(), &buf)) {
1276 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001277 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001278 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001279
1280 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001281 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001282 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001283 fb_queue_erase(partition.c_str());
1284 }
1285 flash_buf(partition.c_str(), &buf);
1286 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001287 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001288 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001289
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001290 if (slot_override == "all") {
1291 set_active(transport, "a");
1292 } else {
1293 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001294 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001295}
1296
Elliott Hughesd6365a72017-05-08 18:04:49 -07001297static std::string next_arg(std::vector<std::string>* args) {
1298 if (args->empty()) syntax_error("expected argument");
1299 std::string result = args->front();
1300 args->erase(args->begin());
1301 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001302}
1303
Elliott Hughesd6365a72017-05-08 18:04:49 -07001304static void do_bypass_unlock_command(std::vector<std::string>* args) {
1305 if (args->empty()) syntax_error("missing unlock_bootloader request");
1306
1307 std::string filename = next_arg(args);
1308
1309 int64_t sz;
1310 void* data = load_file(filename.c_str(), &sz);
1311 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
1312 fb_queue_download("unlock_message", data, sz);
1313 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1314}
1315
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001316static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001317 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001318
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001319 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001320 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001321 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001322 }
Elliott Hughes4a667302017-03-15 09:37:28 -07001323 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001324}
1325
Colin Crossf8387882012-05-24 17:18:41 -07001326static int64_t parse_num(const char *arg)
1327{
1328 char *endptr;
1329 unsigned long long num;
1330
1331 num = strtoull(arg, &endptr, 0);
1332 if (endptr == arg) {
1333 return -1;
1334 }
1335
1336 if (*endptr == 'k' || *endptr == 'K') {
1337 if (num >= (-1ULL) / 1024) {
1338 return -1;
1339 }
1340 num *= 1024LL;
1341 endptr++;
1342 } else if (*endptr == 'm' || *endptr == 'M') {
1343 if (num >= (-1ULL) / (1024 * 1024)) {
1344 return -1;
1345 }
1346 num *= 1024LL * 1024LL;
1347 endptr++;
1348 } else if (*endptr == 'g' || *endptr == 'G') {
1349 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1350 return -1;
1351 }
1352 num *= 1024LL * 1024LL * 1024LL;
1353 endptr++;
1354 }
1355
1356 if (*endptr != '\0') {
1357 return -1;
1358 }
1359
1360 if (num > INT64_MAX) {
1361 return -1;
1362 }
1363
1364 return num;
1365}
1366
Connor O'Brience16a8a2017-02-06 14:39:31 -08001367static std::string fb_fix_numeric_var(std::string var) {
1368 // Some bootloaders (angler, for example), send spurious leading whitespace.
1369 var = android::base::Trim(var);
1370 // Some bootloaders (hammerhead, for example) use implicit hex.
1371 // This code used to use strtol with base 16.
1372 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1373 return var;
1374}
1375
1376static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1377 std::string sizeString;
1378 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1379 /* This device does not report flash block sizes, so return 0 */
1380 return 0;
1381 }
1382 sizeString = fb_fix_numeric_var(sizeString);
1383
1384 unsigned size;
1385 if (!android::base::ParseUint(sizeString, &size)) {
1386 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1387 return 0;
1388 }
1389 if (size < 4096 || (size & (size - 1)) != 0) {
1390 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1391 name.c_str(), size);
1392 return 0;
1393 }
1394 return size;
1395}
1396
David Pursell0b156632015-10-30 11:22:01 -07001397static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 const char* partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001399 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001400 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001401 std::string partition_type, partition_size;
1402
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001403 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001404 const char* errMsg = nullptr;
1405 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001406 TemporaryFile output;
1407 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001408
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001409 unsigned int limit = INT_MAX;
1410 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001412 }
1413 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001414 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001416
David Pursell0b156632015-10-30 11:22:01 -07001417 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001418 errMsg = "Can't determine partition type.\n";
1419 goto failed;
1420 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001421 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001422 if (partition_type != type_override) {
1423 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughesd6365a72017-05-08 18:04:49 -07001424 partition, partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001425 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001426 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001427 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001428
David Pursell0b156632015-10-30 11:22:01 -07001429 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001430 errMsg = "Unable to get partition size\n";
1431 goto failed;
1432 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001433 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001434 if (partition_size != size_override) {
1435 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughesd6365a72017-05-08 18:04:49 -07001436 partition, partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001437 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001438 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001439 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001440 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001441
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001442 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001443 if (!gen) {
1444 if (skip_if_not_supported) {
1445 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001446 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001447 return;
1448 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001449 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1450 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001451 return;
1452 }
1453
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001454 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001455 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001456 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1457 return;
1458 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001459
Connor O'Brience16a8a2017-02-06 14:39:31 -08001460 unsigned eraseBlkSize, logicalBlkSize;
1461 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1462 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1463
Jin Qian4a335822017-04-18 16:23:18 -07001464 if (fs_generator_generate(gen, output.path, size, initial_dir,
1465 eraseBlkSize, logicalBlkSize)) {
Jin Qian1e0df162017-08-23 11:43:57 -07001466 die("Cannot generate image for %s\n", partition);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001467 return;
1468 }
1469
Jin Qian4a335822017-04-18 16:23:18 -07001470 fd.reset(open(output.path, O_RDONLY));
1471 if (fd == -1) {
1472 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1473 return;
1474 }
1475 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001476 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001477 return;
1478 }
1479 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001480 return;
1481
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001482failed:
1483 if (skip_if_not_supported) {
1484 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001485 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001486 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001487 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001488}
1489
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001490int main(int argc, char **argv)
1491{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001492 bool wants_wipe = false;
1493 bool wants_reboot = false;
1494 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001495 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001496 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001497 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001498 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001499 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001500 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001501 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001502 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001503 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001504 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001505 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001506
JP Abgrall7b8970c2013-03-07 17:06:41 -08001507 const struct option longopts[] = {
1508 {"base", required_argument, 0, 'b'},
1509 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001510 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001511 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001512 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001513 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001514 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001515 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001516 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001517 {"help", no_argument, 0, 'h'},
1518 {"unbuffered", no_argument, 0, 0},
1519 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001520 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001521 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001522 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001523 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001524 {"skip-reboot", no_argument, 0, 0},
David Zeuthenb6ea4352017-08-07 14:29:26 -04001525 {"disable-verity", no_argument, 0, 0},
1526 {"disable-verification", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001527#if !defined(_WIN32)
1528 {"wipe-and-use-fbe", no_argument, 0, 0},
1529#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001530 {0, 0, 0, 0}
1531 };
Colin Cross8879f982012-05-22 17:53:34 -07001532
1533 serial = getenv("ANDROID_SERIAL");
1534
1535 while (1) {
Elliott Hughes45964a82017-05-03 22:43:23 -07001536 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lc:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001537 if (c < 0) {
1538 break;
1539 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001540 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001541 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001542 case 'a':
1543 wants_set_active = true;
1544 if (optarg)
1545 next_active = optarg;
1546 break;
Colin Cross8879f982012-05-22 17:53:34 -07001547 case 'b':
1548 base_addr = strtoul(optarg, 0, 16);
1549 break;
Colin Cross8879f982012-05-22 17:53:34 -07001550 case 'c':
1551 cmdline = optarg;
1552 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001553 case 'h':
Elliott Hughesd6365a72017-05-08 18:04:49 -07001554 return show_help();
Colin Cross8879f982012-05-22 17:53:34 -07001555 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001556 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001557 unsigned long val;
1558
1559 val = strtoul(optarg, &endptr, 0);
1560 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1561 die("invalid vendor id '%s'", optarg);
1562 vendor_id = (unsigned short)val;
1563 break;
1564 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001565 case 'k':
1566 kernel_offset = strtoul(optarg, 0, 16);
1567 break;
1568 case 'l':
1569 long_listing = 1;
1570 break;
1571 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001572 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001573 if (!page_size) die("invalid page size");
1574 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001575 case 'r':
1576 ramdisk_offset = strtoul(optarg, 0, 16);
1577 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001578 case 't':
1579 tags_offset = strtoul(optarg, 0, 16);
1580 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001581 case 's':
1582 serial = optarg;
1583 break;
1584 case 'S':
1585 sparse_limit = parse_num(optarg);
1586 if (sparse_limit < 0) {
1587 die("invalid sparse limit");
1588 }
1589 break;
1590 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001591 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001592 break;
1593 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001594 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001595 break;
Colin Cross8879f982012-05-22 17:53:34 -07001596 case '?':
1597 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001598 case 0:
1599 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001600 setvbuf(stdout, nullptr, _IONBF, 0);
1601 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001602 } else if (strcmp("version", longopts[longindex].name) == 0) {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001603 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001604 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001605 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001606 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1607 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001608 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1609 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001610 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1611 skip_reboot = true;
David Zeuthenb6ea4352017-08-07 14:29:26 -04001612 } else if (strcmp("disable-verity", longopts[longindex].name) == 0 ) {
1613 g_disable_verity = true;
1614 } else if (strcmp("disable-verification", longopts[longindex].name) == 0 ) {
1615 g_disable_verification = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001616#if !defined(_WIN32)
1617 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1618 wants_wipe = true;
1619 set_fbe_marker = true;
1620#endif
1621 } else {
1622 fprintf(stderr, "Internal error in options processing for %s\n",
1623 longopts[longindex].name);
1624 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001625 }
1626 break;
Colin Cross8879f982012-05-22 17:53:34 -07001627 default:
1628 abort();
1629 }
1630 }
1631
1632 argc -= optind;
1633 argv += optind;
1634
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001636
Colin Cross8fb6e062012-07-24 16:36:41 -07001637 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001638 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001639 return 0;
1640 }
1641
Colin Crossc7b75dc2012-08-29 18:17:06 -07001642 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001643 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001644 }
1645
David Pursell0b156632015-10-30 11:22:01 -07001646 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001647 if (transport == nullptr) {
1648 return 1;
1649 }
1650
Daniel Rosenberg80919472016-06-30 19:25:31 -07001651 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1652 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1653 }
1654 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1655 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001656
1657 if (wants_set_active) {
1658 if (next_active == "") {
1659 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001660 std::string current_slot;
1661 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001662 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001663 } else {
1664 wants_set_active = false;
1665 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001666 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001667 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001668 }
1669 }
1670 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001671
Elliott Hughesd6365a72017-05-08 18:04:49 -07001672 std::vector<std::string> args(argv, argv + argc);
1673 while (!args.empty()) {
1674 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001675
Elliott Hughesd6365a72017-05-08 18:04:49 -07001676 if (command == "getvar") {
1677 std::string variable = next_arg(&args);
1678 fb_queue_display(variable.c_str(), variable.c_str());
1679 } else if (command == "erase") {
1680 std::string partition = next_arg(&args);
1681 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001682 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001683 if (fb_getvar(transport, std::string("partition-type:") + partition,
1684 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001685 fs_get_generator(partition_type) != nullptr) {
1686 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1687 partition_type.c_str());
1688 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001689
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001690 fb_queue_erase(partition.c_str());
1691 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001692 do_for_partitions(transport, partition, slot_override, erase, true);
1693 } else if (android::base::StartsWith(command, "format")) {
1694 // Parsing for: "format[:[type][:[size]]]"
1695 // Some valid things:
1696 // - select only the size, and leave default fs type:
1697 // format::0x4000000 userdata
1698 // - default fs type and size:
1699 // format userdata
1700 // format:: userdata
1701 std::vector<std::string> pieces = android::base::Split(command, ":");
1702 std::string type_override;
1703 if (pieces.size() > 1) type_override = pieces[1].c_str();
1704 std::string size_override;
1705 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001706
Elliott Hughesd6365a72017-05-08 18:04:49 -07001707 std::string partition = next_arg(&args);
1708
1709 auto format = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001710 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001711 fb_queue_erase(partition.c_str());
1712 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001713 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override,
1714 "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001715 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001716 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1717 } else if (command == "signature") {
1718 std::string filename = next_arg(&args);
1719 data = load_file(filename.c_str(), &sz);
1720 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001721 if (sz != 256) die("signature must be 256 bytes");
1722 fb_queue_download("signature", data, sz);
1723 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001725 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001726
1727 if (args.size() == 1) {
1728 std::string what = next_arg(&args);
1729 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001730 wants_reboot = false;
1731 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001732 } else if (what == "emergency") {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001733 wants_reboot = false;
1734 wants_reboot_emergency = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001735 } else {
1736 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001737 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001738
Elliott Hughesca85df02015-02-25 10:02:00 -08001739 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001740 if (!args.empty()) syntax_error("junk after reboot command");
1741 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001742 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001743 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001744 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 } else if (command == "boot") {
1746 std::string kernel = next_arg(&args);
1747 std::string ramdisk;
1748 if (!args.empty()) ramdisk = next_arg(&args);
1749 std::string second_stage;
1750 if (!args.empty()) second_stage = next_arg(&args);
1751
1752 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001753 fb_queue_download("boot.img", data, sz);
1754 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001755 } else if (command == "flash") {
1756 std::string pname = next_arg(&args);
1757
Elliott Hughes2810d002016-04-25 14:31:18 -07001758 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001759 if (!args.empty()) {
1760 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001761 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001762 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001763 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001764 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001765
1766 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001767 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001768 fb_queue_erase(partition.c_str());
1769 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001770 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001771 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001772 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1773 } else if (command == "flash:raw") {
1774 std::string partition = next_arg(&args);
1775 std::string kernel = next_arg(&args);
1776 std::string ramdisk;
1777 if (!args.empty()) ramdisk = next_arg(&args);
1778 std::string second_stage;
1779 if (!args.empty()) second_stage = next_arg(&args);
1780
1781 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
1782 auto flashraw = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001783 fb_queue_flash(partition.c_str(), data, sz);
1784 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 do_for_partitions(transport, partition, slot_override, flashraw, true);
1786 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001787 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001788 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1789 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001790 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001791 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001792 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001793 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001794 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001795 bool slot_all = (slot_override == "all");
1796 if (slot_all) {
1797 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1798 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001799 std::string filename = "update.zip";
1800 if (!args.empty()) {
1801 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001802 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001803 do_update(transport, filename.c_str(), slot_override, erase_first,
1804 skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001805 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001806 } else if (command == "set_active") {
1807 std::string slot = verify_slot(transport, next_arg(&args), false);
1808
David Pursell04396f62016-12-15 16:16:10 -08001809 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1810 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1811 // do have slot-suffixes.
1812 std::string var;
1813 if (!fb_getvar(transport, "slot-count", &var) &&
1814 fb_getvar(transport, "slot-suffixes", &var)) {
1815 slot = "_" + slot;
1816 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001817 fb_set_active(slot.c_str());
Elliott Hughesd6365a72017-05-08 18:04:49 -07001818 } else if (command == "stage") {
1819 std::string filename = next_arg(&args);
1820
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001821 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001822 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1823 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001824 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001825 fb_queue_download_fd(filename.c_str(), buf.fd, buf.sz);
1826 } else if (command == "get_staged") {
1827 std::string filename = next_arg(&args);
1828 fb_queue_upload(filename.c_str());
1829 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001830 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001831 } else if (command == "flashing") {
1832 if (args.empty()) {
1833 syntax_error("missing 'flashing' command");
1834 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1835 args[0] == "unlock_critical" ||
1836 args[0] == "lock_critical" ||
1837 args[0] == "get_unlock_ability" ||
1838 args[0] == "get_unlock_bootloader_nonce" ||
1839 args[0] == "lock_bootloader")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001840 do_oem_command("flashing", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001841 } else if (args.size() == 2 && args[0] == "unlock_bootloader") {
1842 do_bypass_unlock_command(&args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001843 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001844 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001845 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001846 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001847 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001848 }
1849 }
1850
1851 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001852 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001853 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001854 if (set_fbe_marker) {
1855 fprintf(stderr, "setting FBE marker...\n");
1856 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1857 if (initial_userdata_dir.empty()) {
1858 return 1;
1859 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001860 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001861 delete_fbemarker_tmpdir(initial_userdata_dir);
1862 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001863 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001864 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001865
1866 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001867 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001868 fprintf(stderr, "wiping cache...\n");
1869 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001870 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001871 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001872 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001873 if (wants_set_active) {
1874 fb_set_active(next_active.c_str());
1875 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001876 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001877 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001878 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001879 } else if (wants_reboot_bootloader) {
1880 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001881 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001882 } else if (wants_reboot_emergency) {
1883 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1884 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001885 }
1886
David Pursell0b156632015-10-30 11:22:01 -07001887 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001888}