blob: 220a96a9f968cf358d7d265ff65ebddfe1bbce22 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070081static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050082// Don't resparse files in too-big chunks.
83// libsparse will support INT_MAX, but this results in large allocations, so
84// let's keep it at 1GB to avoid memory pressure on the host.
85static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t sparse_limit = -1;
87static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughesfc797672015-04-07 20:12:50 -070089static unsigned page_size = 2048;
90static unsigned base_addr = 0x10000000;
91static unsigned kernel_offset = 0x00008000;
92static unsigned ramdisk_offset = 0x01000000;
93static unsigned second_offset = 0x00f00000;
94static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080095
David Zeuthenb6ea4352017-08-07 14:29:26 -040096static bool g_disable_verity = false;
97static bool g_disable_verification = false;
98
Paul Crowley8f7f56e2015-11-27 09:29:37 +000099static const std::string convert_fbe_marker_filename("convert_fbe");
100
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500102 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103 FB_BUFFER_SPARSE,
104};
105
106struct fastboot_buffer {
107 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700108 void* data;
109 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500110 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
112
113static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700114 const char* nickname;
115 const char* img_name;
116 const char* sig_name;
117 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700118 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700119 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700120} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700121 // clang-format off
122 { "boot", "boot.img", "boot.sig", "boot", false, false },
123 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
124 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700125 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800126 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800127 { "product", "product.img", "product.sig", "product", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700128 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
129 { "system", "system.img", "system.sig", "system", false, false },
130 { nullptr, "system_other.img", "system.sig", "system", true, true },
131 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
132 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
133 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
134 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700135};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700136
Elliott Hughes45964a82017-05-03 22:43:23 -0700137static std::string find_item_given_name(const char* img_name) {
138 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700139 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700140 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700141 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700142 return android::base::StringPrintf("%s/%s", dir, img_name);
143}
144
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700145static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700146 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700147 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700148 return find_item_given_name(images[i].img_name);
149 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 }
151
Elliott Hughesd6365a72017-05-08 18:04:49 -0700152 if (item == "userdata") return find_item_given_name("userdata.img");
153 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700154
Elliott Hughesd6365a72017-05-08 18:04:49 -0700155 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700156 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157}
158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159static int64_t get_file_size(int fd) {
160 struct stat sb;
161 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700162}
163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800165 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 *sz = get_file_size(fd);
169 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700170 goto oops;
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 data = (char*) malloc(*sz);
174 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 close(fd);
178
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return data;
180
181oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 close(fd);
184 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800185 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 return 0;
187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughes2810d002016-04-25 14:31:18 -0700189static void* load_file(const std::string& path, int64_t* sz) {
190 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700191 if (fd == -1) return nullptr;
192 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700193}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Elliott Hughesfc797672015-04-07 20:12:50 -0700195static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700196 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700197 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700198 return -1;
199 }
200
201 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
202 return -1;
203 }
204
Scott Anderson13081c62012-04-06 12:39:30 -0700205 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700207 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
208 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 return 0;
210}
211
Elliott Hughesfc797672015-04-07 20:12:50 -0700212static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800213 return match_fastboot_with_serial(info, serial);
214}
215
Elliott Hughesfc797672015-04-07 20:12:50 -0700216static int list_devices_callback(usb_ifc_info* info) {
217 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if (!serial[0]) {
223 serial = "????????????";
224 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700225 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700226 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700228 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 printf("%-22s fastboot", serial.c_str());
230 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800232 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234
235 return -1;
236}
237
David Pursell2ec418a2016-01-20 08:32:08 -0800238// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
239// a specific device, otherwise the first USB device found will be used.
240//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700241// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800242// Otherwise it blocks until the target is available.
243//
244// The returned Transport is a singleton, so multiple calls to this function will return the same
245// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700246static Transport* open_device() {
247 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800248 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
David Pursell2ec418a2016-01-20 08:32:08 -0800250 if (transport != nullptr) {
251 return transport;
252 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253
David Pursell4601c972016-02-05 15:35:09 -0800254 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800255 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800256 int port = 0;
257 if (serial != nullptr) {
258 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800259
David Pursell4601c972016-02-05 15:35:09 -0800260 if (android::base::StartsWith(serial, "tcp:")) {
261 protocol = Socket::Protocol::kTcp;
262 port = tcp::kDefaultPort;
263 net_address = serial + strlen("tcp:");
264 } else if (android::base::StartsWith(serial, "udp:")) {
265 protocol = Socket::Protocol::kUdp;
266 port = udp::kDefaultPort;
267 net_address = serial + strlen("udp:");
268 }
269
270 if (net_address != nullptr) {
271 std::string error;
272 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700273 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800274 }
David Pursell2ec418a2016-01-20 08:32:08 -0800275 }
276 }
277
278 while (true) {
279 if (!host.empty()) {
280 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800281 if (protocol == Socket::Protocol::kTcp) {
282 transport = tcp::Connect(host, port, &error).release();
283 } else if (protocol == Socket::Protocol::kUdp) {
284 transport = udp::Connect(host, port, &error).release();
285 }
286
David Pursell2ec418a2016-01-20 08:32:08 -0800287 if (transport == nullptr && announce) {
288 fprintf(stderr, "error: %s\n", error.c_str());
289 }
290 } else {
291 transport = usb_open(match_fastboot);
292 }
293
294 if (transport != nullptr) {
295 return transport;
296 }
297
David Pursell0b156632015-10-30 11:22:01 -0700298 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800299 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700300 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800302 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
304}
305
Elliott Hughesfc797672015-04-07 20:12:50 -0700306static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 // We don't actually open a USB device here,
308 // just getting our callback called so we can
309 // list all the connected devices.
310 usb_open(list_devices_callback);
311}
312
Elliott Hughesd6365a72017-05-08 18:04:49 -0700313static void syntax_error(const char* fmt, ...) {
314 fprintf(stderr, "fastboot: usage: ");
315
316 va_list ap;
317 va_start(ap, fmt);
318 vfprintf(stderr, fmt, ap);
319 va_end(ap);
320
321 fprintf(stderr, "\n");
322 exit(1);
323}
324
325static int show_help() {
326 // clang-format off
327 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
Elliott Hughesb76188f2018-04-03 13:30:18 -0700329 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700331 "flashing:\n"
332 " update ZIP Flash all partitions from an update.zip package.\n"
333 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
334 " On A/B devices, flashed slot is set as active.\n"
335 " Secondary images may be flashed to inactive slot.\n"
336 " flash PARTITION [FILENAME]\n"
337 " Flash given partition only.\n"
338 "\n"
339 "basics:\n"
340 " devices [-l] List devices in bootloader (-l: with device paths).\n"
341 " getvar NAME Display given bootloader variable.\n"
342 " reboot [bootloader] Reboot device.\n"
343 "\n"
344 "locking/unlocking:\n"
345 " flashing lock|unlock Lock/unlock partitions for flashing\n"
346 " flashing lock_critical|unlock_critical\n"
347 " Lock/unlock 'critical' bootloader partitions.\n"
348 " flashing get_unlock_ability\n"
349 " Check whether unlocking is allowed (1) or not(0).\n"
350 "\n"
351 "advanced:\n"
352 " erase PARTITION Erase a flash partition.\n"
353 " format[:FS_TYPE[:SIZE]] PARTITION\n"
354 " Format a flash partition.\n"
355 " set_active SLOT Set the active slot.\n"
356 " oem [COMMAND...] Execute OEM-specific command.\n"
357 "\n"
358 "boot image:\n"
359 " boot KERNEL [RAMDISK [SECOND]]\n"
360 " Download and boot kernel from RAM.\n"
361 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
362 " Create boot image and flash it.\n"
363 // TODO: give -c a long option, and remove the short options for this group?
364 " -c CMDLINE Override kernel command line.\n"
365 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
366 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
367 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
368 " --tags-offset Set tags offset (default: 0x00000100).\n"
369 " --page-size BYTES Set flash page size (default: 2048).\n"
370 " --header-version VERSION Set boot image header version.\n"
371 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700372 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700373 //" continue Continue with autoboot.\n"
374 //"\n"
375 "Android Things:\n"
376 " stage IN_FILE Sends given file to stage for the next command.\n"
377 " get_staged OUT_FILE Writes data staged by the last command to a file.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 "\n"
379 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700380 " -w Wipe userdata.\n"
381 " -u Do not erase partition first when formatting.\n"
382 " -s SERIAL Specify a USB device.\n"
383 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
384 // TODO: remove -i?
385 " -i VENDOR_ID Filter devices by USB vendor id.\n"
386 " -S SIZE[K|M|G] Use sparse files above this limit (0 to disable).\n"
387 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
388 " non-current slot (default: current active slot).\n"
389 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
390 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
391 " --skip-reboot Don't reboot device after flashing.\n"
392 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
393 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000396#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 // TODO: remove --unbuffered?
398 " --unbuffered Don't buffer input or output.\n"
399 " --verbose, -v Verbose output.\n"
400 " --version Display version.\n"
401 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700403 // clang-format off
404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000406
Elliott Hughesd6365a72017-05-08 18:04:49 -0700407static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
408 const std::string& second_stage, int64_t* sz,
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700409 const char* cmdline, uint32_t header_version) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700410 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700411 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700412 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800413
Elliott Hughesfc797672015-04-07 20:12:50 -0700414 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700415 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800416 die("cannot load '%s': too short", kernel.c_str());
417 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700418 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700419 if (cmdline) bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), cmdline);
420 uint32_t header_version_existing =
421 reinterpret_cast<boot_img_hdr_v1*>(kdata)->header_version;
422 if (header_version != header_version_existing) {
423 die("header version mismatch, expected: %" PRIu32 " found %" PRIu32 "",
424 header_version, header_version_existing);
425 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800426
Elliott Hughes4089d342017-10-27 14:21:12 -0700427 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800428
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 *sz = ksize;
430 return kdata;
431 }
432
Elliott Hughesfc797672015-04-07 20:12:50 -0700433 void* rdata = nullptr;
434 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700435 if (!ramdisk.empty()) {
436 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700437 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 }
439
Elliott Hughesfc797672015-04-07 20:12:50 -0700440 void* sdata = nullptr;
441 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700442 if (!second_stage.empty()) {
443 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700444 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200445 }
446
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700448 int64_t bsize = 0;
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700449 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800450 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200451 sdata, ssize, second_offset,
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700452 page_size, base_addr, tags_offset, header_version, &bsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700453 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700454
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700455 if (cmdline) bootimg_set_cmdline(bdata, cmdline);
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800458
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 return bdata;
460}
461
Elliott Hughes23af1122017-11-10 08:42:17 -0800462static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700463 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700464 ZipEntry zip_entry;
465 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700466 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700467 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 }
469
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700470 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
Elliott Hughes23af1122017-11-10 08:42:17 -0800472 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700473 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700474 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700476 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700477 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000478
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 return data;
480}
481
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700482#if defined(_WIN32)
483
484// TODO: move this to somewhere it can be shared.
485
486#include <windows.h>
487
488// Windows' tmpfile(3) requires administrator rights because
489// it creates temporary files in the root directory.
490static FILE* win32_tmpfile() {
491 char temp_path[PATH_MAX];
492 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
493 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700494 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700495 }
496
497 char filename[PATH_MAX];
498 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700499 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700500 }
501
502 return fopen(filename, "w+bTD");
503}
504
505#define tmpfile win32_tmpfile
506
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700508 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000509}
510
Elliott Hughes855cdf82018-04-02 14:24:03 -0700511static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700512 // TODO: reimplement to avoid leaking a FILE*.
513 return fileno(tmpfile());
514}
515
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000516#else
517
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700518static std::string make_temporary_template() {
519 const char* tmpdir = getenv("TMPDIR");
520 if (tmpdir == nullptr) tmpdir = P_tmpdir;
521 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
522}
523
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000524static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700525 std::string result(make_temporary_template());
526 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700527 die("unable to create temporary directory: %s", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000528 }
529 return result;
530}
531
Elliott Hughes855cdf82018-04-02 14:24:03 -0700532static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700533 std::string path_template(make_temporary_template());
534 int fd = mkstemp(&path_template[0]);
535 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700536 die("failed to create temporary file for %s: %s\n", what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700537 }
538 unlink(path_template.c_str());
539 return fd;
540}
541
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700542#endif
543
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544static std::string create_fbemarker_tmpdir() {
545 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000546 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
547 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
548 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700549 die("unable to create FBE marker file %s locally: %s",
550 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551 }
552 close(fd);
553 return dir;
554}
555
556static void delete_fbemarker_tmpdir(const std::string& dir) {
557 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
558 if (unlink(marker_file.c_str()) == -1) {
559 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
560 marker_file.c_str(), errno, strerror(errno));
561 return;
562 }
563 if (rmdir(dir.c_str()) == -1) {
564 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
565 dir.c_str(), errno, strerror(errno));
566 return;
567 }
568}
569
Elliott Hughes45964a82017-05-03 22:43:23 -0700570static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700571 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700572
Yusuke Sato07447542015-06-25 14:39:19 -0700573 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700574 ZipEntry zip_entry;
575 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
576 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000577 return -1;
578 }
579
Elliott Hughes23af1122017-11-10 08:42:17 -0800580 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700581 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800582 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700583 int error = ExtractEntryToFile(zip, &zip_entry, fd);
584 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800585 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700586 }
587
Elliott Hughes4089d342017-10-27 14:21:12 -0700588 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800589 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700590 }
591
Elliott Hughes23af1122017-11-10 08:42:17 -0800592 fprintf(stderr, " took %.3fs\n", now() - start);
593
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700594 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700595}
596
Elliott Hughes5620d222018-03-28 08:20:00 -0700597static char* strip(char* s) {
598 while (*s && isspace(*s)) s++;
599
600 int n = strlen(s);
601 while (n-- > 0) {
602 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 s[n] = 0;
604 }
605 return s;
606}
607
608#define MAX_OPTIONS 32
Elliott Hughes5620d222018-03-28 08:20:00 -0700609static void check_requirement(Transport* transport, char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700611 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 char *x;
613 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800614
Elliott Hughes5620d222018-03-28 08:20:00 -0700615 // "require product=alpha|beta|gamma"
616 // "require version-bootloader=1234"
617 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
618 // "require partition-exists=vendor"
619
620 char* name = line;
621 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 if (!strncmp(name, "reject ", 7)) {
623 name += 7;
624 invert = 1;
625 } else if (!strncmp(name, "require ", 8)) {
626 name += 8;
627 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 } else if (!strncmp(name, "require-for-product:", 20)) {
629 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700630 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700631 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700632 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700633 *name = 0;
634 name += 1;
635 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 }
637
638 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700639 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640 *x = 0;
641 val[0] = x + 1;
642
Elliott Hughes5620d222018-03-28 08:20:00 -0700643 name = strip(name);
644
645 // "require partition-exists=x" is a special case, added because of the trouble we had when
646 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
647 // missing out new partitions. A device with new partitions can use "partition-exists" to
648 // override the `is_optional` field in the `images` array.
649 if (!strcmp(name, "partition-exists")) {
650 const char* partition_name = val[0];
651 std::string has_slot;
652 if (!fb_getvar(transport, std::string("has-slot:") + partition_name, &has_slot) ||
653 (has_slot != "yes" && has_slot != "no")) {
654 die("device doesn't have required partition %s!", partition_name);
655 }
656 bool known_partition = false;
657 for (size_t i = 0; i < arraysize(images); ++i) {
658 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
659 images[i].is_optional = false;
660 known_partition = true;
661 }
662 }
663 if (!known_partition) {
664 die("device requires partition %s which is not known to this version of fastboot",
665 partition_name);
666 }
667 return;
668 }
669
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 for(count = 1; count < MAX_OPTIONS; count++) {
671 x = strchr(val[count - 1],'|');
672 if (x == 0) break;
673 *x = 0;
674 val[count] = x + 1;
675 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800676
Elliott Hughes253c18d2015-03-18 22:47:09 -0700677 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700678 const char* var = name;
679 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
Elliott Hughes253c18d2015-03-18 22:47:09 -0700681 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700682 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683
Elliott Hughes5620d222018-03-28 08:20:00 -0700684 for (size_t i = 0; i < count; ++i) {
685 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
687
Elliott Hughes5620d222018-03-28 08:20:00 -0700688 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689}
690
Elliott Hughes5620d222018-03-28 08:20:00 -0700691static void check_requirements(Transport* transport, char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700692 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700694 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 *s++ = 0;
Elliott Hughes5620d222018-03-28 08:20:00 -0700696 check_requirement(transport, data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 data = s;
698 } else {
699 s++;
700 }
701 }
Elliott Hughes5620d222018-03-28 08:20:00 -0700702 if (fb_execute_queue(transport)) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703}
704
Elliott Hughesfc797672015-04-07 20:12:50 -0700705static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700707 fb_queue_display("Bootloader Version...", "version-bootloader");
708 fb_queue_display("Baseband Version.....", "version-baseband");
709 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 fb_queue_notice("--------------------------------------------");
711}
712
Elliott Hughes4089d342017-10-27 14:21:12 -0700713static struct sparse_file** load_sparse_files(int fd, int max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700714 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700715 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700716
Elliott Hughesfc797672015-04-07 20:12:50 -0700717 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700718 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700719
Elliott Hughes253c18d2015-03-18 22:47:09 -0700720 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700721 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700722
723 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700724 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700725
726 return out_s;
727}
728
David Pursell0b156632015-10-30 11:22:01 -0700729static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700730 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700731 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700732 max_download_size.empty()) {
733 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700734 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700735 }
736
Elliott Hughes77c0e662015-11-02 15:51:12 -0800737 // Some bootloaders (angler, for example) send spurious whitespace too.
738 max_download_size = android::base::Trim(max_download_size);
739
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700741 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800742 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700743 return 0;
744 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700745 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700746 return limit;
747}
748
David Pursell0b156632015-10-30 11:22:01 -0700749static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700750 int64_t limit;
751
752 if (sparse_limit == 0) {
753 return 0;
754 } else if (sparse_limit > 0) {
755 limit = sparse_limit;
756 } else {
757 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700758 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700759 }
760 if (target_sparse_limit > 0) {
761 limit = target_sparse_limit;
762 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700763 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700764 }
765 }
766
767 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500768 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700769 }
770
771 return 0;
772}
773
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700774// Until we get lazy inode table init working in make_ext4fs, we need to
775// erase partitions of type ext4 before flashing a filesystem so no stale
776// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700777static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800778 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700779 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800780 return false;
781 }
782 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700783}
784
Elliott Hughes53ec4952016-05-11 12:39:27 -0700785static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700786 int64_t sz = get_file_size(fd);
787 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700788 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700789 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700790
Elliott Hughesfc797672015-04-07 20:12:50 -0700791 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700792 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700793 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700794 sparse_file** s = load_sparse_files(fd, limit);
795 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700796 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700797 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798 buf->type = FB_BUFFER_SPARSE;
799 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700800 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500801 buf->type = FB_BUFFER_FD;
802 buf->data = nullptr;
803 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000804 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700805 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700806
Elliott Hughes53ec4952016-05-11 12:39:27 -0700807 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700808}
809
Elliott Hughes53ec4952016-05-11 12:39:27 -0700810static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500811 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
812
Elliott Hughes53ec4952016-05-11 12:39:27 -0700813 if (fd == -1) {
814 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700815 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500816
817 struct stat s;
818 if (fstat(fd, &s)) {
819 return false;
820 }
821 if (!S_ISREG(s.st_mode)) {
822 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
823 return false;
824 }
825
826 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700827}
828
David Zeuthenb6ea4352017-08-07 14:29:26 -0400829static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
830 // Buffer needs to be at least the size of the VBMeta struct which
831 // is 256 bytes.
832 if (buf->sz < 256) {
833 return;
834 }
835
Elliott Hughes855cdf82018-04-02 14:24:03 -0700836 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400837
838 std::string data;
839 if (!android::base::ReadFdToString(buf->fd, &data)) {
840 die("Failed reading from vbmeta");
841 }
842
843 // There's a 32-bit big endian |flags| field at offset 120 where
844 // bit 0 corresponds to disable-verity and bit 1 corresponds to
845 // disable-verification.
846 //
847 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
848 // the VBMeta struct.
849 if (g_disable_verity) {
850 data[123] |= 0x01;
851 }
852 if (g_disable_verification) {
853 data[123] |= 0x02;
854 }
855
856 if (!android::base::WriteStringToFd(data, fd)) {
857 die("Failed writing to modified vbmeta");
858 }
859 close(buf->fd);
860 buf->fd = fd;
861 lseek(fd, 0, SEEK_SET);
862}
863
Elliott Hughes5620d222018-03-28 08:20:00 -0700864static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700865{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700866 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700867
David Zeuthenb6ea4352017-08-07 14:29:26 -0400868 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
869 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700870 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400871 rewrite_vbmeta_buffer(buf);
872 }
873
Rom Lemarchand622810c2013-06-28 09:54:59 -0700874 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800875 case FB_BUFFER_SPARSE: {
876 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700877 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700878 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700879 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800880 sparse_files.emplace_back(*s, sz);
881 ++s;
882 }
883
884 for (size_t i = 0; i < sparse_files.size(); ++i) {
885 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700886 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700887 }
888 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800889 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500890 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700891 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700892 break;
893 default:
894 die("unknown buffer type: %d", buf->type);
895 }
896}
897
Daniel Rosenberg80919472016-06-30 19:25:31 -0700898static std::string get_current_slot(Transport* transport)
899{
900 std::string current_slot;
901 if (fb_getvar(transport, "current-slot", &current_slot)) {
902 if (current_slot == "_a") return "a"; // Legacy support
903 if (current_slot == "_b") return "b"; // Legacy support
904 return current_slot;
905 }
906 return "";
907}
908
909// Legacy support
910static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700911 std::vector<std::string> suffixes;
912 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700913 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700914 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700915 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700916 suffixes = android::base::Split(suffix_list, ",");
917 // Unfortunately some devices will return an error message in the
918 // guise of a valid value. If we only see only one suffix, it's probably
919 // not real.
920 if (suffixes.size() == 1) {
921 suffixes.clear();
922 }
923 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700924}
925
Daniel Rosenberg80919472016-06-30 19:25:31 -0700926// Legacy support
927static bool supports_AB_obsolete(Transport* transport) {
928 return !get_suffixes_obsolete(transport).empty();
929}
930
931static int get_slot_count(Transport* transport) {
932 std::string var;
933 int count;
934 if (!fb_getvar(transport, "slot-count", &var)) {
935 if (supports_AB_obsolete(transport)) return 2; // Legacy support
936 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700937 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700938 return count;
939}
940
Alex Lightbb9b8a52016-06-29 09:26:44 -0700941static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700942 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700943}
944
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700945// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700946static std::string get_other_slot(const std::string& current_slot, int count) {
947 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700948
Daniel Rosenberg80919472016-06-30 19:25:31 -0700949 char next = (current_slot[0] - 'a' + 1)%count + 'a';
950 return std::string(1, next);
951}
952
953static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
954 return get_other_slot(current_slot, get_slot_count(transport));
955}
956
957static std::string get_other_slot(Transport* transport, int count) {
958 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700959}
960
Alex Lightbb9b8a52016-06-29 09:26:44 -0700961static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700962 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700963}
964
Daniel Rosenberg80919472016-06-30 19:25:31 -0700965static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
966 std::string slot = slot_name;
967 if (slot == "_a") slot = "a"; // Legacy support
968 if (slot == "_b") slot = "b"; // Legacy support
969 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800970 if (allow_all) {
971 return "all";
972 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973 int count = get_slot_count(transport);
974 if (count > 0) {
975 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800976 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700977 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800978 }
979 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700980 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800981
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 int count = get_slot_count(transport);
Elliott Hughes4089d342017-10-27 14:21:12 -0700983 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800984
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985 if (slot == "other") {
986 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700987 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700988 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800989 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700990 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800991 }
992
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
994
995 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
996 for (int i=0; i<count; i++) {
997 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700999
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000 exit(1);
1001}
1002
Daniel Rosenberg80919472016-06-30 19:25:31 -07001003static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001004 return verify_slot(transport, slot, true);
1005}
1006
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001008 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001009 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001010 std::string current_slot;
1011
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001013 /* If has-slot is not supported, the answer is no. */
1014 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001016 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001017 if (slot == "") {
1018 current_slot = get_current_slot(transport);
1019 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001020 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001022 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001023 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 }
1026 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001027 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001028 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001029 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001030 }
1031 func(part);
1032 }
1033}
1034
David Pursell0b156632015-10-30 11:22:01 -07001035/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1036 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1037 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1038 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001039 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001040static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001041 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001042 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001043
Daniel Rosenberg80919472016-06-30 19:25:31 -07001044 if (slot == "all") {
1045 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001046 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001047 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001048 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001049 for (int i=0; i < get_slot_count(transport); i++) {
1050 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001051 }
1052 } else {
David Pursell0b156632015-10-30 11:22:01 -07001053 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001054 }
1055 } else {
David Pursell0b156632015-10-30 11:22:01 -07001056 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001057 }
1058}
1059
David Pursell0b156632015-10-30 11:22:01 -07001060static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001061 struct fastboot_buffer buf;
1062
Elliott Hughes53ec4952016-05-11 12:39:27 -07001063 if (!load_buf(transport, fname, &buf)) {
1064 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001065 }
1066 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001067}
1068
Elliott Hughes45964a82017-05-03 22:43:23 -07001069static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001070 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001071 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001072 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001073 fb_queue_download("signature", data, sz);
1074 fb_queue_command("signature", "installing signature");
1075}
1076
Daniel Rosenberg13454092016-06-27 19:43:11 -07001077// Sets slot_override as the active slot. If slot_override is blank,
1078// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001079static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001080 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001081 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001082 if (supports_AB_obsolete(transport)) {
1083 separator = "_"; // Legacy support
1084 } else {
1085 return;
1086 }
1087 }
1088 if (slot_override != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001089 fb_set_active(separator + slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001090 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001091 std::string current_slot = get_current_slot(transport);
1092 if (current_slot != "") {
Elliott Hughes5620d222018-03-28 08:20:00 -07001093 fb_set_active(separator + current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001094 }
1095 }
1096}
1097
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001098static 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 -08001099 queue_info_dump();
1100
Wink Savilleb98762f2011-04-04 17:54:59 -07001101 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1102
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001103 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001104 int error = OpenArchive(filename, &zip);
1105 if (error != 0) {
1106 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001107 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001108
Elliott Hughesfc797672015-04-07 20:12:50 -07001109 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001110 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001111 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001112 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113 }
1114
Elliott Hughes5620d222018-03-28 08:20:00 -07001115 check_requirements(transport, reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001116
Alex Lightbb9b8a52016-06-29 09:26:44 -07001117 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001118 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001119 if (slot_override != "") {
1120 secondary = get_other_slot(transport, slot_override);
1121 } else {
1122 secondary = get_other_slot(transport);
1123 }
1124 if (secondary == "") {
1125 if (supports_AB(transport)) {
1126 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1127 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001128 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 }
1130 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001131 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001132 const char* slot = slot_override.c_str();
1133 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001134 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001135 slot = secondary.c_str();
1136 } else {
1137 continue;
1138 }
1139 }
1140
Elliott Hughes253c18d2015-03-18 22:47:09 -07001141 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001142 if (fd == -1) {
1143 if (images[i].is_optional) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001144 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001145 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001146 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001147 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001148
Elliott Hughes253c18d2015-03-18 22:47:09 -07001149 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001150 if (!load_buf_fd(transport, fd, &buf)) {
1151 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1152 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001153
Elliott Hughes4089d342017-10-27 14:21:12 -07001154 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001155 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001156 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001157 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001158 }
1159 flash_buf(partition.c_str(), &buf);
1160 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001161 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001162 * program exits.
1163 */
1164 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001165 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001166 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001167
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001168 if (slot_override == "all") {
1169 set_active(transport, "a");
1170 } else {
1171 set_active(transport, slot_override);
1172 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001173
1174 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001175}
1176
Alex Lightbb9b8a52016-06-29 09:26:44 -07001177static void do_send_signature(const std::string& fn) {
1178 std::size_t extension_loc = fn.find(".img");
1179 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001180
Alex Lightbb9b8a52016-06-29 09:26:44 -07001181 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001182
1183 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001184 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001185 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001186
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187 fb_queue_download("signature", data, sz);
1188 fb_queue_command("signature", "installing signature");
1189}
1190
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001191static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001192 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001193 queue_info_dump();
1194
Wink Savilleb98762f2011-04-04 17:54:59 -07001195 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1196
Elliott Hughes45964a82017-05-03 22:43:23 -07001197 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001198 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001199
Elliott Hughesfc797672015-04-07 20:12:50 -07001200 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001201 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001202 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001203
Elliott Hughes5620d222018-03-28 08:20:00 -07001204 check_requirements(transport, reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001205
Alex Lightbb9b8a52016-06-29 09:26:44 -07001206 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001207 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001208 if (slot_override != "") {
1209 secondary = get_other_slot(transport, slot_override);
1210 } else {
1211 secondary = get_other_slot(transport);
1212 }
1213 if (secondary == "") {
1214 if (supports_AB(transport)) {
1215 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1216 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001217 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001218 }
1219 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001220
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001221 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001222 const char* slot = NULL;
1223 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001224 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001225 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001226 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001227 }
1228 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001229 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001230 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001231 if (!load_buf(transport, fname.c_str(), &buf)) {
1232 if (images[i].is_optional) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001233 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001234 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001235
1236 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001237 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001238 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001239 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001240 }
1241 flash_buf(partition.c_str(), &buf);
1242 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001243 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001244 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001245
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001246 if (slot_override == "all") {
1247 set_active(transport, "a");
1248 } else {
1249 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001250 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001251}
1252
Elliott Hughesd6365a72017-05-08 18:04:49 -07001253static std::string next_arg(std::vector<std::string>* args) {
1254 if (args->empty()) syntax_error("expected argument");
1255 std::string result = args->front();
1256 args->erase(args->begin());
1257 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001258}
1259
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001260static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001261 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001262
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001263 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001264 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001265 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001266 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001267 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001268}
1269
Colin Crossf8387882012-05-24 17:18:41 -07001270static int64_t parse_num(const char *arg)
1271{
1272 char *endptr;
1273 unsigned long long num;
1274
1275 num = strtoull(arg, &endptr, 0);
1276 if (endptr == arg) {
1277 return -1;
1278 }
1279
1280 if (*endptr == 'k' || *endptr == 'K') {
1281 if (num >= (-1ULL) / 1024) {
1282 return -1;
1283 }
1284 num *= 1024LL;
1285 endptr++;
1286 } else if (*endptr == 'm' || *endptr == 'M') {
1287 if (num >= (-1ULL) / (1024 * 1024)) {
1288 return -1;
1289 }
1290 num *= 1024LL * 1024LL;
1291 endptr++;
1292 } else if (*endptr == 'g' || *endptr == 'G') {
1293 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1294 return -1;
1295 }
1296 num *= 1024LL * 1024LL * 1024LL;
1297 endptr++;
1298 }
1299
1300 if (*endptr != '\0') {
1301 return -1;
1302 }
1303
1304 if (num > INT64_MAX) {
1305 return -1;
1306 }
1307
1308 return num;
1309}
1310
Connor O'Brience16a8a2017-02-06 14:39:31 -08001311static std::string fb_fix_numeric_var(std::string var) {
1312 // Some bootloaders (angler, for example), send spurious leading whitespace.
1313 var = android::base::Trim(var);
1314 // Some bootloaders (hammerhead, for example) use implicit hex.
1315 // This code used to use strtol with base 16.
1316 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1317 return var;
1318}
1319
1320static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1321 std::string sizeString;
Elliott Hughes5620d222018-03-28 08:20:00 -07001322 if (!fb_getvar(transport, name, &sizeString) || sizeString.empty()) {
1323 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001324 return 0;
1325 }
1326 sizeString = fb_fix_numeric_var(sizeString);
1327
1328 unsigned size;
1329 if (!android::base::ParseUint(sizeString, &size)) {
1330 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1331 return 0;
1332 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001333 if ((size & (size - 1)) != 0) {
1334 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001335 return 0;
1336 }
1337 return size;
1338}
1339
David Pursell0b156632015-10-30 11:22:01 -07001340static void fb_perform_format(Transport* transport,
Elliott Hughes5620d222018-03-28 08:20:00 -07001341 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001342 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001343 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001344 std::string partition_type, partition_size;
1345
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001346 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001347 const char* errMsg = nullptr;
1348 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001349 TemporaryFile output;
1350 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001351
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001352 unsigned int limit = INT_MAX;
1353 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001354 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001355 }
1356 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001357 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001358 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001359
Elliott Hughes5620d222018-03-28 08:20:00 -07001360 if (!fb_getvar(transport, "partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001361 errMsg = "Can't determine partition type.\n";
1362 goto failed;
1363 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001364 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001365 if (partition_type != type_override) {
1366 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001367 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001368 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001369 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001370 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001371
Elliott Hughes5620d222018-03-28 08:20:00 -07001372 if (!fb_getvar(transport, "partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001373 errMsg = "Unable to get partition size\n";
1374 goto failed;
1375 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001376 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001377 if (partition_size != size_override) {
1378 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001379 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001380 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001381 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001382 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001383 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001384
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001385 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001386 if (!gen) {
1387 if (skip_if_not_supported) {
1388 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001389 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390 return;
1391 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001392 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1393 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001394 return;
1395 }
1396
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001397 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001398 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001399 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1400 return;
1401 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001402
Connor O'Brience16a8a2017-02-06 14:39:31 -08001403 unsigned eraseBlkSize, logicalBlkSize;
1404 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1405 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1406
Jin Qian4a335822017-04-18 16:23:18 -07001407 if (fs_generator_generate(gen, output.path, size, initial_dir,
1408 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001409 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001410 return;
1411 }
1412
Jin Qian4a335822017-04-18 16:23:18 -07001413 fd.reset(open(output.path, O_RDONLY));
1414 if (fd == -1) {
1415 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1416 return;
1417 }
1418 if (!load_buf_fd(transport, fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001420 return;
1421 }
1422 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001423 return;
1424
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001425failed:
1426 if (skip_if_not_supported) {
1427 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001428 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001429 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001430 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001431}
1432
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001433int main(int argc, char **argv)
1434{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001435 bool wants_wipe = false;
1436 bool wants_reboot = false;
1437 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001438 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001439 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001440 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001441 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001442 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001443 void *data;
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001444 uint32_t header_version = 0;
Elliott Hughesfc797672015-04-07 20:12:50 -07001445 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001446 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001447 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001448 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001449
JP Abgrall7b8970c2013-03-07 17:06:41 -08001450 const struct option longopts[] = {
1451 {"base", required_argument, 0, 'b'},
1452 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001453 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001454 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001455 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001456 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001457 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001458 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001459 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001460 {"help", no_argument, 0, 'h'},
1461 {"unbuffered", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001462 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001463 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001464 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001465 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001466 {"skip-reboot", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001467 {"verbose", no_argument, 0, 'v'},
1468 {"version", no_argument, 0, 0},
David Zeuthenb6ea4352017-08-07 14:29:26 -04001469 {"disable-verity", no_argument, 0, 0},
1470 {"disable-verification", no_argument, 0, 0},
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001471 {"header-version", required_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001472#if !defined(_WIN32)
1473 {"wipe-and-use-fbe", no_argument, 0, 0},
1474#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001475 {0, 0, 0, 0}
1476 };
Colin Cross8879f982012-05-22 17:53:34 -07001477
1478 serial = getenv("ANDROID_SERIAL");
1479
1480 while (1) {
Elliott Hughesf238d872018-03-29 14:46:29 -07001481 int c = getopt_long(argc, argv, "vwub:k:n:r:t:s:S:lc:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001482 if (c < 0) {
1483 break;
1484 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001485 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001486 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001487 case 'a':
1488 wants_set_active = true;
1489 if (optarg)
1490 next_active = optarg;
1491 break;
Colin Cross8879f982012-05-22 17:53:34 -07001492 case 'b':
1493 base_addr = strtoul(optarg, 0, 16);
1494 break;
Colin Cross8879f982012-05-22 17:53:34 -07001495 case 'c':
1496 cmdline = optarg;
1497 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001498 case 'h':
Elliott Hughesd6365a72017-05-08 18:04:49 -07001499 return show_help();
Colin Cross8879f982012-05-22 17:53:34 -07001500 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001501 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001502 unsigned long val;
1503
1504 val = strtoul(optarg, &endptr, 0);
1505 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1506 die("invalid vendor id '%s'", optarg);
1507 vendor_id = (unsigned short)val;
1508 break;
1509 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001510 case 'k':
1511 kernel_offset = strtoul(optarg, 0, 16);
1512 break;
1513 case 'l':
1514 long_listing = 1;
1515 break;
1516 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001517 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001518 if (!page_size) die("invalid page size");
1519 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001520 case 'r':
1521 ramdisk_offset = strtoul(optarg, 0, 16);
1522 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001523 case 't':
1524 tags_offset = strtoul(optarg, 0, 16);
1525 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001526 case 's':
1527 serial = optarg;
1528 break;
1529 case 'S':
1530 sparse_limit = parse_num(optarg);
Elliott Hughes4089d342017-10-27 14:21:12 -07001531 if (sparse_limit < 0) die("invalid sparse limit");
JP Abgrall7b8970c2013-03-07 17:06:41 -08001532 break;
1533 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001534 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001535 break;
Elliott Hughesf238d872018-03-29 14:46:29 -07001536 case 'v':
Elliott Hughes855cdf82018-04-02 14:24:03 -07001537 set_verbose();
Elliott Hughesf238d872018-03-29 14:46:29 -07001538 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001539 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001540 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001541 break;
Colin Cross8879f982012-05-22 17:53:34 -07001542 case '?':
1543 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001544 case 0:
1545 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001546 setvbuf(stdout, nullptr, _IONBF, 0);
1547 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001548 } else if (strcmp("version", longopts[longindex].name) == 0) {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001549 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001550 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001551 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001552 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1553 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001554 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1555 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001556 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1557 skip_reboot = true;
David Zeuthenb6ea4352017-08-07 14:29:26 -04001558 } else if (strcmp("disable-verity", longopts[longindex].name) == 0 ) {
1559 g_disable_verity = true;
1560 } else if (strcmp("disable-verification", longopts[longindex].name) == 0 ) {
1561 g_disable_verification = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001562#if !defined(_WIN32)
1563 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1564 wants_wipe = true;
1565 set_fbe_marker = true;
1566#endif
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001567 } else if (strcmp("header-version", longopts[longindex].name) == 0) {
1568 header_version = strtoul(optarg, nullptr, 0);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001569 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001570 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001571 }
1572 break;
Colin Cross8879f982012-05-22 17:53:34 -07001573 default:
1574 abort();
1575 }
1576 }
1577
1578 argc -= optind;
1579 argv += optind;
1580
Elliott Hughesd6365a72017-05-08 18:04:49 -07001581 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001582
Colin Cross8fb6e062012-07-24 16:36:41 -07001583 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001584 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001585 return 0;
1586 }
1587
Colin Crossc7b75dc2012-08-29 18:17:06 -07001588 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001589 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001590 }
1591
David Pursell0b156632015-10-30 11:22:01 -07001592 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001593 if (transport == nullptr) {
1594 return 1;
1595 }
1596
Elliott Hughes5620d222018-03-28 08:20:00 -07001597 const double start = now();
1598
Daniel Rosenberg80919472016-06-30 19:25:31 -07001599 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1600 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1601 }
1602 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1603 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001604
1605 if (wants_set_active) {
1606 if (next_active == "") {
1607 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001608 std::string current_slot;
1609 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001610 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001611 } else {
1612 wants_set_active = false;
1613 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001614 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001615 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001616 }
1617 }
1618 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001619
Elliott Hughesd6365a72017-05-08 18:04:49 -07001620 std::vector<std::string> args(argv, argv + argc);
1621 while (!args.empty()) {
1622 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001623
Elliott Hughesd6365a72017-05-08 18:04:49 -07001624 if (command == "getvar") {
1625 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001626 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001627 } else if (command == "erase") {
1628 std::string partition = next_arg(&args);
1629 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001630 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001631 if (fb_getvar(transport, std::string("partition-type:") + partition,
1632 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001633 fs_get_generator(partition_type) != nullptr) {
1634 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1635 partition_type.c_str());
1636 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001637
Elliott Hughes5620d222018-03-28 08:20:00 -07001638 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001639 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001640 do_for_partitions(transport, partition, slot_override, erase, true);
1641 } else if (android::base::StartsWith(command, "format")) {
1642 // Parsing for: "format[:[type][:[size]]]"
1643 // Some valid things:
1644 // - select only the size, and leave default fs type:
1645 // format::0x4000000 userdata
1646 // - default fs type and size:
1647 // format userdata
1648 // format:: userdata
1649 std::vector<std::string> pieces = android::base::Split(command, ":");
1650 std::string type_override;
1651 if (pieces.size() > 1) type_override = pieces[1].c_str();
1652 std::string size_override;
1653 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001654
Elliott Hughesd6365a72017-05-08 18:04:49 -07001655 std::string partition = next_arg(&args);
1656
1657 auto format = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001658 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001659 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001660 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001661 fb_perform_format(transport, partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001662 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001663 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1664 } else if (command == "signature") {
1665 std::string filename = next_arg(&args);
1666 data = load_file(filename.c_str(), &sz);
1667 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001668 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001669 fb_queue_download("signature", data, sz);
1670 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001671 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001672 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001673
1674 if (args.size() == 1) {
1675 std::string what = next_arg(&args);
1676 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001677 wants_reboot = false;
1678 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001679 } else {
1680 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001681 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001682
Elliott Hughesca85df02015-02-25 10:02:00 -08001683 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001684 if (!args.empty()) syntax_error("junk after reboot command");
1685 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001686 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001687 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001688 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001689 } else if (command == "boot") {
1690 std::string kernel = next_arg(&args);
1691 std::string ramdisk;
1692 if (!args.empty()) ramdisk = next_arg(&args);
1693 std::string second_stage;
1694 if (!args.empty()) second_stage = next_arg(&args);
1695
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001696 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001697 fb_queue_download("boot.img", data, sz);
1698 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001699 } else if (command == "flash") {
1700 std::string pname = next_arg(&args);
1701
Elliott Hughes2810d002016-04-25 14:31:18 -07001702 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 if (!args.empty()) {
1704 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001705 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001706 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001708 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001709
1710 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001711 if (erase_first && needs_erase(transport, partition.c_str())) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001712 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001713 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001714 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001715 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001716 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1717 } else if (command == "flash:raw") {
1718 std::string partition = next_arg(&args);
1719 std::string kernel = next_arg(&args);
1720 std::string ramdisk;
1721 if (!args.empty()) ramdisk = next_arg(&args);
1722 std::string second_stage;
1723 if (!args.empty()) second_stage = next_arg(&args);
1724
Hridya Valsaraju7d824132018-03-30 16:15:33 -07001725 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline, header_version);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001726 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001727 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001728 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001729 do_for_partitions(transport, partition, slot_override, flashraw, true);
1730 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001731 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001732 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1733 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001734 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001735 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001736 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001737 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001738 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001739 bool slot_all = (slot_override == "all");
1740 if (slot_all) {
1741 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1742 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001743 std::string filename = "update.zip";
1744 if (!args.empty()) {
1745 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001746 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001747 do_update(transport, filename.c_str(), slot_override, erase_first,
1748 skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001749 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001750 } else if (command == "set_active") {
1751 std::string slot = verify_slot(transport, next_arg(&args), false);
1752
David Pursell04396f62016-12-15 16:16:10 -08001753 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1754 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1755 // do have slot-suffixes.
1756 std::string var;
1757 if (!fb_getvar(transport, "slot-count", &var) &&
1758 fb_getvar(transport, "slot-suffixes", &var)) {
1759 slot = "_" + slot;
1760 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001761 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001762 } else if (command == "stage") {
1763 std::string filename = next_arg(&args);
1764
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001765 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001766 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1767 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001768 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001769 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001770 } else if (command == "get_staged") {
1771 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001772 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001773 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001774 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001775 } else if (command == "flashing") {
1776 if (args.empty()) {
1777 syntax_error("missing 'flashing' command");
1778 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1779 args[0] == "unlock_critical" ||
1780 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001781 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001782 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001783 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001784 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001785 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001786 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001787 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001788 }
1789 }
1790
1791 if (wants_wipe) {
1792 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001793 if (set_fbe_marker) {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001794 fprintf(stderr, "setting FBE marker on initial userdata...\n");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001795 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Elliott Hughesd6365a72017-05-08 18:04:49 -07001796 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001797 delete_fbemarker_tmpdir(initial_userdata_dir);
1798 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001799 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001800 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001801
1802 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001803 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001804 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001805 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001806 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001807 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001808 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001809 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001810 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001811 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001812 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001813 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001814 } else if (wants_reboot_bootloader) {
1815 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001816 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001817 }
1818
Elliott Hughes5620d222018-03-28 08:20:00 -07001819 int status = fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
1820 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1821 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001822}