blob: dc94952ba465560c4e3291a535a35f5b872a1175 [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"
Aaron Wisnerdb511202018-06-26 15:38:35 -050063#include "engine.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;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
Elliott Hughes577e8b42018-04-05 16:12:47 -070080static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070085static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static unsigned g_base_addr = 0x10000000;
89static boot_img_hdr_v1 g_boot_img_hdr = {};
90static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080091
David Zeuthenb6ea4352017-08-07 14:29:26 -040092static bool g_disable_verity = false;
93static bool g_disable_verification = false;
94
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
108
109static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700110 const char* nickname;
111 const char* img_name;
112 const char* sig_name;
113 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700114 bool optional_if_no_image;
115 bool optional_if_no_partition;
116 bool IsSecondary() const { return nickname == nullptr; }
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700117} images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100118 // clang-format off
Elliott Hughes45964a82017-05-03 22:43:23 -0700119 { "boot", "boot.img", "boot.sig", "boot", false, false },
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700120 { nullptr, "boot_other.img", "boot.sig", "boot", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700121 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700122 { "dts", "dt.img", "dt.sig", "dts", true, false },
Bowgo Tsai017217e2018-03-29 01:24:12 +0800123 { "odm", "odm.img", "odm.sig", "odm", true, false },
Bowgo Tsai0afc6b02018-03-26 15:45:01 +0800124 { "product", "product.img", "product.sig", "product", true, false },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100125 { "product-services",
126 "product-services.img",
127 "product-services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700128 "product_services",
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700129 true, true },
Elliott Hughes45964a82017-05-03 22:43:23 -0700130 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700131 { "super", "super.img", "super.sig", "super", true, true },
132 { "system", "system.img", "system.sig", "system", false, true },
133 { nullptr, "system_other.img", "system.sig", "system", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700134 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700135 { "vendor", "vendor.img", "vendor.sig", "vendor", true, true },
136 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, false },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100137 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700138};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700139
Elliott Hughes45964a82017-05-03 22:43:23 -0700140static std::string find_item_given_name(const char* img_name) {
141 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700142 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700143 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700144 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700145 return android::base::StringPrintf("%s/%s", dir, img_name);
146}
147
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700148static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700149 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700150 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700151 return find_item_given_name(images[i].img_name);
152 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 }
154
Elliott Hughesd6365a72017-05-08 18:04:49 -0700155 if (item == "userdata") return find_item_given_name("userdata.img");
156 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700157
Elliott Hughesd6365a72017-05-08 18:04:49 -0700158 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700159 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160}
161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162static int64_t get_file_size(int fd) {
163 struct stat sb;
164 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700165}
166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800168 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700169 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 *sz = get_file_size(fd);
172 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700173 goto oops;
174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176 data = (char*) malloc(*sz);
177 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Elliott Hughesfc797672015-04-07 20:12:50 -0700179 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 close(fd);
181
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 return data;
183
184oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800185 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 close(fd);
187 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800188 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 return 0;
190}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Elliott Hughes2810d002016-04-25 14:31:18 -0700192static void* load_file(const std::string& path, int64_t* sz) {
193 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700194 if (fd == -1) return nullptr;
195 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700196}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197
Elliott Hughesfc797672015-04-07 20:12:50 -0700198static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700199 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
200 return -1;
201 }
202
Scott Anderson13081c62012-04-06 12:39:30 -0700203 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700205 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
206 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 return 0;
208}
209
Elliott Hughesfc797672015-04-07 20:12:50 -0700210static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800211 return match_fastboot_with_serial(info, serial);
212}
213
Elliott Hughesfc797672015-04-07 20:12:50 -0700214static int list_devices_callback(usb_ifc_info* info) {
215 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800216 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700217 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220 if (!serial[0]) {
221 serial = "????????????";
222 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700223 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700224 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800225 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700226 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%-22s fastboot", serial.c_str());
228 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700229 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800230 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 }
232
233 return -1;
234}
235
David Pursell2ec418a2016-01-20 08:32:08 -0800236// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
237// a specific device, otherwise the first USB device found will be used.
238//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700239// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800240// Otherwise it blocks until the target is available.
241//
242// The returned Transport is a singleton, so multiple calls to this function will return the same
243// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700244static Transport* open_device() {
245 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800246 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247
David Pursell2ec418a2016-01-20 08:32:08 -0800248 if (transport != nullptr) {
249 return transport;
250 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800251
David Pursell4601c972016-02-05 15:35:09 -0800252 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800253 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800254 int port = 0;
255 if (serial != nullptr) {
256 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800257
David Pursell4601c972016-02-05 15:35:09 -0800258 if (android::base::StartsWith(serial, "tcp:")) {
259 protocol = Socket::Protocol::kTcp;
260 port = tcp::kDefaultPort;
261 net_address = serial + strlen("tcp:");
262 } else if (android::base::StartsWith(serial, "udp:")) {
263 protocol = Socket::Protocol::kUdp;
264 port = udp::kDefaultPort;
265 net_address = serial + strlen("udp:");
266 }
267
268 if (net_address != nullptr) {
269 std::string error;
270 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700271 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800272 }
David Pursell2ec418a2016-01-20 08:32:08 -0800273 }
274 }
275
276 while (true) {
277 if (!host.empty()) {
278 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800279 if (protocol == Socket::Protocol::kTcp) {
280 transport = tcp::Connect(host, port, &error).release();
281 } else if (protocol == Socket::Protocol::kUdp) {
282 transport = udp::Connect(host, port, &error).release();
283 }
284
David Pursell2ec418a2016-01-20 08:32:08 -0800285 if (transport == nullptr && announce) {
286 fprintf(stderr, "error: %s\n", error.c_str());
287 }
288 } else {
289 transport = usb_open(match_fastboot);
290 }
291
292 if (transport != nullptr) {
293 return transport;
294 }
295
David Pursell0b156632015-10-30 11:22:01 -0700296 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800297 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700298 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800300 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
302}
303
Elliott Hughesfc797672015-04-07 20:12:50 -0700304static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 // We don't actually open a USB device here,
306 // just getting our callback called so we can
307 // list all the connected devices.
308 usb_open(list_devices_callback);
309}
310
Elliott Hughesd6365a72017-05-08 18:04:49 -0700311static void syntax_error(const char* fmt, ...) {
312 fprintf(stderr, "fastboot: usage: ");
313
314 va_list ap;
315 va_start(ap, fmt);
316 vfprintf(stderr, fmt, ap);
317 va_end(ap);
318
319 fprintf(stderr, "\n");
320 exit(1);
321}
322
323static int show_help() {
324 // clang-format off
325 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700326// 1 2 3 4 5 6 7 8
327// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700328 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700330 "flashing:\n"
331 " update ZIP Flash all partitions from an update.zip package.\n"
332 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
333 " On A/B devices, flashed slot is set as active.\n"
334 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700335 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
336 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700337 "\n"
338 "basics:\n"
339 " devices [-l] List devices in bootloader (-l: with device paths).\n"
340 " getvar NAME Display given bootloader variable.\n"
341 " reboot [bootloader] Reboot device.\n"
342 "\n"
343 "locking/unlocking:\n"
344 " flashing lock|unlock Lock/unlock partitions for flashing\n"
345 " flashing lock_critical|unlock_critical\n"
346 " Lock/unlock 'critical' bootloader partitions.\n"
347 " flashing get_unlock_ability\n"
348 " Check whether unlocking is allowed (1) or not(0).\n"
349 "\n"
350 "advanced:\n"
351 " erase PARTITION Erase a flash partition.\n"
352 " format[:FS_TYPE[:SIZE]] PARTITION\n"
353 " Format a flash partition.\n"
354 " set_active SLOT Set the active slot.\n"
355 " oem [COMMAND...] Execute OEM-specific command.\n"
356 "\n"
357 "boot image:\n"
358 " boot KERNEL [RAMDISK [SECOND]]\n"
359 " Download and boot kernel from RAM.\n"
360 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
361 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700362 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700363 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
364 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
365 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
366 " --tags-offset Set tags offset (default: 0x00000100).\n"
367 " --page-size BYTES Set flash page size (default: 2048).\n"
368 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700369 " --os-version MAJOR[.MINOR[.PATCH]]\n"
370 " Set boot image OS version (default: 0.0.0).\n"
371 " --os-patch-level YYYY-MM-DD\n"
372 " Set boot image OS security patch level.\n"
373 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700374 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700375 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700376 //" continue Continue with autoboot.\n"
377 //"\n"
378 "Android Things:\n"
379 " stage IN_FILE Sends given file to stage for the next command.\n"
380 " 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 -0800381 "\n"
382 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700384 " -s SERIAL Specify a USB device.\n"
385 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700386 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700387 " --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,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700408 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700409 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700411 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700414 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800415 die("cannot load '%s': too short", kernel.c_str());
416 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700418 if (!g_cmdline.empty()) {
419 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
420 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
Elliott Hughes4089d342017-10-27 14:21:12 -0700422 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800423
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 *sz = ksize;
425 return kdata;
426 }
427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 void* rdata = nullptr;
429 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700430 if (!ramdisk.empty()) {
431 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700432 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 }
434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 void* sdata = nullptr;
436 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700437 if (!second_stage.empty()) {
438 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700439 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200440 }
441
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800442 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700443 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
444 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700445 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700446
Elliott Hughes577e8b42018-04-05 16:12:47 -0700447 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
448 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 return bdata;
451}
452
Elliott Hughes23af1122017-11-10 08:42:17 -0800453static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700454 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700455 ZipEntry zip_entry;
456 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700457 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700458 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 }
460
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700461 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462
Elliott Hughes23af1122017-11-10 08:42:17 -0800463 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700464 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700465 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700467 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700468 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 return data;
471}
472
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700473#if defined(_WIN32)
474
475// TODO: move this to somewhere it can be shared.
476
477#include <windows.h>
478
479// Windows' tmpfile(3) requires administrator rights because
480// it creates temporary files in the root directory.
481static FILE* win32_tmpfile() {
482 char temp_path[PATH_MAX];
483 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
484 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700485 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700486 }
487
488 char filename[PATH_MAX];
489 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700490 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700491 }
492
493 return fopen(filename, "w+bTD");
494}
495
496#define tmpfile win32_tmpfile
497
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000498static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700499 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000500}
501
Elliott Hughes855cdf82018-04-02 14:24:03 -0700502static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700503 // TODO: reimplement to avoid leaking a FILE*.
504 return fileno(tmpfile());
505}
506
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507#else
508
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700509static std::string make_temporary_template() {
510 const char* tmpdir = getenv("TMPDIR");
511 if (tmpdir == nullptr) tmpdir = P_tmpdir;
512 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
513}
514
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000515static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700516 std::string result(make_temporary_template());
517 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700518 die("unable to create temporary directory with template %s: %s",
519 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000520 }
521 return result;
522}
523
Elliott Hughes855cdf82018-04-02 14:24:03 -0700524static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700525 std::string path_template(make_temporary_template());
526 int fd = mkstemp(&path_template[0]);
527 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700528 die("failed to create temporary file for %s with template %s: %s\n",
529 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700530 }
531 unlink(path_template.c_str());
532 return fd;
533}
534
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700535#endif
536
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000537static std::string create_fbemarker_tmpdir() {
538 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000539 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
540 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
541 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700542 die("unable to create FBE marker file %s locally: %s",
543 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544 }
545 close(fd);
546 return dir;
547}
548
549static void delete_fbemarker_tmpdir(const std::string& dir) {
550 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
551 if (unlink(marker_file.c_str()) == -1) {
552 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
553 marker_file.c_str(), errno, strerror(errno));
554 return;
555 }
556 if (rmdir(dir.c_str()) == -1) {
557 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
558 dir.c_str(), errno, strerror(errno));
559 return;
560 }
561}
562
Elliott Hughes45964a82017-05-03 22:43:23 -0700563static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700564 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565
Yusuke Sato07447542015-06-25 14:39:19 -0700566 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700567 ZipEntry zip_entry;
568 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
569 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000570 return -1;
571 }
572
Elliott Hughes23af1122017-11-10 08:42:17 -0800573 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700574 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800575 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700576 int error = ExtractEntryToFile(zip, &zip_entry, fd);
577 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800578 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700579 }
580
Elliott Hughes4089d342017-10-27 14:21:12 -0700581 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800582 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700583 }
584
Elliott Hughes23af1122017-11-10 08:42:17 -0800585 fprintf(stderr, " took %.3fs\n", now() - start);
586
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700587 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700588}
589
Elliott Hughes5620d222018-03-28 08:20:00 -0700590static char* strip(char* s) {
591 while (*s && isspace(*s)) s++;
592
593 int n = strlen(s);
594 while (n-- > 0) {
595 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 s[n] = 0;
597 }
598 return s;
599}
600
601#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500602static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700604 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800605 char *x;
606 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800607
Elliott Hughes5620d222018-03-28 08:20:00 -0700608 // "require product=alpha|beta|gamma"
609 // "require version-bootloader=1234"
610 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
611 // "require partition-exists=vendor"
612
613 char* name = line;
614 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800615 if (!strncmp(name, "reject ", 7)) {
616 name += 7;
617 invert = 1;
618 } else if (!strncmp(name, "require ", 8)) {
619 name += 8;
620 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700621 } else if (!strncmp(name, "require-for-product:", 20)) {
622 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700623 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700624 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700625 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700626 *name = 0;
627 name += 1;
628 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 }
630
631 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700632 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 *x = 0;
634 val[0] = x + 1;
635
Elliott Hughes5620d222018-03-28 08:20:00 -0700636 name = strip(name);
637
638 // "require partition-exists=x" is a special case, added because of the trouble we had when
639 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
640 // missing out new partitions. A device with new partitions can use "partition-exists" to
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700641 // override the fields `optional_if_no_image` and 'optional_if_no_partition' in the `images`
642 // array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700643 if (!strcmp(name, "partition-exists")) {
644 const char* partition_name = val[0];
645 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500646 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700647 (has_slot != "yes" && has_slot != "no")) {
648 die("device doesn't have required partition %s!", partition_name);
649 }
650 bool known_partition = false;
651 for (size_t i = 0; i < arraysize(images); ++i) {
652 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700653 images[i].optional_if_no_image = false;
654 images[i].optional_if_no_partition = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700655 known_partition = true;
656 }
657 }
658 if (!known_partition) {
659 die("device requires partition %s which is not known to this version of fastboot",
660 partition_name);
661 }
662 return;
663 }
664
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 for(count = 1; count < MAX_OPTIONS; count++) {
666 x = strchr(val[count - 1],'|');
667 if (x == 0) break;
668 *x = 0;
669 val[count] = x + 1;
670 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800671
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700673 const char* var = name;
674 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800675
Elliott Hughes253c18d2015-03-18 22:47:09 -0700676 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700677 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678
Elliott Hughes5620d222018-03-28 08:20:00 -0700679 for (size_t i = 0; i < count; ++i) {
680 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 }
682
Elliott Hughes5620d222018-03-28 08:20:00 -0700683 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800684}
685
Aaron Wisnerdb511202018-06-26 15:38:35 -0500686static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700687 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700689 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500691 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 data = s;
693 } else {
694 s++;
695 }
696 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500697 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698}
699
Elliott Hughesfc797672015-04-07 20:12:50 -0700700static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700702 fb_queue_display("Bootloader Version...", "version-bootloader");
703 fb_queue_display("Baseband Version.....", "version-baseband");
704 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 fb_queue_notice("--------------------------------------------");
706}
707
Tao Bao41cf35f2018-04-24 10:54:21 -0700708static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700709 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700710 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700711
Tao Bao41cf35f2018-04-24 10:54:21 -0700712 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
713 die("invalid max size %" PRId64, max_size);
714 }
715
Elliott Hughesfc797672015-04-07 20:12:50 -0700716 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700717 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700718
Elliott Hughes253c18d2015-03-18 22:47:09 -0700719 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700720 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700721
722 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700723 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700724
725 return out_s;
726}
727
Aaron Wisnerdb511202018-06-26 15:38:35 -0500728static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700729 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500730 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700731 max_download_size.empty()) {
732 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700733 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700734 }
735
Elliott Hughes77c0e662015-11-02 15:51:12 -0800736 // Some bootloaders (angler, for example) send spurious whitespace too.
737 max_download_size = android::base::Trim(max_download_size);
738
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700739 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700740 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800741 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700742 return 0;
743 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700744 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700745 return limit;
746}
747
Aaron Wisnerdb511202018-06-26 15:38:35 -0500748static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700749 int64_t limit = sparse_limit;
750 if (limit == 0) {
751 // Unlimited, so see what the target device's limit is.
752 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700753 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500754 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700755 }
756 if (target_sparse_limit > 0) {
757 limit = target_sparse_limit;
758 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700759 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700760 }
761 }
762
763 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500764 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700765 }
766
767 return 0;
768}
769
Aaron Wisnerdb511202018-06-26 15:38:35 -0500770static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700771 int64_t sz = get_file_size(fd);
772 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700773 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700774 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700775
Elliott Hughesfc797672015-04-07 20:12:50 -0700776 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500777 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700778 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700779 sparse_file** s = load_sparse_files(fd, limit);
780 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700781 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700782 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700783 buf->type = FB_BUFFER_SPARSE;
784 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700785 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500786 buf->type = FB_BUFFER_FD;
787 buf->data = nullptr;
788 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000789 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700790 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791
Elliott Hughes53ec4952016-05-11 12:39:27 -0700792 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700793}
794
Aaron Wisnerdb511202018-06-26 15:38:35 -0500795static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500796 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
797
Elliott Hughes53ec4952016-05-11 12:39:27 -0700798 if (fd == -1) {
799 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500801
802 struct stat s;
803 if (fstat(fd, &s)) {
804 return false;
805 }
806 if (!S_ISREG(s.st_mode)) {
807 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
808 return false;
809 }
810
Aaron Wisnerdb511202018-06-26 15:38:35 -0500811 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812}
813
David Zeuthenb6ea4352017-08-07 14:29:26 -0400814static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
815 // Buffer needs to be at least the size of the VBMeta struct which
816 // is 256 bytes.
817 if (buf->sz < 256) {
818 return;
819 }
820
Elliott Hughes855cdf82018-04-02 14:24:03 -0700821 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400822
823 std::string data;
824 if (!android::base::ReadFdToString(buf->fd, &data)) {
825 die("Failed reading from vbmeta");
826 }
827
828 // There's a 32-bit big endian |flags| field at offset 120 where
829 // bit 0 corresponds to disable-verity and bit 1 corresponds to
830 // disable-verification.
831 //
832 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
833 // the VBMeta struct.
834 if (g_disable_verity) {
835 data[123] |= 0x01;
836 }
837 if (g_disable_verification) {
838 data[123] |= 0x02;
839 }
840
841 if (!android::base::WriteStringToFd(data, fd)) {
842 die("Failed writing to modified vbmeta");
843 }
844 close(buf->fd);
845 buf->fd = fd;
846 lseek(fd, 0, SEEK_SET);
847}
848
Elliott Hughes5620d222018-03-28 08:20:00 -0700849static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700850{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700851 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700852
David Zeuthenb6ea4352017-08-07 14:29:26 -0400853 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
854 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700855 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400856 rewrite_vbmeta_buffer(buf);
857 }
858
Rom Lemarchand622810c2013-06-28 09:54:59 -0700859 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800860 case FB_BUFFER_SPARSE: {
861 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700862 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700864 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800865 sparse_files.emplace_back(*s, sz);
866 ++s;
867 }
868
869 for (size_t i = 0; i < sparse_files.size(); ++i) {
870 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700871 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700872 }
873 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800874 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500875 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700876 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700877 break;
878 default:
879 die("unknown buffer type: %d", buf->type);
880 }
881}
882
Aaron Wisnerdb511202018-06-26 15:38:35 -0500883static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700884 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500885 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700886 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700887}
888
Aaron Wisnerdb511202018-06-26 15:38:35 -0500889static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700890 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700891 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500892 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700893 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700894 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700895 return count;
896}
897
Aaron Wisnerdb511202018-06-26 15:38:35 -0500898static bool supports_AB() {
899 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700900}
901
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700902// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903static std::string get_other_slot(const std::string& current_slot, int count) {
904 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700905
Daniel Rosenberg80919472016-06-30 19:25:31 -0700906 char next = (current_slot[0] - 'a' + 1)%count + 'a';
907 return std::string(1, next);
908}
909
Aaron Wisnerdb511202018-06-26 15:38:35 -0500910static std::string get_other_slot(const std::string& current_slot) {
911 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700912}
913
Aaron Wisnerdb511202018-06-26 15:38:35 -0500914static std::string get_other_slot(int count) {
915 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700916}
917
Aaron Wisnerdb511202018-06-26 15:38:35 -0500918static std::string get_other_slot() {
919 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700920}
921
Aaron Wisnerdb511202018-06-26 15:38:35 -0500922static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700923 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700924 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800925 if (allow_all) {
926 return "all";
927 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500928 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700929 if (count > 0) {
930 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800931 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700932 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800933 }
934 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700935 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800936
Aaron Wisnerdb511202018-06-26 15:38:35 -0500937 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700938 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800939
Daniel Rosenberg80919472016-06-30 19:25:31 -0700940 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500941 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700942 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700943 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800944 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700945 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800946 }
947
Daniel Rosenberg80919472016-06-30 19:25:31 -0700948 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
949
950 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
951 for (int i=0; i<count; i++) {
952 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700953 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700954
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700955 exit(1);
956}
957
Aaron Wisnerdb511202018-06-26 15:38:35 -0500958static std::string verify_slot(const std::string& slot) {
959 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800960}
961
Aaron Wisnerdb511202018-06-26 15:38:35 -0500962static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700963 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800964 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700965 std::string current_slot;
966
Aaron Wisnerdb511202018-06-26 15:38:35 -0500967 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800968 /* If has-slot is not supported, the answer is no. */
969 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700970 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800971 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500973 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700974 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700975 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700976 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700980 }
981 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700983 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700984 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 }
986 func(part);
987 }
988}
989
David Pursell0b156632015-10-30 11:22:01 -0700990/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
991 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
992 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
993 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994 */
Aaron Wisnerdb511202018-06-26 15:38:35 -0500995static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700996 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800997 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998
Daniel Rosenberg80919472016-06-30 19:25:31 -0700999 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001000 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001001 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001002 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001003 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001004 for (int i=0; i < get_slot_count(); i++) {
1005 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001006 }
1007 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001008 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001009 }
1010 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001011 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001012 }
1013}
1014
Aaron Wisnerdb511202018-06-26 15:38:35 -05001015static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001016 struct fastboot_buffer buf;
1017
Aaron Wisnerdb511202018-06-26 15:38:35 -05001018 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001019 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001020 }
1021 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001022}
1023
Elliott Hughes45964a82017-05-03 22:43:23 -07001024static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001025 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001026 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001027 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001028 fb_queue_download("signature", data, sz);
1029 fb_queue_command("signature", "installing signature");
1030}
1031
Daniel Rosenberg13454092016-06-27 19:43:11 -07001032// Sets slot_override as the active slot. If slot_override is blank,
1033// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001034static void set_active(const std::string& slot_override) {
1035 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001036
Daniel Rosenberg80919472016-06-30 19:25:31 -07001037 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001038 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001039 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001040 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001041 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001042 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001043 }
1044 }
1045}
1046
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001047static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1048 std::string has_slot;
1049 std::string partition_name = partition;
1050
1051 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1052 if (slot == "") {
1053 std::string current_slot = get_current_slot();
1054 if (current_slot == "") {
1055 die("Failed to identify current slot");
1056 }
1057 partition_name += "_" + current_slot;
1058 } else {
1059 partition_name += "_" + slot;
1060 }
1061 }
1062 std::string partition_size;
1063 return fb_getvar("partition-size:" + partition_name, &partition_size);
1064}
1065
Aaron Wisnerdb511202018-06-26 15:38:35 -05001066static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001067 queue_info_dump();
1068
Wink Savilleb98762f2011-04-04 17:54:59 -07001069 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1070
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001071 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001072 int error = OpenArchive(filename, &zip);
1073 if (error != 0) {
1074 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001075 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001076
Elliott Hughesfc797672015-04-07 20:12:50 -07001077 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001078 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001079 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001080 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001081 }
1082
Aaron Wisnerdb511202018-06-26 15:38:35 -05001083 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084
Alex Lightbb9b8a52016-06-29 09:26:44 -07001085 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001086 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001087 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001088 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001089 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001090 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001091 }
1092 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001093 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001094 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1095 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001096 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001097 }
1098 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001099 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001100 const char* slot = slot_override.c_str();
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001101 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001102 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001103 slot = secondary.c_str();
1104 } else {
1105 continue;
1106 }
1107 }
1108
Elliott Hughes253c18d2015-03-18 22:47:09 -07001109 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001110 if (fd == -1) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001111 if (images[i].optional_if_no_image) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001112 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001113 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001114 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001115 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001116
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001117 if (images[i].optional_if_no_partition &&
1118 !if_partition_exists(images[i].part_name, slot)) {
1119 continue;
1120 }
1121
Elliott Hughes253c18d2015-03-18 22:47:09 -07001122 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001123 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001124 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1125 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001126
Elliott Hughes4089d342017-10-27 14:21:12 -07001127 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001128 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001129 flash_buf(partition.c_str(), &buf);
1130 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001131 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001132 * program exits.
1133 */
1134 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001135 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001136 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001137
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001138 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001139 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001140 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001141 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001142 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001143
1144 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001145}
1146
Alex Lightbb9b8a52016-06-29 09:26:44 -07001147static void do_send_signature(const std::string& fn) {
1148 std::size_t extension_loc = fn.find(".img");
1149 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001150
Alex Lightbb9b8a52016-06-29 09:26:44 -07001151 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001152
1153 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001154 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001155 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 fb_queue_download("signature", data, sz);
1158 fb_queue_command("signature", "installing signature");
1159}
1160
Aaron Wisnerdb511202018-06-26 15:38:35 -05001161static void do_flashall(const std::string& slot_override, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001162 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163 queue_info_dump();
1164
Wink Savilleb98762f2011-04-04 17:54:59 -07001165 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1166
Elliott Hughes45964a82017-05-03 22:43:23 -07001167 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001168 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169
Elliott Hughesfc797672015-04-07 20:12:50 -07001170 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001171 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001172 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001173
Aaron Wisnerdb511202018-06-26 15:38:35 -05001174 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001175
Alex Lightbb9b8a52016-06-29 09:26:44 -07001176 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001177 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001178 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001179 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001180 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001181 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001182 }
1183 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001184 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001185 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1186 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001187 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001188 }
1189 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001190
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001191 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001192 const char* slot = NULL;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001193 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001194 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001195 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001196 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001197 }
1198 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001199 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001200 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001201 if (!load_buf(fname.c_str(), &buf)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001202 if (images[i].optional_if_no_image) continue;
Elliott Hughes4089d342017-10-27 14:21:12 -07001203 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001204 }
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001205 if (images[i].optional_if_no_partition &&
1206 !if_partition_exists(images[i].part_name, slot)) {
1207 continue;
1208 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001209 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001210 do_send_signature(fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001211 flash_buf(partition.c_str(), &buf);
1212 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001213 do_for_partitions(images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001215
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001216 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001217 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001218 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001219 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221}
1222
Elliott Hughesd6365a72017-05-08 18:04:49 -07001223static std::string next_arg(std::vector<std::string>* args) {
1224 if (args->empty()) syntax_error("expected argument");
1225 std::string result = args->front();
1226 args->erase(args->begin());
1227 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001228}
1229
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001230static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001231 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001232
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001233 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001234 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001235 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001236 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001237 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001238}
1239
Connor O'Brience16a8a2017-02-06 14:39:31 -08001240static std::string fb_fix_numeric_var(std::string var) {
1241 // Some bootloaders (angler, for example), send spurious leading whitespace.
1242 var = android::base::Trim(var);
1243 // Some bootloaders (hammerhead, for example) use implicit hex.
1244 // This code used to use strtol with base 16.
1245 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1246 return var;
1247}
1248
Aaron Wisnerdb511202018-06-26 15:38:35 -05001249static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001250 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001251 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001252 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001253 return 0;
1254 }
1255 sizeString = fb_fix_numeric_var(sizeString);
1256
1257 unsigned size;
1258 if (!android::base::ParseUint(sizeString, &size)) {
1259 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1260 return 0;
1261 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001262 if ((size & (size - 1)) != 0) {
1263 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001264 return 0;
1265 }
1266 return size;
1267}
1268
Aaron Wisnerdb511202018-06-26 15:38:35 -05001269static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001270 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001271 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001272 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001273 std::string partition_type, partition_size;
1274
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001275 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001276 const char* errMsg = nullptr;
1277 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001278 TemporaryFile output;
1279 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001280
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001281 unsigned int limit = INT_MAX;
1282 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001283 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001284 }
1285 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001286 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001287 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288
Aaron Wisnerdb511202018-06-26 15:38:35 -05001289 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001290 errMsg = "Can't determine partition type.\n";
1291 goto failed;
1292 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001293 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001294 if (partition_type != type_override) {
1295 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001296 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001297 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001298 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001299 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001300
Aaron Wisnerdb511202018-06-26 15:38:35 -05001301 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001302 errMsg = "Unable to get partition size\n";
1303 goto failed;
1304 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001305 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001306 if (partition_size != size_override) {
1307 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001308 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001309 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001310 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001311 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001312 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001313
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001314 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001315 if (!gen) {
1316 if (skip_if_not_supported) {
1317 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001318 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001319 return;
1320 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001321 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1322 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001323 return;
1324 }
1325
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001326 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001327 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001328 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1329 return;
1330 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001331
Connor O'Brience16a8a2017-02-06 14:39:31 -08001332 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001333 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1334 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001335
Jin Qian4a335822017-04-18 16:23:18 -07001336 if (fs_generator_generate(gen, output.path, size, initial_dir,
1337 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001338 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001339 return;
1340 }
1341
Jin Qian4a335822017-04-18 16:23:18 -07001342 fd.reset(open(output.path, O_RDONLY));
1343 if (fd == -1) {
1344 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1345 return;
1346 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001347 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001348 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349 return;
1350 }
1351 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001352 return;
1353
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001354failed:
1355 if (skip_if_not_supported) {
1356 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001357 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001358 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001359 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001360}
1361
Aaron Wisnerdb511202018-06-26 15:38:35 -05001362int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001363 bool wants_wipe = false;
1364 bool wants_reboot = false;
1365 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001366 bool wants_reboot_recovery = false;
1367 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001368 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001369 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001370 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001371 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001372 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001373 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001374 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001375 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001376 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001377
Elliott Hughes577e8b42018-04-05 16:12:47 -07001378 g_boot_img_hdr.kernel_addr = 0x00008000;
1379 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1380 g_boot_img_hdr.second_addr = 0x00f00000;
1381 g_boot_img_hdr.tags_addr = 0x00000100;
1382 g_boot_img_hdr.page_size = 2048;
1383
JP Abgrall7b8970c2013-03-07 17:06:41 -08001384 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001385 {"base", required_argument, 0, 0},
1386 {"cmdline", required_argument, 0, 0},
1387 {"disable-verification", no_argument, 0, 0},
1388 {"disable-verity", no_argument, 0, 0},
1389 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001390 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001391 {"kernel-offset", required_argument, 0, 0},
1392 {"os-patch-level", required_argument, 0, 0},
1393 {"os-version", required_argument, 0, 0},
1394 {"page-size", required_argument, 0, 0},
1395 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001396 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001397 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001398 {"skip-secondary", no_argument, 0, 0},
1399 {"slot", required_argument, 0, 0},
1400 {"tags-offset", required_argument, 0, 0},
1401 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001402 {"verbose", no_argument, 0, 'v'},
1403 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001404#if !defined(_WIN32)
1405 {"wipe-and-use-fbe", no_argument, 0, 0},
1406#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001407 {0, 0, 0, 0}
1408 };
Colin Cross8879f982012-05-22 17:53:34 -07001409
1410 serial = getenv("ANDROID_SERIAL");
1411
Elliott Hughes577e8b42018-04-05 16:12:47 -07001412 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001413 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001414 if (c == 0) {
1415 std::string name{longopts[longindex].name};
1416 if (name == "base") {
1417 g_base_addr = strtoul(optarg, 0, 16);
1418 } else if (name == "cmdline") {
1419 g_cmdline = optarg;
1420 } else if (name == "disable-verification") {
1421 g_disable_verification = true;
1422 } else if (name == "disable-verity") {
1423 g_disable_verity = true;
1424 } else if (name == "header-version") {
1425 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1426 } else if (name == "kernel-offset") {
1427 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1428 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001429 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001430 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001431 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001432 } else if (name == "page-size") {
1433 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1434 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1435 } else if (name == "ramdisk-offset") {
1436 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1437 } else if (name == "skip-reboot") {
1438 skip_reboot = true;
1439 } else if (name == "skip-secondary") {
1440 skip_secondary = true;
1441 } else if (name == "slot") {
1442 slot_override = optarg;
1443 } else if (name == "tags-offset") {
1444 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1445 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001446 setvbuf(stdout, nullptr, _IONBF, 0);
1447 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001448 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001449 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001450 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001451 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001452#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001453 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001454 wants_wipe = true;
1455 set_fbe_marker = true;
1456#endif
1457 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001458 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001459 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001460 } else {
1461 switch (c) {
1462 case 'a':
1463 wants_set_active = true;
1464 if (optarg) next_active = optarg;
1465 break;
1466 case 'h':
1467 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001468 case 'l':
1469 g_long_listing = true;
1470 break;
1471 case 's':
1472 serial = optarg;
1473 break;
1474 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001475 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1476 die("invalid sparse limit %s", optarg);
1477 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001478 break;
1479 case 'v':
1480 set_verbose();
1481 break;
1482 case 'w':
1483 wants_wipe = true;
1484 break;
1485 case '?':
1486 return 1;
1487 default:
1488 abort();
1489 }
Colin Cross8879f982012-05-22 17:53:34 -07001490 }
1491 }
1492
1493 argc -= optind;
1494 argv += optind;
1495
Elliott Hughesd6365a72017-05-08 18:04:49 -07001496 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001497
Colin Cross8fb6e062012-07-24 16:36:41 -07001498 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001499 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001500 return 0;
1501 }
1502
Colin Crossc7b75dc2012-08-29 18:17:06 -07001503 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001504 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001505 }
1506
David Pursell0b156632015-10-30 11:22:01 -07001507 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001508 if (transport == nullptr) {
1509 return 1;
1510 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001511 fastboot::FastBootDriver fb(transport);
1512 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001513
Elliott Hughes5620d222018-03-28 08:20:00 -07001514 const double start = now();
1515
Aaron Wisnerdb511202018-06-26 15:38:35 -05001516 if (slot_override != "") slot_override = verify_slot(slot_override);
1517 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001518
1519 if (wants_set_active) {
1520 if (next_active == "") {
1521 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001522 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001523 if (fb_getvar("current-slot", &current_slot)) {
1524 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001525 } else {
1526 wants_set_active = false;
1527 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001528 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001529 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001530 }
1531 }
1532 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001533
Elliott Hughesd6365a72017-05-08 18:04:49 -07001534 std::vector<std::string> args(argv, argv + argc);
1535 while (!args.empty()) {
1536 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001537
Elliott Hughesd6365a72017-05-08 18:04:49 -07001538 if (command == "getvar") {
1539 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001540 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001541 } else if (command == "erase") {
1542 std::string partition = next_arg(&args);
1543 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001544 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001545 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001546 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001547 fs_get_generator(partition_type) != nullptr) {
1548 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1549 partition_type.c_str());
1550 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001551
Elliott Hughes5620d222018-03-28 08:20:00 -07001552 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001553 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001554 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001555 } else if (android::base::StartsWith(command, "format")) {
1556 // Parsing for: "format[:[type][:[size]]]"
1557 // Some valid things:
1558 // - select only the size, and leave default fs type:
1559 // format::0x4000000 userdata
1560 // - default fs type and size:
1561 // format userdata
1562 // format:: userdata
1563 std::vector<std::string> pieces = android::base::Split(command, ":");
1564 std::string type_override;
1565 if (pieces.size() > 1) type_override = pieces[1].c_str();
1566 std::string size_override;
1567 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001568
Elliott Hughesd6365a72017-05-08 18:04:49 -07001569 std::string partition = next_arg(&args);
1570
1571 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001572 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001573 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001574 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001575 } else if (command == "signature") {
1576 std::string filename = next_arg(&args);
1577 data = load_file(filename.c_str(), &sz);
1578 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001579 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001580 fb_queue_download("signature", data, sz);
1581 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001582 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001583 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001584
1585 if (args.size() == 1) {
1586 std::string what = next_arg(&args);
1587 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001588 wants_reboot = false;
1589 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001590 } else if (what == "recovery") {
1591 wants_reboot = false;
1592 wants_reboot_recovery = true;
1593 } else if (what == "fastboot") {
1594 wants_reboot = false;
1595 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001596 } else {
1597 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001598 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001599
Elliott Hughesca85df02015-02-25 10:02:00 -08001600 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001601 if (!args.empty()) syntax_error("junk after reboot command");
1602 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001603 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001604 } else if (command == "reboot-recovery") {
1605 wants_reboot_recovery = true;
1606 } else if (command == "reboot-fastboot") {
1607 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001608 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001609 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001610 } else if (command == "boot") {
1611 std::string kernel = next_arg(&args);
1612 std::string ramdisk;
1613 if (!args.empty()) ramdisk = next_arg(&args);
1614 std::string second_stage;
1615 if (!args.empty()) second_stage = next_arg(&args);
1616
Elliott Hughes577e8b42018-04-05 16:12:47 -07001617 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618 fb_queue_download("boot.img", data, sz);
1619 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001620 } else if (command == "flash") {
1621 std::string pname = next_arg(&args);
1622
Elliott Hughes2810d002016-04-25 14:31:18 -07001623 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001624 if (!args.empty()) {
1625 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001626 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001627 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001628 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001629 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001630
1631 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001632 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001633 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001634 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001635 } else if (command == "flash:raw") {
1636 std::string partition = next_arg(&args);
1637 std::string kernel = next_arg(&args);
1638 std::string ramdisk;
1639 if (!args.empty()) ramdisk = next_arg(&args);
1640 std::string second_stage;
1641 if (!args.empty()) second_stage = next_arg(&args);
1642
Elliott Hughes577e8b42018-04-05 16:12:47 -07001643 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001644 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001645 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001646 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001647 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001648 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001649 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001650 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
Aaron Wisnerdb511202018-06-26 15:38:35 -05001651 do_flashall(slot_override, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001652 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001653 do_flashall(slot_override, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001654 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001655 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001656 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001657 bool slot_all = (slot_override == "all");
1658 if (slot_all) {
1659 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1660 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001661 std::string filename = "update.zip";
1662 if (!args.empty()) {
1663 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001664 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001665 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001666 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001667 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001668 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001669 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001670 } else if (command == "stage") {
1671 std::string filename = next_arg(&args);
1672
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001673 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001674 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001675 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001676 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001677 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001678 } else if (command == "get_staged") {
1679 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001680 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001681 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001682 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001683 } else if (command == "flashing") {
1684 if (args.empty()) {
1685 syntax_error("missing 'flashing' command");
1686 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1687 args[0] == "unlock_critical" ||
1688 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001689 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001690 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001691 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001692 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001693 }
David Anderson0d4277d2018-07-31 13:27:37 -07001694 } else if (command == "create-logical-partition") {
1695 std::string partition = next_arg(&args);
1696 std::string size = next_arg(&args);
1697 fb_queue_create_partition(partition, size);
1698 } else if (command == "delete-logical-partition") {
1699 std::string partition = next_arg(&args);
1700 fb_queue_delete_partition(partition);
1701 } else if (command == "resize-logical-partition") {
1702 std::string partition = next_arg(&args);
1703 std::string size = next_arg(&args);
1704 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001705 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001706 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 }
1708 }
1709
1710 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001711 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1712 for (const auto& partition : partitions) {
1713 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001714 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001715 if (partition_type.empty()) continue;
1716 fb_queue_erase(partition);
1717 if (partition == "userdata" && set_fbe_marker) {
1718 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1719 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001720 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001721 delete_fbemarker_tmpdir(initial_userdata_dir);
1722 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001723 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001724 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001725 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001726 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001727 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001728 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001729 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001730 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001731 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001732 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001733 } else if (wants_reboot_bootloader) {
1734 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001735 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001736 } else if (wants_reboot_recovery) {
1737 fb_queue_command("reboot-recovery", "rebooting into recovery");
1738 fb_queue_wait_for_disconnect();
1739 } else if (wants_reboot_fastboot) {
1740 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1741 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001742 }
1743
Aaron Wisnerdb511202018-06-26 15:38:35 -05001744 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001745 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1746 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001747}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001748
Aaron Wisnerdb511202018-06-26 15:38:35 -05001749void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001750 unsigned year, month, day;
1751 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1752 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1753 }
1754 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1755 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1756 hdr->SetOsPatchLevel(year, month);
1757}
1758
Aaron Wisnerdb511202018-06-26 15:38:35 -05001759void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001760 unsigned major = 0, minor = 0, patch = 0;
1761 std::vector<std::string> versions = android::base::Split(arg, ".");
1762 if (versions.size() < 1 || versions.size() > 3 ||
1763 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1764 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1765 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1766 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1767 syntax_error("bad OS version: %s", arg);
1768 }
1769 hdr->SetOsVersion(major, minor, patch);
1770}