blob: 3e090d7a59439dd99c6abeb11b91aa8799ee91e2 [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
Tom Cherry9027af02018-09-24 15:48:09 -070029#include "fastboot.h"
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Tom Cherry4aa60b32018-09-05 15:11:44 -070048#include <regex>
Elliott Hughes290a2282016-11-14 17:08:47 -080049#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080050#include <utility>
51#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070052
Elliott Hughes82ff3152016-08-31 15:07:18 -070053#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070054#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080056#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070057#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080058#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050059#include <android-base/unique_fd.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070060#include <build/version.h>
61#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070062#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070063#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Elliott Hughes253c18d2015-03-18 22:47:09 -070065#include "bootimg_utils.h"
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070066#include "constants.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080067#include "diagnose_usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070068#include "fastboot_driver.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070069#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080070#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070071#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080072#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070073#include "usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070074#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Tom Cherrydfd85df2018-09-20 14:45:05 -070076using android::base::ReadFully;
Tom Cherry4aa60b32018-09-05 15:11:44 -070077using android::base::Split;
78using android::base::Trim;
Chris Fries0ea946c2017-04-12 10:25:57 -050079using android::base::unique_fd;
80
David Pursell2ec418a2016-01-20 08:32:08 -080081static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070082
Elliott Hughes577e8b42018-04-05 16:12:47 -070083static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050084// Don't resparse files in too-big chunks.
85// libsparse will support INT_MAX, but this results in large allocations, so
86// let's keep it at 1GB to avoid memory pressure on the host.
87static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070088static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070089static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughes577e8b42018-04-05 16:12:47 -070091static unsigned g_base_addr = 0x10000000;
92static boot_img_hdr_v1 g_boot_img_hdr = {};
93static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080094
David Zeuthenb6ea4352017-08-07 14:29:26 -040095static bool g_disable_verity = false;
96static bool g_disable_verification = false;
97
Paul Crowley8f7f56e2015-11-27 09:29:37 +000098static const std::string convert_fbe_marker_filename("convert_fbe");
99
Tom Cherry9027af02018-09-24 15:48:09 -0700100fastboot::FastBootDriver* fb = nullptr;
101
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500103 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 FB_BUFFER_SPARSE,
105};
106
107struct fastboot_buffer {
108 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700109 void* data;
110 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500111 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700112 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700113};
114
David Anderson0debda02018-08-28 13:54:03 -0700115enum class ImageType {
116 // Must be flashed for device to boot into the kernel.
117 BootCritical,
118 // Normal partition to be flashed during "flashall".
119 Normal,
120 // Partition that is never flashed during "flashall".
121 Extra
122};
123
David Anderson32e376f2018-08-16 13:43:11 -0700124struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700125 const char* nickname;
126 const char* img_name;
127 const char* sig_name;
128 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700129 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700130 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700131 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700132};
133
134static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100135 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700136 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
137 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
138 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
139 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
140 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
141 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
142 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100143 { "product_services",
144 "product_services.img",
145 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700146 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700147 true, ImageType::Normal },
148 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
149 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
150 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
151 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
152 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
153 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson166bfef2018-10-15 14:46:59 -0700154 { "vbmeta_system",
155 "vbmeta_system.img",
156 "vbmeta_system.sig",
157 "vbmeta_system",
David Anderson1109c922018-09-17 17:32:54 -0700158 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700159 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
160 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100161 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700162};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700163
David Anderson0debda02018-08-28 13:54:03 -0700164static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700165 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700166 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700167 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700168 }
David Anderson0debda02018-08-28 13:54:03 -0700169 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700170}
171
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700172static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700173 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700174 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700175 return find_item_given_name(images[i].img_name);
176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
178
Elliott Hughesd6365a72017-05-08 18:04:49 -0700179 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700180 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181}
182
Tom Cherry9027af02018-09-24 15:48:09 -0700183double last_start_time;
184
185static void Status(const std::string& message) {
186 static constexpr char kStatusFormat[] = "%-50s ";
187 fprintf(stderr, kStatusFormat, message.c_str());
188 last_start_time = now();
189}
190
191static void Epilog(int status) {
192 if (status) {
193 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
194 die("Command failed");
195 } else {
196 double split = now();
197 fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
198 }
199}
200
201static void InfoMessage(const std::string& info) {
202 fprintf(stderr, "(bootloader) %s\n", info.c_str());
203}
204
Elliott Hughesfc797672015-04-07 20:12:50 -0700205static int64_t get_file_size(int fd) {
206 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700207 if (fstat(fd, &sb) == -1) {
208 die("could not get file size");
209 }
210 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700211}
212
Tom Cherrydfd85df2018-09-20 14:45:05 -0700213bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
214 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215
Tom Cherrydfd85df2018-09-20 14:45:05 -0700216 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
217 if (fd == -1) {
218 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700219 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220
Tom Cherrydfd85df2018-09-20 14:45:05 -0700221 out->resize(get_file_size(fd));
222 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700223}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700226 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
227 return -1;
228 }
229
Scott Anderson13081c62012-04-06 12:39:30 -0700230 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700232 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
233 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 return 0;
235}
236
Elliott Hughesfc797672015-04-07 20:12:50 -0700237static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800238 return match_fastboot_with_serial(info, serial);
239}
240
Elliott Hughesfc797672015-04-07 20:12:50 -0700241static int list_devices_callback(usb_ifc_info* info) {
242 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800243 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700244 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800245 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700246 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 if (!serial[0]) {
248 serial = "????????????";
249 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700250 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700251 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800252 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700253 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800254 printf("%-22s fastboot", serial.c_str());
255 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700256 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800257 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258 }
259
260 return -1;
261}
262
David Pursell2ec418a2016-01-20 08:32:08 -0800263// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
264// a specific device, otherwise the first USB device found will be used.
265//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700266// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800267// Otherwise it blocks until the target is available.
268//
269// The returned Transport is a singleton, so multiple calls to this function will return the same
270// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700271static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800272 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800273
David Pursell4601c972016-02-05 15:35:09 -0800274 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800275 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800276 int port = 0;
277 if (serial != nullptr) {
278 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800279
David Pursell4601c972016-02-05 15:35:09 -0800280 if (android::base::StartsWith(serial, "tcp:")) {
281 protocol = Socket::Protocol::kTcp;
282 port = tcp::kDefaultPort;
283 net_address = serial + strlen("tcp:");
284 } else if (android::base::StartsWith(serial, "udp:")) {
285 protocol = Socket::Protocol::kUdp;
286 port = udp::kDefaultPort;
287 net_address = serial + strlen("udp:");
288 }
289
290 if (net_address != nullptr) {
291 std::string error;
292 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700293 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800294 }
David Pursell2ec418a2016-01-20 08:32:08 -0800295 }
296 }
297
David Anderson1d887432018-08-27 16:47:32 -0700298 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800299 while (true) {
300 if (!host.empty()) {
301 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800302 if (protocol == Socket::Protocol::kTcp) {
303 transport = tcp::Connect(host, port, &error).release();
304 } else if (protocol == Socket::Protocol::kUdp) {
305 transport = udp::Connect(host, port, &error).release();
306 }
307
David Pursell2ec418a2016-01-20 08:32:08 -0800308 if (transport == nullptr && announce) {
309 fprintf(stderr, "error: %s\n", error.c_str());
310 }
311 } else {
312 transport = usb_open(match_fastboot);
313 }
314
315 if (transport != nullptr) {
316 return transport;
317 }
318
David Pursell0b156632015-10-30 11:22:01 -0700319 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800320 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700321 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800323 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324 }
325}
326
Elliott Hughesfc797672015-04-07 20:12:50 -0700327static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 // We don't actually open a USB device here,
329 // just getting our callback called so we can
330 // list all the connected devices.
331 usb_open(list_devices_callback);
332}
333
Elliott Hughesd6365a72017-05-08 18:04:49 -0700334static void syntax_error(const char* fmt, ...) {
335 fprintf(stderr, "fastboot: usage: ");
336
337 va_list ap;
338 va_start(ap, fmt);
339 vfprintf(stderr, fmt, ap);
340 va_end(ap);
341
342 fprintf(stderr, "\n");
343 exit(1);
344}
345
346static int show_help() {
347 // clang-format off
348 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700349// 1 2 3 4 5 6 7 8
350// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700351 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700353 "flashing:\n"
354 " update ZIP Flash all partitions from an update.zip package.\n"
355 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
356 " On A/B devices, flashed slot is set as active.\n"
357 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700358 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
359 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700360 "\n"
361 "basics:\n"
362 " devices [-l] List devices in bootloader (-l: with device paths).\n"
363 " getvar NAME Display given bootloader variable.\n"
364 " reboot [bootloader] Reboot device.\n"
365 "\n"
366 "locking/unlocking:\n"
367 " flashing lock|unlock Lock/unlock partitions for flashing\n"
368 " flashing lock_critical|unlock_critical\n"
369 " Lock/unlock 'critical' bootloader partitions.\n"
370 " flashing get_unlock_ability\n"
371 " Check whether unlocking is allowed (1) or not(0).\n"
372 "\n"
373 "advanced:\n"
374 " erase PARTITION Erase a flash partition.\n"
375 " format[:FS_TYPE[:SIZE]] PARTITION\n"
376 " Format a flash partition.\n"
377 " set_active SLOT Set the active slot.\n"
378 " oem [COMMAND...] Execute OEM-specific command.\n"
379 "\n"
380 "boot image:\n"
381 " boot KERNEL [RAMDISK [SECOND]]\n"
382 " Download and boot kernel from RAM.\n"
383 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
384 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700385 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700386 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
387 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
388 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
389 " --tags-offset Set tags offset (default: 0x00000100).\n"
390 " --page-size BYTES Set flash page size (default: 2048).\n"
391 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700392 " --os-version MAJOR[.MINOR[.PATCH]]\n"
393 " Set boot image OS version (default: 0.0.0).\n"
394 " --os-patch-level YYYY-MM-DD\n"
395 " Set boot image OS security patch level.\n"
396 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700398 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700399 //" continue Continue with autoboot.\n"
400 //"\n"
401 "Android Things:\n"
402 " stage IN_FILE Sends given file to stage for the next command.\n"
403 " 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 -0800404 "\n"
405 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700406 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700407 " -s SERIAL Specify a USB device.\n"
408 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700409 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700410 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
411 " non-current slot (default: current active slot).\n"
412 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
413 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
414 " --skip-reboot Don't reboot device after flashing.\n"
415 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
416 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000417#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700418 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000419#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700420 // TODO: remove --unbuffered?
421 " --unbuffered Don't buffer input or output.\n"
422 " --verbose, -v Verbose output.\n"
423 " --version Display version.\n"
424 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700426 // clang-format off
427 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800428}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000429
Tom Cherrydfd85df2018-09-20 14:45:05 -0700430static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
431 const std::string& second_stage) {
432 std::vector<char> kernel_data;
433 if (!ReadFileToVector(kernel, &kernel_data)) {
434 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
435 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800436
Elliott Hughesfc797672015-04-07 20:12:50 -0700437 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700438 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800439 die("cannot load '%s': too short", kernel.c_str());
440 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700441 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700442 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700443 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700444 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800445
Elliott Hughes4089d342017-10-27 14:21:12 -0700446 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800447
Tom Cherrydfd85df2018-09-20 14:45:05 -0700448 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 }
450
Tom Cherrydfd85df2018-09-20 14:45:05 -0700451 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700452 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700453 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
454 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
455 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 }
457
Tom Cherrydfd85df2018-09-20 14:45:05 -0700458 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700459 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700460 if (!ReadFileToVector(second_stage, &second_stage_data)) {
461 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
462 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200463 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700465
Tom Cherrydfd85df2018-09-20 14:45:05 -0700466 std::vector<char> out;
467 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
468 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800469
Tom Cherrydfd85df2018-09-20 14:45:05 -0700470 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
471 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
472
473 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474}
475
Tom Cherrydfd85df2018-09-20 14:45:05 -0700476static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
477 std::vector<char>* out) {
478 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700479 ZipEntry zip_entry;
480 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700481 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
482 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 }
484
Tom Cherrydfd85df2018-09-20 14:45:05 -0700485 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486
Tom Cherrydfd85df2018-09-20 14:45:05 -0700487 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
488 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489
Tom Cherrydfd85df2018-09-20 14:45:05 -0700490 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
491 out->size());
492 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000493
Tom Cherrydfd85df2018-09-20 14:45:05 -0700494 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495}
496
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700497#if defined(_WIN32)
498
499// TODO: move this to somewhere it can be shared.
500
501#include <windows.h>
502
503// Windows' tmpfile(3) requires administrator rights because
504// it creates temporary files in the root directory.
505static FILE* win32_tmpfile() {
506 char temp_path[PATH_MAX];
507 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
508 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700509 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700510 }
511
512 char filename[PATH_MAX];
513 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700514 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700515 }
516
517 return fopen(filename, "w+bTD");
518}
519
520#define tmpfile win32_tmpfile
521
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000522static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700523 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000524}
525
Elliott Hughes855cdf82018-04-02 14:24:03 -0700526static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700527 // TODO: reimplement to avoid leaking a FILE*.
528 return fileno(tmpfile());
529}
530
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000531#else
532
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700533static std::string make_temporary_template() {
534 const char* tmpdir = getenv("TMPDIR");
535 if (tmpdir == nullptr) tmpdir = P_tmpdir;
536 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
537}
538
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000539static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700540 std::string result(make_temporary_template());
541 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700542 die("unable to create temporary directory with template %s: %s",
543 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544 }
545 return result;
546}
547
Elliott Hughes855cdf82018-04-02 14:24:03 -0700548static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700549 std::string path_template(make_temporary_template());
550 int fd = mkstemp(&path_template[0]);
551 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700552 die("failed to create temporary file for %s with template %s: %s\n",
553 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700554 }
555 unlink(path_template.c_str());
556 return fd;
557}
558
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700559#endif
560
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000561static std::string create_fbemarker_tmpdir() {
562 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000563 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
564 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
565 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700566 die("unable to create FBE marker file %s locally: %s",
567 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000568 }
569 close(fd);
570 return dir;
571}
572
573static void delete_fbemarker_tmpdir(const std::string& dir) {
574 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
575 if (unlink(marker_file.c_str()) == -1) {
576 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
577 marker_file.c_str(), errno, strerror(errno));
578 return;
579 }
580 if (rmdir(dir.c_str()) == -1) {
581 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
582 dir.c_str(), errno, strerror(errno));
583 return;
584 }
585}
586
Elliott Hughes45964a82017-05-03 22:43:23 -0700587static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700588 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700589
Yusuke Sato07447542015-06-25 14:39:19 -0700590 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700591 ZipEntry zip_entry;
592 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
593 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700594 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000595 return -1;
596 }
597
Elliott Hughes23af1122017-11-10 08:42:17 -0800598 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700599 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800600 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700601 int error = ExtractEntryToFile(zip, &zip_entry, fd);
602 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800603 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700604 }
605
Elliott Hughes4089d342017-10-27 14:21:12 -0700606 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800607 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700608 }
609
Elliott Hughes23af1122017-11-10 08:42:17 -0800610 fprintf(stderr, " took %.3fs\n", now() - start);
611
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700612 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700613}
614
Tom Cherry4aa60b32018-09-05 15:11:44 -0700615static void CheckRequirement(const std::string& cur_product, const std::string& var,
616 const std::string& product, bool invert,
617 const std::vector<std::string>& options) {
618 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700619
Tom Cherry4aa60b32018-09-05 15:11:44 -0700620 double start = now();
621
622 if (!product.empty()) {
623 if (product != cur_product) {
624 double split = now();
625 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
626 cur_product.c_str(), product.c_str(), (split - start));
627 return;
628 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800629 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700630
631 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700632 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700633 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700634 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
635 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700636 die("requirements not met!");
637 }
638
639 bool match = false;
640 for (const auto& option : options) {
641 if (option == var_value || (option.back() == '*' &&
642 !var_value.compare(0, option.length() - 1, option, 0,
643 option.length() - 1))) {
644 match = true;
645 break;
646 }
647 }
648
649 if (invert) {
650 match = !match;
651 }
652
653 if (match) {
654 double split = now();
655 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
656 return;
657 }
658
659 fprintf(stderr, "FAILED\n\n");
660 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
661 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
662 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
663 fprintf(stderr, " or '%s'", it->c_str());
664 }
665 fprintf(stderr, ".\n\n");
666 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800667}
668
Tom Cherry4aa60b32018-09-05 15:11:44 -0700669bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
670 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700671 // "require product=alpha|beta|gamma"
672 // "require version-bootloader=1234"
673 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
674 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700675 *product = "";
676 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700677
Tom Cherry4aa60b32018-09-05 15:11:44 -0700678 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
679 auto require_product_regex =
680 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
681 std::smatch match_results;
682
683 if (std::regex_match(line, match_results, require_reject_regex)) {
684 *invert = Trim(match_results[1]) == "reject";
685 } else if (std::regex_match(line, match_results, require_product_regex)) {
686 *product = match_results[1];
687 } else {
688 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 }
690
Tom Cherry4aa60b32018-09-05 15:11:44 -0700691 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700692 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700693 if (*name == "board") {
694 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 }
696
Tom Cherry4aa60b32018-09-05 15:11:44 -0700697 auto raw_options = Split(match_results[3], "|");
698 for (const auto& option : raw_options) {
699 auto trimmed_option = Trim(option);
700 options->emplace_back(trimmed_option);
701 }
702
703 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704}
705
Tom Cherry4aa60b32018-09-05 15:11:44 -0700706// "require partition-exists=x" is a special case, added because of the trouble we had when
707// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
708// missing out new partitions. A device with new partitions can use "partition-exists" to
709// override the fields `optional_if_no_image` in the `images` array.
710static void HandlePartitionExists(const std::vector<std::string>& options) {
711 const std::string& partition_name = options[0];
712 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700713 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700714 (has_slot != "yes" && has_slot != "no")) {
715 die("device doesn't have required partition %s!", partition_name.c_str());
716 }
717 bool known_partition = false;
718 for (size_t i = 0; i < arraysize(images); ++i) {
719 if (images[i].nickname && images[i].nickname == partition_name) {
720 images[i].optional_if_no_image = false;
721 known_partition = true;
722 }
723 }
724 if (!known_partition) {
725 die("device requires partition %s which is not known to this version of fastboot",
726 partition_name.c_str());
727 }
728}
729
730static void CheckRequirements(const std::string& data) {
731 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700732 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
733 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700734 }
735
736 auto lines = Split(data, "\n");
737 for (const auto& line : lines) {
738 if (line.empty()) {
739 continue;
740 }
741
742 std::string name;
743 std::string product;
744 bool invert;
745 std::vector<std::string> options;
746
747 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
748 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
749 continue;
750 }
751 if (name == "partition-exists") {
752 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800753 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700754 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800755 }
756 }
757}
758
Tom Cherry9027af02018-09-24 15:48:09 -0700759static void DisplayVarOrError(const std::string& label, const std::string& var) {
760 std::string value;
761
762 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
763 Status("getvar:" + var);
764 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
765 return;
766 }
767 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
768}
769
770static void DumpInfo() {
771 fprintf(stderr, "--------------------------------------------\n");
772 DisplayVarOrError("Bootloader Version...", "version-bootloader");
773 DisplayVarOrError("Baseband Version.....", "version-baseband");
774 DisplayVarOrError("Serial Number........", "serialno");
775 fprintf(stderr, "--------------------------------------------\n");
776
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800777}
778
Tao Bao41cf35f2018-04-24 10:54:21 -0700779static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700780 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700781 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700782
Tao Bao41cf35f2018-04-24 10:54:21 -0700783 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
784 die("invalid max size %" PRId64, max_size);
785 }
786
Elliott Hughesfc797672015-04-07 20:12:50 -0700787 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700788 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700789
Elliott Hughes253c18d2015-03-18 22:47:09 -0700790 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700791 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700792
793 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700794 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700795
796 return out_s;
797}
798
Aaron Wisnerdb511202018-06-26 15:38:35 -0500799static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700800 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700801 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700802 max_download_size.empty()) {
803 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700804 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700805 }
806
Elliott Hughes77c0e662015-11-02 15:51:12 -0800807 // Some bootloaders (angler, for example) send spurious whitespace too.
808 max_download_size = android::base::Trim(max_download_size);
809
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700810 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700811 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800812 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700813 return 0;
814 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700815 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700816 return limit;
817}
818
Aaron Wisnerdb511202018-06-26 15:38:35 -0500819static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700820 int64_t limit = sparse_limit;
821 if (limit == 0) {
822 // Unlimited, so see what the target device's limit is.
823 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700824 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500825 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700826 }
827 if (target_sparse_limit > 0) {
828 limit = target_sparse_limit;
829 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700830 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700831 }
832 }
833
834 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500835 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700836 }
837
838 return 0;
839}
840
Aaron Wisnerdb511202018-06-26 15:38:35 -0500841static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700842 int64_t sz = get_file_size(fd);
843 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700844 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700845 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700846
David Anderson32e376f2018-08-16 13:43:11 -0700847 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
848 buf->image_size = sparse_file_len(s, false, false);
849 sparse_file_destroy(s);
850 } else {
851 buf->image_size = sz;
852 }
853
Elliott Hughesa56a7292018-10-26 10:34:53 -0700854 lseek(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500855 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700856 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700857 sparse_file** s = load_sparse_files(fd, limit);
858 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700859 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700860 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700861 buf->type = FB_BUFFER_SPARSE;
862 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700863 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500864 buf->type = FB_BUFFER_FD;
865 buf->data = nullptr;
866 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000867 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700868 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700869
Elliott Hughes53ec4952016-05-11 12:39:27 -0700870 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700871}
872
Aaron Wisnerdb511202018-06-26 15:38:35 -0500873static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500874 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
875
Elliott Hughes53ec4952016-05-11 12:39:27 -0700876 if (fd == -1) {
877 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700878 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500879
880 struct stat s;
881 if (fstat(fd, &s)) {
882 return false;
883 }
884 if (!S_ISREG(s.st_mode)) {
885 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
886 return false;
887 }
888
Aaron Wisnerdb511202018-06-26 15:38:35 -0500889 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700890}
891
David Zeuthenb6ea4352017-08-07 14:29:26 -0400892static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
893 // Buffer needs to be at least the size of the VBMeta struct which
894 // is 256 bytes.
895 if (buf->sz < 256) {
896 return;
897 }
898
Elliott Hughes855cdf82018-04-02 14:24:03 -0700899 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400900
901 std::string data;
902 if (!android::base::ReadFdToString(buf->fd, &data)) {
903 die("Failed reading from vbmeta");
904 }
905
906 // There's a 32-bit big endian |flags| field at offset 120 where
907 // bit 0 corresponds to disable-verity and bit 1 corresponds to
908 // disable-verification.
909 //
910 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
911 // the VBMeta struct.
912 if (g_disable_verity) {
913 data[123] |= 0x01;
914 }
915 if (g_disable_verification) {
916 data[123] |= 0x02;
917 }
918
919 if (!android::base::WriteStringToFd(data, fd)) {
920 die("Failed writing to modified vbmeta");
921 }
922 close(buf->fd);
923 buf->fd = fd;
924 lseek(fd, 0, SEEK_SET);
925}
926
Elliott Hughes5620d222018-03-28 08:20:00 -0700927static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700928{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700929 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700930
David Zeuthenb6ea4352017-08-07 14:29:26 -0400931 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
932 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700933 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400934 rewrite_vbmeta_buffer(buf);
935 }
936
Rom Lemarchand622810c2013-06-28 09:54:59 -0700937 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800938 case FB_BUFFER_SPARSE: {
939 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700940 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700941 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700942 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800943 sparse_files.emplace_back(*s, sz);
944 ++s;
945 }
946
947 for (size_t i = 0; i < sparse_files.size(); ++i) {
948 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700949 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700950 }
951 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800952 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500953 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700954 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700955 break;
956 default:
957 die("unknown buffer type: %d", buf->type);
958 }
959}
960
Aaron Wisnerdb511202018-06-26 15:38:35 -0500961static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700962 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700963 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700964 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700965}
966
Aaron Wisnerdb511202018-06-26 15:38:35 -0500967static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700969 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700970 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
971 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700972 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700974 return count;
975}
976
Aaron Wisnerdb511202018-06-26 15:38:35 -0500977static bool supports_AB() {
978 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700979}
980
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700981// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982static std::string get_other_slot(const std::string& current_slot, int count) {
983 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700984
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985 char next = (current_slot[0] - 'a' + 1)%count + 'a';
986 return std::string(1, next);
987}
988
Aaron Wisnerdb511202018-06-26 15:38:35 -0500989static std::string get_other_slot(const std::string& current_slot) {
990 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991}
992
Aaron Wisnerdb511202018-06-26 15:38:35 -0500993static std::string get_other_slot(int count) {
994 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995}
996
Aaron Wisnerdb511202018-06-26 15:38:35 -0500997static std::string get_other_slot() {
998 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700999}
1000
Aaron Wisnerdb511202018-06-26 15:38:35 -05001001static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001002 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001003 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001004 if (allow_all) {
1005 return "all";
1006 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001007 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001008 if (count > 0) {
1009 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001010 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001011 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001012 }
1013 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001014 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001015
Aaron Wisnerdb511202018-06-26 15:38:35 -05001016 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001017 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001018
Daniel Rosenberg80919472016-06-30 19:25:31 -07001019 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001020 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001021 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001022 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001023 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001024 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001025 }
1026
Daniel Rosenberg80919472016-06-30 19:25:31 -07001027 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1028
1029 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1030 for (int i=0; i<count; i++) {
1031 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001032 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001033
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001034 exit(1);
1035}
1036
Aaron Wisnerdb511202018-06-26 15:38:35 -05001037static std::string verify_slot(const std::string& slot) {
1038 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001039}
1040
Aaron Wisnerdb511202018-06-26 15:38:35 -05001041static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001042 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001043 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001044 std::string current_slot;
1045
Tom Cherry9027af02018-09-24 15:48:09 -07001046 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001047 /* If has-slot is not supported, the answer is no. */
1048 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001049 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001050 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001051 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001052 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001053 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001054 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001055 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001056 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001057 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001058 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001059 }
1060 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001061 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001062 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001063 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001064 }
1065 func(part);
1066 }
1067}
1068
David Pursell0b156632015-10-30 11:22:01 -07001069/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1070 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1071 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1072 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001073 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001074static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001075 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001076 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001077
Daniel Rosenberg80919472016-06-30 19:25:31 -07001078 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001079 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001080 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001081 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001082 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001083 for (int i=0; i < get_slot_count(); i++) {
1084 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001085 }
1086 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001087 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001088 }
1089 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001090 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001091 }
1092}
1093
David Anderson629e51c2018-10-25 12:51:25 -07001094static bool is_logical(const std::string& partition) {
1095 std::string value;
1096 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
1097}
1098
Aaron Wisnerdb511202018-06-26 15:38:35 -05001099static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001100 struct fastboot_buffer buf;
1101
Aaron Wisnerdb511202018-06-26 15:38:35 -05001102 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001103 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001104 }
David Anderson629e51c2018-10-25 12:51:25 -07001105 if (is_logical(pname)) {
1106 fb->ResizePartition(pname, std::to_string(buf.image_size));
1107 }
Rom Lemarchand622810c2013-06-28 09:54:59 -07001108 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001109}
1110
Daniel Rosenberg13454092016-06-27 19:43:11 -07001111// Sets slot_override as the active slot. If slot_override is blank,
1112// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001113static void set_active(const std::string& slot_override) {
1114 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001115
Daniel Rosenberg80919472016-06-30 19:25:31 -07001116 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001117 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001118 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001119 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001120 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001121 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001122 }
1123 }
1124}
1125
David Anderson32e376f2018-08-16 13:43:11 -07001126static bool is_userspace_fastboot() {
1127 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001128 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001129}
1130
David Anderson1d887432018-08-27 16:47:32 -07001131static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001132 fb->RebootTo("fastboot");
1133
1134 auto* old_transport = fb->set_transport(nullptr);
1135 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001136
1137 // Give the current connection time to close.
1138 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1139
Tom Cherry9027af02018-09-24 15:48:09 -07001140 fb->set_transport(open_device());
David Anderson1d887432018-08-27 16:47:32 -07001141}
1142
David Andersoncf444f32018-08-29 14:15:49 -07001143class ImageSource {
1144 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001145 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001146 virtual int OpenFile(const std::string& name) const = 0;
1147};
David Anderson32e376f2018-08-16 13:43:11 -07001148
David Andersoncf444f32018-08-29 14:15:49 -07001149class FlashAllTool {
1150 public:
1151 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001152
David Andersoncf444f32018-08-29 14:15:49 -07001153 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001154
David Andersoncf444f32018-08-29 14:15:49 -07001155 private:
1156 void CheckRequirements();
1157 void DetermineSecondarySlot();
1158 void CollectImages();
1159 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1160 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1161 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001162
David Andersoncf444f32018-08-29 14:15:49 -07001163 const ImageSource& source_;
1164 std::string slot_override_;
1165 bool skip_secondary_;
1166 bool wipe_;
1167 std::string secondary_slot_;
1168 std::vector<std::pair<const Image*, std::string>> boot_images_;
1169 std::vector<std::pair<const Image*, std::string>> os_images_;
1170};
1171
1172FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1173 : source_(source),
1174 slot_override_(slot_override),
1175 skip_secondary_(skip_secondary),
1176 wipe_(wipe)
1177{
David Anderson32e376f2018-08-16 13:43:11 -07001178}
1179
David Andersoncf444f32018-08-29 14:15:49 -07001180void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001181 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001182 CheckRequirements();
David Anderson96a9fd42018-11-05 15:21:44 -08001183
1184 // Change the slot first, so we boot into the correct recovery image when
1185 // using fastbootd.
1186 if (slot_override_ == "all") {
1187 set_active("a");
1188 } else {
1189 set_active(slot_override_);
1190 }
1191
David Andersoncf444f32018-08-29 14:15:49 -07001192 DetermineSecondarySlot();
1193 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001194
David Anderson95d40932018-08-28 12:14:33 -07001195 // First flash boot partitions. We allow this to happen either in userspace
1196 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001197 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001198
1199 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001200 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001201
1202 // Resize any logical partition to 0, so each partition is reset to 0
1203 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001204 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001205 auto resize_partition = [](const std::string& partition) -> void {
1206 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001207 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001208 }
1209 };
David Anderson95d40932018-08-28 12:14:33 -07001210 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001211 }
1212
David Anderson95d40932018-08-28 12:14:33 -07001213 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001214 FlashImages(os_images_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001215}
1216
David Andersoncf444f32018-08-29 14:15:49 -07001217void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001218 std::vector<char> contents;
1219 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001220 die("could not read android-info.txt");
1221 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001222 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001223}
1224
1225void FlashAllTool::DetermineSecondarySlot() {
1226 if (skip_secondary_) {
1227 return;
1228 }
David Anderson96a9fd42018-11-05 15:21:44 -08001229 if (slot_override_ != "" && slot_override_ != "all") {
David Andersoncf444f32018-08-29 14:15:49 -07001230 secondary_slot_ = get_other_slot(slot_override_);
1231 } else {
1232 secondary_slot_ = get_other_slot();
1233 }
1234 if (secondary_slot_ == "") {
1235 if (supports_AB()) {
1236 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1237 }
1238 skip_secondary_ = true;
1239 }
1240}
1241
1242void FlashAllTool::CollectImages() {
1243 for (size_t i = 0; i < arraysize(images); ++i) {
1244 std::string slot = slot_override_;
1245 if (images[i].IsSecondary()) {
1246 if (skip_secondary_) {
1247 continue;
1248 }
1249 slot = secondary_slot_;
1250 }
1251 if (images[i].type == ImageType::BootCritical) {
1252 boot_images_.emplace_back(&images[i], slot);
1253 } else if (images[i].type == ImageType::Normal) {
1254 os_images_.emplace_back(&images[i], slot);
1255 }
1256 }
1257}
1258
1259void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1260 for (const auto& [image, slot] : images) {
1261 fastboot_buffer buf;
1262 int fd = source_.OpenFile(image->img_name);
1263 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1264 if (image->optional_if_no_image) {
1265 continue;
1266 }
1267 die("could not load '%s': %s", image->img_name, strerror(errno));
1268 }
1269 FlashImage(*image, slot, &buf);
1270 }
1271}
1272
1273void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1274 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001275 std::vector<char> signature_data;
1276 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001277 fb->Download("signature", signature_data);
1278 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001279 }
1280
1281 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001282 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001283 }
1284 flash_buf(partition_name.c_str(), buf);
1285 };
1286 do_for_partitions(image.part_name, slot, flash, false);
1287}
1288
1289void FlashAllTool::UpdateSuperPartition() {
David Andersoncf444f32018-08-29 14:15:49 -07001290 int fd = source_.OpenFile("super_empty.img");
1291 if (fd < 0) {
1292 return;
1293 }
1294 if (!is_userspace_fastboot()) {
1295 reboot_to_userspace_fastboot();
1296 }
David Anderson0444a8c2018-10-25 12:01:17 -07001297 if (!is_userspace_fastboot()) {
1298 die("Failed to boot into userspace; one or more components might be unbootable.");
1299 }
David Andersoncf444f32018-08-29 14:15:49 -07001300
David Anderson90fe0a42018-11-05 18:01:32 -08001301 std::string super_name;
1302 if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
1303 super_name = "super";
1304 }
1305 fb->Download(super_name, fd, get_file_size(fd));
1306
1307 std::string command = "update-super:" + super_name;
David Andersoncf444f32018-08-29 14:15:49 -07001308 if (wipe_) {
1309 command += ":wipe";
1310 }
Tom Cherry9027af02018-09-24 15:48:09 -07001311 fb->RawCommand(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001312}
1313
1314class ZipImageSource final : public ImageSource {
1315 public:
1316 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001317 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001318 int OpenFile(const std::string& name) const override;
1319
1320 private:
1321 ZipArchiveHandle zip_;
1322};
1323
Tom Cherrydfd85df2018-09-20 14:45:05 -07001324bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1325 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001326}
1327
1328int ZipImageSource::OpenFile(const std::string& name) const {
1329 return unzip_to_file(zip_, name.c_str());
1330}
1331
1332static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001333 ZipArchiveHandle zip;
1334 int error = OpenArchive(filename, &zip);
1335 if (error != 0) {
1336 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1337 }
1338
1339 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1340 tool.Flash();
1341
1342 CloseArchive(zip);
1343}
1344
1345class LocalImageSource final : public ImageSource {
1346 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001347 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001348 int OpenFile(const std::string& name) const override;
1349};
1350
Tom Cherrydfd85df2018-09-20 14:45:05 -07001351bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001352 auto path = find_item_given_name(name);
1353 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001354 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001355 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001356 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001357}
1358
1359int LocalImageSource::OpenFile(const std::string& name) const {
1360 auto path = find_item_given_name(name);
1361 return open(path.c_str(), O_RDONLY);
1362}
1363
1364static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001365 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1366 tool.Flash();
1367}
1368
Elliott Hughesd6365a72017-05-08 18:04:49 -07001369static std::string next_arg(std::vector<std::string>* args) {
1370 if (args->empty()) syntax_error("expected argument");
1371 std::string result = args->front();
1372 args->erase(args->begin());
1373 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001374}
1375
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001376static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001377 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001378
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001379 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001380 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001381 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001382 }
Tom Cherry9027af02018-09-24 15:48:09 -07001383 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001384}
1385
Connor O'Brience16a8a2017-02-06 14:39:31 -08001386static std::string fb_fix_numeric_var(std::string var) {
1387 // Some bootloaders (angler, for example), send spurious leading whitespace.
1388 var = android::base::Trim(var);
1389 // Some bootloaders (hammerhead, for example) use implicit hex.
1390 // This code used to use strtol with base 16.
1391 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1392 return var;
1393}
1394
Aaron Wisnerdb511202018-06-26 15:38:35 -05001395static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001396 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001397 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001398 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001399 return 0;
1400 }
1401 sizeString = fb_fix_numeric_var(sizeString);
1402
1403 unsigned size;
1404 if (!android::base::ParseUint(sizeString, &size)) {
1405 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1406 return 0;
1407 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001408 if ((size & (size - 1)) != 0) {
1409 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001410 return 0;
1411 }
1412 return size;
1413}
1414
Aaron Wisnerdb511202018-06-26 15:38:35 -05001415static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001416 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001417 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001418 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 std::string partition_type, partition_size;
1420
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001422 const char* errMsg = nullptr;
1423 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001424 TemporaryFile output;
1425 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001426
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 unsigned int limit = INT_MAX;
1428 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001429 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001430 }
1431 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001432 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001433 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001434
Tom Cherry9027af02018-09-24 15:48:09 -07001435 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001436 errMsg = "Can't determine partition type.\n";
1437 goto failed;
1438 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001439 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001440 if (partition_type != type_override) {
1441 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001442 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001443 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001444 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001445 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446
Tom Cherry9027af02018-09-24 15:48:09 -07001447 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001448 errMsg = "Unable to get partition size\n";
1449 goto failed;
1450 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001451 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001452 if (partition_size != size_override) {
1453 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001454 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001455 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001456 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001457 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001458 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001459
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001460 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001461 if (!gen) {
1462 if (skip_if_not_supported) {
1463 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001464 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001465 return;
1466 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001467 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1468 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001469 return;
1470 }
1471
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001472 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001473 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001474 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1475 return;
1476 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001477
Connor O'Brience16a8a2017-02-06 14:39:31 -08001478 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001479 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1480 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001481
Jin Qian4a335822017-04-18 16:23:18 -07001482 if (fs_generator_generate(gen, output.path, size, initial_dir,
1483 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001484 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001485 return;
1486 }
1487
Jin Qian4a335822017-04-18 16:23:18 -07001488 fd.reset(open(output.path, O_RDONLY));
1489 if (fd == -1) {
1490 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1491 return;
1492 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001493 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001494 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001495 return;
1496 }
1497 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001498 return;
1499
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001500failed:
1501 if (skip_if_not_supported) {
1502 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001503 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001504 }
Tom Cherry9027af02018-09-24 15:48:09 -07001505 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001506}
1507
Aaron Wisnerdb511202018-06-26 15:38:35 -05001508int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001509 bool wants_wipe = false;
1510 bool wants_reboot = false;
1511 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001512 bool wants_reboot_recovery = false;
1513 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001514 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001515 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001516 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001517 bool set_fbe_marker = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001518 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001519 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001520 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001521
Elliott Hughes577e8b42018-04-05 16:12:47 -07001522 g_boot_img_hdr.kernel_addr = 0x00008000;
1523 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1524 g_boot_img_hdr.second_addr = 0x00f00000;
1525 g_boot_img_hdr.tags_addr = 0x00000100;
1526 g_boot_img_hdr.page_size = 2048;
1527
JP Abgrall7b8970c2013-03-07 17:06:41 -08001528 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001529 {"base", required_argument, 0, 0},
1530 {"cmdline", required_argument, 0, 0},
1531 {"disable-verification", no_argument, 0, 0},
1532 {"disable-verity", no_argument, 0, 0},
1533 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001534 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001535 {"kernel-offset", required_argument, 0, 0},
1536 {"os-patch-level", required_argument, 0, 0},
1537 {"os-version", required_argument, 0, 0},
1538 {"page-size", required_argument, 0, 0},
1539 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001540 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001541 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001542 {"skip-secondary", no_argument, 0, 0},
1543 {"slot", required_argument, 0, 0},
1544 {"tags-offset", required_argument, 0, 0},
1545 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001546 {"verbose", no_argument, 0, 'v'},
1547 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001548#if !defined(_WIN32)
1549 {"wipe-and-use-fbe", no_argument, 0, 0},
1550#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001551 {0, 0, 0, 0}
1552 };
Colin Cross8879f982012-05-22 17:53:34 -07001553
1554 serial = getenv("ANDROID_SERIAL");
1555
Elliott Hughes577e8b42018-04-05 16:12:47 -07001556 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001557 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001558 if (c == 0) {
1559 std::string name{longopts[longindex].name};
1560 if (name == "base") {
1561 g_base_addr = strtoul(optarg, 0, 16);
1562 } else if (name == "cmdline") {
1563 g_cmdline = optarg;
1564 } else if (name == "disable-verification") {
1565 g_disable_verification = true;
1566 } else if (name == "disable-verity") {
1567 g_disable_verity = true;
1568 } else if (name == "header-version") {
1569 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1570 } else if (name == "kernel-offset") {
1571 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1572 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001573 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001574 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001575 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001576 } else if (name == "page-size") {
1577 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1578 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1579 } else if (name == "ramdisk-offset") {
1580 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1581 } else if (name == "skip-reboot") {
1582 skip_reboot = true;
1583 } else if (name == "skip-secondary") {
1584 skip_secondary = true;
1585 } else if (name == "slot") {
1586 slot_override = optarg;
1587 } else if (name == "tags-offset") {
1588 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1589 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001590 setvbuf(stdout, nullptr, _IONBF, 0);
1591 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001592 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001593 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001594 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001595 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001596#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001597 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001598 wants_wipe = true;
1599 set_fbe_marker = true;
1600#endif
1601 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001602 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001603 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001604 } else {
1605 switch (c) {
1606 case 'a':
1607 wants_set_active = true;
1608 if (optarg) next_active = optarg;
1609 break;
1610 case 'h':
1611 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001612 case 'l':
1613 g_long_listing = true;
1614 break;
1615 case 's':
1616 serial = optarg;
1617 break;
1618 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001619 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1620 die("invalid sparse limit %s", optarg);
1621 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001622 break;
1623 case 'v':
1624 set_verbose();
1625 break;
1626 case 'w':
1627 wants_wipe = true;
1628 break;
1629 case '?':
1630 return 1;
1631 default:
1632 abort();
1633 }
Colin Cross8879f982012-05-22 17:53:34 -07001634 }
1635 }
1636
1637 argc -= optind;
1638 argv += optind;
1639
Elliott Hughesd6365a72017-05-08 18:04:49 -07001640 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001641
Colin Cross8fb6e062012-07-24 16:36:41 -07001642 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001643 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001644 return 0;
1645 }
1646
Colin Crossc7b75dc2012-08-29 18:17:06 -07001647 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001648 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001649 }
1650
David Pursell0b156632015-10-30 11:22:01 -07001651 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001652 if (transport == nullptr) {
1653 return 1;
1654 }
Tom Cherry9027af02018-09-24 15:48:09 -07001655 fastboot::DriverCallbacks driver_callbacks = {
1656 .prolog = Status,
1657 .epilog = Epilog,
1658 .info = InfoMessage,
1659 };
1660 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1661 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001662
Elliott Hughes5620d222018-03-28 08:20:00 -07001663 const double start = now();
1664
Aaron Wisnerdb511202018-06-26 15:38:35 -05001665 if (slot_override != "") slot_override = verify_slot(slot_override);
1666 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001667
1668 if (wants_set_active) {
1669 if (next_active == "") {
1670 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001671 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001672 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001673 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001674 } else {
1675 wants_set_active = false;
1676 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001677 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001678 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001679 }
1680 }
1681 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001682
Elliott Hughesd6365a72017-05-08 18:04:49 -07001683 std::vector<std::string> args(argv, argv + argc);
1684 while (!args.empty()) {
1685 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001686
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001687 if (command == FB_CMD_GETVAR) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001688 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001689 DisplayVarOrError(variable, variable);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001690 } else if (command == FB_CMD_ERASE) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001691 std::string partition = next_arg(&args);
1692 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001693 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001694 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001695 fs_get_generator(partition_type) != nullptr) {
1696 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1697 partition_type.c_str());
1698 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001699
Tom Cherry9027af02018-09-24 15:48:09 -07001700 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001701 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001702 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 } else if (android::base::StartsWith(command, "format")) {
1704 // Parsing for: "format[:[type][:[size]]]"
1705 // Some valid things:
1706 // - select only the size, and leave default fs type:
1707 // format::0x4000000 userdata
1708 // - default fs type and size:
1709 // format userdata
1710 // format:: userdata
1711 std::vector<std::string> pieces = android::base::Split(command, ":");
1712 std::string type_override;
1713 if (pieces.size() > 1) type_override = pieces[1].c_str();
1714 std::string size_override;
1715 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001716
Elliott Hughesd6365a72017-05-08 18:04:49 -07001717 std::string partition = next_arg(&args);
1718
1719 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001720 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001721 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001722 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001723 } else if (command == "signature") {
1724 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001725 std::vector<char> data;
1726 if (!ReadFileToVector(filename, &data)) {
1727 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1728 }
1729 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001730 fb->Download("signature", data);
1731 fb->RawCommand("signature", "installing signature");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001732 } else if (command == FB_CMD_REBOOT) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001733 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001734
1735 if (args.size() == 1) {
1736 std::string what = next_arg(&args);
1737 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001738 wants_reboot = false;
1739 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001740 } else if (what == "recovery") {
1741 wants_reboot = false;
1742 wants_reboot_recovery = true;
1743 } else if (what == "fastboot") {
1744 wants_reboot = false;
1745 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001746 } else {
1747 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001748 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001749
Elliott Hughesca85df02015-02-25 10:02:00 -08001750 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001751 if (!args.empty()) syntax_error("junk after reboot command");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001752 } else if (command == FB_CMD_REBOOT_BOOTLOADER) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001753 wants_reboot_bootloader = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001754 } else if (command == FB_CMD_REBOOT_RECOVERY) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001755 wants_reboot_recovery = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001756 } else if (command == FB_CMD_REBOOT_FASTBOOT) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001757 wants_reboot_fastboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001758 } else if (command == FB_CMD_CONTINUE) {
Tom Cherry9027af02018-09-24 15:48:09 -07001759 fb->Continue();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001760 } else if (command == FB_CMD_BOOT) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001761 std::string kernel = next_arg(&args);
1762 std::string ramdisk;
1763 if (!args.empty()) ramdisk = next_arg(&args);
1764 std::string second_stage;
1765 if (!args.empty()) second_stage = next_arg(&args);
1766
Tom Cherrydfd85df2018-09-20 14:45:05 -07001767 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001768 fb->Download("boot.img", data);
1769 fb->Boot();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001770 } else if (command == FB_CMD_FLASH) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001771 std::string pname = next_arg(&args);
1772
Elliott Hughes2810d002016-04-25 14:31:18 -07001773 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001774 if (!args.empty()) {
1775 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001776 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001777 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001778 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001779 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001780
1781 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001782 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001783 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001784 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 } else if (command == "flash:raw") {
1786 std::string partition = next_arg(&args);
1787 std::string kernel = next_arg(&args);
1788 std::string ramdisk;
1789 if (!args.empty()) ramdisk = next_arg(&args);
1790 std::string second_stage;
1791 if (!args.empty()) second_stage = next_arg(&args);
1792
Tom Cherrydfd85df2018-09-20 14:45:05 -07001793 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1794 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001795 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001796 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001797 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001798 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001799 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001800 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001801 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001802 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001803 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001804 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001805 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001806 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001807 bool slot_all = (slot_override == "all");
1808 if (slot_all) {
1809 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1810 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001811 std::string filename = "update.zip";
1812 if (!args.empty()) {
1813 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001814 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001815 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001816 wants_reboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001817 } else if (command == FB_CMD_SET_ACTIVE) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001818 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001819 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001820 } else if (command == "stage") {
1821 std::string filename = next_arg(&args);
1822
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001823 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001824 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001825 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001826 }
Tom Cherry9027af02018-09-24 15:48:09 -07001827 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001828 } else if (command == "get_staged") {
1829 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001830 fb->Upload(filename);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001831 } else if (command == FB_CMD_OEM) {
1832 do_oem_command(FB_CMD_OEM, &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001833 } else if (command == "flashing") {
1834 if (args.empty()) {
1835 syntax_error("missing 'flashing' command");
1836 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1837 args[0] == "unlock_critical" ||
1838 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001839 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001840 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001841 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001842 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001843 }
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001844 } else if (command == FB_CMD_CREATE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001845 std::string partition = next_arg(&args);
1846 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001847 fb->CreatePartition(partition, size);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001848 } else if (command == FB_CMD_DELETE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001849 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001850 fb->DeletePartition(partition);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001851 } else if (command == FB_CMD_RESIZE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001852 std::string partition = next_arg(&args);
1853 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001854 fb->ResizePartition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001855 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001856 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001857 }
1858 }
1859
1860 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001861 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1862 for (const auto& partition : partitions) {
1863 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001864 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1865 continue;
1866 }
Paul Crowley4d170062018-04-24 17:06:30 -07001867 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001868 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001869 if (partition == "userdata" && set_fbe_marker) {
1870 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1871 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001872 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001873 delete_fbemarker_tmpdir(initial_userdata_dir);
1874 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001875 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001876 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001877 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001878 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001879 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001880 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001881 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001882 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001883 fb->Reboot();
1884 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001885 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001886 fb->RebootTo("bootloader");
1887 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001888 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001889 fb->RebootTo("recovery");
1890 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001891 } else if (wants_reboot_fastboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001892 fb->RebootTo("fastboot");
1893 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001894 }
1895
Elliott Hughes5620d222018-03-28 08:20:00 -07001896 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001897
Tom Cherry9027af02018-09-24 15:48:09 -07001898 auto* old_transport = fb->set_transport(nullptr);
1899 delete old_transport;
1900
Tom Cherry11f12092018-08-29 21:36:28 -07001901 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001902}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001903
Aaron Wisnerdb511202018-06-26 15:38:35 -05001904void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001905 unsigned year, month, day;
1906 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1907 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1908 }
1909 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1910 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1911 hdr->SetOsPatchLevel(year, month);
1912}
1913
Aaron Wisnerdb511202018-06-26 15:38:35 -05001914void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001915 unsigned major = 0, minor = 0, patch = 0;
1916 std::vector<std::string> versions = android::base::Split(arg, ".");
1917 if (versions.size() < 1 || versions.size() > 3 ||
1918 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1919 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1920 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1921 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1922 syntax_error("bad OS version: %s", arg);
1923 }
1924 hdr->SetOsVersion(major, minor, patch);
1925}