blob: dc6e6981d53d49927a3d4423c02ba228dcc4c4cb [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080048#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080049#include <utility>
50#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070051
Elliott Hughes82ff3152016-08-31 15:07:18 -070052#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070053#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080055#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070056#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080057#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050058#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070059#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070060#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Elliott Hughes253c18d2015-03-18 22:47:09 -070062#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080063#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070065#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080066#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070067#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080068#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070069#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Chris Fries0ea946c2017-04-12 10:25:57 -050071using android::base::unique_fd;
72
Colin Crossf8387882012-05-24 17:18:41 -070073#ifndef O_BINARY
74#define O_BINARY 0
75#endif
76
Wink Savilleb98762f2011-04-04 17:54:59 -070077char cur_product[FB_RESPONSE_SZ + 1];
78
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* serial = nullptr;
80static const char* product = nullptr;
81static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070083static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050084// Don't resparse files in too-big chunks.
85// libsparse will support INT_MAX, but this results in large allocations, so
86// let's keep it at 1GB to avoid memory pressure on the host.
87static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070088static int64_t sparse_limit = -1;
89static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughesfc797672015-04-07 20:12:50 -070091static unsigned page_size = 2048;
92static unsigned base_addr = 0x10000000;
93static unsigned kernel_offset = 0x00008000;
94static unsigned ramdisk_offset = 0x01000000;
95static unsigned second_offset = 0x00f00000;
96static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080097
Paul Crowley8f7f56e2015-11-27 09:29:37 +000098static const std::string convert_fbe_marker_filename("convert_fbe");
99
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500101 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 FB_BUFFER_SPARSE,
103};
104
105struct fastboot_buffer {
106 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700107 void* data;
108 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500109 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
111
112static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700113 char img_name[17];
114 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115 char part_name[9];
116 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700117 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700118} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700119 {"boot.img", "boot.sig", "boot", false, false},
120 {"boot_other.img", "boot.sig", "boot", true, true},
121 {"recovery.img", "recovery.sig", "recovery", true, false},
122 {"system.img", "system.sig", "system", false, false},
123 {"system_other.img", "system.sig", "system", true, true},
124 {"vendor.img", "vendor.sig", "vendor", true, false},
125 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700126 {"vbmeta.img", "vbmeta.sig", "vbmeta", true, false},
Yueyao Zhue67da712017-05-03 14:33:32 -0700127 {"dtbo.img", "dtbo.sig", "dtbo", true, false},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700128};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700129
Alex Lightbb9b8a52016-06-29 09:26:44 -0700130static std::string find_item_given_name(const char* img_name, const char* product) {
131 if(product) {
Elliott Hughes82ff3152016-08-31 15:07:18 -0700132 std::string path = android::base::GetExecutablePath();
Alex Lightbb9b8a52016-06-29 09:26:44 -0700133 path.erase(path.find_last_of('/'));
134 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
135 path.c_str(), product, img_name);
136 }
137
138 char *dir = getenv("ANDROID_PRODUCT_OUT");
139 if (dir == nullptr || dir[0] == '\0') {
140 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
141 }
142
143 return android::base::StringPrintf("%s/%s", dir, img_name);
144}
145
146std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700147 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700148
Elliott Hughes2810d002016-04-25 14:31:18 -0700149 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 fn = "boot.img";
151 } else if(!strcmp(item,"recovery")) {
152 fn = "recovery.img";
153 } else if(!strcmp(item,"system")) {
154 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700155 } else if(!strcmp(item,"vendor")) {
156 fn = "vendor.img";
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700157 } else if(!strcmp(item,"vbmeta")) {
158 fn = "vbmeta.img";
Yueyao Zhue67da712017-05-03 14:33:32 -0700159 } else if(!strcmp(item,"dtbo")) {
160 fn = "dtbo.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 } else if(!strcmp(item,"userdata")) {
162 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700163 } else if(!strcmp(item,"cache")) {
164 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 } else if(!strcmp(item,"info")) {
166 fn = "android-info.txt";
167 } else {
168 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700169 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 }
171
Alex Lightbb9b8a52016-06-29 09:26:44 -0700172 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173}
174
Elliott Hughesfc797672015-04-07 20:12:50 -0700175static int64_t get_file_size(int fd) {
176 struct stat sb;
177 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700178}
179
Elliott Hughesfc797672015-04-07 20:12:50 -0700180static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800181 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184 *sz = get_file_size(fd);
185 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700186 goto oops;
187 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 data = (char*) malloc(*sz);
190 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughesfc797672015-04-07 20:12:50 -0700192 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 close(fd);
194
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 return data;
196
197oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800198 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 close(fd);
200 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800201 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202 return 0;
203}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
Elliott Hughes2810d002016-04-25 14:31:18 -0700205static void* load_file(const std::string& path, int64_t* sz) {
206 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700207 if (fd == -1) return nullptr;
208 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700209}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700212 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700213 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700214 return -1;
215 }
216
217 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
218 return -1;
219 }
220
Scott Anderson13081c62012-04-06 12:39:30 -0700221 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700223 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
224 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 return 0;
226}
227
Elliott Hughesfc797672015-04-07 20:12:50 -0700228static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800229 return match_fastboot_with_serial(info, serial);
230}
231
Elliott Hughesfc797672015-04-07 20:12:50 -0700232static int list_devices_callback(usb_ifc_info* info) {
233 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800234 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700235 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800236 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 if (!serial[0]) {
239 serial = "????????????";
240 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700241 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700242 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800243 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700244 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800245 printf("%-22s fastboot", serial.c_str());
246 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700247 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800248 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
250
251 return -1;
252}
253
David Pursell2ec418a2016-01-20 08:32:08 -0800254// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
255// a specific device, otherwise the first USB device found will be used.
256//
257// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
258// Otherwise it blocks until the target is available.
259//
260// The returned Transport is a singleton, so multiple calls to this function will return the same
261// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700262static Transport* open_device() {
263 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800264 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265
David Pursell2ec418a2016-01-20 08:32:08 -0800266 if (transport != nullptr) {
267 return transport;
268 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800269
David Pursell4601c972016-02-05 15:35:09 -0800270 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800271 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800272 int port = 0;
273 if (serial != nullptr) {
274 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800275
David Pursell4601c972016-02-05 15:35:09 -0800276 if (android::base::StartsWith(serial, "tcp:")) {
277 protocol = Socket::Protocol::kTcp;
278 port = tcp::kDefaultPort;
279 net_address = serial + strlen("tcp:");
280 } else if (android::base::StartsWith(serial, "udp:")) {
281 protocol = Socket::Protocol::kUdp;
282 port = udp::kDefaultPort;
283 net_address = serial + strlen("udp:");
284 }
285
286 if (net_address != nullptr) {
287 std::string error;
288 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
289 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
290 error.c_str());
291 return nullptr;
292 }
David Pursell2ec418a2016-01-20 08:32:08 -0800293 }
294 }
295
296 while (true) {
297 if (!host.empty()) {
298 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800299 if (protocol == Socket::Protocol::kTcp) {
300 transport = tcp::Connect(host, port, &error).release();
301 } else if (protocol == Socket::Protocol::kUdp) {
302 transport = udp::Connect(host, port, &error).release();
303 }
304
David Pursell2ec418a2016-01-20 08:32:08 -0800305 if (transport == nullptr && announce) {
306 fprintf(stderr, "error: %s\n", error.c_str());
307 }
308 } else {
309 transport = usb_open(match_fastboot);
310 }
311
312 if (transport != nullptr) {
313 return transport;
314 }
315
David Pursell0b156632015-10-30 11:22:01 -0700316 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800317 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700318 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800320 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 }
322}
323
Elliott Hughesfc797672015-04-07 20:12:50 -0700324static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 // We don't actually open a USB device here,
326 // just getting our callback called so we can
327 // list all the connected devices.
328 usb_open(list_devices_callback);
329}
330
Elliott Hughesfc797672015-04-07 20:12:50 -0700331static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 fprintf(stderr,
333/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
334 "usage: fastboot [ <option> ] <command>\n"
335 "\n"
336 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700337 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700338 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700339 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700340 " if found -- recovery. If the device\n"
341 " supports slots, the slot that has\n"
342 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700343 " Secondary images may be flashed to\n"
344 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700345 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
346 " flashing lock Locks the device. Prevents flashing.\n"
347 " flashing unlock Unlocks the device. Allows flashing\n"
348 " any partition except\n"
349 " bootloader-related partitions.\n"
350 " flashing lock_critical Prevents flashing bootloader-related\n"
351 " partitions.\n"
352 " flashing unlock_critical Enables flashing bootloader-related\n"
353 " partitions.\n"
354 " flashing get_unlock_ability Queries bootloader to see if the\n"
355 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700356 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700357 " unlock nonce.\n"
358 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700359 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700360 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700361 " erase <partition> Erase a flash partition.\n"
362 " format[:[<fs type>][:[<size>]] <partition>\n"
363 " Format a flash partition. Can\n"
364 " override the fs type and/or size\n"
365 " the bootloader reports.\n"
366 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700367 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800368 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700369 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
370 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
371 " Create bootimage and flash it.\n"
372 " devices [-l] List all connected devices [with\n"
373 " device paths].\n"
374 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800375 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700376 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800377 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
378 " stage <infile> Sends contents of <infile> to stage for\n"
379 " the next command. Supported only on\n"
380 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800381 " get_staged <outfile> Receives data to <outfile> staged by the\n"
382 " last command. Supported only on Android\n"
383 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700384 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 "\n"
386 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700387 " -w Erase userdata and cache (and format\n"
388 " if supported by partition type).\n"
389 " -u Do not erase partition before\n"
390 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800391 " -s <specific device> Specify a device. For USB, provide either\n"
392 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700393 " For ethernet, provide an address in the\n"
394 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800395 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700396 " -p <product> Specify product name.\n"
397 " -c <cmdline> Override kernel commandline.\n"
398 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800399 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700400 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800401 " --kernel-offset Specify a custom kernel offset.\n"
402 " (default: 0x00008000)\n"
403 " --ramdisk-offset Specify a custom ramdisk offset.\n"
404 " (default: 0x01000000)\n"
405 " --tags-offset Specify a custom tags offset.\n"
406 " (default: 0x00000100)\n"
407 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700408 " (default: 2048).\n"
409 " -S <size>[K|M|G] Automatically sparse files greater\n"
410 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700411 " --slot <slot> Specify slot name to be used if the\n"
412 " device supports slots. All operations\n"
413 " on partitions that support slots will\n"
414 " be done on the slot specified.\n"
415 " 'all' can be given to refer to all slots.\n"
416 " 'other' can be given to refer to a\n"
417 " non-current slot. If this flag is not\n"
418 " used, slotted partitions will default\n"
419 " to the current active slot.\n"
420 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800421 " provided, this will default to the value\n"
422 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800423 " supported, this does nothing. This will\n"
424 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700425 " --skip-secondary Will not flash secondary slots when\n"
426 " performing a flashall or update. This\n"
427 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700428 " --skip-reboot Will not reboot the device when\n"
429 " performing commands that normally\n"
430 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000431#if !defined(_WIN32)
432 " --wipe-and-use-fbe On devices which support it,\n"
433 " erase userdata and cache, and\n"
434 " enable file-based encryption\n"
435#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800436 " --unbuffered Do not buffer input or output.\n"
437 " --version Display version.\n"
438 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000441
Elliott Hughesfc797672015-04-07 20:12:50 -0700442static void* load_bootable_image(const char* kernel, const char* ramdisk,
443 const char* secondstage, int64_t* sz,
444 const char* cmdline) {
445 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 fprintf(stderr, "no image specified\n");
447 return 0;
448 }
449
Elliott Hughesfc797672015-04-07 20:12:50 -0700450 int64_t ksize;
451 void* kdata = load_file(kernel, &ksize);
452 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800453 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 return 0;
455 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800456
Elliott Hughesfc797672015-04-07 20:12:50 -0700457 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700459 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800460
Elliott Hughesfc797672015-04-07 20:12:50 -0700461 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
463 return 0;
464 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800465
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 *sz = ksize;
467 return kdata;
468 }
469
Elliott Hughesfc797672015-04-07 20:12:50 -0700470 void* rdata = nullptr;
471 int64_t rsize = 0;
472 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700474 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800475 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 return 0;
477 }
478 }
479
Elliott Hughesfc797672015-04-07 20:12:50 -0700480 void* sdata = nullptr;
481 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200482 if (secondstage) {
483 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700484 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200485 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
486 return 0;
487 }
488 }
489
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700491 int64_t bsize = 0;
492 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800493 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200494 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800495 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700496 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 fprintf(stderr,"failed to create boot.img\n");
498 return 0;
499 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700500 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
501 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 return bdata;
505}
506
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700507static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700508 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700509 ZipEntry zip_entry;
510 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700511 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000512 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 }
514
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700515 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700517 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700518 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700519 if (data == nullptr) {
520 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000521 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522 }
523
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700524 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
525 if (error != 0) {
526 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800527 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000528 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000530
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531 return data;
532}
533
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700534#if defined(_WIN32)
535
536// TODO: move this to somewhere it can be shared.
537
538#include <windows.h>
539
540// Windows' tmpfile(3) requires administrator rights because
541// it creates temporary files in the root directory.
542static FILE* win32_tmpfile() {
543 char temp_path[PATH_MAX];
544 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
545 if (nchars == 0 || nchars >= sizeof(temp_path)) {
546 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
547 return nullptr;
548 }
549
550 char filename[PATH_MAX];
551 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
552 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
553 return nullptr;
554 }
555
556 return fopen(filename, "w+bTD");
557}
558
559#define tmpfile win32_tmpfile
560
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000561static std::string make_temporary_directory() {
562 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
563 return "";
564}
565
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700566static int make_temporary_fd() {
567 // TODO: reimplement to avoid leaking a FILE*.
568 return fileno(tmpfile());
569}
570
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000571#else
572
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700573static std::string make_temporary_template() {
574 const char* tmpdir = getenv("TMPDIR");
575 if (tmpdir == nullptr) tmpdir = P_tmpdir;
576 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
577}
578
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000579static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700580 std::string result(make_temporary_template());
581 if (mkdtemp(&result[0]) == nullptr) {
582 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000583 return "";
584 }
585 return result;
586}
587
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700588static int make_temporary_fd() {
589 std::string path_template(make_temporary_template());
590 int fd = mkstemp(&path_template[0]);
591 if (fd == -1) {
592 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
593 return -1;
594 }
595 unlink(path_template.c_str());
596 return fd;
597}
598
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700599#endif
600
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000601static std::string create_fbemarker_tmpdir() {
602 std::string dir = make_temporary_directory();
603 if (dir.empty()) {
604 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
605 return "";
606 }
607 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
608 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
609 if (fd == -1) {
610 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
611 marker_file.c_str(), errno, strerror(errno));
612 return "";
613 }
614 close(fd);
615 return dir;
616}
617
618static void delete_fbemarker_tmpdir(const std::string& dir) {
619 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
620 if (unlink(marker_file.c_str()) == -1) {
621 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
622 marker_file.c_str(), errno, strerror(errno));
623 return;
624 }
625 if (rmdir(dir.c_str()) == -1) {
626 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
627 dir.c_str(), errno, strerror(errno));
628 return;
629 }
630}
631
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700632static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700633 unique_fd fd(make_temporary_fd());
634 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700635 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
636 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700637 return -1;
638 }
639
Yusuke Sato07447542015-06-25 14:39:19 -0700640 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700641 ZipEntry zip_entry;
642 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
643 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000644 return -1;
645 }
646
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700647 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
648 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700649 int error = ExtractEntryToFile(zip, &zip_entry, fd);
650 if (error != 0) {
651 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
652 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700653 }
654
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000655 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700656 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700657 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700658}
659
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660static char *strip(char *s)
661{
662 int n;
663 while(*s && isspace(*s)) s++;
664 n = strlen(s);
665 while(n-- > 0) {
666 if(!isspace(s[n])) break;
667 s[n] = 0;
668 }
669 return s;
670}
671
672#define MAX_OPTIONS 32
673static int setup_requirement_line(char *name)
674{
675 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700676 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677 unsigned n, count;
678 char *x;
679 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800680
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 if (!strncmp(name, "reject ", 7)) {
682 name += 7;
683 invert = 1;
684 } else if (!strncmp(name, "require ", 8)) {
685 name += 8;
686 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700687 } else if (!strncmp(name, "require-for-product:", 20)) {
688 // Get the product and point name past it
689 prod = name + 20;
690 name = strchr(name, ' ');
691 if (!name) return -1;
692 *name = 0;
693 name += 1;
694 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 }
696
697 x = strchr(name, '=');
698 if (x == 0) return 0;
699 *x = 0;
700 val[0] = x + 1;
701
702 for(count = 1; count < MAX_OPTIONS; count++) {
703 x = strchr(val[count - 1],'|');
704 if (x == 0) break;
705 *x = 0;
706 val[count] = x + 1;
707 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800708
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 name = strip(name);
710 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800711
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 name = strip(name);
713 if (name == 0) return -1;
714
Elliott Hughes253c18d2015-03-18 22:47:09 -0700715 const char* var = name;
716 // Work around an unfortunate name mismatch.
717 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718
Elliott Hughes253c18d2015-03-18 22:47:09 -0700719 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 if (out == 0) return -1;
721
722 for(n = 0; n < count; n++) {
723 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700724 if (out[n] == 0) {
725 for(size_t i = 0; i < n; ++i) {
726 free((char*) out[i]);
727 }
728 free(out);
729 return -1;
730 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 }
732
Elliott Hughes253c18d2015-03-18 22:47:09 -0700733 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734 return 0;
735}
736
Elliott Hughesfc797672015-04-07 20:12:50 -0700737static void setup_requirements(char* data, int64_t sz) {
738 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800739 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700740 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741 *s++ = 0;
742 if (setup_requirement_line(data)) {
743 die("out of memory");
744 }
745 data = s;
746 } else {
747 s++;
748 }
749 }
750}
751
Elliott Hughesfc797672015-04-07 20:12:50 -0700752static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 fb_queue_notice("--------------------------------------------");
754 fb_queue_display("version-bootloader", "Bootloader Version...");
755 fb_queue_display("version-baseband", "Baseband Version.....");
756 fb_queue_display("serialno", "Serial Number........");
757 fb_queue_notice("--------------------------------------------");
758}
759
Rom Lemarchand622810c2013-06-28 09:54:59 -0700760static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700761{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700762 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700763 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700764 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700765 }
766
Elliott Hughesfc797672015-04-07 20:12:50 -0700767 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700768 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700769 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700770 }
771
Elliott Hughes253c18d2015-03-18 22:47:09 -0700772 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700773 if (!out_s) {
774 die("Failed to allocate sparse file array\n");
775 }
776
777 files = sparse_file_resparse(s, max_size, out_s, files);
778 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700780 }
781
782 return out_s;
783}
784
David Pursell0b156632015-10-30 11:22:01 -0700785static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700786 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700787 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
788 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800789 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700790 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700791 }
792
Elliott Hughes77c0e662015-11-02 15:51:12 -0800793 // Some bootloaders (angler, for example) send spurious whitespace too.
794 max_download_size = android::base::Trim(max_download_size);
795
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700796 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700797 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800798 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700799 return 0;
800 }
801 if (limit > 0) {
802 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
803 }
Colin Crossf8387882012-05-24 17:18:41 -0700804 return limit;
805}
806
David Pursell0b156632015-10-30 11:22:01 -0700807static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700808 int64_t limit;
809
810 if (sparse_limit == 0) {
811 return 0;
812 } else if (sparse_limit > 0) {
813 limit = sparse_limit;
814 } else {
815 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700816 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700817 }
818 if (target_sparse_limit > 0) {
819 limit = target_sparse_limit;
820 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700821 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700822 }
823 }
824
825 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500826 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700827 }
828
829 return 0;
830}
831
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700832// Until we get lazy inode table init working in make_ext4fs, we need to
833// erase partitions of type ext4 before flashing a filesystem so no stale
834// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700835static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800836 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700837 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800838 return false;
839 }
840 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700841}
842
Elliott Hughes53ec4952016-05-11 12:39:27 -0700843static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700844 int64_t sz = get_file_size(fd);
845 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700846 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700847 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700848
Elliott Hughesfc797672015-04-07 20:12:50 -0700849 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700850 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700851 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700852 sparse_file** s = load_sparse_files(fd, limit);
853 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700854 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700855 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700856 buf->type = FB_BUFFER_SPARSE;
857 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700858 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500859 buf->type = FB_BUFFER_FD;
860 buf->data = nullptr;
861 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000862 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700863 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864
Elliott Hughes53ec4952016-05-11 12:39:27 -0700865 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700866}
867
Elliott Hughes53ec4952016-05-11 12:39:27 -0700868static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500869 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
870
Elliott Hughes53ec4952016-05-11 12:39:27 -0700871 if (fd == -1) {
872 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700873 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500874
875 struct stat s;
876 if (fstat(fd, &s)) {
877 return false;
878 }
879 if (!S_ISREG(s.st_mode)) {
880 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
881 return false;
882 }
883
884 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700885}
886
887static void flash_buf(const char *pname, struct fastboot_buffer *buf)
888{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700889 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700890
891 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800892 case FB_BUFFER_SPARSE: {
893 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700894 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700895 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700896 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800897 sparse_files.emplace_back(*s, sz);
898 ++s;
899 }
900
901 for (size_t i = 0; i < sparse_files.size(); ++i) {
902 const auto& pair = sparse_files[i];
903 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700904 }
905 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800906 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500907 case FB_BUFFER_FD:
908 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700909 break;
910 default:
911 die("unknown buffer type: %d", buf->type);
912 }
913}
914
Daniel Rosenberg80919472016-06-30 19:25:31 -0700915static std::string get_current_slot(Transport* transport)
916{
917 std::string current_slot;
918 if (fb_getvar(transport, "current-slot", &current_slot)) {
919 if (current_slot == "_a") return "a"; // Legacy support
920 if (current_slot == "_b") return "b"; // Legacy support
921 return current_slot;
922 }
923 return "";
924}
925
926// Legacy support
927static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700928 std::vector<std::string> suffixes;
929 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700930 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700931 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700932 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700933 suffixes = android::base::Split(suffix_list, ",");
934 // Unfortunately some devices will return an error message in the
935 // guise of a valid value. If we only see only one suffix, it's probably
936 // not real.
937 if (suffixes.size() == 1) {
938 suffixes.clear();
939 }
940 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700941}
942
Daniel Rosenberg80919472016-06-30 19:25:31 -0700943// Legacy support
944static bool supports_AB_obsolete(Transport* transport) {
945 return !get_suffixes_obsolete(transport).empty();
946}
947
948static int get_slot_count(Transport* transport) {
949 std::string var;
950 int count;
951 if (!fb_getvar(transport, "slot-count", &var)) {
952 if (supports_AB_obsolete(transport)) return 2; // Legacy support
953 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700954 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700955 return count;
956}
957
Alex Lightbb9b8a52016-06-29 09:26:44 -0700958static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700959 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700960}
961
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700962// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963static std::string get_other_slot(const std::string& current_slot, int count) {
964 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700965
Daniel Rosenberg80919472016-06-30 19:25:31 -0700966 char next = (current_slot[0] - 'a' + 1)%count + 'a';
967 return std::string(1, next);
968}
969
970static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
971 return get_other_slot(current_slot, get_slot_count(transport));
972}
973
974static std::string get_other_slot(Transport* transport, int count) {
975 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700976}
977
Alex Lightbb9b8a52016-06-29 09:26:44 -0700978static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700980}
981
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
983 std::string slot = slot_name;
984 if (slot == "_a") slot = "a"; // Legacy support
985 if (slot == "_b") slot = "b"; // Legacy support
986 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800987 if (allow_all) {
988 return "all";
989 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 int count = get_slot_count(transport);
991 if (count > 0) {
992 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800993 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800994 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800995 }
996 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800998
Daniel Rosenberg80919472016-06-30 19:25:31 -0700999 int count = get_slot_count(transport);
1000 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001001
Daniel Rosenberg80919472016-06-30 19:25:31 -07001002 if (slot == "other") {
1003 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001004 if (other == "") {
1005 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001006 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001007 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001008 }
1009
Daniel Rosenberg80919472016-06-30 19:25:31 -07001010 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1011
1012 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1013 for (int i=0; i<count; i++) {
1014 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001016
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017 exit(1);
1018}
1019
Daniel Rosenberg80919472016-06-30 19:25:31 -07001020static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001021 return verify_slot(transport, slot, true);
1022}
1023
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001025 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001026 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001027 std::string current_slot;
1028
Daniel Rosenberg80919472016-06-30 19:25:31 -07001029 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001030 /* If has-slot is not supported, the answer is no. */
1031 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001032 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001033 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001034 if (slot == "") {
1035 current_slot = get_current_slot(transport);
1036 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 die("Failed to identify current slot.\n");
1038 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001039 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001040 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001041 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001042 }
1043 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001044 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001045 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001046 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001047 }
1048 func(part);
1049 }
1050}
1051
David Pursell0b156632015-10-30 11:22:01 -07001052/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1053 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1054 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1055 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001056 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001057static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001058 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001059 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001060
Daniel Rosenberg80919472016-06-30 19:25:31 -07001061 if (slot == "all") {
1062 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1063 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001064 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001065 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001066 for (int i=0; i < get_slot_count(transport); i++) {
1067 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001068 }
1069 } else {
David Pursell0b156632015-10-30 11:22:01 -07001070 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001071 }
1072 } else {
David Pursell0b156632015-10-30 11:22:01 -07001073 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001074 }
1075}
1076
David Pursell0b156632015-10-30 11:22:01 -07001077static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001078 struct fastboot_buffer buf;
1079
Elliott Hughes53ec4952016-05-11 12:39:27 -07001080 if (!load_buf(transport, fname, &buf)) {
1081 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001082 }
1083 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001084}
1085
Elliott Hughesfc797672015-04-07 20:12:50 -07001086static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1087 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001088 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001089 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001090 fb_queue_download("signature", data, sz);
1091 fb_queue_command("signature", "installing signature");
1092}
1093
Daniel Rosenberg13454092016-06-27 19:43:11 -07001094// Sets slot_override as the active slot. If slot_override is blank,
1095// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001096static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001097 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001098 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001099 if (supports_AB_obsolete(transport)) {
1100 separator = "_"; // Legacy support
1101 } else {
1102 return;
1103 }
1104 }
1105 if (slot_override != "") {
1106 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001107 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001108 std::string current_slot = get_current_slot(transport);
1109 if (current_slot != "") {
1110 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001111 }
1112 }
1113}
1114
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001115static 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 -08001116 queue_info_dump();
1117
Wink Savilleb98762f2011-04-04 17:54:59 -07001118 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1119
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001120 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001121 int error = OpenArchive(filename, &zip);
1122 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001123 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001124 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001125 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001126
Elliott Hughesfc797672015-04-07 20:12:50 -07001127 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001128 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001129 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001130 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001131 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001132 }
1133
Elliott Hughes253c18d2015-03-18 22:47:09 -07001134 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001135
Alex Lightbb9b8a52016-06-29 09:26:44 -07001136 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001137 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001138 if (slot_override != "") {
1139 secondary = get_other_slot(transport, slot_override);
1140 } else {
1141 secondary = get_other_slot(transport);
1142 }
1143 if (secondary == "") {
1144 if (supports_AB(transport)) {
1145 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1146 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001147 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001148 }
1149 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001150 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001151 const char* slot = slot_override.c_str();
1152 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001153 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001154 slot = secondary.c_str();
1155 } else {
1156 continue;
1157 }
1158 }
1159
Elliott Hughes253c18d2015-03-18 22:47:09 -07001160 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001161 if (fd == -1) {
1162 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001163 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001164 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001165 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001166 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001167 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001168 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001169 if (!load_buf_fd(transport, fd, &buf)) {
1170 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1171 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001172
1173 auto update = [&](const std::string &partition) {
1174 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001175 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001176 fb_queue_erase(partition.c_str());
1177 }
1178 flash_buf(partition.c_str(), &buf);
1179 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001180 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001181 * program exits.
1182 */
1183 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001184 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001185 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001186
1187 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001188 if (slot_override == "all") {
1189 set_active(transport, "a");
1190 } else {
1191 set_active(transport, slot_override);
1192 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193}
1194
Alex Lightbb9b8a52016-06-29 09:26:44 -07001195static void do_send_signature(const std::string& fn) {
1196 std::size_t extension_loc = fn.find(".img");
1197 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001198
Alex Lightbb9b8a52016-06-29 09:26:44 -07001199 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001200
1201 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001202 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001203 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001204
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001205 fb_queue_download("signature", data, sz);
1206 fb_queue_command("signature", "installing signature");
1207}
1208
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001209static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001210 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001211 queue_info_dump();
1212
Wink Savilleb98762f2011-04-04 17:54:59 -07001213 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1214
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001215 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001216 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001217
Elliott Hughesfc797672015-04-07 20:12:50 -07001218 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001219 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001220 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001221
1222 setup_requirements(reinterpret_cast<char*>(data), sz);
1223
Alex Lightbb9b8a52016-06-29 09:26:44 -07001224 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001225 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001226 if (slot_override != "") {
1227 secondary = get_other_slot(transport, slot_override);
1228 } else {
1229 secondary = get_other_slot(transport);
1230 }
1231 if (secondary == "") {
1232 if (supports_AB(transport)) {
1233 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1234 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001235 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001236 }
1237 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001238
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001239 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001240 const char* slot = NULL;
1241 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001242 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001243 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001244 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001245 }
1246 if (!slot) continue;
1247 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001248 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001249 if (!load_buf(transport, fname.c_str(), &buf)) {
1250 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001251 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001252 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001253
1254 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001255 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001256 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001257 fb_queue_erase(partition.c_str());
1258 }
1259 flash_buf(partition.c_str(), &buf);
1260 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001261 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001262 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001263
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001264 if (slot_override == "all") {
1265 set_active(transport, "a");
1266 } else {
1267 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001268 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269}
1270
1271#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001272#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273
Elliott Hughes45be42e2015-09-02 20:15:48 -07001274static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001275{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001276 if (argc <= 2) return 0;
1277 skip(2);
1278
1279 /*
1280 * Process unlock_bootloader, we have to load the message file
1281 * and send that to the remote device.
1282 */
1283 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001284
1285 int64_t sz;
1286 void* data = load_file(*argv, &sz);
1287 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001288 fb_queue_download("unlock_message", data, sz);
1289 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1290 skip(1);
1291 return 0;
1292}
1293
Elliott Hughes4a667302017-03-15 09:37:28 -07001294static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001295 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001296
Elliott Hughes4a667302017-03-15 09:37:28 -07001297 std::string command;
1298 while (argc > 0) {
1299 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001300 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001301 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001302 }
1303
Elliott Hughes4a667302017-03-15 09:37:28 -07001304 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001305 return 0;
1306}
1307
Colin Crossf8387882012-05-24 17:18:41 -07001308static int64_t parse_num(const char *arg)
1309{
1310 char *endptr;
1311 unsigned long long num;
1312
1313 num = strtoull(arg, &endptr, 0);
1314 if (endptr == arg) {
1315 return -1;
1316 }
1317
1318 if (*endptr == 'k' || *endptr == 'K') {
1319 if (num >= (-1ULL) / 1024) {
1320 return -1;
1321 }
1322 num *= 1024LL;
1323 endptr++;
1324 } else if (*endptr == 'm' || *endptr == 'M') {
1325 if (num >= (-1ULL) / (1024 * 1024)) {
1326 return -1;
1327 }
1328 num *= 1024LL * 1024LL;
1329 endptr++;
1330 } else if (*endptr == 'g' || *endptr == 'G') {
1331 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1332 return -1;
1333 }
1334 num *= 1024LL * 1024LL * 1024LL;
1335 endptr++;
1336 }
1337
1338 if (*endptr != '\0') {
1339 return -1;
1340 }
1341
1342 if (num > INT64_MAX) {
1343 return -1;
1344 }
1345
1346 return num;
1347}
1348
Connor O'Brience16a8a2017-02-06 14:39:31 -08001349static std::string fb_fix_numeric_var(std::string var) {
1350 // Some bootloaders (angler, for example), send spurious leading whitespace.
1351 var = android::base::Trim(var);
1352 // Some bootloaders (hammerhead, for example) use implicit hex.
1353 // This code used to use strtol with base 16.
1354 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1355 return var;
1356}
1357
1358static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1359 std::string sizeString;
1360 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1361 /* This device does not report flash block sizes, so return 0 */
1362 return 0;
1363 }
1364 sizeString = fb_fix_numeric_var(sizeString);
1365
1366 unsigned size;
1367 if (!android::base::ParseUint(sizeString, &size)) {
1368 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1369 return 0;
1370 }
1371 if (size < 4096 || (size & (size - 1)) != 0) {
1372 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1373 name.c_str(), size);
1374 return 0;
1375 }
1376 return size;
1377}
1378
David Pursell0b156632015-10-30 11:22:01 -07001379static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001381 const char* type_override, const char* size_override,
1382 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001383 std::string partition_type, partition_size;
1384
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001386 const char* errMsg = nullptr;
1387 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001388 int fd;
1389
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001390 unsigned int limit = INT_MAX;
1391 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001393 }
1394 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001395 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001397
David Pursell0b156632015-10-30 11:22:01 -07001398 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001399 errMsg = "Can't determine partition type.\n";
1400 goto failed;
1401 }
JP Abgrall7e859742014-05-06 15:14:15 -07001402 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001403 if (partition_type != type_override) {
1404 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1405 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001406 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001407 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001408 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001409
David Pursell0b156632015-10-30 11:22:01 -07001410 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411 errMsg = "Unable to get partition size\n";
1412 goto failed;
1413 }
JP Abgrall7e859742014-05-06 15:14:15 -07001414 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 if (partition_size != size_override) {
1416 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1417 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001418 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001420 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001421 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001422
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001423 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001424 if (!gen) {
1425 if (skip_if_not_supported) {
1426 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001428 return;
1429 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001430 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1431 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001432 return;
1433 }
1434
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001435 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001436 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001437 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1438 return;
1439 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001441 fd = make_temporary_fd();
1442 if (fd == -1) return;
Connor O'Brience16a8a2017-02-06 14:39:31 -08001443
1444 unsigned eraseBlkSize, logicalBlkSize;
1445 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1446 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1447
1448 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001449 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001450 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001451 return;
1452 }
1453
Elliott Hughes53ec4952016-05-11 12:39:27 -07001454 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001455 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456 close(fd);
1457 return;
1458 }
1459 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001460 return;
1461
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462failed:
1463 if (skip_if_not_supported) {
1464 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001465 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001466 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001467 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001468}
1469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001470int main(int argc, char **argv)
1471{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001472 bool wants_wipe = false;
1473 bool wants_reboot = false;
1474 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001475 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001476 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001477 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001478 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001479 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001480 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001481 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001482 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001483 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001484 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001485 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001486
JP Abgrall7b8970c2013-03-07 17:06:41 -08001487 const struct option longopts[] = {
1488 {"base", required_argument, 0, 'b'},
1489 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001490 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001491 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001492 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001493 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001494 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001495 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001496 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001497 {"help", no_argument, 0, 'h'},
1498 {"unbuffered", no_argument, 0, 0},
1499 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001500 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001501 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001502 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001503 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001504 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001505#if !defined(_WIN32)
1506 {"wipe-and-use-fbe", no_argument, 0, 0},
1507#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001508 {0, 0, 0, 0}
1509 };
Colin Cross8879f982012-05-22 17:53:34 -07001510
1511 serial = getenv("ANDROID_SERIAL");
1512
1513 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001514 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001515 if (c < 0) {
1516 break;
1517 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001518 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001519 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001520 case 'a':
1521 wants_set_active = true;
1522 if (optarg)
1523 next_active = optarg;
1524 break;
Colin Cross8879f982012-05-22 17:53:34 -07001525 case 'b':
1526 base_addr = strtoul(optarg, 0, 16);
1527 break;
Colin Cross8879f982012-05-22 17:53:34 -07001528 case 'c':
1529 cmdline = optarg;
1530 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001531 case 'h':
1532 usage();
1533 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001534 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001535 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001536 unsigned long val;
1537
1538 val = strtoul(optarg, &endptr, 0);
1539 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1540 die("invalid vendor id '%s'", optarg);
1541 vendor_id = (unsigned short)val;
1542 break;
1543 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001544 case 'k':
1545 kernel_offset = strtoul(optarg, 0, 16);
1546 break;
1547 case 'l':
1548 long_listing = 1;
1549 break;
1550 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001551 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001552 if (!page_size) die("invalid page size");
1553 break;
1554 case 'p':
1555 product = optarg;
1556 break;
1557 case 'r':
1558 ramdisk_offset = strtoul(optarg, 0, 16);
1559 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001560 case 't':
1561 tags_offset = strtoul(optarg, 0, 16);
1562 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001563 case 's':
1564 serial = optarg;
1565 break;
1566 case 'S':
1567 sparse_limit = parse_num(optarg);
1568 if (sparse_limit < 0) {
1569 die("invalid sparse limit");
1570 }
1571 break;
1572 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001573 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001574 break;
1575 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001576 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001577 break;
Colin Cross8879f982012-05-22 17:53:34 -07001578 case '?':
1579 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001580 case 0:
1581 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001582 setvbuf(stdout, nullptr, _IONBF, 0);
1583 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001584 } else if (strcmp("version", longopts[longindex].name) == 0) {
1585 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001586 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001587 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001588 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1589 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001590 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1591 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001592 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1593 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001594#if !defined(_WIN32)
1595 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1596 wants_wipe = true;
1597 set_fbe_marker = true;
1598#endif
1599 } else {
1600 fprintf(stderr, "Internal error in options processing for %s\n",
1601 longopts[longindex].name);
1602 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001603 }
1604 break;
Colin Cross8879f982012-05-22 17:53:34 -07001605 default:
1606 abort();
1607 }
1608 }
1609
1610 argc -= optind;
1611 argv += optind;
1612
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001613 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001614 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001615 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001616 }
1617
Colin Cross8fb6e062012-07-24 16:36:41 -07001618 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001619 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001620 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001621 return 0;
1622 }
1623
Colin Crossc7b75dc2012-08-29 18:17:06 -07001624 if (argc > 0 && !strcmp(*argv, "help")) {
1625 usage();
1626 return 0;
1627 }
1628
David Pursell0b156632015-10-30 11:22:01 -07001629 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001630 if (transport == nullptr) {
1631 return 1;
1632 }
1633
Daniel Rosenberg80919472016-06-30 19:25:31 -07001634 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1635 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1636 }
1637 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1638 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001639
1640 if (wants_set_active) {
1641 if (next_active == "") {
1642 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001643 std::string current_slot;
1644 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001645 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001646 } else {
1647 wants_set_active = false;
1648 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001649 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001650 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001651 }
1652 }
1653 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001654
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001655 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001656 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001657 require(2);
1658 fb_queue_display(argv[1], argv[1]);
1659 skip(2);
1660 } else if(!strcmp(*argv, "erase")) {
1661 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001662
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001663 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001664 std::string partition_type;
1665 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001666 fs_get_generator(partition_type) != nullptr) {
1667 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1668 partition_type.c_str());
1669 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001670
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001671 fb_queue_erase(partition.c_str());
1672 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001673 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001674 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001675 } else if(!strncmp(*argv, "format", strlen("format"))) {
1676 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001677 char *type_override = nullptr;
1678 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001679 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001680 /*
1681 * Parsing for: "format[:[type][:[size]]]"
1682 * Some valid things:
1683 * - select ontly the size, and leave default fs type:
1684 * format::0x4000000 userdata
1685 * - default fs type and size:
1686 * format userdata
1687 * format:: userdata
1688 */
1689 overrides = strchr(*argv, ':');
1690 if (overrides) {
1691 overrides++;
1692 size_override = strchr(overrides, ':');
1693 if (size_override) {
1694 size_override[0] = '\0';
1695 size_override++;
1696 }
1697 type_override = overrides;
1698 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001699 if (type_override && !type_override[0]) type_override = nullptr;
1700 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001701
1702 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001703 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001704 fb_queue_erase(partition.c_str());
1705 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001706 fb_perform_format(transport, partition.c_str(), 0,
1707 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001708 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001709 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001710 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001711 } else if(!strcmp(*argv, "signature")) {
1712 require(2);
1713 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001714 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001715 if (sz != 256) die("signature must be 256 bytes");
1716 fb_queue_download("signature", data, sz);
1717 fb_queue_command("signature", "installing signature");
1718 skip(2);
1719 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001720 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001721 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001722 if (argc > 0) {
1723 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001724 wants_reboot = false;
1725 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001726 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001727 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001728 wants_reboot = false;
1729 wants_reboot_emergency = true;
1730 skip(1);
1731 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001732 }
1733 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001734 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001735 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001736 skip(1);
1737 } else if (!strcmp(*argv, "continue")) {
1738 fb_queue_command("continue", "resuming boot");
1739 skip(1);
1740 } else if(!strcmp(*argv, "boot")) {
1741 char *kname = 0;
1742 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001743 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001744 skip(1);
1745 if (argc > 0) {
1746 kname = argv[0];
1747 skip(1);
1748 }
1749 if (argc > 0) {
1750 rname = argv[0];
1751 skip(1);
1752 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001753 if (argc > 0) {
1754 sname = argv[0];
1755 skip(1);
1756 }
1757 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001758 if (data == 0) return 1;
1759 fb_queue_download("boot.img", data, sz);
1760 fb_queue_command("boot", "booting");
1761 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001762 char* pname = argv[1];
1763 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001764 require(2);
1765 if (argc > 2) {
1766 fname = argv[2];
1767 skip(3);
1768 } else {
1769 fname = find_item(pname, product);
1770 skip(2);
1771 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001772 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001773
1774 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001775 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001776 fb_queue_erase(partition.c_str());
1777 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001778 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001779 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001780 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001781 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001782 char *kname = argv[2];
1783 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001784 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001785 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001786 skip(3);
1787 if (argc > 0) {
1788 rname = argv[0];
1789 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001790 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001791 if (argc > 0) {
1792 sname = argv[0];
1793 skip(1);
1794 }
1795 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001796 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001797 auto flashraw = [&](const std::string &partition) {
1798 fb_queue_flash(partition.c_str(), data, sz);
1799 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001800 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001801 } else if(!strcmp(*argv, "flashall")) {
1802 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001803 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001804 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1805 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001806 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001807 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001808 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001809 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001810 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001811 bool slot_all = (slot_override == "all");
1812 if (slot_all) {
1813 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1814 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001815 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001816 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001817 skip(2);
1818 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001819 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001820 skip(1);
1821 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001822 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001823 } else if(!strcmp(*argv, "set_active")) {
1824 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001825 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001826 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1827 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1828 // do have slot-suffixes.
1829 std::string var;
1830 if (!fb_getvar(transport, "slot-count", &var) &&
1831 fb_getvar(transport, "slot-suffixes", &var)) {
1832 slot = "_" + slot;
1833 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001834 fb_set_active(slot.c_str());
1835 skip(2);
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001836 } else if(!strcmp(*argv, "stage")) {
1837 require(2);
1838 std::string infile(argv[1]);
1839 skip(2);
1840 struct fastboot_buffer buf;
1841 if (!load_buf(transport, infile.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1842 die("cannot load '%s'", infile.c_str());
1843 }
1844 fb_queue_download_fd(infile.c_str(), buf.fd, buf.sz);
Jocelyn Bohr91fefad2017-01-27 14:17:56 -08001845 } else if(!strcmp(*argv, "get_staged")) {
1846 require(2);
1847 char *outfile = argv[1];
1848 skip(2);
1849 fb_queue_upload(outfile);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001850 } else if(!strcmp(*argv, "oem")) {
1851 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001852 } else if(!strcmp(*argv, "flashing")) {
1853 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1854 !strcmp(*(argv+1), "lock") ||
1855 !strcmp(*(argv+1), "unlock_critical") ||
1856 !strcmp(*(argv+1), "lock_critical") ||
1857 !strcmp(*(argv+1), "get_unlock_ability") ||
1858 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1859 !strcmp(*(argv+1), "lock_bootloader"))) {
1860 argc = do_oem_command(argc, argv);
1861 } else
1862 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1863 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001864 } else {
1865 usage();
1866 return 1;
1867 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001868 } else {
1869 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001870 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001871 }
1872 }
1873
1874 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001875 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001876 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001877 if (set_fbe_marker) {
1878 fprintf(stderr, "setting FBE marker...\n");
1879 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1880 if (initial_userdata_dir.empty()) {
1881 return 1;
1882 }
1883 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1884 delete_fbemarker_tmpdir(initial_userdata_dir);
1885 } else {
1886 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1887 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001888
1889 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001890 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001891 fprintf(stderr, "wiping cache...\n");
1892 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001893 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001894 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001895 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001896 if (wants_set_active) {
1897 fb_set_active(next_active.c_str());
1898 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001899 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001900 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001901 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001902 } else if (wants_reboot_bootloader) {
1903 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001904 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001905 } else if (wants_reboot_emergency) {
1906 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1907 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001908 }
1909
David Pursell0b156632015-10-30 11:22:01 -07001910 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001911}