blob: 8e6c125c27d398eb01209fe1ec714adcc2840833 [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>
Dan Willemsenab971b52018-08-29 14:58:02 -070058#include <build/version.h>
59#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070060#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070061#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Elliott Hughes253c18d2015-03-18 22:47:09 -070063#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080064#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050065#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070066#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080067#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080069#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070070#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Chris Fries0ea946c2017-04-12 10:25:57 -050072using android::base::unique_fd;
73
Colin Crossf8387882012-05-24 17:18:41 -070074#ifndef O_BINARY
75#define O_BINARY 0
76#endif
77
Wink Savilleb98762f2011-04-04 17:54:59 -070078char cur_product[FB_RESPONSE_SZ + 1];
79
David Pursell2ec418a2016-01-20 08:32:08 -080080static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070081
Elliott Hughes577e8b42018-04-05 16:12:47 -070082static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050083// Don't resparse files in too-big chunks.
84// libsparse will support INT_MAX, but this results in large allocations, so
85// let's keep it at 1GB to avoid memory pressure on the host.
86static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070087static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070088static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Elliott Hughes577e8b42018-04-05 16:12:47 -070090static unsigned g_base_addr = 0x10000000;
91static boot_img_hdr_v1 g_boot_img_hdr = {};
92static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080093
David Zeuthenb6ea4352017-08-07 14:29:26 -040094static bool g_disable_verity = false;
95static bool g_disable_verification = false;
96
Paul Crowley8f7f56e2015-11-27 09:29:37 +000097static const std::string convert_fbe_marker_filename("convert_fbe");
98
Rom Lemarchand622810c2013-06-28 09:54:59 -070099enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500100 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101 FB_BUFFER_SPARSE,
102};
103
104struct fastboot_buffer {
105 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700106 void* data;
107 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500108 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700109 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
111
David Anderson0debda02018-08-28 13:54:03 -0700112enum class ImageType {
113 // Must be flashed for device to boot into the kernel.
114 BootCritical,
115 // Normal partition to be flashed during "flashall".
116 Normal,
117 // Partition that is never flashed during "flashall".
118 Extra
119};
120
David Anderson32e376f2018-08-16 13:43:11 -0700121struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700122 const char* nickname;
123 const char* img_name;
124 const char* sig_name;
125 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700126 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700127 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700128 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700129};
130
131static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100132 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700133 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
134 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
135 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
136 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
137 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
138 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
139 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100140 { "product_services",
141 "product_services.img",
142 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700143 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700144 true, ImageType::Normal },
145 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
146 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
147 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
148 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
149 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
150 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson1109c922018-09-17 17:32:54 -0700151 { "vbmeta_mainline",
152 "vbmeta_mainline.img",
153 "vbmeta_mainline.sig",
154 "vbmeta_mainline",
155 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700156 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
157 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100158 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700159};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700160
David Anderson0debda02018-08-28 13:54:03 -0700161static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700162 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700163 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700164 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700165 }
David Anderson0debda02018-08-28 13:54:03 -0700166 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700167}
168
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700169static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700170 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700171 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700172 return find_item_given_name(images[i].img_name);
173 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 }
175
Elliott Hughesd6365a72017-05-08 18:04:49 -0700176 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700177 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178}
179
Elliott Hughesfc797672015-04-07 20:12:50 -0700180static int64_t get_file_size(int fd) {
181 struct stat sb;
182 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700183}
184
Elliott Hughesfc797672015-04-07 20:12:50 -0700185static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800186 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700187 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 *sz = get_file_size(fd);
190 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700191 goto oops;
192 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Elliott Hughesfc797672015-04-07 20:12:50 -0700194 data = (char*) malloc(*sz);
195 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Elliott Hughesfc797672015-04-07 20:12:50 -0700197 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198 close(fd);
199
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 return data;
201
202oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800203 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 close(fd);
205 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800206 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 return 0;
208}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209
Elliott Hughes2810d002016-04-25 14:31:18 -0700210static void* load_file(const std::string& path, int64_t* sz) {
211 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700212 if (fd == -1) return nullptr;
213 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700214}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
Elliott Hughesfc797672015-04-07 20:12:50 -0700216static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700217 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
218 return -1;
219 }
220
Scott Anderson13081c62012-04-06 12:39:30 -0700221 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700223 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
224 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 return 0;
226}
227
Elliott Hughesfc797672015-04-07 20:12:50 -0700228static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800229 return match_fastboot_with_serial(info, serial);
230}
231
Elliott Hughesfc797672015-04-07 20:12:50 -0700232static int list_devices_callback(usb_ifc_info* info) {
233 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800234 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700235 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800236 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700237 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 if (!serial[0]) {
239 serial = "????????????";
240 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700241 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700242 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800243 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700244 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800245 printf("%-22s fastboot", serial.c_str());
246 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700247 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800248 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249 }
250
251 return -1;
252}
253
David Pursell2ec418a2016-01-20 08:32:08 -0800254// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
255// a specific device, otherwise the first USB device found will be used.
256//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700257// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800258// Otherwise it blocks until the target is available.
259//
260// The returned Transport is a singleton, so multiple calls to this function will return the same
261// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700262static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800263 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800264
David Pursell4601c972016-02-05 15:35:09 -0800265 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800266 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800267 int port = 0;
268 if (serial != nullptr) {
269 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800270
David Pursell4601c972016-02-05 15:35:09 -0800271 if (android::base::StartsWith(serial, "tcp:")) {
272 protocol = Socket::Protocol::kTcp;
273 port = tcp::kDefaultPort;
274 net_address = serial + strlen("tcp:");
275 } else if (android::base::StartsWith(serial, "udp:")) {
276 protocol = Socket::Protocol::kUdp;
277 port = udp::kDefaultPort;
278 net_address = serial + strlen("udp:");
279 }
280
281 if (net_address != nullptr) {
282 std::string error;
283 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700284 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800285 }
David Pursell2ec418a2016-01-20 08:32:08 -0800286 }
287 }
288
David Anderson1d887432018-08-27 16:47:32 -0700289 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800290 while (true) {
291 if (!host.empty()) {
292 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800293 if (protocol == Socket::Protocol::kTcp) {
294 transport = tcp::Connect(host, port, &error).release();
295 } else if (protocol == Socket::Protocol::kUdp) {
296 transport = udp::Connect(host, port, &error).release();
297 }
298
David Pursell2ec418a2016-01-20 08:32:08 -0800299 if (transport == nullptr && announce) {
300 fprintf(stderr, "error: %s\n", error.c_str());
301 }
302 } else {
303 transport = usb_open(match_fastboot);
304 }
305
306 if (transport != nullptr) {
307 return transport;
308 }
309
David Pursell0b156632015-10-30 11:22:01 -0700310 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800311 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700312 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800314 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 }
316}
317
Elliott Hughesfc797672015-04-07 20:12:50 -0700318static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 // We don't actually open a USB device here,
320 // just getting our callback called so we can
321 // list all the connected devices.
322 usb_open(list_devices_callback);
323}
324
Elliott Hughesd6365a72017-05-08 18:04:49 -0700325static void syntax_error(const char* fmt, ...) {
326 fprintf(stderr, "fastboot: usage: ");
327
328 va_list ap;
329 va_start(ap, fmt);
330 vfprintf(stderr, fmt, ap);
331 va_end(ap);
332
333 fprintf(stderr, "\n");
334 exit(1);
335}
336
337static int show_help() {
338 // clang-format off
339 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700340// 1 2 3 4 5 6 7 8
341// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700342 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800343 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700344 "flashing:\n"
345 " update ZIP Flash all partitions from an update.zip package.\n"
346 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
347 " On A/B devices, flashed slot is set as active.\n"
348 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700349 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
350 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700351 "\n"
352 "basics:\n"
353 " devices [-l] List devices in bootloader (-l: with device paths).\n"
354 " getvar NAME Display given bootloader variable.\n"
355 " reboot [bootloader] Reboot device.\n"
356 "\n"
357 "locking/unlocking:\n"
358 " flashing lock|unlock Lock/unlock partitions for flashing\n"
359 " flashing lock_critical|unlock_critical\n"
360 " Lock/unlock 'critical' bootloader partitions.\n"
361 " flashing get_unlock_ability\n"
362 " Check whether unlocking is allowed (1) or not(0).\n"
363 "\n"
364 "advanced:\n"
365 " erase PARTITION Erase a flash partition.\n"
366 " format[:FS_TYPE[:SIZE]] PARTITION\n"
367 " Format a flash partition.\n"
368 " set_active SLOT Set the active slot.\n"
369 " oem [COMMAND...] Execute OEM-specific command.\n"
370 "\n"
371 "boot image:\n"
372 " boot KERNEL [RAMDISK [SECOND]]\n"
373 " Download and boot kernel from RAM.\n"
374 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
375 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700376 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700377 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
378 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
379 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
380 " --tags-offset Set tags offset (default: 0x00000100).\n"
381 " --page-size BYTES Set flash page size (default: 2048).\n"
382 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700383 " --os-version MAJOR[.MINOR[.PATCH]]\n"
384 " Set boot image OS version (default: 0.0.0).\n"
385 " --os-patch-level YYYY-MM-DD\n"
386 " Set boot image OS security patch level.\n"
387 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700388 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700389 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700390 //" continue Continue with autoboot.\n"
391 //"\n"
392 "Android Things:\n"
393 " stage IN_FILE Sends given file to stage for the next command.\n"
394 " 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 -0800395 "\n"
396 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700398 " -s SERIAL Specify a USB device.\n"
399 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700400 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700401 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
402 " non-current slot (default: current active slot).\n"
403 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
404 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
405 " --skip-reboot Don't reboot device after flashing.\n"
406 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
407 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000408#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700409 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000410#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700411 // TODO: remove --unbuffered?
412 " --unbuffered Don't buffer input or output.\n"
413 " --verbose, -v Verbose output.\n"
414 " --version Display version.\n"
415 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 // clang-format off
418 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000420
Elliott Hughesd6365a72017-05-08 18:04:49 -0700421static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700422 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700423 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700424 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700425 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800426
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700428 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800429 die("cannot load '%s': too short", kernel.c_str());
430 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700431 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700432 if (!g_cmdline.empty()) {
433 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
434 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800435
Elliott Hughes4089d342017-10-27 14:21:12 -0700436 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800437
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 *sz = ksize;
439 return kdata;
440 }
441
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 void* rdata = nullptr;
443 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700444 if (!ramdisk.empty()) {
445 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700446 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 }
448
Elliott Hughesfc797672015-04-07 20:12:50 -0700449 void* sdata = nullptr;
450 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700451 if (!second_stage.empty()) {
452 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700453 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200454 }
455
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700457 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
458 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700459 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700460
Elliott Hughes577e8b42018-04-05 16:12:47 -0700461 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
462 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800463
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 return bdata;
465}
466
Elliott Hughes23af1122017-11-10 08:42:17 -0800467static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700468 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700469 ZipEntry zip_entry;
470 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700471 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700472 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 }
474
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700475 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476
Elliott Hughes23af1122017-11-10 08:42:17 -0800477 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700478 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700479 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700481 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700482 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000483
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 return data;
485}
486
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700487#if defined(_WIN32)
488
489// TODO: move this to somewhere it can be shared.
490
491#include <windows.h>
492
493// Windows' tmpfile(3) requires administrator rights because
494// it creates temporary files in the root directory.
495static FILE* win32_tmpfile() {
496 char temp_path[PATH_MAX];
497 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
498 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700499 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700500 }
501
502 char filename[PATH_MAX];
503 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700504 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700505 }
506
507 return fopen(filename, "w+bTD");
508}
509
510#define tmpfile win32_tmpfile
511
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000512static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700513 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000514}
515
Elliott Hughes855cdf82018-04-02 14:24:03 -0700516static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700517 // TODO: reimplement to avoid leaking a FILE*.
518 return fileno(tmpfile());
519}
520
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000521#else
522
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700523static std::string make_temporary_template() {
524 const char* tmpdir = getenv("TMPDIR");
525 if (tmpdir == nullptr) tmpdir = P_tmpdir;
526 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
527}
528
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000529static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700530 std::string result(make_temporary_template());
531 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700532 die("unable to create temporary directory with template %s: %s",
533 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000534 }
535 return result;
536}
537
Elliott Hughes855cdf82018-04-02 14:24:03 -0700538static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700539 std::string path_template(make_temporary_template());
540 int fd = mkstemp(&path_template[0]);
541 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700542 die("failed to create temporary file for %s with template %s: %s\n",
543 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700544 }
545 unlink(path_template.c_str());
546 return fd;
547}
548
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700549#endif
550
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551static std::string create_fbemarker_tmpdir() {
552 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000553 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
554 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
555 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700556 die("unable to create FBE marker file %s locally: %s",
557 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000558 }
559 close(fd);
560 return dir;
561}
562
563static void delete_fbemarker_tmpdir(const std::string& dir) {
564 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
565 if (unlink(marker_file.c_str()) == -1) {
566 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
567 marker_file.c_str(), errno, strerror(errno));
568 return;
569 }
570 if (rmdir(dir.c_str()) == -1) {
571 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
572 dir.c_str(), errno, strerror(errno));
573 return;
574 }
575}
576
Elliott Hughes45964a82017-05-03 22:43:23 -0700577static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700578 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700579
Yusuke Sato07447542015-06-25 14:39:19 -0700580 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700581 ZipEntry zip_entry;
582 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
583 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700584 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000585 return -1;
586 }
587
Elliott Hughes23af1122017-11-10 08:42:17 -0800588 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700589 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800590 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700591 int error = ExtractEntryToFile(zip, &zip_entry, fd);
592 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800593 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700594 }
595
Elliott Hughes4089d342017-10-27 14:21:12 -0700596 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800597 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700598 }
599
Elliott Hughes23af1122017-11-10 08:42:17 -0800600 fprintf(stderr, " took %.3fs\n", now() - start);
601
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700602 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700603}
604
Elliott Hughes5620d222018-03-28 08:20:00 -0700605static char* strip(char* s) {
606 while (*s && isspace(*s)) s++;
607
608 int n = strlen(s);
609 while (n-- > 0) {
610 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800611 s[n] = 0;
612 }
613 return s;
614}
615
616#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500617static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800618 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700619 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 char *x;
621 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800622
Elliott Hughes5620d222018-03-28 08:20:00 -0700623 // "require product=alpha|beta|gamma"
624 // "require version-bootloader=1234"
625 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
626 // "require partition-exists=vendor"
627
628 char* name = line;
629 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 if (!strncmp(name, "reject ", 7)) {
631 name += 7;
632 invert = 1;
633 } else if (!strncmp(name, "require ", 8)) {
634 name += 8;
635 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700636 } else if (!strncmp(name, "require-for-product:", 20)) {
637 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700638 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700639 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700640 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700641 *name = 0;
642 name += 1;
643 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800644 }
645
646 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700647 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 *x = 0;
649 val[0] = x + 1;
650
Elliott Hughes5620d222018-03-28 08:20:00 -0700651 name = strip(name);
652
653 // "require partition-exists=x" is a special case, added because of the trouble we had when
654 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
655 // missing out new partitions. A device with new partitions can use "partition-exists" to
David Anderson0debda02018-08-28 13:54:03 -0700656 // override the fields `optional_if_no_image` in the `images` array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700657 if (!strcmp(name, "partition-exists")) {
658 const char* partition_name = val[0];
659 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500660 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700661 (has_slot != "yes" && has_slot != "no")) {
662 die("device doesn't have required partition %s!", partition_name);
663 }
664 bool known_partition = false;
665 for (size_t i = 0; i < arraysize(images); ++i) {
666 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700667 images[i].optional_if_no_image = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700668 known_partition = true;
669 }
670 }
671 if (!known_partition) {
672 die("device requires partition %s which is not known to this version of fastboot",
673 partition_name);
674 }
675 return;
676 }
677
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 for(count = 1; count < MAX_OPTIONS; count++) {
679 x = strchr(val[count - 1],'|');
680 if (x == 0) break;
681 *x = 0;
682 val[count] = x + 1;
683 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800684
Elliott Hughes253c18d2015-03-18 22:47:09 -0700685 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700686 const char* var = name;
687 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688
Elliott Hughes253c18d2015-03-18 22:47:09 -0700689 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700690 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691
Elliott Hughes5620d222018-03-28 08:20:00 -0700692 for (size_t i = 0; i < count; ++i) {
693 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 }
695
Tom Cherry11f12092018-08-29 21:36:28 -0700696 fb_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697}
698
Aaron Wisnerdb511202018-06-26 15:38:35 -0500699static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700700 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700702 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500704 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 data = s;
706 } else {
707 s++;
708 }
709 }
710}
711
Tom Cherry11f12092018-08-29 21:36:28 -0700712static void dump_info() {
713 fb_notice("--------------------------------------------");
714 fb_display("Bootloader Version...", "version-bootloader");
715 fb_display("Baseband Version.....", "version-baseband");
716 fb_display("Serial Number........", "serialno");
717 fb_notice("--------------------------------------------");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800718}
719
Tao Bao41cf35f2018-04-24 10:54:21 -0700720static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700721 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700722 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700723
Tao Bao41cf35f2018-04-24 10:54:21 -0700724 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
725 die("invalid max size %" PRId64, max_size);
726 }
727
Elliott Hughesfc797672015-04-07 20:12:50 -0700728 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700729 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700730
Elliott Hughes253c18d2015-03-18 22:47:09 -0700731 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700732 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700733
734 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700735 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700736
737 return out_s;
738}
739
Aaron Wisnerdb511202018-06-26 15:38:35 -0500740static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700741 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500742 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700743 max_download_size.empty()) {
744 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700745 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700746 }
747
Elliott Hughes77c0e662015-11-02 15:51:12 -0800748 // Some bootloaders (angler, for example) send spurious whitespace too.
749 max_download_size = android::base::Trim(max_download_size);
750
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700751 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700752 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800753 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700754 return 0;
755 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700756 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700757 return limit;
758}
759
Aaron Wisnerdb511202018-06-26 15:38:35 -0500760static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700761 int64_t limit = sparse_limit;
762 if (limit == 0) {
763 // Unlimited, so see what the target device's limit is.
764 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700765 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500766 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700767 }
768 if (target_sparse_limit > 0) {
769 limit = target_sparse_limit;
770 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700771 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700772 }
773 }
774
775 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500776 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700777 }
778
779 return 0;
780}
781
Aaron Wisnerdb511202018-06-26 15:38:35 -0500782static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700783 int64_t sz = get_file_size(fd);
784 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700785 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700786 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700787
David Anderson32e376f2018-08-16 13:43:11 -0700788 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
789 buf->image_size = sparse_file_len(s, false, false);
790 sparse_file_destroy(s);
791 } else {
792 buf->image_size = sz;
793 }
794
Elliott Hughesfc797672015-04-07 20:12:50 -0700795 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500796 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700797 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700798 sparse_file** s = load_sparse_files(fd, limit);
799 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700800 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700801 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700802 buf->type = FB_BUFFER_SPARSE;
803 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700804 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500805 buf->type = FB_BUFFER_FD;
806 buf->data = nullptr;
807 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000808 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700809 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700810
Elliott Hughes53ec4952016-05-11 12:39:27 -0700811 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812}
813
Aaron Wisnerdb511202018-06-26 15:38:35 -0500814static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500815 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
816
Elliott Hughes53ec4952016-05-11 12:39:27 -0700817 if (fd == -1) {
818 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500820
821 struct stat s;
822 if (fstat(fd, &s)) {
823 return false;
824 }
825 if (!S_ISREG(s.st_mode)) {
826 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
827 return false;
828 }
829
Aaron Wisnerdb511202018-06-26 15:38:35 -0500830 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700831}
832
David Zeuthenb6ea4352017-08-07 14:29:26 -0400833static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
834 // Buffer needs to be at least the size of the VBMeta struct which
835 // is 256 bytes.
836 if (buf->sz < 256) {
837 return;
838 }
839
Elliott Hughes855cdf82018-04-02 14:24:03 -0700840 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400841
842 std::string data;
843 if (!android::base::ReadFdToString(buf->fd, &data)) {
844 die("Failed reading from vbmeta");
845 }
846
847 // There's a 32-bit big endian |flags| field at offset 120 where
848 // bit 0 corresponds to disable-verity and bit 1 corresponds to
849 // disable-verification.
850 //
851 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
852 // the VBMeta struct.
853 if (g_disable_verity) {
854 data[123] |= 0x01;
855 }
856 if (g_disable_verification) {
857 data[123] |= 0x02;
858 }
859
860 if (!android::base::WriteStringToFd(data, fd)) {
861 die("Failed writing to modified vbmeta");
862 }
863 close(buf->fd);
864 buf->fd = fd;
865 lseek(fd, 0, SEEK_SET);
866}
867
Elliott Hughes5620d222018-03-28 08:20:00 -0700868static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700869{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700870 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700871
David Zeuthenb6ea4352017-08-07 14:29:26 -0400872 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
873 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700874 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400875 rewrite_vbmeta_buffer(buf);
876 }
877
Rom Lemarchand622810c2013-06-28 09:54:59 -0700878 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800879 case FB_BUFFER_SPARSE: {
880 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700881 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700883 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800884 sparse_files.emplace_back(*s, sz);
885 ++s;
886 }
887
888 for (size_t i = 0; i < sparse_files.size(); ++i) {
889 const auto& pair = sparse_files[i];
Tom Cherry11f12092018-08-29 21:36:28 -0700890 fb_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700891 }
892 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800893 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500894 case FB_BUFFER_FD:
Tom Cherry11f12092018-08-29 21:36:28 -0700895 fb_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700896 break;
897 default:
898 die("unknown buffer type: %d", buf->type);
899 }
900}
901
Aaron Wisnerdb511202018-06-26 15:38:35 -0500902static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500904 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700905 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700906}
907
Aaron Wisnerdb511202018-06-26 15:38:35 -0500908static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700909 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700910 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500911 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700912 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700913 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700914 return count;
915}
916
Aaron Wisnerdb511202018-06-26 15:38:35 -0500917static bool supports_AB() {
918 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700919}
920
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700921// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700922static std::string get_other_slot(const std::string& current_slot, int count) {
923 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700924
Daniel Rosenberg80919472016-06-30 19:25:31 -0700925 char next = (current_slot[0] - 'a' + 1)%count + 'a';
926 return std::string(1, next);
927}
928
Aaron Wisnerdb511202018-06-26 15:38:35 -0500929static std::string get_other_slot(const std::string& current_slot) {
930 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700931}
932
Aaron Wisnerdb511202018-06-26 15:38:35 -0500933static std::string get_other_slot(int count) {
934 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700935}
936
Aaron Wisnerdb511202018-06-26 15:38:35 -0500937static std::string get_other_slot() {
938 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700939}
940
Aaron Wisnerdb511202018-06-26 15:38:35 -0500941static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700942 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700943 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800944 if (allow_all) {
945 return "all";
946 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500947 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700948 if (count > 0) {
949 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800950 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700951 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800952 }
953 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700954 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800955
Aaron Wisnerdb511202018-06-26 15:38:35 -0500956 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700957 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800958
Daniel Rosenberg80919472016-06-30 19:25:31 -0700959 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500960 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700961 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700962 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800963 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700964 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800965 }
966
Daniel Rosenberg80919472016-06-30 19:25:31 -0700967 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
968
969 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
970 for (int i=0; i<count; i++) {
971 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700972 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700974 exit(1);
975}
976
Aaron Wisnerdb511202018-06-26 15:38:35 -0500977static std::string verify_slot(const std::string& slot) {
978 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800979}
980
Aaron Wisnerdb511202018-06-26 15:38:35 -0500981static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700982 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800983 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700984 std::string current_slot;
985
Aaron Wisnerdb511202018-06-26 15:38:35 -0500986 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800987 /* If has-slot is not supported, the answer is no. */
988 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800990 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500992 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700994 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700998 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 }
1000 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001002 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001003 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001004 }
1005 func(part);
1006 }
1007}
1008
David Pursell0b156632015-10-30 11:22:01 -07001009/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1010 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1011 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1012 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001014static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001015 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001016 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001019 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001020 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001022 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001023 for (int i=0; i < get_slot_count(); i++) {
1024 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 }
1026 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001027 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001028 }
1029 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001030 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001031 }
1032}
1033
Aaron Wisnerdb511202018-06-26 15:38:35 -05001034static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001035 struct fastboot_buffer buf;
1036
Aaron Wisnerdb511202018-06-26 15:38:35 -05001037 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001038 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001039 }
1040 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001041}
1042
Daniel Rosenberg13454092016-06-27 19:43:11 -07001043// Sets slot_override as the active slot. If slot_override is blank,
1044// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001045static void set_active(const std::string& slot_override) {
1046 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001047
Daniel Rosenberg80919472016-06-30 19:25:31 -07001048 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001049 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001050 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001051 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001052 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001053 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001054 }
1055 }
1056}
1057
David Anderson32e376f2018-08-16 13:43:11 -07001058static bool is_userspace_fastboot() {
1059 std::string value;
1060 return fb_getvar("is-userspace", &value) && value == "yes";
1061}
1062
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001063static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1064 std::string has_slot;
1065 std::string partition_name = partition;
1066
1067 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1068 if (slot == "") {
1069 std::string current_slot = get_current_slot();
1070 if (current_slot == "") {
1071 die("Failed to identify current slot");
1072 }
1073 partition_name += "_" + current_slot;
1074 } else {
1075 partition_name += "_" + slot;
1076 }
1077 }
1078 std::string partition_size;
1079 return fb_getvar("partition-size:" + partition_name, &partition_size);
1080}
1081
David Anderson32e376f2018-08-16 13:43:11 -07001082static bool is_logical(const std::string& partition) {
1083 std::string value;
1084 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1085}
1086
David Anderson1d887432018-08-27 16:47:32 -07001087static void reboot_to_userspace_fastboot() {
1088 if (!fb_reboot_to_userspace()) {
1089 die("Must reboot to userspace fastboot to flash logical partitions");
1090 }
1091
1092 // Give the current connection time to close.
1093 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1094
1095 fb_reinit(open_device());
1096}
1097
David Andersoncf444f32018-08-29 14:15:49 -07001098class ImageSource {
1099 public:
1100 virtual void* ReadFile(const std::string& name, int64_t* size) const = 0;
1101 virtual int OpenFile(const std::string& name) const = 0;
1102};
David Anderson32e376f2018-08-16 13:43:11 -07001103
David Andersoncf444f32018-08-29 14:15:49 -07001104class FlashAllTool {
1105 public:
1106 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001107
David Andersoncf444f32018-08-29 14:15:49 -07001108 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001109
David Andersoncf444f32018-08-29 14:15:49 -07001110 private:
1111 void CheckRequirements();
1112 void DetermineSecondarySlot();
1113 void CollectImages();
1114 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1115 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1116 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001117
David Andersoncf444f32018-08-29 14:15:49 -07001118 const ImageSource& source_;
1119 std::string slot_override_;
1120 bool skip_secondary_;
1121 bool wipe_;
1122 std::string secondary_slot_;
1123 std::vector<std::pair<const Image*, std::string>> boot_images_;
1124 std::vector<std::pair<const Image*, std::string>> os_images_;
1125};
1126
1127FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1128 : source_(source),
1129 slot_override_(slot_override),
1130 skip_secondary_(skip_secondary),
1131 wipe_(wipe)
1132{
David Anderson32e376f2018-08-16 13:43:11 -07001133}
1134
David Andersoncf444f32018-08-29 14:15:49 -07001135void FlashAllTool::Flash() {
1136 CheckRequirements();
1137 DetermineSecondarySlot();
1138 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001139
David Anderson95d40932018-08-28 12:14:33 -07001140 // First flash boot partitions. We allow this to happen either in userspace
1141 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001142 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001143
1144 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001145 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001146
1147 // Resize any logical partition to 0, so each partition is reset to 0
1148 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001149 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001150 auto resize_partition = [](const std::string& partition) -> void {
1151 if (is_logical(partition)) {
Tom Cherry11f12092018-08-29 21:36:28 -07001152 fb_resize_partition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001153 }
1154 };
David Anderson95d40932018-08-28 12:14:33 -07001155 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001156 }
1157
David Anderson95d40932018-08-28 12:14:33 -07001158 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001159 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001160
David Andersoncf444f32018-08-29 14:15:49 -07001161 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001162 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001163 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001164 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001165 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001166}
1167
David Andersoncf444f32018-08-29 14:15:49 -07001168void FlashAllTool::CheckRequirements() {
1169 int64_t sz;
1170 void* data = source_.ReadFile("android-info.txt", &sz);
1171 if (data == nullptr) {
1172 die("could not read android-info.txt");
1173 }
1174 check_requirements(reinterpret_cast<char*>(data), sz);
1175}
1176
1177void FlashAllTool::DetermineSecondarySlot() {
1178 if (skip_secondary_) {
1179 return;
1180 }
1181 if (slot_override_ != "") {
1182 secondary_slot_ = get_other_slot(slot_override_);
1183 } else {
1184 secondary_slot_ = get_other_slot();
1185 }
1186 if (secondary_slot_ == "") {
1187 if (supports_AB()) {
1188 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1189 }
1190 skip_secondary_ = true;
1191 }
1192}
1193
1194void FlashAllTool::CollectImages() {
1195 for (size_t i = 0; i < arraysize(images); ++i) {
1196 std::string slot = slot_override_;
1197 if (images[i].IsSecondary()) {
1198 if (skip_secondary_) {
1199 continue;
1200 }
1201 slot = secondary_slot_;
1202 }
1203 if (images[i].type == ImageType::BootCritical) {
1204 boot_images_.emplace_back(&images[i], slot);
1205 } else if (images[i].type == ImageType::Normal) {
1206 os_images_.emplace_back(&images[i], slot);
1207 }
1208 }
1209}
1210
1211void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1212 for (const auto& [image, slot] : images) {
1213 fastboot_buffer buf;
1214 int fd = source_.OpenFile(image->img_name);
1215 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1216 if (image->optional_if_no_image) {
1217 continue;
1218 }
1219 die("could not load '%s': %s", image->img_name, strerror(errno));
1220 }
1221 FlashImage(*image, slot, &buf);
1222 }
1223}
1224
1225void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1226 auto flash = [&, this](const std::string& partition_name) {
1227 int64_t sz;
1228 void* data = source_.ReadFile(image.sig_name, &sz);
1229 if (data) {
Tom Cherry11f12092018-08-29 21:36:28 -07001230 fb_download("signature", data, sz);
1231 fb_command("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001232 }
1233
1234 if (is_logical(partition_name)) {
Tom Cherry11f12092018-08-29 21:36:28 -07001235 fb_resize_partition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001236 }
1237 flash_buf(partition_name.c_str(), buf);
1238 };
1239 do_for_partitions(image.part_name, slot, flash, false);
1240}
1241
1242void FlashAllTool::UpdateSuperPartition() {
1243 if (!if_partition_exists("super", "")) {
1244 return;
1245 }
1246
1247 int fd = source_.OpenFile("super_empty.img");
1248 if (fd < 0) {
1249 return;
1250 }
1251 if (!is_userspace_fastboot()) {
1252 reboot_to_userspace_fastboot();
1253 }
Tom Cherry11f12092018-08-29 21:36:28 -07001254 fb_download_fd("super", fd, get_file_size(fd));
David Andersoncf444f32018-08-29 14:15:49 -07001255
1256 std::string command = "update-super:super";
1257 if (wipe_) {
1258 command += ":wipe";
1259 }
Tom Cherry11f12092018-08-29 21:36:28 -07001260 fb_command(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001261}
1262
1263class ZipImageSource final : public ImageSource {
1264 public:
1265 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
1266 void* ReadFile(const std::string& name, int64_t* size) const override;
1267 int OpenFile(const std::string& name) const override;
1268
1269 private:
1270 ZipArchiveHandle zip_;
1271};
1272
1273void* ZipImageSource::ReadFile(const std::string& name, int64_t* size) const {
1274 return unzip_to_memory(zip_, name.c_str(), size);
1275}
1276
1277int ZipImageSource::OpenFile(const std::string& name) const {
1278 return unzip_to_file(zip_, name.c_str());
1279}
1280
1281static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
Tom Cherry11f12092018-08-29 21:36:28 -07001282 dump_info();
David Andersoncf444f32018-08-29 14:15:49 -07001283
Tom Cherry11f12092018-08-29 21:36:28 -07001284 fb_query_save("product", cur_product, sizeof(cur_product));
David Andersoncf444f32018-08-29 14:15:49 -07001285
1286 ZipArchiveHandle zip;
1287 int error = OpenArchive(filename, &zip);
1288 if (error != 0) {
1289 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1290 }
1291
1292 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1293 tool.Flash();
1294
1295 CloseArchive(zip);
1296}
1297
1298class LocalImageSource final : public ImageSource {
1299 public:
1300 void* ReadFile(const std::string& name, int64_t* size) const override;
1301 int OpenFile(const std::string& name) const override;
1302};
1303
1304void* LocalImageSource::ReadFile(const std::string& name, int64_t* size) const {
1305 auto path = find_item_given_name(name);
1306 if (path.empty()) {
1307 return nullptr;
1308 }
1309 return load_file(path.c_str(), size);
1310}
1311
1312int LocalImageSource::OpenFile(const std::string& name) const {
1313 auto path = find_item_given_name(name);
1314 return open(path.c_str(), O_RDONLY);
1315}
1316
1317static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
1318 std::string fname;
Tom Cherry11f12092018-08-29 21:36:28 -07001319 dump_info();
David Andersoncf444f32018-08-29 14:15:49 -07001320
Tom Cherry11f12092018-08-29 21:36:28 -07001321 fb_query_save("product", cur_product, sizeof(cur_product));
David Andersoncf444f32018-08-29 14:15:49 -07001322
1323 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1324 tool.Flash();
1325}
1326
Elliott Hughesd6365a72017-05-08 18:04:49 -07001327static std::string next_arg(std::vector<std::string>* args) {
1328 if (args->empty()) syntax_error("expected argument");
1329 std::string result = args->front();
1330 args->erase(args->begin());
1331 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001332}
1333
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001334static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001335 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001336
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001337 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001338 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001339 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001340 }
Tom Cherry11f12092018-08-29 21:36:28 -07001341 fb_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001342}
1343
Connor O'Brience16a8a2017-02-06 14:39:31 -08001344static std::string fb_fix_numeric_var(std::string var) {
1345 // Some bootloaders (angler, for example), send spurious leading whitespace.
1346 var = android::base::Trim(var);
1347 // Some bootloaders (hammerhead, for example) use implicit hex.
1348 // This code used to use strtol with base 16.
1349 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1350 return var;
1351}
1352
Aaron Wisnerdb511202018-06-26 15:38:35 -05001353static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001354 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001355 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001356 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001357 return 0;
1358 }
1359 sizeString = fb_fix_numeric_var(sizeString);
1360
1361 unsigned size;
1362 if (!android::base::ParseUint(sizeString, &size)) {
1363 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1364 return 0;
1365 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001366 if ((size & (size - 1)) != 0) {
1367 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001368 return 0;
1369 }
1370 return size;
1371}
1372
Aaron Wisnerdb511202018-06-26 15:38:35 -05001373static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001374 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001375 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001376 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001377 std::string partition_type, partition_size;
1378
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001379 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 const char* errMsg = nullptr;
1381 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001382 TemporaryFile output;
1383 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001384
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001385 unsigned int limit = INT_MAX;
1386 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001387 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001388 }
1389 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001391 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392
Aaron Wisnerdb511202018-06-26 15:38:35 -05001393 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001394 errMsg = "Can't determine partition type.\n";
1395 goto failed;
1396 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001397 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 if (partition_type != type_override) {
1399 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001400 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001401 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001402 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001403 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001404
Aaron Wisnerdb511202018-06-26 15:38:35 -05001405 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001406 errMsg = "Unable to get partition size\n";
1407 goto failed;
1408 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001409 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001410 if (partition_size != size_override) {
1411 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001412 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001413 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001415 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001416 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001417
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001418 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419 if (!gen) {
1420 if (skip_if_not_supported) {
1421 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001422 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001423 return;
1424 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001425 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1426 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001427 return;
1428 }
1429
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001430 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001431 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001432 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1433 return;
1434 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001435
Connor O'Brience16a8a2017-02-06 14:39:31 -08001436 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001437 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1438 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001439
Jin Qian4a335822017-04-18 16:23:18 -07001440 if (fs_generator_generate(gen, output.path, size, initial_dir,
1441 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001442 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001443 return;
1444 }
1445
Jin Qian4a335822017-04-18 16:23:18 -07001446 fd.reset(open(output.path, O_RDONLY));
1447 if (fd == -1) {
1448 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1449 return;
1450 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001451 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001452 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001453 return;
1454 }
1455 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456 return;
1457
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001458failed:
1459 if (skip_if_not_supported) {
1460 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001461 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001463 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001464}
1465
Aaron Wisnerdb511202018-06-26 15:38:35 -05001466int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001467 bool wants_wipe = false;
1468 bool wants_reboot = false;
1469 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001470 bool wants_reboot_recovery = false;
1471 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001472 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001473 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001474 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001475 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001476 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001477 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001478 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001479 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001480 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001481
Elliott Hughes577e8b42018-04-05 16:12:47 -07001482 g_boot_img_hdr.kernel_addr = 0x00008000;
1483 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1484 g_boot_img_hdr.second_addr = 0x00f00000;
1485 g_boot_img_hdr.tags_addr = 0x00000100;
1486 g_boot_img_hdr.page_size = 2048;
1487
JP Abgrall7b8970c2013-03-07 17:06:41 -08001488 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001489 {"base", required_argument, 0, 0},
1490 {"cmdline", required_argument, 0, 0},
1491 {"disable-verification", no_argument, 0, 0},
1492 {"disable-verity", no_argument, 0, 0},
1493 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001494 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001495 {"kernel-offset", required_argument, 0, 0},
1496 {"os-patch-level", required_argument, 0, 0},
1497 {"os-version", required_argument, 0, 0},
1498 {"page-size", required_argument, 0, 0},
1499 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001500 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001501 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001502 {"skip-secondary", no_argument, 0, 0},
1503 {"slot", required_argument, 0, 0},
1504 {"tags-offset", required_argument, 0, 0},
1505 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001506 {"verbose", no_argument, 0, 'v'},
1507 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001508#if !defined(_WIN32)
1509 {"wipe-and-use-fbe", no_argument, 0, 0},
1510#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001511 {0, 0, 0, 0}
1512 };
Colin Cross8879f982012-05-22 17:53:34 -07001513
1514 serial = getenv("ANDROID_SERIAL");
1515
Elliott Hughes577e8b42018-04-05 16:12:47 -07001516 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001517 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001518 if (c == 0) {
1519 std::string name{longopts[longindex].name};
1520 if (name == "base") {
1521 g_base_addr = strtoul(optarg, 0, 16);
1522 } else if (name == "cmdline") {
1523 g_cmdline = optarg;
1524 } else if (name == "disable-verification") {
1525 g_disable_verification = true;
1526 } else if (name == "disable-verity") {
1527 g_disable_verity = true;
1528 } else if (name == "header-version") {
1529 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1530 } else if (name == "kernel-offset") {
1531 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1532 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001533 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001534 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001535 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001536 } else if (name == "page-size") {
1537 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1538 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1539 } else if (name == "ramdisk-offset") {
1540 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1541 } else if (name == "skip-reboot") {
1542 skip_reboot = true;
1543 } else if (name == "skip-secondary") {
1544 skip_secondary = true;
1545 } else if (name == "slot") {
1546 slot_override = optarg;
1547 } else if (name == "tags-offset") {
1548 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1549 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001550 setvbuf(stdout, nullptr, _IONBF, 0);
1551 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001552 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001553 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001554 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001555 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001556#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001557 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001558 wants_wipe = true;
1559 set_fbe_marker = true;
1560#endif
1561 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001562 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001563 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001564 } else {
1565 switch (c) {
1566 case 'a':
1567 wants_set_active = true;
1568 if (optarg) next_active = optarg;
1569 break;
1570 case 'h':
1571 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001572 case 'l':
1573 g_long_listing = true;
1574 break;
1575 case 's':
1576 serial = optarg;
1577 break;
1578 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001579 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1580 die("invalid sparse limit %s", optarg);
1581 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001582 break;
1583 case 'v':
1584 set_verbose();
1585 break;
1586 case 'w':
1587 wants_wipe = true;
1588 break;
1589 case '?':
1590 return 1;
1591 default:
1592 abort();
1593 }
Colin Cross8879f982012-05-22 17:53:34 -07001594 }
1595 }
1596
1597 argc -= optind;
1598 argv += optind;
1599
Elliott Hughesd6365a72017-05-08 18:04:49 -07001600 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001601
Colin Cross8fb6e062012-07-24 16:36:41 -07001602 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001603 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001604 return 0;
1605 }
1606
Colin Crossc7b75dc2012-08-29 18:17:06 -07001607 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001608 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001609 }
1610
David Pursell0b156632015-10-30 11:22:01 -07001611 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001612 if (transport == nullptr) {
1613 return 1;
1614 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001615 fastboot::FastBootDriver fb(transport);
1616 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001617
Elliott Hughes5620d222018-03-28 08:20:00 -07001618 const double start = now();
1619
Aaron Wisnerdb511202018-06-26 15:38:35 -05001620 if (slot_override != "") slot_override = verify_slot(slot_override);
1621 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001622
1623 if (wants_set_active) {
1624 if (next_active == "") {
1625 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001626 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001627 if (fb_getvar("current-slot", &current_slot)) {
1628 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001629 } else {
1630 wants_set_active = false;
1631 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001632 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001633 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001634 }
1635 }
1636 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001637
Elliott Hughesd6365a72017-05-08 18:04:49 -07001638 std::vector<std::string> args(argv, argv + argc);
1639 while (!args.empty()) {
1640 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001641
Elliott Hughesd6365a72017-05-08 18:04:49 -07001642 if (command == "getvar") {
1643 std::string variable = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001644 fb_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001645 } else if (command == "erase") {
1646 std::string partition = next_arg(&args);
1647 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001648 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001649 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001650 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001651 fs_get_generator(partition_type) != nullptr) {
1652 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1653 partition_type.c_str());
1654 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001655
Tom Cherry11f12092018-08-29 21:36:28 -07001656 fb_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001657 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001658 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001659 } else if (android::base::StartsWith(command, "format")) {
1660 // Parsing for: "format[:[type][:[size]]]"
1661 // Some valid things:
1662 // - select only the size, and leave default fs type:
1663 // format::0x4000000 userdata
1664 // - default fs type and size:
1665 // format userdata
1666 // format:: userdata
1667 std::vector<std::string> pieces = android::base::Split(command, ":");
1668 std::string type_override;
1669 if (pieces.size() > 1) type_override = pieces[1].c_str();
1670 std::string size_override;
1671 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001672
Elliott Hughesd6365a72017-05-08 18:04:49 -07001673 std::string partition = next_arg(&args);
1674
1675 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001676 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001677 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001678 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001679 } else if (command == "signature") {
1680 std::string filename = next_arg(&args);
1681 data = load_file(filename.c_str(), &sz);
1682 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001683 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
Tom Cherry11f12092018-08-29 21:36:28 -07001684 fb_download("signature", data, sz);
1685 fb_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001686 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001687 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001688
1689 if (args.size() == 1) {
1690 std::string what = next_arg(&args);
1691 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001692 wants_reboot = false;
1693 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001694 } else if (what == "recovery") {
1695 wants_reboot = false;
1696 wants_reboot_recovery = true;
1697 } else if (what == "fastboot") {
1698 wants_reboot = false;
1699 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001700 } else {
1701 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001702 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703
Elliott Hughesca85df02015-02-25 10:02:00 -08001704 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001705 if (!args.empty()) syntax_error("junk after reboot command");
1706 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001707 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001708 } else if (command == "reboot-recovery") {
1709 wants_reboot_recovery = true;
1710 } else if (command == "reboot-fastboot") {
1711 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001712 } else if (command == "continue") {
Tom Cherry11f12092018-08-29 21:36:28 -07001713 fb_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001714 } else if (command == "boot") {
1715 std::string kernel = next_arg(&args);
1716 std::string ramdisk;
1717 if (!args.empty()) ramdisk = next_arg(&args);
1718 std::string second_stage;
1719 if (!args.empty()) second_stage = next_arg(&args);
1720
Elliott Hughes577e8b42018-04-05 16:12:47 -07001721 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Tom Cherry11f12092018-08-29 21:36:28 -07001722 fb_download("boot.img", data, sz);
1723 fb_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 } else if (command == "flash") {
1725 std::string pname = next_arg(&args);
1726
Elliott Hughes2810d002016-04-25 14:31:18 -07001727 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001728 if (!args.empty()) {
1729 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001730 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001731 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001732 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001733 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001734
1735 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001736 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001737 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001738 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001739 } else if (command == "flash:raw") {
1740 std::string partition = next_arg(&args);
1741 std::string kernel = next_arg(&args);
1742 std::string ramdisk;
1743 if (!args.empty()) ramdisk = next_arg(&args);
1744 std::string second_stage;
1745 if (!args.empty()) second_stage = next_arg(&args);
1746
Elliott Hughes577e8b42018-04-05 16:12:47 -07001747 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001748 auto flashraw = [&](const std::string& partition) {
Tom Cherry11f12092018-08-29 21:36:28 -07001749 fb_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001750 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001751 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001752 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001753 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001754 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001755 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001756 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001757 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001758 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001759 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001760 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001761 bool slot_all = (slot_override == "all");
1762 if (slot_all) {
1763 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1764 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 std::string filename = "update.zip";
1766 if (!args.empty()) {
1767 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001768 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001769 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001770 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001771 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001772 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001773 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001774 } else if (command == "stage") {
1775 std::string filename = next_arg(&args);
1776
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001777 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001778 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001779 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001780 }
Tom Cherry11f12092018-08-29 21:36:28 -07001781 fb_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001782 } else if (command == "get_staged") {
1783 std::string filename = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001784 fb_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001786 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001787 } else if (command == "flashing") {
1788 if (args.empty()) {
1789 syntax_error("missing 'flashing' command");
1790 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1791 args[0] == "unlock_critical" ||
1792 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001793 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001794 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001795 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001796 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001797 }
David Anderson0d4277d2018-07-31 13:27:37 -07001798 } else if (command == "create-logical-partition") {
1799 std::string partition = next_arg(&args);
1800 std::string size = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001801 fb_create_partition(partition, size);
David Anderson0d4277d2018-07-31 13:27:37 -07001802 } else if (command == "delete-logical-partition") {
1803 std::string partition = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001804 fb_delete_partition(partition);
David Anderson0d4277d2018-07-31 13:27:37 -07001805 } else if (command == "resize-logical-partition") {
1806 std::string partition = next_arg(&args);
1807 std::string size = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001808 fb_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001809 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001810 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001811 }
1812 }
1813
1814 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001815 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1816 for (const auto& partition : partitions) {
1817 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001818 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001819 if (partition_type.empty()) continue;
Tom Cherry11f12092018-08-29 21:36:28 -07001820 fb_erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001821 if (partition == "userdata" && set_fbe_marker) {
1822 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1823 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001824 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001825 delete_fbemarker_tmpdir(initial_userdata_dir);
1826 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001827 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001828 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001829 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001830 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001831 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001832 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001833 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001834 if (wants_reboot && !skip_reboot) {
Tom Cherry11f12092018-08-29 21:36:28 -07001835 fb_reboot();
1836 fb_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001837 } else if (wants_reboot_bootloader) {
Tom Cherry11f12092018-08-29 21:36:28 -07001838 fb_command("reboot-bootloader", "rebooting into bootloader");
1839 fb_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001840 } else if (wants_reboot_recovery) {
Tom Cherry11f12092018-08-29 21:36:28 -07001841 fb_command("reboot-recovery", "rebooting into recovery");
1842 fb_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001843 } else if (wants_reboot_fastboot) {
Tom Cherry11f12092018-08-29 21:36:28 -07001844 fb_command("reboot-fastboot", "rebooting into fastboot");
1845 fb_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001846 }
1847
Elliott Hughes5620d222018-03-28 08:20:00 -07001848 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001849
1850 if (Transport* old_transport = fb.set_transport(nullptr)) {
1851 delete old_transport;
1852 }
Tom Cherry11f12092018-08-29 21:36:28 -07001853 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001854}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001855
Aaron Wisnerdb511202018-06-26 15:38:35 -05001856void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001857 unsigned year, month, day;
1858 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1859 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1860 }
1861 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1862 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1863 hdr->SetOsPatchLevel(year, month);
1864}
1865
Aaron Wisnerdb511202018-06-26 15:38:35 -05001866void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001867 unsigned major = 0, minor = 0, patch = 0;
1868 std::vector<std::string> versions = android::base::Split(arg, ".");
1869 if (versions.size() < 1 || versions.size() > 3 ||
1870 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1871 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1872 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1873 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1874 syntax_error("bad OS version: %s", arg);
1875 }
1876 hdr->SetOsVersion(major, minor, patch);
1877}