blob: 03850a3ac9550b905d04f1e8953fb1b6e0d15aea [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 },
Bowgo Tsaide2472d2018-03-29 01:24:12 +0800125 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai7e50de42018-03-26 15:45:01 +0800126 { "product", "product.img", "product.sig", "product", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700127 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
128 { "system", "system.img", "system.sig", "system", false, false },
129 { nullptr, "system_other.img", "system.sig", "system", true, true },
130 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
131 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
132 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
133 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700134};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700135
Elliott Hughes45964a82017-05-03 22:43:23 -0700136static std::string find_item_given_name(const char* img_name) {
137 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700138 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700139 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700140 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700141 return android::base::StringPrintf("%s/%s", dir, img_name);
142}
143
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700144static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700145 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700146 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700147 return find_item_given_name(images[i].img_name);
148 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 }
150
Elliott Hughesd6365a72017-05-08 18:04:49 -0700151 if (item == "userdata") return find_item_given_name("userdata.img");
152 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700153
Elliott Hughesd6365a72017-05-08 18:04:49 -0700154 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700155 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156}
157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158static int64_t get_file_size(int fd) {
159 struct stat sb;
160 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700161}
162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800164 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700165 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 *sz = get_file_size(fd);
168 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700169 goto oops;
170 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172 data = (char*) malloc(*sz);
173 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174
Elliott Hughesfc797672015-04-07 20:12:50 -0700175 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 close(fd);
177
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 return data;
179
180oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800181 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 close(fd);
183 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800184 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 return 0;
186}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187
Elliott Hughes2810d002016-04-25 14:31:18 -0700188static void* load_file(const std::string& path, int64_t* sz) {
189 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700190 if (fd == -1) return nullptr;
191 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700192}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Elliott Hughesfc797672015-04-07 20:12:50 -0700194static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700195 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700196 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700197 return -1;
198 }
199
200 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
201 return -1;
202 }
203
Scott Anderson13081c62012-04-06 12:39:30 -0700204 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800205 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700206 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
207 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 return 0;
209}
210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800212 return match_fastboot_with_serial(info, serial);
213}
214
Elliott Hughesfc797672015-04-07 20:12:50 -0700215static int list_devices_callback(usb_ifc_info* info) {
216 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700218 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 if (!serial[0]) {
222 serial = "????????????";
223 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700224 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700225 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700227 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800228 printf("%-22s fastboot", serial.c_str());
229 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700230 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 }
233
234 return -1;
235}
236
David Pursell2ec418a2016-01-20 08:32:08 -0800237// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
238// a specific device, otherwise the first USB device found will be used.
239//
240// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
241// Otherwise it blocks until the target is available.
242//
243// The returned Transport is a singleton, so multiple calls to this function will return the same
244// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700245static Transport* open_device() {
246 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800247 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248
David Pursell2ec418a2016-01-20 08:32:08 -0800249 if (transport != nullptr) {
250 return transport;
251 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800252
David Pursell4601c972016-02-05 15:35:09 -0800253 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800254 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800255 int port = 0;
256 if (serial != nullptr) {
257 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800258
David Pursell4601c972016-02-05 15:35:09 -0800259 if (android::base::StartsWith(serial, "tcp:")) {
260 protocol = Socket::Protocol::kTcp;
261 port = tcp::kDefaultPort;
262 net_address = serial + strlen("tcp:");
263 } else if (android::base::StartsWith(serial, "udp:")) {
264 protocol = Socket::Protocol::kUdp;
265 port = udp::kDefaultPort;
266 net_address = serial + strlen("udp:");
267 }
268
269 if (net_address != nullptr) {
270 std::string error;
271 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
272 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
273 error.c_str());
274 return nullptr;
275 }
David Pursell2ec418a2016-01-20 08:32:08 -0800276 }
277 }
278
279 while (true) {
280 if (!host.empty()) {
281 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800282 if (protocol == Socket::Protocol::kTcp) {
283 transport = tcp::Connect(host, port, &error).release();
284 } else if (protocol == Socket::Protocol::kUdp) {
285 transport = udp::Connect(host, port, &error).release();
286 }
287
David Pursell2ec418a2016-01-20 08:32:08 -0800288 if (transport == nullptr && announce) {
289 fprintf(stderr, "error: %s\n", error.c_str());
290 }
291 } else {
292 transport = usb_open(match_fastboot);
293 }
294
295 if (transport != nullptr) {
296 return transport;
297 }
298
David Pursell0b156632015-10-30 11:22:01 -0700299 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800300 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700301 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800303 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 }
305}
306
Elliott Hughesfc797672015-04-07 20:12:50 -0700307static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 // We don't actually open a USB device here,
309 // just getting our callback called so we can
310 // list all the connected devices.
311 usb_open(list_devices_callback);
312}
313
Elliott Hughesd6365a72017-05-08 18:04:49 -0700314static void syntax_error(const char* fmt, ...) {
315 fprintf(stderr, "fastboot: usage: ");
316
317 va_list ap;
318 va_start(ap, fmt);
319 vfprintf(stderr, fmt, ap);
320 va_end(ap);
321
322 fprintf(stderr, "\n");
323 exit(1);
324}
325
326static int show_help() {
327 // clang-format off
328 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
330 "usage: fastboot [ <option> ] <command>\n"
331 "\n"
332 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700333 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700334 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700335 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700336 " if found -- recovery. If the device\n"
337 " supports slots, the slot that has\n"
338 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700339 " Secondary images may be flashed to\n"
340 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700341 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
342 " flashing lock Locks the device. Prevents flashing.\n"
343 " flashing unlock Unlocks the device. Allows flashing\n"
344 " any partition except\n"
345 " bootloader-related partitions.\n"
346 " flashing lock_critical Prevents flashing bootloader-related\n"
347 " partitions.\n"
348 " flashing unlock_critical Enables flashing bootloader-related\n"
349 " partitions.\n"
350 " flashing get_unlock_ability Queries bootloader to see if the\n"
351 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700352 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700353 " unlock nonce.\n"
354 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700355 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700356 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700357 " erase <partition> Erase a flash partition.\n"
358 " format[:[<fs type>][:[<size>]] <partition>\n"
359 " Format a flash partition. Can\n"
360 " override the fs type and/or size\n"
361 " the bootloader reports.\n"
362 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700363 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800364 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700365 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
Elliott Hughesd6365a72017-05-08 18:04:49 -0700366 " flash:raw <bootable-partition> <kernel> [ <ramdisk> [ <second> ] ]\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700367 " Create bootimage and flash it.\n"
368 " devices [-l] List all connected devices [with\n"
369 " device paths].\n"
370 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800371 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700372 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800373 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
374 " stage <infile> Sends contents of <infile> to stage for\n"
375 " the next command. Supported only on\n"
376 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800377 " get_staged <outfile> Receives data to <outfile> staged by the\n"
378 " last command. Supported only on Android\n"
379 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700380 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 "\n"
382 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700383 " -w Erase userdata and cache (and format\n"
384 " if supported by partition type).\n"
385 " -u Do not erase partition before\n"
386 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800387 " -s <specific device> Specify a device. For USB, provide either\n"
388 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700389 " For ethernet, provide an address in the\n"
390 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800391 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700392 " -c <cmdline> Override kernel commandline.\n"
393 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800394 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700395 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800396 " --kernel-offset Specify a custom kernel offset.\n"
397 " (default: 0x00008000)\n"
398 " --ramdisk-offset Specify a custom ramdisk offset.\n"
399 " (default: 0x01000000)\n"
400 " --tags-offset Specify a custom tags offset.\n"
401 " (default: 0x00000100)\n"
402 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700403 " (default: 2048).\n"
404 " -S <size>[K|M|G] Automatically sparse files greater\n"
405 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700406 " --slot <slot> Specify slot name to be used if the\n"
407 " device supports slots. All operations\n"
408 " on partitions that support slots will\n"
409 " be done on the slot specified.\n"
410 " 'all' can be given to refer to all slots.\n"
411 " 'other' can be given to refer to a\n"
412 " non-current slot. If this flag is not\n"
413 " used, slotted partitions will default\n"
414 " to the current active slot.\n"
415 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800416 " provided, this will default to the value\n"
417 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800418 " supported, this does nothing. This will\n"
419 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700420 " --skip-secondary Will not flash secondary slots when\n"
421 " performing a flashall or update. This\n"
422 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700423 " --skip-reboot Will not reboot the device when\n"
424 " performing commands that normally\n"
425 " trigger a reboot.\n"
David Zeuthenb6ea4352017-08-07 14:29:26 -0400426 " --disable-verity Set the disable-verity flag in the\n"
427 " the vbmeta image being flashed.\n"
428 " --disable-verification Set the disable-verification flag in"
429 " the vbmeta image being flashed.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000430#if !defined(_WIN32)
431 " --wipe-and-use-fbe On devices which support it,\n"
432 " erase userdata and cache, and\n"
433 " enable file-based encryption\n"
434#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800435 " --unbuffered Do not buffer input or output.\n"
436 " --version Display version.\n"
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700437 " --header-version Set boot image header version while\n"
438 " using flash:raw and boot commands to \n"
439 " to create a boot image.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800440 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700442 // clang-format off
443 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000445
Elliott Hughesd6365a72017-05-08 18:04:49 -0700446static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
447 const std::string& second_stage, int64_t* sz,
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700448 const char* cmdline, uint32_t header_version) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700449 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700450 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700451 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800452
Elliott Hughesfc797672015-04-07 20:12:50 -0700453 // Is this actually a boot image?
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700454 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800455 die("cannot load '%s': too short", kernel.c_str());
456 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700457 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700458 if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800459
Elliott Hughes4089d342017-10-27 14:21:12 -0700460 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 *sz = ksize;
463 return kdata;
464 }
465
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 void* rdata = nullptr;
467 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700468 if (!ramdisk.empty()) {
469 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700470 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 }
472
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 void* sdata = nullptr;
474 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700475 if (!second_stage.empty()) {
476 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700477 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200478 }
479
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700481 int64_t bsize = 0;
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700482 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800483 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200484 sdata, ssize, second_offset,
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700485 page_size, base_addr, tags_offset, header_version, &bsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700486 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700487
Hridya Valsaraju88de5552018-03-30 16:15:33 -0700488 if (cmdline) bootimg_set_cmdline(bdata, cmdline);
Elliott Hughesfc797672015-04-07 20:12:50 -0700489 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800491
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 return bdata;
493}
494
Elliott Hughes23af1122017-11-10 08:42:17 -0800495static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700496 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700497 ZipEntry zip_entry;
498 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700499 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700500 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 }
502
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700503 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504
Elliott Hughes23af1122017-11-10 08:42:17 -0800505 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700506 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700507 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700509 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700510 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000511
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 return data;
513}
514
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700515#if defined(_WIN32)
516
517// TODO: move this to somewhere it can be shared.
518
519#include <windows.h>
520
521// Windows' tmpfile(3) requires administrator rights because
522// it creates temporary files in the root directory.
523static FILE* win32_tmpfile() {
524 char temp_path[PATH_MAX];
525 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
526 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700527 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700528 }
529
530 char filename[PATH_MAX];
531 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700532 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700533 }
534
535 return fopen(filename, "w+bTD");
536}
537
538#define tmpfile win32_tmpfile
539
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000540static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700541 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000542}
543
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700544static int make_temporary_fd() {
545 // TODO: reimplement to avoid leaking a FILE*.
546 return fileno(tmpfile());
547}
548
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000549#else
550
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700551static std::string make_temporary_template() {
552 const char* tmpdir = getenv("TMPDIR");
553 if (tmpdir == nullptr) tmpdir = P_tmpdir;
554 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
555}
556
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000557static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700558 std::string result(make_temporary_template());
559 if (mkdtemp(&result[0]) == nullptr) {
560 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000561 return "";
562 }
563 return result;
564}
565
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700566static int make_temporary_fd() {
567 std::string path_template(make_temporary_template());
568 int fd = mkstemp(&path_template[0]);
569 if (fd == -1) {
570 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
571 return -1;
572 }
573 unlink(path_template.c_str());
574 return fd;
575}
576
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700577#endif
578
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000579static std::string create_fbemarker_tmpdir() {
580 std::string dir = make_temporary_directory();
581 if (dir.empty()) {
582 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
583 return "";
584 }
585 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
586 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
587 if (fd == -1) {
588 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
589 marker_file.c_str(), errno, strerror(errno));
590 return "";
591 }
592 close(fd);
593 return dir;
594}
595
596static void delete_fbemarker_tmpdir(const std::string& dir) {
597 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
598 if (unlink(marker_file.c_str()) == -1) {
599 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
600 marker_file.c_str(), errno, strerror(errno));
601 return;
602 }
603 if (rmdir(dir.c_str()) == -1) {
604 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
605 dir.c_str(), errno, strerror(errno));
606 return;
607 }
608}
609
Elliott Hughes45964a82017-05-03 22:43:23 -0700610static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700611 unique_fd fd(make_temporary_fd());
612 if (fd == -1) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700613 die("failed to create temporary file for '%s': %s", entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700614 }
615
Yusuke Sato07447542015-06-25 14:39:19 -0700616 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700617 ZipEntry zip_entry;
618 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
619 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000620 return -1;
621 }
622
Elliott Hughes23af1122017-11-10 08:42:17 -0800623 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700624 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800625 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700626 int error = ExtractEntryToFile(zip, &zip_entry, fd);
627 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800628 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700629 }
630
Elliott Hughes4089d342017-10-27 14:21:12 -0700631 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800632 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700633 }
634
Elliott Hughes23af1122017-11-10 08:42:17 -0800635 fprintf(stderr, " took %.3fs\n", now() - start);
636
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700637 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638}
639
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700640static char* strip(char* s) {
641 while (*s && isspace(*s)) s++;
642
643 int n = strlen(s);
644 while (n-- > 0) {
645 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800646 s[n] = 0;
647 }
648 return s;
649}
650
651#define MAX_OPTIONS 32
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700652static void check_requirement(Transport* transport, char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800653 char *val[MAX_OPTIONS];
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700654 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 char *x;
656 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800657
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700658 // "require product=alpha|beta|gamma"
659 // "require version-bootloader=1234"
660 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
661 // "require partition-exists=vendor"
662
663 char* name = line;
664 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 if (!strncmp(name, "reject ", 7)) {
666 name += 7;
667 invert = 1;
668 } else if (!strncmp(name, "require ", 8)) {
669 name += 8;
670 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700671 } else if (!strncmp(name, "require-for-product:", 20)) {
672 // Get the product and point name past it
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700673 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700674 name = strchr(name, ' ');
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700675 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700676 *name = 0;
677 name += 1;
678 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 }
680
681 x = strchr(name, '=');
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700682 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683 *x = 0;
684 val[0] = x + 1;
685
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700686 name = strip(name);
687
688 // "require partition-exists=x" is a special case, added because of the trouble we had when
689 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
690 // missing out new partitions. A device with new partitions can use "partition-exists" to
691 // override the `is_optional` field in the `images` array.
692 if (!strcmp(name, "partition-exists")) {
693 const char* partition_name = val[0];
694 std::string has_slot;
695 if (!fb_getvar(transport, std::string("has-slot:") + partition_name, &has_slot) ||
696 (has_slot != "yes" && has_slot != "no")) {
697 die("device doesn't have required partition %s!", partition_name);
698 }
699 bool known_partition = false;
700 for (size_t i = 0; i < arraysize(images); ++i) {
701 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
702 images[i].is_optional = false;
703 known_partition = true;
704 }
705 }
706 if (!known_partition) {
707 die("device requires partition %s which is not known to this version of fastboot",
708 partition_name);
709 }
710 return;
711 }
712
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800713 for(count = 1; count < MAX_OPTIONS; count++) {
714 x = strchr(val[count - 1],'|');
715 if (x == 0) break;
716 *x = 0;
717 val[count] = x + 1;
718 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800719
Elliott Hughes253c18d2015-03-18 22:47:09 -0700720 // Work around an unfortunate name mismatch.
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700721 const char* var = name;
722 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723
Elliott Hughes253c18d2015-03-18 22:47:09 -0700724 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700725 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700727 for (size_t i = 0; i < count; ++i) {
728 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 }
730
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700731 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800732}
733
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700734static void check_requirements(Transport* transport, char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700735 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800736 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700737 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800738 *s++ = 0;
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700739 check_requirement(transport, data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800740 data = s;
741 } else {
742 s++;
743 }
744 }
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700745 if (fb_execute_queue(transport)) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800746}
747
Elliott Hughesfc797672015-04-07 20:12:50 -0700748static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800749 fb_queue_notice("--------------------------------------------");
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700750 fb_queue_display("Bootloader Version...", "version-bootloader");
751 fb_queue_display("Baseband Version.....", "version-baseband");
752 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 fb_queue_notice("--------------------------------------------");
754}
755
Elliott Hughes4089d342017-10-27 14:21:12 -0700756static struct sparse_file** load_sparse_files(int fd, int max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700757 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700758 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700759
Elliott Hughesfc797672015-04-07 20:12:50 -0700760 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700761 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700762
Elliott Hughes253c18d2015-03-18 22:47:09 -0700763 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700764 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700765
766 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700767 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700768
769 return out_s;
770}
771
David Pursell0b156632015-10-30 11:22:01 -0700772static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700773 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700774 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
775 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800776 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700777 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700778 }
779
Elliott Hughes77c0e662015-11-02 15:51:12 -0800780 // Some bootloaders (angler, for example) send spurious whitespace too.
781 max_download_size = android::base::Trim(max_download_size);
782
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700783 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700784 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800785 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700786 return 0;
787 }
788 if (limit > 0) {
789 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
790 }
Colin Crossf8387882012-05-24 17:18:41 -0700791 return limit;
792}
793
David Pursell0b156632015-10-30 11:22:01 -0700794static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700795 int64_t limit;
796
797 if (sparse_limit == 0) {
798 return 0;
799 } else if (sparse_limit > 0) {
800 limit = sparse_limit;
801 } else {
802 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700803 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700804 }
805 if (target_sparse_limit > 0) {
806 limit = target_sparse_limit;
807 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700808 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700809 }
810 }
811
812 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500813 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700814 }
815
816 return 0;
817}
818
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700819// Until we get lazy inode table init working in make_ext4fs, we need to
820// erase partitions of type ext4 before flashing a filesystem so no stale
821// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700822static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800823 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700824 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800825 return false;
826 }
827 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700828}
829
Elliott Hughes53ec4952016-05-11 12:39:27 -0700830static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700831 int64_t sz = get_file_size(fd);
832 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700833 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700834 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700835
Elliott Hughesfc797672015-04-07 20:12:50 -0700836 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700837 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700838 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700839 sparse_file** s = load_sparse_files(fd, limit);
840 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700841 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700842 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700843 buf->type = FB_BUFFER_SPARSE;
844 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700845 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500846 buf->type = FB_BUFFER_FD;
847 buf->data = nullptr;
848 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000849 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700850 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851
Elliott Hughes53ec4952016-05-11 12:39:27 -0700852 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700853}
854
Elliott Hughes53ec4952016-05-11 12:39:27 -0700855static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500856 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
857
Elliott Hughes53ec4952016-05-11 12:39:27 -0700858 if (fd == -1) {
859 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500861
862 struct stat s;
863 if (fstat(fd, &s)) {
864 return false;
865 }
866 if (!S_ISREG(s.st_mode)) {
867 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
868 return false;
869 }
870
871 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700872}
873
David Zeuthenb6ea4352017-08-07 14:29:26 -0400874static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
875 // Buffer needs to be at least the size of the VBMeta struct which
876 // is 256 bytes.
877 if (buf->sz < 256) {
878 return;
879 }
880
881 int fd = make_temporary_fd();
882 if (fd == -1) {
883 die("Failed to create temporary file for vbmeta rewriting");
884 }
885
886 std::string data;
887 if (!android::base::ReadFdToString(buf->fd, &data)) {
888 die("Failed reading from vbmeta");
889 }
890
891 // There's a 32-bit big endian |flags| field at offset 120 where
892 // bit 0 corresponds to disable-verity and bit 1 corresponds to
893 // disable-verification.
894 //
895 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
896 // the VBMeta struct.
897 if (g_disable_verity) {
898 data[123] |= 0x01;
899 }
900 if (g_disable_verification) {
901 data[123] |= 0x02;
902 }
903
904 if (!android::base::WriteStringToFd(data, fd)) {
905 die("Failed writing to modified vbmeta");
906 }
907 close(buf->fd);
908 buf->fd = fd;
909 lseek(fd, 0, SEEK_SET);
910}
911
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700912static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700913{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700914 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700915
David Zeuthenb6ea4352017-08-07 14:29:26 -0400916 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
917 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700918 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400919 rewrite_vbmeta_buffer(buf);
920 }
921
Rom Lemarchand622810c2013-06-28 09:54:59 -0700922 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800923 case FB_BUFFER_SPARSE: {
924 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700925 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700926 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700927 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800928 sparse_files.emplace_back(*s, sz);
929 ++s;
930 }
931
932 for (size_t i = 0; i < sparse_files.size(); ++i) {
933 const auto& pair = sparse_files[i];
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700934 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700935 }
936 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800937 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500938 case FB_BUFFER_FD:
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -0700939 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700940 break;
941 default:
942 die("unknown buffer type: %d", buf->type);
943 }
944}
945
Daniel Rosenberg80919472016-06-30 19:25:31 -0700946static std::string get_current_slot(Transport* transport)
947{
948 std::string current_slot;
949 if (fb_getvar(transport, "current-slot", &current_slot)) {
950 if (current_slot == "_a") return "a"; // Legacy support
951 if (current_slot == "_b") return "b"; // Legacy support
952 return current_slot;
953 }
954 return "";
955}
956
957// Legacy support
958static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700959 std::vector<std::string> suffixes;
960 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700961 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700962 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700963 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700964 suffixes = android::base::Split(suffix_list, ",");
965 // Unfortunately some devices will return an error message in the
966 // guise of a valid value. If we only see only one suffix, it's probably
967 // not real.
968 if (suffixes.size() == 1) {
969 suffixes.clear();
970 }
971 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700972}
973
Daniel Rosenberg80919472016-06-30 19:25:31 -0700974// Legacy support
975static bool supports_AB_obsolete(Transport* transport) {
976 return !get_suffixes_obsolete(transport).empty();
977}
978
979static int get_slot_count(Transport* transport) {
980 std::string var;
981 int count;
982 if (!fb_getvar(transport, "slot-count", &var)) {
983 if (supports_AB_obsolete(transport)) return 2; // Legacy support
984 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700985 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 return count;
987}
988
Alex Lightbb9b8a52016-06-29 09:26:44 -0700989static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700991}
992
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700993// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700994static std::string get_other_slot(const std::string& current_slot, int count) {
995 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700996
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997 char next = (current_slot[0] - 'a' + 1)%count + 'a';
998 return std::string(1, next);
999}
1000
1001static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
1002 return get_other_slot(current_slot, get_slot_count(transport));
1003}
1004
1005static std::string get_other_slot(Transport* transport, int count) {
1006 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007}
1008
Alex Lightbb9b8a52016-06-29 09:26:44 -07001009static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001010 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -07001011}
1012
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
1014 std::string slot = slot_name;
1015 if (slot == "_a") slot = "a"; // Legacy support
1016 if (slot == "_b") slot = "b"; // Legacy support
1017 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001018 if (allow_all) {
1019 return "all";
1020 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001021 int count = get_slot_count(transport);
1022 if (count > 0) {
1023 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001024 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001025 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001026 }
1027 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001028 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001029
Daniel Rosenberg80919472016-06-30 19:25:31 -07001030 int count = get_slot_count(transport);
Elliott Hughes4089d342017-10-27 14:21:12 -07001031 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001032
Daniel Rosenberg80919472016-06-30 19:25:31 -07001033 if (slot == "other") {
1034 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001035 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001036 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001037 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001038 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001039 }
1040
Daniel Rosenberg80919472016-06-30 19:25:31 -07001041 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1042
1043 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1044 for (int i=0; i<count; i++) {
1045 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001046 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001047
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001048 exit(1);
1049}
1050
Daniel Rosenberg80919472016-06-30 19:25:31 -07001051static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001052 return verify_slot(transport, slot, true);
1053}
1054
Daniel Rosenberg80919472016-06-30 19:25:31 -07001055static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001056 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001057 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001058 std::string current_slot;
1059
Daniel Rosenberg80919472016-06-30 19:25:31 -07001060 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001061 /* If has-slot is not supported, the answer is no. */
1062 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001063 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001064 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001065 if (slot == "") {
1066 current_slot = get_current_slot(transport);
1067 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001068 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001069 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001070 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001071 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001072 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001073 }
1074 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001075 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001076 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001077 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001078 }
1079 func(part);
1080 }
1081}
1082
David Pursell0b156632015-10-30 11:22:01 -07001083/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1084 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1085 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1086 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001087 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001088static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001089 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001090 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001091
Daniel Rosenberg80919472016-06-30 19:25:31 -07001092 if (slot == "all") {
1093 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001094 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001095 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001096 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001097 for (int i=0; i < get_slot_count(transport); i++) {
1098 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001099 }
1100 } else {
David Pursell0b156632015-10-30 11:22:01 -07001101 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001102 }
1103 } else {
David Pursell0b156632015-10-30 11:22:01 -07001104 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001105 }
1106}
1107
David Pursell0b156632015-10-30 11:22:01 -07001108static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001109 struct fastboot_buffer buf;
1110
Elliott Hughes53ec4952016-05-11 12:39:27 -07001111 if (!load_buf(transport, fname, &buf)) {
1112 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001113 }
1114 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001115}
1116
Elliott Hughes45964a82017-05-03 22:43:23 -07001117static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001118 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001119 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001120 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001121 fb_queue_download("signature", data, sz);
1122 fb_queue_command("signature", "installing signature");
1123}
1124
Daniel Rosenberg13454092016-06-27 19:43:11 -07001125// Sets slot_override as the active slot. If slot_override is blank,
1126// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001127static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001128 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001130 if (supports_AB_obsolete(transport)) {
1131 separator = "_"; // Legacy support
1132 } else {
1133 return;
1134 }
1135 }
1136 if (slot_override != "") {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001137 fb_set_active(separator + slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001138 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001139 std::string current_slot = get_current_slot(transport);
1140 if (current_slot != "") {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001141 fb_set_active(separator + current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001142 }
1143 }
1144}
1145
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001146static 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 -08001147 queue_info_dump();
1148
Wink Savilleb98762f2011-04-04 17:54:59 -07001149 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1150
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001151 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001152 int error = OpenArchive(filename, &zip);
1153 if (error != 0) {
1154 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001155 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001156
Elliott Hughesfc797672015-04-07 20:12:50 -07001157 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001158 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001159 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001160 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001161 }
1162
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001163 check_requirements(transport, reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001164
Alex Lightbb9b8a52016-06-29 09:26:44 -07001165 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001166 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001167 if (slot_override != "") {
1168 secondary = get_other_slot(transport, slot_override);
1169 } else {
1170 secondary = get_other_slot(transport);
1171 }
1172 if (secondary == "") {
1173 if (supports_AB(transport)) {
1174 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1175 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001176 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001177 }
1178 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001179 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001180 const char* slot = slot_override.c_str();
1181 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001182 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001183 slot = secondary.c_str();
1184 } else {
1185 continue;
1186 }
1187 }
1188
Elliott Hughes253c18d2015-03-18 22:47:09 -07001189 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001190 if (fd == -1) {
1191 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001192 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001193 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001194 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001195 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001196
Elliott Hughes253c18d2015-03-18 22:47:09 -07001197 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001198 if (!load_buf_fd(transport, fd, &buf)) {
1199 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1200 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001201
Elliott Hughes4089d342017-10-27 14:21:12 -07001202 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001203 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001204 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001205 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001206 }
1207 flash_buf(partition.c_str(), &buf);
1208 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001209 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001210 * program exits.
1211 */
1212 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001213 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001215
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001216 if (slot_override == "all") {
1217 set_active(transport, "a");
1218 } else {
1219 set_active(transport, slot_override);
1220 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001221
1222 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223}
1224
Alex Lightbb9b8a52016-06-29 09:26:44 -07001225static void do_send_signature(const std::string& fn) {
1226 std::size_t extension_loc = fn.find(".img");
1227 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001228
Alex Lightbb9b8a52016-06-29 09:26:44 -07001229 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001230
1231 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001232 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001233 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001234
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235 fb_queue_download("signature", data, sz);
1236 fb_queue_command("signature", "installing signature");
1237}
1238
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001239static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001240 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001241 queue_info_dump();
1242
Wink Savilleb98762f2011-04-04 17:54:59 -07001243 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1244
Elliott Hughes45964a82017-05-03 22:43:23 -07001245 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001246 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247
Elliott Hughesfc797672015-04-07 20:12:50 -07001248 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001249 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001250 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001251
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001252 check_requirements(transport, reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001253
Alex Lightbb9b8a52016-06-29 09:26:44 -07001254 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001255 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001256 if (slot_override != "") {
1257 secondary = get_other_slot(transport, slot_override);
1258 } else {
1259 secondary = get_other_slot(transport);
1260 }
1261 if (secondary == "") {
1262 if (supports_AB(transport)) {
1263 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1264 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001265 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001266 }
1267 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001268
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001269 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001270 const char* slot = NULL;
1271 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001272 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001273 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001274 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001275 }
1276 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001277 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001278 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001279 if (!load_buf(transport, fname.c_str(), &buf)) {
1280 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001281 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001282 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001283
1284 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001285 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001286 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001287 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001288 }
1289 flash_buf(partition.c_str(), &buf);
1290 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001291 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001293
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001294 if (slot_override == "all") {
1295 set_active(transport, "a");
1296 } else {
1297 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001298 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001299}
1300
Elliott Hughesd6365a72017-05-08 18:04:49 -07001301static std::string next_arg(std::vector<std::string>* args) {
1302 if (args->empty()) syntax_error("expected argument");
1303 std::string result = args->front();
1304 args->erase(args->begin());
1305 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001306}
1307
Elliott Hughesd6365a72017-05-08 18:04:49 -07001308static void do_bypass_unlock_command(std::vector<std::string>* args) {
1309 if (args->empty()) syntax_error("missing unlock_bootloader request");
1310
1311 std::string filename = next_arg(args);
1312
1313 int64_t sz;
1314 void* data = load_file(filename.c_str(), &sz);
1315 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
1316 fb_queue_download("unlock_message", data, sz);
1317 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1318}
1319
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001320static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001321 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001322
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001323 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001324 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001325 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001326 }
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001327 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001328}
1329
Colin Crossf8387882012-05-24 17:18:41 -07001330static int64_t parse_num(const char *arg)
1331{
1332 char *endptr;
1333 unsigned long long num;
1334
1335 num = strtoull(arg, &endptr, 0);
1336 if (endptr == arg) {
1337 return -1;
1338 }
1339
1340 if (*endptr == 'k' || *endptr == 'K') {
1341 if (num >= (-1ULL) / 1024) {
1342 return -1;
1343 }
1344 num *= 1024LL;
1345 endptr++;
1346 } else if (*endptr == 'm' || *endptr == 'M') {
1347 if (num >= (-1ULL) / (1024 * 1024)) {
1348 return -1;
1349 }
1350 num *= 1024LL * 1024LL;
1351 endptr++;
1352 } else if (*endptr == 'g' || *endptr == 'G') {
1353 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1354 return -1;
1355 }
1356 num *= 1024LL * 1024LL * 1024LL;
1357 endptr++;
1358 }
1359
1360 if (*endptr != '\0') {
1361 return -1;
1362 }
1363
1364 if (num > INT64_MAX) {
1365 return -1;
1366 }
1367
1368 return num;
1369}
1370
Connor O'Brience16a8a2017-02-06 14:39:31 -08001371static std::string fb_fix_numeric_var(std::string var) {
1372 // Some bootloaders (angler, for example), send spurious leading whitespace.
1373 var = android::base::Trim(var);
1374 // Some bootloaders (hammerhead, for example) use implicit hex.
1375 // This code used to use strtol with base 16.
1376 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1377 return var;
1378}
1379
1380static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1381 std::string sizeString;
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001382 if (!fb_getvar(transport, name, &sizeString) || sizeString.empty()) {
1383 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001384 return 0;
1385 }
1386 sizeString = fb_fix_numeric_var(sizeString);
1387
1388 unsigned size;
1389 if (!android::base::ParseUint(sizeString, &size)) {
1390 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1391 return 0;
1392 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001393 if ((size & (size - 1)) != 0) {
1394 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001395 return 0;
1396 }
1397 return size;
1398}
1399
David Pursell0b156632015-10-30 11:22:01 -07001400static void fb_perform_format(Transport* transport,
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001401 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001402 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001403 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001404 std::string partition_type, partition_size;
1405
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001406 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001407 const char* errMsg = nullptr;
1408 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001409 TemporaryFile output;
1410 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001412 unsigned int limit = INT_MAX;
1413 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001414 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 }
1416 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001417 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001418 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001420 if (!fb_getvar(transport, "partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 errMsg = "Can't determine partition type.\n";
1422 goto failed;
1423 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001424 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001425 if (partition_type != type_override) {
1426 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001427 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001428 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001429 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001430 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001431
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001432 if (!fb_getvar(transport, "partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001433 errMsg = "Unable to get partition size\n";
1434 goto failed;
1435 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001436 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001437 if (partition_size != size_override) {
1438 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001439 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001440 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001441 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001442 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001443 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001444
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001445 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446 if (!gen) {
1447 if (skip_if_not_supported) {
1448 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001449 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001450 return;
1451 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001452 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1453 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001454 return;
1455 }
1456
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001457 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001458 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001459 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1460 return;
1461 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462
Connor O'Brience16a8a2017-02-06 14:39:31 -08001463 unsigned eraseBlkSize, logicalBlkSize;
1464 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1465 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1466
Jin Qian4a335822017-04-18 16:23:18 -07001467 if (fs_generator_generate(gen, output.path, size, initial_dir,
1468 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001469 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001470 return;
1471 }
1472
Jin Qian4a335822017-04-18 16:23:18 -07001473 fd.reset(open(output.path, O_RDONLY));
1474 if (fd == -1) {
1475 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1476 return;
1477 }
1478 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001479 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001480 return;
1481 }
1482 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001483 return;
1484
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001485failed:
1486 if (skip_if_not_supported) {
1487 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001488 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001489 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001490 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001491}
1492
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001493int main(int argc, char **argv)
1494{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001495 bool wants_wipe = false;
1496 bool wants_reboot = false;
1497 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001498 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001499 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001500 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001501 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001502 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001503 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001504 void *data;
Hridya Valsaraju88de5552018-03-30 16:15:33 -07001505 uint32_t header_version = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -07001506 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001507 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001508 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001509 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001510
JP Abgrall7b8970c2013-03-07 17:06:41 -08001511 const struct option longopts[] = {
1512 {"base", required_argument, 0, 'b'},
1513 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001514 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001515 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001516 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001517 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001518 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001519 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001520 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001521 {"help", no_argument, 0, 'h'},
1522 {"unbuffered", no_argument, 0, 0},
1523 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001524 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001525 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001526 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001527 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001528 {"skip-reboot", no_argument, 0, 0},
David Zeuthenb6ea4352017-08-07 14:29:26 -04001529 {"disable-verity", no_argument, 0, 0},
1530 {"disable-verification", no_argument, 0, 0},
Hridya Valsaraju88de5552018-03-30 16:15:33 -07001531 {"header-version", required_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001532#if !defined(_WIN32)
1533 {"wipe-and-use-fbe", no_argument, 0, 0},
1534#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001535 {0, 0, 0, 0}
1536 };
Colin Cross8879f982012-05-22 17:53:34 -07001537
1538 serial = getenv("ANDROID_SERIAL");
1539
1540 while (1) {
Elliott Hughes45964a82017-05-03 22:43:23 -07001541 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 -07001542 if (c < 0) {
1543 break;
1544 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001545 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001546 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001547 case 'a':
1548 wants_set_active = true;
1549 if (optarg)
1550 next_active = optarg;
1551 break;
Colin Cross8879f982012-05-22 17:53:34 -07001552 case 'b':
1553 base_addr = strtoul(optarg, 0, 16);
1554 break;
Colin Cross8879f982012-05-22 17:53:34 -07001555 case 'c':
1556 cmdline = optarg;
1557 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001558 case 'h':
Elliott Hughesd6365a72017-05-08 18:04:49 -07001559 return show_help();
Colin Cross8879f982012-05-22 17:53:34 -07001560 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001561 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001562 unsigned long val;
1563
1564 val = strtoul(optarg, &endptr, 0);
1565 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1566 die("invalid vendor id '%s'", optarg);
1567 vendor_id = (unsigned short)val;
1568 break;
1569 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001570 case 'k':
1571 kernel_offset = strtoul(optarg, 0, 16);
1572 break;
1573 case 'l':
1574 long_listing = 1;
1575 break;
1576 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001577 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001578 if (!page_size) die("invalid page size");
1579 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001580 case 'r':
1581 ramdisk_offset = strtoul(optarg, 0, 16);
1582 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001583 case 't':
1584 tags_offset = strtoul(optarg, 0, 16);
1585 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001586 case 's':
1587 serial = optarg;
1588 break;
1589 case 'S':
1590 sparse_limit = parse_num(optarg);
Elliott Hughes4089d342017-10-27 14:21:12 -07001591 if (sparse_limit < 0) die("invalid sparse limit");
JP Abgrall7b8970c2013-03-07 17:06:41 -08001592 break;
1593 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001594 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001595 break;
1596 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001597 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001598 break;
Colin Cross8879f982012-05-22 17:53:34 -07001599 case '?':
1600 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001601 case 0:
1602 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001603 setvbuf(stdout, nullptr, _IONBF, 0);
1604 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001605 } else if (strcmp("version", longopts[longindex].name) == 0) {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001606 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001607 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001608 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001609 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1610 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001611 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1612 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001613 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1614 skip_reboot = true;
David Zeuthenb6ea4352017-08-07 14:29:26 -04001615 } else if (strcmp("disable-verity", longopts[longindex].name) == 0 ) {
1616 g_disable_verity = true;
1617 } else if (strcmp("disable-verification", longopts[longindex].name) == 0 ) {
1618 g_disable_verification = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001619#if !defined(_WIN32)
1620 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1621 wants_wipe = true;
1622 set_fbe_marker = true;
1623#endif
Hridya Valsaraju88de5552018-03-30 16:15:33 -07001624 } else if (strcmp("header-version", longopts[longindex].name) == 0) {
1625 header_version = strtoul(optarg, nullptr, 0);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001626 } else {
1627 fprintf(stderr, "Internal error in options processing for %s\n",
1628 longopts[longindex].name);
1629 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001630 }
1631 break;
Colin Cross8879f982012-05-22 17:53:34 -07001632 default:
1633 abort();
1634 }
1635 }
1636
1637 argc -= optind;
1638 argv += optind;
1639
Elliott Hughesd6365a72017-05-08 18:04:49 -07001640 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001641
Colin Cross8fb6e062012-07-24 16:36:41 -07001642 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001643 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001644 return 0;
1645 }
1646
Colin Crossc7b75dc2012-08-29 18:17:06 -07001647 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001648 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001649 }
1650
David Pursell0b156632015-10-30 11:22:01 -07001651 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001652 if (transport == nullptr) {
1653 return 1;
1654 }
1655
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001656 const double start = now();
1657
Daniel Rosenberg80919472016-06-30 19:25:31 -07001658 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1659 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1660 }
1661 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1662 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001663
1664 if (wants_set_active) {
1665 if (next_active == "") {
1666 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001667 std::string current_slot;
1668 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001669 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001670 } else {
1671 wants_set_active = false;
1672 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001673 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001674 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001675 }
1676 }
1677 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001678
Elliott Hughesd6365a72017-05-08 18:04:49 -07001679 std::vector<std::string> args(argv, argv + argc);
1680 while (!args.empty()) {
1681 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001682
Elliott Hughesd6365a72017-05-08 18:04:49 -07001683 if (command == "getvar") {
1684 std::string variable = next_arg(&args);
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001685 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001686 } else if (command == "erase") {
1687 std::string partition = next_arg(&args);
1688 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001689 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001690 if (fb_getvar(transport, std::string("partition-type:") + partition,
1691 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001692 fs_get_generator(partition_type) != nullptr) {
1693 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1694 partition_type.c_str());
1695 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001696
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001697 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001698 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001699 do_for_partitions(transport, partition, slot_override, erase, true);
1700 } else if (android::base::StartsWith(command, "format")) {
1701 // Parsing for: "format[:[type][:[size]]]"
1702 // Some valid things:
1703 // - select only the size, and leave default fs type:
1704 // format::0x4000000 userdata
1705 // - default fs type and size:
1706 // format userdata
1707 // format:: userdata
1708 std::vector<std::string> pieces = android::base::Split(command, ":");
1709 std::string type_override;
1710 if (pieces.size() > 1) type_override = pieces[1].c_str();
1711 std::string size_override;
1712 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001713
Elliott Hughesd6365a72017-05-08 18:04:49 -07001714 std::string partition = next_arg(&args);
1715
1716 auto format = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001717 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001718 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001719 }
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001720 fb_perform_format(transport, partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001721 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001722 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1723 } else if (command == "signature") {
1724 std::string filename = next_arg(&args);
1725 data = load_file(filename.c_str(), &sz);
1726 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001727 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001728 fb_queue_download("signature", data, sz);
1729 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001730 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001731 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001732
1733 if (args.size() == 1) {
1734 std::string what = next_arg(&args);
1735 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001736 wants_reboot = false;
1737 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001738 } else if (what == "emergency") {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001739 wants_reboot = false;
1740 wants_reboot_emergency = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001741 } else {
1742 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001743 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001744
Elliott Hughesca85df02015-02-25 10:02:00 -08001745 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001746 if (!args.empty()) syntax_error("junk after reboot command");
1747 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001748 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001749 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001750 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001751 } else if (command == "boot") {
1752 std::string kernel = next_arg(&args);
1753 std::string ramdisk;
1754 if (!args.empty()) ramdisk = next_arg(&args);
1755 std::string second_stage;
1756 if (!args.empty()) second_stage = next_arg(&args);
1757
Hridya Valsaraju88de5552018-03-30 16:15:33 -07001758 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001759 fb_queue_download("boot.img", data, sz);
1760 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001761 } else if (command == "flash") {
1762 std::string pname = next_arg(&args);
1763
Elliott Hughes2810d002016-04-25 14:31:18 -07001764 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 if (!args.empty()) {
1766 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001767 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001768 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001769 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001770 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001771
1772 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001773 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001774 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001775 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001776 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001777 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001778 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1779 } else if (command == "flash:raw") {
1780 std::string partition = next_arg(&args);
1781 std::string kernel = next_arg(&args);
1782 std::string ramdisk;
1783 if (!args.empty()) ramdisk = next_arg(&args);
1784 std::string second_stage;
1785 if (!args.empty()) second_stage = next_arg(&args);
1786
Hridya Valsaraju88de5552018-03-30 16:15:33 -07001787 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001788 auto flashraw = [&](const std::string& partition) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001789 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001790 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001791 do_for_partitions(transport, partition, slot_override, flashraw, true);
1792 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001793 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001794 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1795 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001796 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001797 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001798 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001799 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001800 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001801 bool slot_all = (slot_override == "all");
1802 if (slot_all) {
1803 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1804 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001805 std::string filename = "update.zip";
1806 if (!args.empty()) {
1807 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001808 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001809 do_update(transport, filename.c_str(), slot_override, erase_first,
1810 skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001811 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001812 } else if (command == "set_active") {
1813 std::string slot = verify_slot(transport, next_arg(&args), false);
1814
David Pursell04396f62016-12-15 16:16:10 -08001815 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1816 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1817 // do have slot-suffixes.
1818 std::string var;
1819 if (!fb_getvar(transport, "slot-count", &var) &&
1820 fb_getvar(transport, "slot-suffixes", &var)) {
1821 slot = "_" + slot;
1822 }
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001823 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001824 } else if (command == "stage") {
1825 std::string filename = next_arg(&args);
1826
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001827 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001828 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1829 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001830 }
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001831 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001832 } else if (command == "get_staged") {
1833 std::string filename = next_arg(&args);
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001834 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001835 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001836 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001837 } else if (command == "flashing") {
1838 if (args.empty()) {
1839 syntax_error("missing 'flashing' command");
1840 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1841 args[0] == "unlock_critical" ||
1842 args[0] == "lock_critical" ||
1843 args[0] == "get_unlock_ability" ||
1844 args[0] == "get_unlock_bootloader_nonce" ||
1845 args[0] == "lock_bootloader")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001846 do_oem_command("flashing", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001847 } else if (args.size() == 2 && args[0] == "unlock_bootloader") {
1848 do_bypass_unlock_command(&args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001849 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001850 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001851 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001852 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001853 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001854 }
1855 }
1856
1857 if (wants_wipe) {
Paul Crowleyd8525002018-04-24 17:06:30 -07001858 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1859 for (const auto& partition : partitions) {
1860 std::string partition_type;
1861 if (!fb_getvar(transport, std::string{"partition-type:"} + partition, &partition_type)) continue;
1862 if (partition_type.empty()) continue;
1863 fb_queue_erase(partition);
1864 if (partition == "userdata" && set_fbe_marker) {
1865 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1866 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1867 fb_perform_format(transport, partition, 1, "", "", initial_userdata_dir);
1868 delete_fbemarker_tmpdir(initial_userdata_dir);
1869 } else {
1870 fb_perform_format(transport, partition, 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001871 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001872 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001873 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001874 if (wants_set_active) {
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001875 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001876 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001877 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001878 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001879 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001880 } else if (wants_reboot_bootloader) {
1881 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001882 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001883 } else if (wants_reboot_emergency) {
1884 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1885 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001886 }
1887
Elliott Hughes9b7cd9a2018-03-28 08:20:00 -07001888 int status = fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
1889 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1890 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001891}