blob: 0ce385599a0c0ccf1e0d791556aa57cfc09d9ab2 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070081static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050082// Don't resparse files in too-big chunks.
83// libsparse will support INT_MAX, but this results in large allocations, so
84// let's keep it at 1GB to avoid memory pressure on the host.
85static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t sparse_limit = -1;
87static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughesfc797672015-04-07 20:12:50 -070089static unsigned page_size = 2048;
90static unsigned base_addr = 0x10000000;
91static unsigned kernel_offset = 0x00008000;
92static unsigned ramdisk_offset = 0x01000000;
93static unsigned second_offset = 0x00f00000;
94static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080095
David Zeuthenb6ea4352017-08-07 14:29:26 -040096static bool g_disable_verity = false;
97static bool g_disable_verification = false;
98
Paul Crowley8f7f56e2015-11-27 09:29:37 +000099static const std::string convert_fbe_marker_filename("convert_fbe");
100
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500102 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103 FB_BUFFER_SPARSE,
104};
105
106struct fastboot_buffer {
107 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700108 void* data;
109 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500110 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
112
113static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700114 const char* nickname;
115 const char* img_name;
116 const char* sig_name;
117 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700118 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700119 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700120} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700121 // clang-format off
122 { "boot", "boot.img", "boot.sig", "boot", false, false },
123 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
124 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700125 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800126 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800127 { "product", "product.img", "product.sig", "product", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700128 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
129 { "system", "system.img", "system.sig", "system", false, false },
130 { nullptr, "system_other.img", "system.sig", "system", true, true },
131 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
132 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
133 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
134 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700135};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700136
Elliott Hughes45964a82017-05-03 22:43:23 -0700137static std::string find_item_given_name(const char* img_name) {
138 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700139 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700140 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700141 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700142 return android::base::StringPrintf("%s/%s", dir, img_name);
143}
144
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700145static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700146 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700147 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700148 return find_item_given_name(images[i].img_name);
149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 }
151
Elliott Hughesd6365a72017-05-08 18:04:49 -0700152 if (item == "userdata") return find_item_given_name("userdata.img");
153 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700154
Elliott Hughesd6365a72017-05-08 18:04:49 -0700155 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700156 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157}
158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159static int64_t get_file_size(int fd) {
160 struct stat sb;
161 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700162}
163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800165 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 *sz = get_file_size(fd);
169 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700170 goto oops;
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 data = (char*) malloc(*sz);
174 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 close(fd);
178
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return data;
180
181oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 close(fd);
184 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800185 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 return 0;
187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughes2810d002016-04-25 14:31:18 -0700189static void* load_file(const std::string& path, int64_t* sz) {
190 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700191 if (fd == -1) return nullptr;
192 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700193}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Elliott Hughesfc797672015-04-07 20:12:50 -0700195static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700196 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700197 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700198 return -1;
199 }
200
201 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
202 return -1;
203 }
204
Scott Anderson13081c62012-04-06 12:39:30 -0700205 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700207 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
208 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 return 0;
210}
211
Elliott Hughesfc797672015-04-07 20:12:50 -0700212static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800213 return match_fastboot_with_serial(info, serial);
214}
215
Elliott Hughesfc797672015-04-07 20:12:50 -0700216static int list_devices_callback(usb_ifc_info* info) {
217 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if (!serial[0]) {
223 serial = "????????????";
224 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700225 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700226 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700228 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 printf("%-22s fastboot", serial.c_str());
230 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800232 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234
235 return -1;
236}
237
David Pursell2ec418a2016-01-20 08:32:08 -0800238// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
239// a specific device, otherwise the first USB device found will be used.
240//
241// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
242// Otherwise it blocks until the target is available.
243//
244// The returned Transport is a singleton, so multiple calls to this function will return the same
245// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700246static Transport* open_device() {
247 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800248 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
David Pursell2ec418a2016-01-20 08:32:08 -0800250 if (transport != nullptr) {
251 return transport;
252 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253
David Pursell4601c972016-02-05 15:35:09 -0800254 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800255 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800256 int port = 0;
257 if (serial != nullptr) {
258 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800259
David Pursell4601c972016-02-05 15:35:09 -0800260 if (android::base::StartsWith(serial, "tcp:")) {
261 protocol = Socket::Protocol::kTcp;
262 port = tcp::kDefaultPort;
263 net_address = serial + strlen("tcp:");
264 } else if (android::base::StartsWith(serial, "udp:")) {
265 protocol = Socket::Protocol::kUdp;
266 port = udp::kDefaultPort;
267 net_address = serial + strlen("udp:");
268 }
269
270 if (net_address != nullptr) {
271 std::string error;
272 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
273 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
274 error.c_str());
275 return nullptr;
276 }
David Pursell2ec418a2016-01-20 08:32:08 -0800277 }
278 }
279
280 while (true) {
281 if (!host.empty()) {
282 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800283 if (protocol == Socket::Protocol::kTcp) {
284 transport = tcp::Connect(host, port, &error).release();
285 } else if (protocol == Socket::Protocol::kUdp) {
286 transport = udp::Connect(host, port, &error).release();
287 }
288
David Pursell2ec418a2016-01-20 08:32:08 -0800289 if (transport == nullptr && announce) {
290 fprintf(stderr, "error: %s\n", error.c_str());
291 }
292 } else {
293 transport = usb_open(match_fastboot);
294 }
295
296 if (transport != nullptr) {
297 return transport;
298 }
299
David Pursell0b156632015-10-30 11:22:01 -0700300 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800301 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700302 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800304 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 }
306}
307
Elliott Hughesfc797672015-04-07 20:12:50 -0700308static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 // We don't actually open a USB device here,
310 // just getting our callback called so we can
311 // list all the connected devices.
312 usb_open(list_devices_callback);
313}
314
Elliott Hughesd6365a72017-05-08 18:04:49 -0700315static void syntax_error(const char* fmt, ...) {
316 fprintf(stderr, "fastboot: usage: ");
317
318 va_list ap;
319 va_start(ap, fmt);
320 vfprintf(stderr, fmt, ap);
321 va_end(ap);
322
323 fprintf(stderr, "\n");
324 exit(1);
325}
326
327static int show_help() {
328 // clang-format off
329 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
331 "usage: fastboot [ <option> ] <command>\n"
332 "\n"
333 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700334 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700335 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700336 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700337 " if found -- recovery. If the device\n"
338 " supports slots, the slot that has\n"
339 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700340 " Secondary images may be flashed to\n"
341 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700342 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
343 " flashing lock Locks the device. Prevents flashing.\n"
344 " flashing unlock Unlocks the device. Allows flashing\n"
345 " any partition except\n"
346 " bootloader-related partitions.\n"
347 " flashing lock_critical Prevents flashing bootloader-related\n"
348 " partitions.\n"
349 " flashing unlock_critical Enables flashing bootloader-related\n"
350 " partitions.\n"
351 " flashing get_unlock_ability Queries bootloader to see if the\n"
352 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700353 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700354 " unlock nonce.\n"
355 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700356 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700357 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700358 " erase <partition> Erase a flash partition.\n"
359 " format[:[<fs type>][:[<size>]] <partition>\n"
360 " Format a flash partition. Can\n"
361 " override the fs type and/or size\n"
362 " the bootloader reports.\n"
363 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700364 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800365 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
Elliott Hughesd6365a72017-05-08 18:04:49 -0700367 " flash:raw <bootable-partition> <kernel> [ <ramdisk> [ <second> ] ]\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700368 " Create bootimage and flash it.\n"
369 " devices [-l] List all connected devices [with\n"
370 " device paths].\n"
371 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800372 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800374 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
375 " stage <infile> Sends contents of <infile> to stage for\n"
376 " the next command. Supported only on\n"
377 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800378 " get_staged <outfile> Receives data to <outfile> staged by the\n"
379 " last command. Supported only on Android\n"
380 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700381 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 "\n"
383 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700384 " -w Erase userdata and cache (and format\n"
385 " if supported by partition type).\n"
386 " -u Do not erase partition before\n"
387 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800388 " -s <specific device> Specify a device. For USB, provide either\n"
389 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700390 " For ethernet, provide an address in the\n"
391 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800392 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700393 " -c <cmdline> Override kernel commandline.\n"
394 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800395 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700396 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800397 " --kernel-offset Specify a custom kernel offset.\n"
398 " (default: 0x00008000)\n"
399 " --ramdisk-offset Specify a custom ramdisk offset.\n"
400 " (default: 0x01000000)\n"
401 " --tags-offset Specify a custom tags offset.\n"
402 " (default: 0x00000100)\n"
403 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700404 " (default: 2048).\n"
405 " -S <size>[K|M|G] Automatically sparse files greater\n"
406 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700407 " --slot <slot> Specify slot name to be used if the\n"
408 " device supports slots. All operations\n"
409 " on partitions that support slots will\n"
410 " be done on the slot specified.\n"
411 " 'all' can be given to refer to all slots.\n"
412 " 'other' can be given to refer to a\n"
413 " non-current slot. If this flag is not\n"
414 " used, slotted partitions will default\n"
415 " to the current active slot.\n"
416 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800417 " provided, this will default to the value\n"
418 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800419 " supported, this does nothing. This will\n"
420 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700421 " --skip-secondary Will not flash secondary slots when\n"
422 " performing a flashall or update. This\n"
423 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700424 " --skip-reboot Will not reboot the device when\n"
425 " performing commands that normally\n"
426 " trigger a reboot.\n"
David Zeuthenb6ea4352017-08-07 14:29:26 -0400427 " --disable-verity Set the disable-verity flag in the\n"
428 " the vbmeta image being flashed.\n"
429 " --disable-verification Set the disable-verification flag in"
430 " the vbmeta image being flashed.\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"
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700438 " --header-version Set boot image header version while\n"
439 " using flash:raw and boot commands to \n"
440 " to create a boot image.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800441 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700443 // clang-format off
444 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000446
Elliott Hughesd6365a72017-05-08 18:04:49 -0700447static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
448 const std::string& second_stage, int64_t* sz,
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700449 const char* cmdline, uint32_t header_version) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700450 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700451 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700452 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800453
Elliott Hughesfc797672015-04-07 20:12:50 -0700454 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700455 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800456 die("cannot load '%s': too short", kernel.c_str());
457 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700458 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700459 if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), cmdline);
460 uint32_t header_version_existing =
461 reinterpret_cast<boot_img_hdr_v1*>(kdata)->header_version;
462 if (header_version != header_version_existing) {
463 die("header version mismatch, expected: %" PRIu32 " found %" PRIu32 "",
464 header_version, header_version_existing);
465 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800466
Elliott Hughes4089d342017-10-27 14:21:12 -0700467 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800468
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 *sz = ksize;
470 return kdata;
471 }
472
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 void* rdata = nullptr;
474 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700475 if (!ramdisk.empty()) {
476 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700477 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478 }
479
Elliott Hughesfc797672015-04-07 20:12:50 -0700480 void* sdata = nullptr;
481 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700482 if (!second_stage.empty()) {
483 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700484 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200485 }
486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700488 int64_t bsize = 0;
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700489 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800490 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200491 sdata, ssize, second_offset,
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700492 page_size, base_addr, tags_offset, header_version, &bsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700493 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700494
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700495 if (cmdline) bootimg_set_cmdline(bdata, cmdline);
Elliott Hughesfc797672015-04-07 20:12:50 -0700496 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 return bdata;
500}
501
Elliott Hughes23af1122017-11-10 08:42:17 -0800502static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700503 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700504 ZipEntry zip_entry;
505 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700506 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700507 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 }
509
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700510 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511
Elliott Hughes23af1122017-11-10 08:42:17 -0800512 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700513 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700514 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700516 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700517 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000518
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 return data;
520}
521
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700522#if defined(_WIN32)
523
524// TODO: move this to somewhere it can be shared.
525
526#include <windows.h>
527
528// Windows' tmpfile(3) requires administrator rights because
529// it creates temporary files in the root directory.
530static FILE* win32_tmpfile() {
531 char temp_path[PATH_MAX];
532 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
533 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700534 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700535 }
536
537 char filename[PATH_MAX];
538 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700539 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700540 }
541
542 return fopen(filename, "w+bTD");
543}
544
545#define tmpfile win32_tmpfile
546
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000547static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700548 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000549}
550
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700551static int make_temporary_fd() {
552 // TODO: reimplement to avoid leaking a FILE*.
553 return fileno(tmpfile());
554}
555
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000556#else
557
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700558static std::string make_temporary_template() {
559 const char* tmpdir = getenv("TMPDIR");
560 if (tmpdir == nullptr) tmpdir = P_tmpdir;
561 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
562}
563
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000564static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700565 std::string result(make_temporary_template());
566 if (mkdtemp(&result[0]) == nullptr) {
567 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000568 return "";
569 }
570 return result;
571}
572
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700573static int make_temporary_fd() {
574 std::string path_template(make_temporary_template());
575 int fd = mkstemp(&path_template[0]);
576 if (fd == -1) {
577 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
578 return -1;
579 }
580 unlink(path_template.c_str());
581 return fd;
582}
583
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700584#endif
585
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000586static std::string create_fbemarker_tmpdir() {
587 std::string dir = make_temporary_directory();
588 if (dir.empty()) {
589 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
590 return "";
591 }
592 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
593 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
594 if (fd == -1) {
595 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
596 marker_file.c_str(), errno, strerror(errno));
597 return "";
598 }
599 close(fd);
600 return dir;
601}
602
603static void delete_fbemarker_tmpdir(const std::string& dir) {
604 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
605 if (unlink(marker_file.c_str()) == -1) {
606 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
607 marker_file.c_str(), errno, strerror(errno));
608 return;
609 }
610 if (rmdir(dir.c_str()) == -1) {
611 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
612 dir.c_str(), errno, strerror(errno));
613 return;
614 }
615}
616
Elliott Hughes45964a82017-05-03 22:43:23 -0700617static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700618 unique_fd fd(make_temporary_fd());
619 if (fd == -1) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700620 die("failed to create temporary file for '%s': %s", entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700621 }
622
Yusuke Sato07447542015-06-25 14:39:19 -0700623 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700624 ZipEntry zip_entry;
625 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
626 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000627 return -1;
628 }
629
Elliott Hughes23af1122017-11-10 08:42:17 -0800630 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700631 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800632 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700633 int error = ExtractEntryToFile(zip, &zip_entry, fd);
634 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800635 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700636 }
637
Elliott Hughes4089d342017-10-27 14:21:12 -0700638 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800639 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700640 }
641
Elliott Hughes23af1122017-11-10 08:42:17 -0800642 fprintf(stderr, " took %.3fs\n", now() - start);
643
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700644 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700645}
646
Elliott Hughes5620d222018-03-28 08:20:00 -0700647static char* strip(char* s) {
648 while (*s && isspace(*s)) s++;
649
650 int n = strlen(s);
651 while (n-- > 0) {
652 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 s[n] = 0;
654 }
655 return s;
656}
657
658#define MAX_OPTIONS 32
Elliott Hughes5620d222018-03-28 08:20:00 -0700659static void check_requirement(Transport* transport, char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800660 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700661 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 char *x;
663 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800664
Elliott Hughes5620d222018-03-28 08:20:00 -0700665 // "require product=alpha|beta|gamma"
666 // "require version-bootloader=1234"
667 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
668 // "require partition-exists=vendor"
669
670 char* name = line;
671 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672 if (!strncmp(name, "reject ", 7)) {
673 name += 7;
674 invert = 1;
675 } else if (!strncmp(name, "require ", 8)) {
676 name += 8;
677 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700678 } else if (!strncmp(name, "require-for-product:", 20)) {
679 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700680 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700681 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700682 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700683 *name = 0;
684 name += 1;
685 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
687
688 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700689 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 *x = 0;
691 val[0] = x + 1;
692
Elliott Hughes5620d222018-03-28 08:20:00 -0700693 name = strip(name);
694
695 // "require partition-exists=x" is a special case, added because of the trouble we had when
696 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
697 // missing out new partitions. A device with new partitions can use "partition-exists" to
698 // override the `is_optional` field in the `images` array.
699 if (!strcmp(name, "partition-exists")) {
700 const char* partition_name = val[0];
701 std::string has_slot;
702 if (!fb_getvar(transport, std::string("has-slot:") + partition_name, &has_slot) ||
703 (has_slot != "yes" && has_slot != "no")) {
704 die("device doesn't have required partition %s!", partition_name);
705 }
706 bool known_partition = false;
707 for (size_t i = 0; i < arraysize(images); ++i) {
708 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
709 images[i].is_optional = false;
710 known_partition = true;
711 }
712 }
713 if (!known_partition) {
714 die("device requires partition %s which is not known to this version of fastboot",
715 partition_name);
716 }
717 return;
718 }
719
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 for(count = 1; count < MAX_OPTIONS; count++) {
721 x = strchr(val[count - 1],'|');
722 if (x == 0) break;
723 *x = 0;
724 val[count] = x + 1;
725 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800726
Elliott Hughes253c18d2015-03-18 22:47:09 -0700727 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700728 const char* var = name;
729 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800730
Elliott Hughes253c18d2015-03-18 22:47:09 -0700731 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700732 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800733
Elliott Hughes5620d222018-03-28 08:20:00 -0700734 for (size_t i = 0; i < count; ++i) {
735 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 }
737
Elliott Hughes5620d222018-03-28 08:20:00 -0700738 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800739}
740
Elliott Hughes5620d222018-03-28 08:20:00 -0700741static void check_requirements(Transport* transport, char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700742 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800743 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700744 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800745 *s++ = 0;
Elliott Hughes5620d222018-03-28 08:20:00 -0700746 check_requirement(transport, data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800747 data = s;
748 } else {
749 s++;
750 }
751 }
Elliott Hughes5620d222018-03-28 08:20:00 -0700752 if (fb_execute_queue(transport)) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753}
754
Elliott Hughesfc797672015-04-07 20:12:50 -0700755static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700757 fb_queue_display("Bootloader Version...", "version-bootloader");
758 fb_queue_display("Baseband Version.....", "version-baseband");
759 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 fb_queue_notice("--------------------------------------------");
761}
762
Elliott Hughes4089d342017-10-27 14:21:12 -0700763static struct sparse_file** load_sparse_files(int fd, int max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700764 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700765 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700766
Elliott Hughesfc797672015-04-07 20:12:50 -0700767 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700768 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700769
Elliott Hughes253c18d2015-03-18 22:47:09 -0700770 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700771 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700772
773 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700774 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700775
776 return out_s;
777}
778
David Pursell0b156632015-10-30 11:22:01 -0700779static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700780 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700781 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
782 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800783 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700784 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700785 }
786
Elliott Hughes77c0e662015-11-02 15:51:12 -0800787 // Some bootloaders (angler, for example) send spurious whitespace too.
788 max_download_size = android::base::Trim(max_download_size);
789
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700790 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700791 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800792 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700793 return 0;
794 }
795 if (limit > 0) {
796 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
797 }
Colin Crossf8387882012-05-24 17:18:41 -0700798 return limit;
799}
800
David Pursell0b156632015-10-30 11:22:01 -0700801static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700802 int64_t limit;
803
804 if (sparse_limit == 0) {
805 return 0;
806 } else if (sparse_limit > 0) {
807 limit = sparse_limit;
808 } else {
809 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700810 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700811 }
812 if (target_sparse_limit > 0) {
813 limit = target_sparse_limit;
814 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700815 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700816 }
817 }
818
819 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500820 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700821 }
822
823 return 0;
824}
825
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700826// Until we get lazy inode table init working in make_ext4fs, we need to
827// erase partitions of type ext4 before flashing a filesystem so no stale
828// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700829static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800830 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700831 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800832 return false;
833 }
834 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700835}
836
Elliott Hughes53ec4952016-05-11 12:39:27 -0700837static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700838 int64_t sz = get_file_size(fd);
839 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700840 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700841 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700842
Elliott Hughesfc797672015-04-07 20:12:50 -0700843 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700844 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700845 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700846 sparse_file** s = load_sparse_files(fd, limit);
847 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700848 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700849 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700850 buf->type = FB_BUFFER_SPARSE;
851 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700852 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500853 buf->type = FB_BUFFER_FD;
854 buf->data = nullptr;
855 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000856 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700857 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700858
Elliott Hughes53ec4952016-05-11 12:39:27 -0700859 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860}
861
Elliott Hughes53ec4952016-05-11 12:39:27 -0700862static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500863 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
864
Elliott Hughes53ec4952016-05-11 12:39:27 -0700865 if (fd == -1) {
866 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700867 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500868
869 struct stat s;
870 if (fstat(fd, &s)) {
871 return false;
872 }
873 if (!S_ISREG(s.st_mode)) {
874 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
875 return false;
876 }
877
878 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700879}
880
David Zeuthenb6ea4352017-08-07 14:29:26 -0400881static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
882 // Buffer needs to be at least the size of the VBMeta struct which
883 // is 256 bytes.
884 if (buf->sz < 256) {
885 return;
886 }
887
888 int fd = make_temporary_fd();
889 if (fd == -1) {
890 die("Failed to create temporary file for vbmeta rewriting");
891 }
892
893 std::string data;
894 if (!android::base::ReadFdToString(buf->fd, &data)) {
895 die("Failed reading from vbmeta");
896 }
897
898 // There's a 32-bit big endian |flags| field at offset 120 where
899 // bit 0 corresponds to disable-verity and bit 1 corresponds to
900 // disable-verification.
901 //
902 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
903 // the VBMeta struct.
904 if (g_disable_verity) {
905 data[123] |= 0x01;
906 }
907 if (g_disable_verification) {
908 data[123] |= 0x02;
909 }
910
911 if (!android::base::WriteStringToFd(data, fd)) {
912 die("Failed writing to modified vbmeta");
913 }
914 close(buf->fd);
915 buf->fd = fd;
916 lseek(fd, 0, SEEK_SET);
917}
918
Elliott Hughes5620d222018-03-28 08:20:00 -0700919static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700920{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700921 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700922
David Zeuthenb6ea4352017-08-07 14:29:26 -0400923 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
924 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700925 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400926 rewrite_vbmeta_buffer(buf);
927 }
928
Rom Lemarchand622810c2013-06-28 09:54:59 -0700929 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800930 case FB_BUFFER_SPARSE: {
931 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700932 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700933 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700934 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800935 sparse_files.emplace_back(*s, sz);
936 ++s;
937 }
938
939 for (size_t i = 0; i < sparse_files.size(); ++i) {
940 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700941 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700942 }
943 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800944 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500945 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700946 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700947 break;
948 default:
949 die("unknown buffer type: %d", buf->type);
950 }
951}
952
Daniel Rosenberg80919472016-06-30 19:25:31 -0700953static std::string get_current_slot(Transport* transport)
954{
955 std::string current_slot;
956 if (fb_getvar(transport, "current-slot", &current_slot)) {
957 if (current_slot == "_a") return "a"; // Legacy support
958 if (current_slot == "_b") return "b"; // Legacy support
959 return current_slot;
960 }
961 return "";
962}
963
964// Legacy support
965static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700966 std::vector<std::string> suffixes;
967 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700968 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700969 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700970 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700971 suffixes = android::base::Split(suffix_list, ",");
972 // Unfortunately some devices will return an error message in the
973 // guise of a valid value. If we only see only one suffix, it's probably
974 // not real.
975 if (suffixes.size() == 1) {
976 suffixes.clear();
977 }
978 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700979}
980
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981// Legacy support
982static bool supports_AB_obsolete(Transport* transport) {
983 return !get_suffixes_obsolete(transport).empty();
984}
985
986static int get_slot_count(Transport* transport) {
987 std::string var;
988 int count;
989 if (!fb_getvar(transport, "slot-count", &var)) {
990 if (supports_AB_obsolete(transport)) return 2; // Legacy support
991 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700992 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993 return count;
994}
995
Alex Lightbb9b8a52016-06-29 09:26:44 -0700996static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700998}
999
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001000// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001static std::string get_other_slot(const std::string& current_slot, int count) {
1002 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001003
Daniel Rosenberg80919472016-06-30 19:25:31 -07001004 char next = (current_slot[0] - 'a' + 1)%count + 'a';
1005 return std::string(1, next);
1006}
1007
1008static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
1009 return get_other_slot(current_slot, get_slot_count(transport));
1010}
1011
1012static std::string get_other_slot(Transport* transport, int count) {
1013 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001014}
1015
Alex Lightbb9b8a52016-06-29 09:26:44 -07001016static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001017 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -07001018}
1019
Daniel Rosenberg80919472016-06-30 19:25:31 -07001020static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
1021 std::string slot = slot_name;
1022 if (slot == "_a") slot = "a"; // Legacy support
1023 if (slot == "_b") slot = "b"; // Legacy support
1024 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001025 if (allow_all) {
1026 return "all";
1027 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001028 int count = get_slot_count(transport);
1029 if (count > 0) {
1030 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001031 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001032 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001033 }
1034 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001035 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001036
Daniel Rosenberg80919472016-06-30 19:25:31 -07001037 int count = get_slot_count(transport);
Elliott Hughes4089d342017-10-27 14:21:12 -07001038 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001039
Daniel Rosenberg80919472016-06-30 19:25:31 -07001040 if (slot == "other") {
1041 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001042 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001043 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001044 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001045 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001046 }
1047
Daniel Rosenberg80919472016-06-30 19:25:31 -07001048 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1049
1050 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1051 for (int i=0; i<count; i++) {
1052 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001053 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001054
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001055 exit(1);
1056}
1057
Daniel Rosenberg80919472016-06-30 19:25:31 -07001058static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001059 return verify_slot(transport, slot, true);
1060}
1061
Daniel Rosenberg80919472016-06-30 19:25:31 -07001062static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001063 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001064 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001065 std::string current_slot;
1066
Daniel Rosenberg80919472016-06-30 19:25:31 -07001067 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001068 /* If has-slot is not supported, the answer is no. */
1069 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001070 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001071 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001072 if (slot == "") {
1073 current_slot = get_current_slot(transport);
1074 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001075 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001076 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001077 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001078 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001079 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001080 }
1081 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001082 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001083 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001084 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001085 }
1086 func(part);
1087 }
1088}
1089
David Pursell0b156632015-10-30 11:22:01 -07001090/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1091 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1092 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1093 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001094 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001095static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001096 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001097 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001098
Daniel Rosenberg80919472016-06-30 19:25:31 -07001099 if (slot == "all") {
1100 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001101 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001102 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001103 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001104 for (int i=0; i < get_slot_count(transport); i++) {
1105 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001106 }
1107 } else {
David Pursell0b156632015-10-30 11:22:01 -07001108 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001109 }
1110 } else {
David Pursell0b156632015-10-30 11:22:01 -07001111 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001112 }
1113}
1114
David Pursell0b156632015-10-30 11:22:01 -07001115static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001116 struct fastboot_buffer buf;
1117
Elliott Hughes53ec4952016-05-11 12:39:27 -07001118 if (!load_buf(transport, fname, &buf)) {
1119 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001120 }
1121 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001122}
1123
Elliott Hughes45964a82017-05-03 22:43:23 -07001124static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001125 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001126 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001127 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001128 fb_queue_download("signature", data, sz);
1129 fb_queue_command("signature", "installing signature");
1130}
1131
Daniel Rosenberg13454092016-06-27 19:43:11 -07001132// Sets slot_override as the active slot. If slot_override is blank,
1133// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001134static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001135 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001136 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001137 if (supports_AB_obsolete(transport)) {
1138 separator = "_"; // Legacy support
1139 } else {
1140 return;
1141 }
1142 }
1143 if (slot_override != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001144 fb_set_active(separator + slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001145 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001146 std::string current_slot = get_current_slot(transport);
1147 if (current_slot != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001148 fb_set_active(separator + current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001149 }
1150 }
1151}
1152
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001153static 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 -08001154 queue_info_dump();
1155
Wink Savilleb98762f2011-04-04 17:54:59 -07001156 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1157
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001158 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001159 int error = OpenArchive(filename, &zip);
1160 if (error != 0) {
1161 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001162 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163
Elliott Hughesfc797672015-04-07 20:12:50 -07001164 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001165 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001166 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001167 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001168 }
1169
Elliott Hughes5620d222018-03-28 08:20:00 -07001170 check_requirements(transport, reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171
Alex Lightbb9b8a52016-06-29 09:26:44 -07001172 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001173 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001174 if (slot_override != "") {
1175 secondary = get_other_slot(transport, slot_override);
1176 } else {
1177 secondary = get_other_slot(transport);
1178 }
1179 if (secondary == "") {
1180 if (supports_AB(transport)) {
1181 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1182 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001183 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001184 }
1185 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001186 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001187 const char* slot = slot_override.c_str();
1188 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001189 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001190 slot = secondary.c_str();
1191 } else {
1192 continue;
1193 }
1194 }
1195
Elliott Hughes253c18d2015-03-18 22:47:09 -07001196 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001197 if (fd == -1) {
1198 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001199 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001200 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001201 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001202 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001203
Elliott Hughes253c18d2015-03-18 22:47:09 -07001204 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001205 if (!load_buf_fd(transport, fd, &buf)) {
1206 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1207 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001208
Elliott Hughes4089d342017-10-27 14:21:12 -07001209 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001210 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001211 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001212 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001213 }
1214 flash_buf(partition.c_str(), &buf);
1215 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001216 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001217 * program exits.
1218 */
1219 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001220 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001222
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001223 if (slot_override == "all") {
1224 set_active(transport, "a");
1225 } else {
1226 set_active(transport, slot_override);
1227 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001228
1229 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230}
1231
Alex Lightbb9b8a52016-06-29 09:26:44 -07001232static void do_send_signature(const std::string& fn) {
1233 std::size_t extension_loc = fn.find(".img");
1234 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001235
Alex Lightbb9b8a52016-06-29 09:26:44 -07001236 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001237
1238 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001239 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001240 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001241
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001242 fb_queue_download("signature", data, sz);
1243 fb_queue_command("signature", "installing signature");
1244}
1245
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001246static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001247 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001248 queue_info_dump();
1249
Wink Savilleb98762f2011-04-04 17:54:59 -07001250 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1251
Elliott Hughes45964a82017-05-03 22:43:23 -07001252 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001253 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254
Elliott Hughesfc797672015-04-07 20:12:50 -07001255 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001256 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001257 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001258
Elliott Hughes5620d222018-03-28 08:20:00 -07001259 check_requirements(transport, reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001260
Alex Lightbb9b8a52016-06-29 09:26:44 -07001261 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001262 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001263 if (slot_override != "") {
1264 secondary = get_other_slot(transport, slot_override);
1265 } else {
1266 secondary = get_other_slot(transport);
1267 }
1268 if (secondary == "") {
1269 if (supports_AB(transport)) {
1270 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1271 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001272 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001273 }
1274 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001275
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001276 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001277 const char* slot = NULL;
1278 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001279 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001280 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001281 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001282 }
1283 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001284 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001285 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001286 if (!load_buf(transport, fname.c_str(), &buf)) {
1287 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001288 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001289 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001290
1291 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001292 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001293 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001294 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001295 }
1296 flash_buf(partition.c_str(), &buf);
1297 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001298 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001299 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001300
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001301 if (slot_override == "all") {
1302 set_active(transport, "a");
1303 } else {
1304 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001305 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001306}
1307
Elliott Hughesd6365a72017-05-08 18:04:49 -07001308static std::string next_arg(std::vector<std::string>* args) {
1309 if (args->empty()) syntax_error("expected argument");
1310 std::string result = args->front();
1311 args->erase(args->begin());
1312 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001313}
1314
Elliott Hughesd6365a72017-05-08 18:04:49 -07001315static void do_bypass_unlock_command(std::vector<std::string>* args) {
1316 if (args->empty()) syntax_error("missing unlock_bootloader request");
1317
1318 std::string filename = next_arg(args);
1319
1320 int64_t sz;
1321 void* data = load_file(filename.c_str(), &sz);
1322 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
1323 fb_queue_download("unlock_message", data, sz);
1324 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1325}
1326
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001327static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001328 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001329
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001330 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001331 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001332 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001333 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001334 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001335}
1336
Colin Crossf8387882012-05-24 17:18:41 -07001337static int64_t parse_num(const char *arg)
1338{
1339 char *endptr;
1340 unsigned long long num;
1341
1342 num = strtoull(arg, &endptr, 0);
1343 if (endptr == arg) {
1344 return -1;
1345 }
1346
1347 if (*endptr == 'k' || *endptr == 'K') {
1348 if (num >= (-1ULL) / 1024) {
1349 return -1;
1350 }
1351 num *= 1024LL;
1352 endptr++;
1353 } else if (*endptr == 'm' || *endptr == 'M') {
1354 if (num >= (-1ULL) / (1024 * 1024)) {
1355 return -1;
1356 }
1357 num *= 1024LL * 1024LL;
1358 endptr++;
1359 } else if (*endptr == 'g' || *endptr == 'G') {
1360 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1361 return -1;
1362 }
1363 num *= 1024LL * 1024LL * 1024LL;
1364 endptr++;
1365 }
1366
1367 if (*endptr != '\0') {
1368 return -1;
1369 }
1370
1371 if (num > INT64_MAX) {
1372 return -1;
1373 }
1374
1375 return num;
1376}
1377
Connor O'Brience16a8a2017-02-06 14:39:31 -08001378static std::string fb_fix_numeric_var(std::string var) {
1379 // Some bootloaders (angler, for example), send spurious leading whitespace.
1380 var = android::base::Trim(var);
1381 // Some bootloaders (hammerhead, for example) use implicit hex.
1382 // This code used to use strtol with base 16.
1383 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1384 return var;
1385}
1386
1387static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1388 std::string sizeString;
Elliott Hughes5620d222018-03-28 08:20:00 -07001389 if (!fb_getvar(transport, name, &sizeString) || sizeString.empty()) {
1390 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001391 return 0;
1392 }
1393 sizeString = fb_fix_numeric_var(sizeString);
1394
1395 unsigned size;
1396 if (!android::base::ParseUint(sizeString, &size)) {
1397 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1398 return 0;
1399 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001400 if ((size & (size - 1)) != 0) {
1401 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001402 return 0;
1403 }
1404 return size;
1405}
1406
David Pursell0b156632015-10-30 11:22:01 -07001407static void fb_perform_format(Transport* transport,
Elliott Hughes5620d222018-03-28 08:20:00 -07001408 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001409 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001410 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001411 std::string partition_type, partition_size;
1412
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001413 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 const char* errMsg = nullptr;
1415 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001416 TemporaryFile output;
1417 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001418
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 unsigned int limit = INT_MAX;
1420 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001422 }
1423 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001424 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001425 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001426
Elliott Hughes5620d222018-03-28 08:20:00 -07001427 if (!fb_getvar(transport, "partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001428 errMsg = "Can't determine partition type.\n";
1429 goto failed;
1430 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001431 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001432 if (partition_type != type_override) {
1433 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001434 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001435 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001436 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001437 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001438
Elliott Hughes5620d222018-03-28 08:20:00 -07001439 if (!fb_getvar(transport, "partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440 errMsg = "Unable to get partition size\n";
1441 goto failed;
1442 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001443 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001444 if (partition_size != size_override) {
1445 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001446 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001447 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001448 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001449 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001450 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001451
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001452 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001453 if (!gen) {
1454 if (skip_if_not_supported) {
1455 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001456 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001457 return;
1458 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001459 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1460 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001461 return;
1462 }
1463
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001464 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001465 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001466 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1467 return;
1468 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001469
Connor O'Brience16a8a2017-02-06 14:39:31 -08001470 unsigned eraseBlkSize, logicalBlkSize;
1471 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1472 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1473
Jin Qian4a335822017-04-18 16:23:18 -07001474 if (fs_generator_generate(gen, output.path, size, initial_dir,
1475 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001476 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001477 return;
1478 }
1479
Jin Qian4a335822017-04-18 16:23:18 -07001480 fd.reset(open(output.path, O_RDONLY));
1481 if (fd == -1) {
1482 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1483 return;
1484 }
1485 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001486 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001487 return;
1488 }
1489 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001490 return;
1491
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001492failed:
1493 if (skip_if_not_supported) {
1494 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001495 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001496 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001497 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001498}
1499
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001500int main(int argc, char **argv)
1501{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001502 bool wants_wipe = false;
1503 bool wants_reboot = false;
1504 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001505 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001506 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001507 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001508 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001509 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001510 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001511 void *data;
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001512 uint32_t header_version = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -07001513 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001514 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001515 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001516 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001517
JP Abgrall7b8970c2013-03-07 17:06:41 -08001518 const struct option longopts[] = {
1519 {"base", required_argument, 0, 'b'},
1520 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001521 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001522 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001523 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001524 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001525 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001526 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001527 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001528 {"help", no_argument, 0, 'h'},
1529 {"unbuffered", no_argument, 0, 0},
1530 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001531 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001532 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001533 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001534 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001535 {"skip-reboot", no_argument, 0, 0},
David Zeuthenb6ea4352017-08-07 14:29:26 -04001536 {"disable-verity", no_argument, 0, 0},
1537 {"disable-verification", no_argument, 0, 0},
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001538 {"header-version", required_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001539#if !defined(_WIN32)
1540 {"wipe-and-use-fbe", no_argument, 0, 0},
1541#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001542 {0, 0, 0, 0}
1543 };
Colin Cross8879f982012-05-22 17:53:34 -07001544
1545 serial = getenv("ANDROID_SERIAL");
1546
1547 while (1) {
Elliott Hughes45964a82017-05-03 22:43:23 -07001548 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lc:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001549 if (c < 0) {
1550 break;
1551 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001552 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001553 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001554 case 'a':
1555 wants_set_active = true;
1556 if (optarg)
1557 next_active = optarg;
1558 break;
Colin Cross8879f982012-05-22 17:53:34 -07001559 case 'b':
1560 base_addr = strtoul(optarg, 0, 16);
1561 break;
Colin Cross8879f982012-05-22 17:53:34 -07001562 case 'c':
1563 cmdline = optarg;
1564 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001565 case 'h':
Elliott Hughesd6365a72017-05-08 18:04:49 -07001566 return show_help();
Colin Cross8879f982012-05-22 17:53:34 -07001567 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001568 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001569 unsigned long val;
1570
1571 val = strtoul(optarg, &endptr, 0);
1572 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1573 die("invalid vendor id '%s'", optarg);
1574 vendor_id = (unsigned short)val;
1575 break;
1576 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001577 case 'k':
1578 kernel_offset = strtoul(optarg, 0, 16);
1579 break;
1580 case 'l':
1581 long_listing = 1;
1582 break;
1583 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001584 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001585 if (!page_size) die("invalid page size");
1586 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001587 case 'r':
1588 ramdisk_offset = strtoul(optarg, 0, 16);
1589 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001590 case 't':
1591 tags_offset = strtoul(optarg, 0, 16);
1592 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001593 case 's':
1594 serial = optarg;
1595 break;
1596 case 'S':
1597 sparse_limit = parse_num(optarg);
Elliott Hughes4089d342017-10-27 14:21:12 -07001598 if (sparse_limit < 0) die("invalid sparse limit");
JP Abgrall7b8970c2013-03-07 17:06:41 -08001599 break;
1600 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001601 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001602 break;
1603 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001604 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001605 break;
Colin Cross8879f982012-05-22 17:53:34 -07001606 case '?':
1607 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001608 case 0:
1609 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001610 setvbuf(stdout, nullptr, _IONBF, 0);
1611 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001612 } else if (strcmp("version", longopts[longindex].name) == 0) {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001613 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001614 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001615 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001616 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1617 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001618 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1619 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001620 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1621 skip_reboot = true;
David Zeuthenb6ea4352017-08-07 14:29:26 -04001622 } else if (strcmp("disable-verity", longopts[longindex].name) == 0 ) {
1623 g_disable_verity = true;
1624 } else if (strcmp("disable-verification", longopts[longindex].name) == 0 ) {
1625 g_disable_verification = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001626#if !defined(_WIN32)
1627 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1628 wants_wipe = true;
1629 set_fbe_marker = true;
1630#endif
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001631 } else if (strcmp("header-version", longopts[longindex].name) == 0) {
1632 header_version = strtoul(optarg, nullptr, 0);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001633 } else {
1634 fprintf(stderr, "Internal error in options processing for %s\n",
1635 longopts[longindex].name);
1636 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001637 }
1638 break;
Colin Cross8879f982012-05-22 17:53:34 -07001639 default:
1640 abort();
1641 }
1642 }
1643
1644 argc -= optind;
1645 argv += optind;
1646
Elliott Hughesd6365a72017-05-08 18:04:49 -07001647 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001648
Colin Cross8fb6e062012-07-24 16:36:41 -07001649 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001650 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001651 return 0;
1652 }
1653
Colin Crossc7b75dc2012-08-29 18:17:06 -07001654 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001655 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001656 }
1657
David Pursell0b156632015-10-30 11:22:01 -07001658 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001659 if (transport == nullptr) {
1660 return 1;
1661 }
1662
Elliott Hughes5620d222018-03-28 08:20:00 -07001663 const double start = now();
1664
Daniel Rosenberg80919472016-06-30 19:25:31 -07001665 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1666 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1667 }
1668 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1669 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001670
1671 if (wants_set_active) {
1672 if (next_active == "") {
1673 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001674 std::string current_slot;
1675 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001676 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001677 } else {
1678 wants_set_active = false;
1679 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001681 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001682 }
1683 }
1684 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001685
Elliott Hughesd6365a72017-05-08 18:04:49 -07001686 std::vector<std::string> args(argv, argv + argc);
1687 while (!args.empty()) {
1688 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001689
Elliott Hughesd6365a72017-05-08 18:04:49 -07001690 if (command == "getvar") {
1691 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001692 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001693 } else if (command == "erase") {
1694 std::string partition = next_arg(&args);
1695 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001696 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001697 if (fb_getvar(transport, std::string("partition-type:") + partition,
1698 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001699 fs_get_generator(partition_type) != nullptr) {
1700 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1701 partition_type.c_str());
1702 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001703
Elliott Hughes5620d222018-03-28 08:20:00 -07001704 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001705 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001706 do_for_partitions(transport, partition, slot_override, erase, true);
1707 } else if (android::base::StartsWith(command, "format")) {
1708 // Parsing for: "format[:[type][:[size]]]"
1709 // Some valid things:
1710 // - select only the size, and leave default fs type:
1711 // format::0x4000000 userdata
1712 // - default fs type and size:
1713 // format userdata
1714 // format:: userdata
1715 std::vector<std::string> pieces = android::base::Split(command, ":");
1716 std::string type_override;
1717 if (pieces.size() > 1) type_override = pieces[1].c_str();
1718 std::string size_override;
1719 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001720
Elliott Hughesd6365a72017-05-08 18:04:49 -07001721 std::string partition = next_arg(&args);
1722
1723 auto format = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001724 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001725 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001726 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001727 fb_perform_format(transport, partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001728 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001729 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1730 } else if (command == "signature") {
1731 std::string filename = next_arg(&args);
1732 data = load_file(filename.c_str(), &sz);
1733 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001734 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001735 fb_queue_download("signature", data, sz);
1736 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001737 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001738 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001739
1740 if (args.size() == 1) {
1741 std::string what = next_arg(&args);
1742 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001743 wants_reboot = false;
1744 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 } else if (what == "emergency") {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001746 wants_reboot = false;
1747 wants_reboot_emergency = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001748 } else {
1749 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001750 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001751
Elliott Hughesca85df02015-02-25 10:02:00 -08001752 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001753 if (!args.empty()) syntax_error("junk after reboot command");
1754 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001755 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001756 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001757 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001758 } else if (command == "boot") {
1759 std::string kernel = next_arg(&args);
1760 std::string ramdisk;
1761 if (!args.empty()) ramdisk = next_arg(&args);
1762 std::string second_stage;
1763 if (!args.empty()) second_stage = next_arg(&args);
1764
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001765 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001766 fb_queue_download("boot.img", data, sz);
1767 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001768 } else if (command == "flash") {
1769 std::string pname = next_arg(&args);
1770
Elliott Hughes2810d002016-04-25 14:31:18 -07001771 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001772 if (!args.empty()) {
1773 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001774 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001775 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001776 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001777 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001778
1779 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001780 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001781 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001782 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001783 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001784 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1786 } else if (command == "flash:raw") {
1787 std::string partition = next_arg(&args);
1788 std::string kernel = next_arg(&args);
1789 std::string ramdisk;
1790 if (!args.empty()) ramdisk = next_arg(&args);
1791 std::string second_stage;
1792 if (!args.empty()) second_stage = next_arg(&args);
1793
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001794 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001795 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001796 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001797 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001798 do_for_partitions(transport, partition, slot_override, flashraw, true);
1799 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001800 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001801 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1802 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001803 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001804 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001805 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001806 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001807 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001808 bool slot_all = (slot_override == "all");
1809 if (slot_all) {
1810 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1811 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001812 std::string filename = "update.zip";
1813 if (!args.empty()) {
1814 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001815 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001816 do_update(transport, filename.c_str(), slot_override, erase_first,
1817 skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001818 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001819 } else if (command == "set_active") {
1820 std::string slot = verify_slot(transport, next_arg(&args), false);
1821
David Pursell04396f62016-12-15 16:16:10 -08001822 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1823 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1824 // do have slot-suffixes.
1825 std::string var;
1826 if (!fb_getvar(transport, "slot-count", &var) &&
1827 fb_getvar(transport, "slot-suffixes", &var)) {
1828 slot = "_" + slot;
1829 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001830 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001831 } else if (command == "stage") {
1832 std::string filename = next_arg(&args);
1833
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001834 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001835 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1836 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001837 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001838 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001839 } else if (command == "get_staged") {
1840 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001841 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001842 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001843 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001844 } else if (command == "flashing") {
1845 if (args.empty()) {
1846 syntax_error("missing 'flashing' command");
1847 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1848 args[0] == "unlock_critical" ||
1849 args[0] == "lock_critical" ||
1850 args[0] == "get_unlock_ability" ||
1851 args[0] == "get_unlock_bootloader_nonce" ||
1852 args[0] == "lock_bootloader")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001853 do_oem_command("flashing", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001854 } else if (args.size() == 2 && args[0] == "unlock_bootloader") {
1855 do_bypass_unlock_command(&args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001856 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001857 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001858 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001859 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001860 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001861 }
1862 }
1863
1864 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001865 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001866 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001867 if (set_fbe_marker) {
1868 fprintf(stderr, "setting FBE marker...\n");
1869 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1870 if (initial_userdata_dir.empty()) {
1871 return 1;
1872 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001873 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001874 delete_fbemarker_tmpdir(initial_userdata_dir);
1875 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001876 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001877 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001878
1879 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001880 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001881 fprintf(stderr, "wiping cache...\n");
1882 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001883 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001884 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001885 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001886 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001887 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001888 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001889 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001890 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001891 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001892 } else if (wants_reboot_bootloader) {
1893 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001894 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001895 } else if (wants_reboot_emergency) {
1896 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1897 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001898 }
1899
Elliott Hughes5620d222018-03-28 08:20:00 -07001900 int status = fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
1901 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1902 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001903}