blob: c97619c3844468ba8d8eda78da7328fcf4c74d3f [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>
Jin Qian4a335822017-04-18 16:23:18 -070059#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050060#include <android-base/unique_fd.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070061#include <build/version.h>
62#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070063#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070064#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Elliott Hughes253c18d2015-03-18 22:47:09 -070066#include "bootimg_utils.h"
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070067#include "constants.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080068#include "diagnose_usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070069#include "fastboot_driver.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070070#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080071#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070072#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080073#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070074#include "usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070075#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Tom Cherrydfd85df2018-09-20 14:45:05 -070077using android::base::ReadFully;
Tom Cherry4aa60b32018-09-05 15:11:44 -070078using android::base::Split;
79using android::base::Trim;
Chris Fries0ea946c2017-04-12 10:25:57 -050080using android::base::unique_fd;
81
Colin Crossf8387882012-05-24 17:18:41 -070082#ifndef O_BINARY
83#define O_BINARY 0
84#endif
85
David Pursell2ec418a2016-01-20 08:32:08 -080086static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050089// Don't resparse files in too-big chunks.
90// libsparse will support INT_MAX, but this results in large allocations, so
91// let's keep it at 1GB to avoid memory pressure on the host.
92static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070093static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070094static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095
Elliott Hughes577e8b42018-04-05 16:12:47 -070096static unsigned g_base_addr = 0x10000000;
97static boot_img_hdr_v1 g_boot_img_hdr = {};
98static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080099
David Zeuthenb6ea4352017-08-07 14:29:26 -0400100static bool g_disable_verity = false;
101static bool g_disable_verification = false;
102
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000103static const std::string convert_fbe_marker_filename("convert_fbe");
104
Tom Cherry9027af02018-09-24 15:48:09 -0700105fastboot::FastBootDriver* fb = nullptr;
106
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500108 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700109 FB_BUFFER_SPARSE,
110};
111
112struct fastboot_buffer {
113 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700114 void* data;
115 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500116 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700117 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700118};
119
David Anderson0debda02018-08-28 13:54:03 -0700120enum class ImageType {
121 // Must be flashed for device to boot into the kernel.
122 BootCritical,
123 // Normal partition to be flashed during "flashall".
124 Normal,
125 // Partition that is never flashed during "flashall".
126 Extra
127};
128
David Anderson32e376f2018-08-16 13:43:11 -0700129struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700130 const char* nickname;
131 const char* img_name;
132 const char* sig_name;
133 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700134 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700135 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700136 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700137};
138
139static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100140 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700141 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
142 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
143 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
144 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
145 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
146 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
147 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100148 { "product_services",
149 "product_services.img",
150 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700151 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700152 true, ImageType::Normal },
153 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
154 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
155 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
156 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
157 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
158 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson1109c922018-09-17 17:32:54 -0700159 { "vbmeta_mainline",
160 "vbmeta_mainline.img",
161 "vbmeta_mainline.sig",
162 "vbmeta_mainline",
163 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700164 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
165 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100166 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700167};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700168
David Anderson0debda02018-08-28 13:54:03 -0700169static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700170 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700171 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700172 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700173 }
David Anderson0debda02018-08-28 13:54:03 -0700174 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700175}
176
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700177static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700178 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700179 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700180 return find_item_given_name(images[i].img_name);
181 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 }
183
Elliott Hughesd6365a72017-05-08 18:04:49 -0700184 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700185 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186}
187
Tom Cherry9027af02018-09-24 15:48:09 -0700188double last_start_time;
189
190static void Status(const std::string& message) {
191 static constexpr char kStatusFormat[] = "%-50s ";
192 fprintf(stderr, kStatusFormat, message.c_str());
193 last_start_time = now();
194}
195
196static void Epilog(int status) {
197 if (status) {
198 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
199 die("Command failed");
200 } else {
201 double split = now();
202 fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
203 }
204}
205
206static void InfoMessage(const std::string& info) {
207 fprintf(stderr, "(bootloader) %s\n", info.c_str());
208}
209
Elliott Hughesfc797672015-04-07 20:12:50 -0700210static int64_t get_file_size(int fd) {
211 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700212 if (fstat(fd, &sb) == -1) {
213 die("could not get file size");
214 }
215 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700216}
217
Tom Cherrydfd85df2018-09-20 14:45:05 -0700218bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
219 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220
Tom Cherrydfd85df2018-09-20 14:45:05 -0700221 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
222 if (fd == -1) {
223 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700224 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225
Tom Cherrydfd85df2018-09-20 14:45:05 -0700226 out->resize(get_file_size(fd));
227 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700228}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229
Elliott Hughesfc797672015-04-07 20:12:50 -0700230static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700231 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
232 return -1;
233 }
234
Scott Anderson13081c62012-04-06 12:39:30 -0700235 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700237 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
238 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239 return 0;
240}
241
Elliott Hughesfc797672015-04-07 20:12:50 -0700242static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800243 return match_fastboot_with_serial(info, serial);
244}
245
Elliott Hughesfc797672015-04-07 20:12:50 -0700246static int list_devices_callback(usb_ifc_info* info) {
247 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800248 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700249 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800250 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700251 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800252 if (!serial[0]) {
253 serial = "????????????";
254 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700255 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700256 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800257 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700258 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800259 printf("%-22s fastboot", serial.c_str());
260 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700261 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800262 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800263 }
264
265 return -1;
266}
267
David Pursell2ec418a2016-01-20 08:32:08 -0800268// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
269// a specific device, otherwise the first USB device found will be used.
270//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700271// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800272// Otherwise it blocks until the target is available.
273//
274// The returned Transport is a singleton, so multiple calls to this function will return the same
275// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700276static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800277 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278
David Pursell4601c972016-02-05 15:35:09 -0800279 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800280 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800281 int port = 0;
282 if (serial != nullptr) {
283 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800284
David Pursell4601c972016-02-05 15:35:09 -0800285 if (android::base::StartsWith(serial, "tcp:")) {
286 protocol = Socket::Protocol::kTcp;
287 port = tcp::kDefaultPort;
288 net_address = serial + strlen("tcp:");
289 } else if (android::base::StartsWith(serial, "udp:")) {
290 protocol = Socket::Protocol::kUdp;
291 port = udp::kDefaultPort;
292 net_address = serial + strlen("udp:");
293 }
294
295 if (net_address != nullptr) {
296 std::string error;
297 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700298 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800299 }
David Pursell2ec418a2016-01-20 08:32:08 -0800300 }
301 }
302
David Anderson1d887432018-08-27 16:47:32 -0700303 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800304 while (true) {
305 if (!host.empty()) {
306 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800307 if (protocol == Socket::Protocol::kTcp) {
308 transport = tcp::Connect(host, port, &error).release();
309 } else if (protocol == Socket::Protocol::kUdp) {
310 transport = udp::Connect(host, port, &error).release();
311 }
312
David Pursell2ec418a2016-01-20 08:32:08 -0800313 if (transport == nullptr && announce) {
314 fprintf(stderr, "error: %s\n", error.c_str());
315 }
316 } else {
317 transport = usb_open(match_fastboot);
318 }
319
320 if (transport != nullptr) {
321 return transport;
322 }
323
David Pursell0b156632015-10-30 11:22:01 -0700324 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800325 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700326 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800328 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 }
330}
331
Elliott Hughesfc797672015-04-07 20:12:50 -0700332static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 // We don't actually open a USB device here,
334 // just getting our callback called so we can
335 // list all the connected devices.
336 usb_open(list_devices_callback);
337}
338
Elliott Hughesd6365a72017-05-08 18:04:49 -0700339static void syntax_error(const char* fmt, ...) {
340 fprintf(stderr, "fastboot: usage: ");
341
342 va_list ap;
343 va_start(ap, fmt);
344 vfprintf(stderr, fmt, ap);
345 va_end(ap);
346
347 fprintf(stderr, "\n");
348 exit(1);
349}
350
351static int show_help() {
352 // clang-format off
353 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700354// 1 2 3 4 5 6 7 8
355// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700356 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800357 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700358 "flashing:\n"
359 " update ZIP Flash all partitions from an update.zip package.\n"
360 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
361 " On A/B devices, flashed slot is set as active.\n"
362 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700363 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
364 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700365 "\n"
366 "basics:\n"
367 " devices [-l] List devices in bootloader (-l: with device paths).\n"
368 " getvar NAME Display given bootloader variable.\n"
369 " reboot [bootloader] Reboot device.\n"
370 "\n"
371 "locking/unlocking:\n"
372 " flashing lock|unlock Lock/unlock partitions for flashing\n"
373 " flashing lock_critical|unlock_critical\n"
374 " Lock/unlock 'critical' bootloader partitions.\n"
375 " flashing get_unlock_ability\n"
376 " Check whether unlocking is allowed (1) or not(0).\n"
377 "\n"
378 "advanced:\n"
379 " erase PARTITION Erase a flash partition.\n"
380 " format[:FS_TYPE[:SIZE]] PARTITION\n"
381 " Format a flash partition.\n"
382 " set_active SLOT Set the active slot.\n"
383 " oem [COMMAND...] Execute OEM-specific command.\n"
384 "\n"
385 "boot image:\n"
386 " boot KERNEL [RAMDISK [SECOND]]\n"
387 " Download and boot kernel from RAM.\n"
388 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
389 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700390 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700391 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
392 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
393 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
394 " --tags-offset Set tags offset (default: 0x00000100).\n"
395 " --page-size BYTES Set flash page size (default: 2048).\n"
396 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700397 " --os-version MAJOR[.MINOR[.PATCH]]\n"
398 " Set boot image OS version (default: 0.0.0).\n"
399 " --os-patch-level YYYY-MM-DD\n"
400 " Set boot image OS security patch level.\n"
401 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700402 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700403 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700404 //" continue Continue with autoboot.\n"
405 //"\n"
406 "Android Things:\n"
407 " stage IN_FILE Sends given file to stage for the next command.\n"
408 " 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 -0800409 "\n"
410 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700411 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700412 " -s SERIAL Specify a USB device.\n"
413 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700414 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700415 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
416 " non-current slot (default: current active slot).\n"
417 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
418 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
419 " --skip-reboot Don't reboot device after flashing.\n"
420 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
421 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000422#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700423 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000424#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700425 // TODO: remove --unbuffered?
426 " --unbuffered Don't buffer input or output.\n"
427 " --verbose, -v Verbose output.\n"
428 " --version Display version.\n"
429 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700431 // clang-format off
432 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000434
Tom Cherrydfd85df2018-09-20 14:45:05 -0700435static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
436 const std::string& second_stage) {
437 std::vector<char> kernel_data;
438 if (!ReadFileToVector(kernel, &kernel_data)) {
439 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
440 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800441
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700443 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800444 die("cannot load '%s': too short", kernel.c_str());
445 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700446 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700447 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700448 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700449 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800450
Elliott Hughes4089d342017-10-27 14:21:12 -0700451 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800452
Tom Cherrydfd85df2018-09-20 14:45:05 -0700453 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 }
455
Tom Cherrydfd85df2018-09-20 14:45:05 -0700456 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700457 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700458 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
459 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
460 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461 }
462
Tom Cherrydfd85df2018-09-20 14:45:05 -0700463 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700464 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700465 if (!ReadFileToVector(second_stage, &second_stage_data)) {
466 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
467 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200468 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700470
Tom Cherrydfd85df2018-09-20 14:45:05 -0700471 std::vector<char> out;
472 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
473 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800474
Tom Cherrydfd85df2018-09-20 14:45:05 -0700475 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
476 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
477
478 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479}
480
Tom Cherrydfd85df2018-09-20 14:45:05 -0700481static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
482 std::vector<char>* out) {
483 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700484 ZipEntry zip_entry;
485 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700486 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
487 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 }
489
Tom Cherrydfd85df2018-09-20 14:45:05 -0700490 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491
Tom Cherrydfd85df2018-09-20 14:45:05 -0700492 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
493 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494
Tom Cherrydfd85df2018-09-20 14:45:05 -0700495 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
496 out->size());
497 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000498
Tom Cherrydfd85df2018-09-20 14:45:05 -0700499 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500}
501
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700502#if defined(_WIN32)
503
504// TODO: move this to somewhere it can be shared.
505
506#include <windows.h>
507
508// Windows' tmpfile(3) requires administrator rights because
509// it creates temporary files in the root directory.
510static FILE* win32_tmpfile() {
511 char temp_path[PATH_MAX];
512 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
513 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700514 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700515 }
516
517 char filename[PATH_MAX];
518 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700519 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700520 }
521
522 return fopen(filename, "w+bTD");
523}
524
525#define tmpfile win32_tmpfile
526
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000527static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700528 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000529}
530
Elliott Hughes855cdf82018-04-02 14:24:03 -0700531static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700532 // TODO: reimplement to avoid leaking a FILE*.
533 return fileno(tmpfile());
534}
535
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000536#else
537
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700538static std::string make_temporary_template() {
539 const char* tmpdir = getenv("TMPDIR");
540 if (tmpdir == nullptr) tmpdir = P_tmpdir;
541 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
542}
543
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700545 std::string result(make_temporary_template());
546 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700547 die("unable to create temporary directory with template %s: %s",
548 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000549 }
550 return result;
551}
552
Elliott Hughes855cdf82018-04-02 14:24:03 -0700553static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700554 std::string path_template(make_temporary_template());
555 int fd = mkstemp(&path_template[0]);
556 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700557 die("failed to create temporary file for %s with template %s: %s\n",
558 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700559 }
560 unlink(path_template.c_str());
561 return fd;
562}
563
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700564#endif
565
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000566static std::string create_fbemarker_tmpdir() {
567 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000568 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
569 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
570 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700571 die("unable to create FBE marker file %s locally: %s",
572 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000573 }
574 close(fd);
575 return dir;
576}
577
578static void delete_fbemarker_tmpdir(const std::string& dir) {
579 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
580 if (unlink(marker_file.c_str()) == -1) {
581 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
582 marker_file.c_str(), errno, strerror(errno));
583 return;
584 }
585 if (rmdir(dir.c_str()) == -1) {
586 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
587 dir.c_str(), errno, strerror(errno));
588 return;
589 }
590}
591
Elliott Hughes45964a82017-05-03 22:43:23 -0700592static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700593 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700594
Yusuke Sato07447542015-06-25 14:39:19 -0700595 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700596 ZipEntry zip_entry;
597 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
598 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700599 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000600 return -1;
601 }
602
Elliott Hughes23af1122017-11-10 08:42:17 -0800603 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700604 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800605 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700606 int error = ExtractEntryToFile(zip, &zip_entry, fd);
607 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800608 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700609 }
610
Elliott Hughes4089d342017-10-27 14:21:12 -0700611 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800612 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700613 }
614
Elliott Hughes23af1122017-11-10 08:42:17 -0800615 fprintf(stderr, " took %.3fs\n", now() - start);
616
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700617 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700618}
619
Tom Cherry4aa60b32018-09-05 15:11:44 -0700620static void CheckRequirement(const std::string& cur_product, const std::string& var,
621 const std::string& product, bool invert,
622 const std::vector<std::string>& options) {
623 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700624
Tom Cherry4aa60b32018-09-05 15:11:44 -0700625 double start = now();
626
627 if (!product.empty()) {
628 if (product != cur_product) {
629 double split = now();
630 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
631 cur_product.c_str(), product.c_str(), (split - start));
632 return;
633 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700635
636 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700637 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700638 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700639 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
640 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700641 die("requirements not met!");
642 }
643
644 bool match = false;
645 for (const auto& option : options) {
646 if (option == var_value || (option.back() == '*' &&
647 !var_value.compare(0, option.length() - 1, option, 0,
648 option.length() - 1))) {
649 match = true;
650 break;
651 }
652 }
653
654 if (invert) {
655 match = !match;
656 }
657
658 if (match) {
659 double split = now();
660 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
661 return;
662 }
663
664 fprintf(stderr, "FAILED\n\n");
665 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
666 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
667 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
668 fprintf(stderr, " or '%s'", it->c_str());
669 }
670 fprintf(stderr, ".\n\n");
671 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672}
673
Tom Cherry4aa60b32018-09-05 15:11:44 -0700674bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
675 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700676 // "require product=alpha|beta|gamma"
677 // "require version-bootloader=1234"
678 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
679 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700680 *product = "";
681 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700682
Tom Cherry4aa60b32018-09-05 15:11:44 -0700683 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
684 auto require_product_regex =
685 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
686 std::smatch match_results;
687
688 if (std::regex_match(line, match_results, require_reject_regex)) {
689 *invert = Trim(match_results[1]) == "reject";
690 } else if (std::regex_match(line, match_results, require_product_regex)) {
691 *product = match_results[1];
692 } else {
693 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694 }
695
Tom Cherry4aa60b32018-09-05 15:11:44 -0700696 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700697 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700698 if (*name == "board") {
699 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700 }
701
Tom Cherry4aa60b32018-09-05 15:11:44 -0700702 auto raw_options = Split(match_results[3], "|");
703 for (const auto& option : raw_options) {
704 auto trimmed_option = Trim(option);
705 options->emplace_back(trimmed_option);
706 }
707
708 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709}
710
Tom Cherry4aa60b32018-09-05 15:11:44 -0700711// "require partition-exists=x" is a special case, added because of the trouble we had when
712// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
713// missing out new partitions. A device with new partitions can use "partition-exists" to
714// override the fields `optional_if_no_image` in the `images` array.
715static void HandlePartitionExists(const std::vector<std::string>& options) {
716 const std::string& partition_name = options[0];
717 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700718 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700719 (has_slot != "yes" && has_slot != "no")) {
720 die("device doesn't have required partition %s!", partition_name.c_str());
721 }
722 bool known_partition = false;
723 for (size_t i = 0; i < arraysize(images); ++i) {
724 if (images[i].nickname && images[i].nickname == partition_name) {
725 images[i].optional_if_no_image = false;
726 known_partition = true;
727 }
728 }
729 if (!known_partition) {
730 die("device requires partition %s which is not known to this version of fastboot",
731 partition_name.c_str());
732 }
733}
734
735static void CheckRequirements(const std::string& data) {
736 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700737 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
738 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700739 }
740
741 auto lines = Split(data, "\n");
742 for (const auto& line : lines) {
743 if (line.empty()) {
744 continue;
745 }
746
747 std::string name;
748 std::string product;
749 bool invert;
750 std::vector<std::string> options;
751
752 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
753 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
754 continue;
755 }
756 if (name == "partition-exists") {
757 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800758 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700759 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800760 }
761 }
762}
763
Tom Cherry9027af02018-09-24 15:48:09 -0700764static void DisplayVarOrError(const std::string& label, const std::string& var) {
765 std::string value;
766
767 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
768 Status("getvar:" + var);
769 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
770 return;
771 }
772 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
773}
774
775static void DumpInfo() {
776 fprintf(stderr, "--------------------------------------------\n");
777 DisplayVarOrError("Bootloader Version...", "version-bootloader");
778 DisplayVarOrError("Baseband Version.....", "version-baseband");
779 DisplayVarOrError("Serial Number........", "serialno");
780 fprintf(stderr, "--------------------------------------------\n");
781
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782}
783
Tao Bao41cf35f2018-04-24 10:54:21 -0700784static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700785 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700786 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700787
Tao Bao41cf35f2018-04-24 10:54:21 -0700788 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
789 die("invalid max size %" PRId64, max_size);
790 }
791
Elliott Hughesfc797672015-04-07 20:12:50 -0700792 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700793 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700794
Elliott Hughes253c18d2015-03-18 22:47:09 -0700795 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700796 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700797
798 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700799 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700800
801 return out_s;
802}
803
Aaron Wisnerdb511202018-06-26 15:38:35 -0500804static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700805 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700806 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700807 max_download_size.empty()) {
808 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700809 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700810 }
811
Elliott Hughes77c0e662015-11-02 15:51:12 -0800812 // Some bootloaders (angler, for example) send spurious whitespace too.
813 max_download_size = android::base::Trim(max_download_size);
814
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700815 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700816 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800817 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700818 return 0;
819 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700820 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700821 return limit;
822}
823
Aaron Wisnerdb511202018-06-26 15:38:35 -0500824static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700825 int64_t limit = sparse_limit;
826 if (limit == 0) {
827 // Unlimited, so see what the target device's limit is.
828 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700829 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500830 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700831 }
832 if (target_sparse_limit > 0) {
833 limit = target_sparse_limit;
834 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700835 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700836 }
837 }
838
839 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500840 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700841 }
842
843 return 0;
844}
845
Aaron Wisnerdb511202018-06-26 15:38:35 -0500846static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700847 int64_t sz = get_file_size(fd);
848 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700849 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700850 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700851
David Anderson32e376f2018-08-16 13:43:11 -0700852 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
853 buf->image_size = sparse_file_len(s, false, false);
854 sparse_file_destroy(s);
855 } else {
856 buf->image_size = sz;
857 }
858
Elliott Hughesfc797672015-04-07 20:12:50 -0700859 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500860 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700861 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700862 sparse_file** s = load_sparse_files(fd, limit);
863 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700864 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700865 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700866 buf->type = FB_BUFFER_SPARSE;
867 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700868 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500869 buf->type = FB_BUFFER_FD;
870 buf->data = nullptr;
871 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000872 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700873 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700874
Elliott Hughes53ec4952016-05-11 12:39:27 -0700875 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700876}
877
Aaron Wisnerdb511202018-06-26 15:38:35 -0500878static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500879 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
880
Elliott Hughes53ec4952016-05-11 12:39:27 -0700881 if (fd == -1) {
882 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700883 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500884
885 struct stat s;
886 if (fstat(fd, &s)) {
887 return false;
888 }
889 if (!S_ISREG(s.st_mode)) {
890 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
891 return false;
892 }
893
Aaron Wisnerdb511202018-06-26 15:38:35 -0500894 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700895}
896
David Zeuthenb6ea4352017-08-07 14:29:26 -0400897static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
898 // Buffer needs to be at least the size of the VBMeta struct which
899 // is 256 bytes.
900 if (buf->sz < 256) {
901 return;
902 }
903
Elliott Hughes855cdf82018-04-02 14:24:03 -0700904 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400905
906 std::string data;
907 if (!android::base::ReadFdToString(buf->fd, &data)) {
908 die("Failed reading from vbmeta");
909 }
910
911 // There's a 32-bit big endian |flags| field at offset 120 where
912 // bit 0 corresponds to disable-verity and bit 1 corresponds to
913 // disable-verification.
914 //
915 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
916 // the VBMeta struct.
917 if (g_disable_verity) {
918 data[123] |= 0x01;
919 }
920 if (g_disable_verification) {
921 data[123] |= 0x02;
922 }
923
924 if (!android::base::WriteStringToFd(data, fd)) {
925 die("Failed writing to modified vbmeta");
926 }
927 close(buf->fd);
928 buf->fd = fd;
929 lseek(fd, 0, SEEK_SET);
930}
931
Elliott Hughes5620d222018-03-28 08:20:00 -0700932static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700933{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700934 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700935
David Zeuthenb6ea4352017-08-07 14:29:26 -0400936 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
937 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700938 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400939 rewrite_vbmeta_buffer(buf);
940 }
941
Rom Lemarchand622810c2013-06-28 09:54:59 -0700942 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800943 case FB_BUFFER_SPARSE: {
944 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700945 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700946 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700947 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800948 sparse_files.emplace_back(*s, sz);
949 ++s;
950 }
951
952 for (size_t i = 0; i < sparse_files.size(); ++i) {
953 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700954 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700955 }
956 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800957 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500958 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700959 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700960 break;
961 default:
962 die("unknown buffer type: %d", buf->type);
963 }
964}
965
Aaron Wisnerdb511202018-06-26 15:38:35 -0500966static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700967 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700968 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700969 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700970}
971
Aaron Wisnerdb511202018-06-26 15:38:35 -0500972static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700973 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700974 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700975 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
976 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700977 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700978 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 return count;
980}
981
Aaron Wisnerdb511202018-06-26 15:38:35 -0500982static bool supports_AB() {
983 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700984}
985
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700986// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987static std::string get_other_slot(const std::string& current_slot, int count) {
988 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700989
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 char next = (current_slot[0] - 'a' + 1)%count + 'a';
991 return std::string(1, next);
992}
993
Aaron Wisnerdb511202018-06-26 15:38:35 -0500994static std::string get_other_slot(const std::string& current_slot) {
995 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996}
997
Aaron Wisnerdb511202018-06-26 15:38:35 -0500998static std::string get_other_slot(int count) {
999 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000}
1001
Aaron Wisnerdb511202018-06-26 15:38:35 -05001002static std::string get_other_slot() {
1003 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -07001004}
1005
Aaron Wisnerdb511202018-06-26 15:38:35 -05001006static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001008 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001009 if (allow_all) {
1010 return "all";
1011 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001012 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013 if (count > 0) {
1014 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001015 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001016 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001017 }
1018 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001019 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001020
Aaron Wisnerdb511202018-06-26 15:38:35 -05001021 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001022 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001023
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001025 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001026 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001027 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001028 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001029 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001030 }
1031
Daniel Rosenberg80919472016-06-30 19:25:31 -07001032 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1033
1034 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1035 for (int i=0; i<count; i++) {
1036 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001039 exit(1);
1040}
1041
Aaron Wisnerdb511202018-06-26 15:38:35 -05001042static std::string verify_slot(const std::string& slot) {
1043 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001044}
1045
Aaron Wisnerdb511202018-06-26 15:38:35 -05001046static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001047 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001048 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001049 std::string current_slot;
1050
Tom Cherry9027af02018-09-24 15:48:09 -07001051 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001052 /* If has-slot is not supported, the answer is no. */
1053 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001054 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001055 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001056 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001057 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001058 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001059 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001060 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001061 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001062 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001063 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001064 }
1065 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001066 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001067 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001068 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001069 }
1070 func(part);
1071 }
1072}
1073
David Pursell0b156632015-10-30 11:22:01 -07001074/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1075 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1076 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1077 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001078 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001079static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001080 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001081 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001082
Daniel Rosenberg80919472016-06-30 19:25:31 -07001083 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001084 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001085 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001087 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001088 for (int i=0; i < get_slot_count(); i++) {
1089 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001090 }
1091 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001092 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001093 }
1094 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001095 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001096 }
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 }
1105 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001106}
1107
Daniel Rosenberg13454092016-06-27 19:43:11 -07001108// Sets slot_override as the active slot. If slot_override is blank,
1109// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001110static void set_active(const std::string& slot_override) {
1111 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001112
Daniel Rosenberg80919472016-06-30 19:25:31 -07001113 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001114 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001115 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001116 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001117 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001118 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001119 }
1120 }
1121}
1122
David Anderson32e376f2018-08-16 13:43:11 -07001123static bool is_userspace_fastboot() {
1124 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001125 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001126}
1127
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001128static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1129 std::string has_slot;
1130 std::string partition_name = partition;
1131
Tom Cherry9027af02018-09-24 15:48:09 -07001132 if (fb->GetVar("has-slot:" + partition, &has_slot) == fastboot::SUCCESS && has_slot == "yes") {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001133 if (slot == "") {
1134 std::string current_slot = get_current_slot();
1135 if (current_slot == "") {
1136 die("Failed to identify current slot");
1137 }
1138 partition_name += "_" + current_slot;
1139 } else {
1140 partition_name += "_" + slot;
1141 }
1142 }
1143 std::string partition_size;
Tom Cherry9027af02018-09-24 15:48:09 -07001144 return fb->GetVar("partition-size:" + partition_name, &partition_size) == fastboot::SUCCESS;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001145}
1146
David Anderson32e376f2018-08-16 13:43:11 -07001147static bool is_logical(const std::string& partition) {
1148 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001149 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001150}
1151
David Anderson1d887432018-08-27 16:47:32 -07001152static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001153 fb->RebootTo("fastboot");
1154
1155 auto* old_transport = fb->set_transport(nullptr);
1156 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001157
1158 // Give the current connection time to close.
1159 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1160
Tom Cherry9027af02018-09-24 15:48:09 -07001161 fb->set_transport(open_device());
David Anderson1d887432018-08-27 16:47:32 -07001162}
1163
David Andersoncf444f32018-08-29 14:15:49 -07001164class ImageSource {
1165 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001166 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001167 virtual int OpenFile(const std::string& name) const = 0;
1168};
David Anderson32e376f2018-08-16 13:43:11 -07001169
David Andersoncf444f32018-08-29 14:15:49 -07001170class FlashAllTool {
1171 public:
1172 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001173
David Andersoncf444f32018-08-29 14:15:49 -07001174 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001175
David Andersoncf444f32018-08-29 14:15:49 -07001176 private:
1177 void CheckRequirements();
1178 void DetermineSecondarySlot();
1179 void CollectImages();
1180 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1181 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1182 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001183
David Andersoncf444f32018-08-29 14:15:49 -07001184 const ImageSource& source_;
1185 std::string slot_override_;
1186 bool skip_secondary_;
1187 bool wipe_;
1188 std::string secondary_slot_;
1189 std::vector<std::pair<const Image*, std::string>> boot_images_;
1190 std::vector<std::pair<const Image*, std::string>> os_images_;
1191};
1192
1193FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1194 : source_(source),
1195 slot_override_(slot_override),
1196 skip_secondary_(skip_secondary),
1197 wipe_(wipe)
1198{
David Anderson32e376f2018-08-16 13:43:11 -07001199}
1200
David Andersoncf444f32018-08-29 14:15:49 -07001201void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001202 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001203 CheckRequirements();
1204 DetermineSecondarySlot();
1205 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001206
David Anderson95d40932018-08-28 12:14:33 -07001207 // First flash boot partitions. We allow this to happen either in userspace
1208 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001209 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001210
1211 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001212 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001213
1214 // Resize any logical partition to 0, so each partition is reset to 0
1215 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001216 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001217 auto resize_partition = [](const std::string& partition) -> void {
1218 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001219 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001220 }
1221 };
David Anderson95d40932018-08-28 12:14:33 -07001222 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001223 }
1224
David Anderson95d40932018-08-28 12:14:33 -07001225 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001226 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001227
David Andersoncf444f32018-08-29 14:15:49 -07001228 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001229 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001230 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001231 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001232 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001233}
1234
David Andersoncf444f32018-08-29 14:15:49 -07001235void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001236 std::vector<char> contents;
1237 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001238 die("could not read android-info.txt");
1239 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001240 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001241}
1242
1243void FlashAllTool::DetermineSecondarySlot() {
1244 if (skip_secondary_) {
1245 return;
1246 }
1247 if (slot_override_ != "") {
1248 secondary_slot_ = get_other_slot(slot_override_);
1249 } else {
1250 secondary_slot_ = get_other_slot();
1251 }
1252 if (secondary_slot_ == "") {
1253 if (supports_AB()) {
1254 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1255 }
1256 skip_secondary_ = true;
1257 }
1258}
1259
1260void FlashAllTool::CollectImages() {
1261 for (size_t i = 0; i < arraysize(images); ++i) {
1262 std::string slot = slot_override_;
1263 if (images[i].IsSecondary()) {
1264 if (skip_secondary_) {
1265 continue;
1266 }
1267 slot = secondary_slot_;
1268 }
1269 if (images[i].type == ImageType::BootCritical) {
1270 boot_images_.emplace_back(&images[i], slot);
1271 } else if (images[i].type == ImageType::Normal) {
1272 os_images_.emplace_back(&images[i], slot);
1273 }
1274 }
1275}
1276
1277void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1278 for (const auto& [image, slot] : images) {
1279 fastboot_buffer buf;
1280 int fd = source_.OpenFile(image->img_name);
1281 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1282 if (image->optional_if_no_image) {
1283 continue;
1284 }
1285 die("could not load '%s': %s", image->img_name, strerror(errno));
1286 }
1287 FlashImage(*image, slot, &buf);
1288 }
1289}
1290
1291void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1292 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001293 std::vector<char> signature_data;
1294 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001295 fb->Download("signature", signature_data);
1296 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001297 }
1298
1299 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001300 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001301 }
1302 flash_buf(partition_name.c_str(), buf);
1303 };
1304 do_for_partitions(image.part_name, slot, flash, false);
1305}
1306
1307void FlashAllTool::UpdateSuperPartition() {
1308 if (!if_partition_exists("super", "")) {
1309 return;
1310 }
1311
1312 int fd = source_.OpenFile("super_empty.img");
1313 if (fd < 0) {
1314 return;
1315 }
1316 if (!is_userspace_fastboot()) {
1317 reboot_to_userspace_fastboot();
1318 }
Tom Cherry9027af02018-09-24 15:48:09 -07001319 fb->Download("super", fd, get_file_size(fd));
David Andersoncf444f32018-08-29 14:15:49 -07001320
1321 std::string command = "update-super:super";
1322 if (wipe_) {
1323 command += ":wipe";
1324 }
Tom Cherry9027af02018-09-24 15:48:09 -07001325 fb->RawCommand(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001326}
1327
1328class ZipImageSource final : public ImageSource {
1329 public:
1330 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001331 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001332 int OpenFile(const std::string& name) const override;
1333
1334 private:
1335 ZipArchiveHandle zip_;
1336};
1337
Tom Cherrydfd85df2018-09-20 14:45:05 -07001338bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1339 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001340}
1341
1342int ZipImageSource::OpenFile(const std::string& name) const {
1343 return unzip_to_file(zip_, name.c_str());
1344}
1345
1346static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001347 ZipArchiveHandle zip;
1348 int error = OpenArchive(filename, &zip);
1349 if (error != 0) {
1350 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1351 }
1352
1353 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1354 tool.Flash();
1355
1356 CloseArchive(zip);
1357}
1358
1359class LocalImageSource final : public ImageSource {
1360 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001361 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001362 int OpenFile(const std::string& name) const override;
1363};
1364
Tom Cherrydfd85df2018-09-20 14:45:05 -07001365bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001366 auto path = find_item_given_name(name);
1367 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001368 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001369 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001370 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001371}
1372
1373int LocalImageSource::OpenFile(const std::string& name) const {
1374 auto path = find_item_given_name(name);
1375 return open(path.c_str(), O_RDONLY);
1376}
1377
1378static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001379 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1380 tool.Flash();
1381}
1382
Elliott Hughesd6365a72017-05-08 18:04:49 -07001383static std::string next_arg(std::vector<std::string>* args) {
1384 if (args->empty()) syntax_error("expected argument");
1385 std::string result = args->front();
1386 args->erase(args->begin());
1387 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001388}
1389
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001390static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001391 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001392
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001393 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001394 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001395 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001396 }
Tom Cherry9027af02018-09-24 15:48:09 -07001397 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001398}
1399
Connor O'Brience16a8a2017-02-06 14:39:31 -08001400static std::string fb_fix_numeric_var(std::string var) {
1401 // Some bootloaders (angler, for example), send spurious leading whitespace.
1402 var = android::base::Trim(var);
1403 // Some bootloaders (hammerhead, for example) use implicit hex.
1404 // This code used to use strtol with base 16.
1405 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1406 return var;
1407}
1408
Aaron Wisnerdb511202018-06-26 15:38:35 -05001409static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001410 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001411 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001412 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001413 return 0;
1414 }
1415 sizeString = fb_fix_numeric_var(sizeString);
1416
1417 unsigned size;
1418 if (!android::base::ParseUint(sizeString, &size)) {
1419 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1420 return 0;
1421 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001422 if ((size & (size - 1)) != 0) {
1423 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001424 return 0;
1425 }
1426 return size;
1427}
1428
Aaron Wisnerdb511202018-06-26 15:38:35 -05001429static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001430 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001431 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001432 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001433 std::string partition_type, partition_size;
1434
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001435 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001436 const char* errMsg = nullptr;
1437 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001438 TemporaryFile output;
1439 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001441 unsigned int limit = INT_MAX;
1442 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001443 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001444 }
1445 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001447 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001448
Tom Cherry9027af02018-09-24 15:48:09 -07001449 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001450 errMsg = "Can't determine partition type.\n";
1451 goto failed;
1452 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001453 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001454 if (partition_type != type_override) {
1455 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001456 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001457 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001458 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001459 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001460
Tom Cherry9027af02018-09-24 15:48:09 -07001461 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462 errMsg = "Unable to get partition size\n";
1463 goto failed;
1464 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001465 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001466 if (partition_size != size_override) {
1467 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001468 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001469 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001470 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001471 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001472 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001473
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001474 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001475 if (!gen) {
1476 if (skip_if_not_supported) {
1477 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001478 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001479 return;
1480 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001481 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1482 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001483 return;
1484 }
1485
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001486 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001487 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001488 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1489 return;
1490 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001491
Connor O'Brience16a8a2017-02-06 14:39:31 -08001492 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001493 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1494 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001495
Jin Qian4a335822017-04-18 16:23:18 -07001496 if (fs_generator_generate(gen, output.path, size, initial_dir,
1497 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001498 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001499 return;
1500 }
1501
Jin Qian4a335822017-04-18 16:23:18 -07001502 fd.reset(open(output.path, O_RDONLY));
1503 if (fd == -1) {
1504 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1505 return;
1506 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001507 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001508 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001509 return;
1510 }
1511 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001512 return;
1513
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001514failed:
1515 if (skip_if_not_supported) {
1516 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001517 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001518 }
Tom Cherry9027af02018-09-24 15:48:09 -07001519 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001520}
1521
Aaron Wisnerdb511202018-06-26 15:38:35 -05001522int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001523 bool wants_wipe = false;
1524 bool wants_reboot = false;
1525 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001526 bool wants_reboot_recovery = false;
1527 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001528 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001529 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001530 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001531 bool set_fbe_marker = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001532 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001533 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001534 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001535
Elliott Hughes577e8b42018-04-05 16:12:47 -07001536 g_boot_img_hdr.kernel_addr = 0x00008000;
1537 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1538 g_boot_img_hdr.second_addr = 0x00f00000;
1539 g_boot_img_hdr.tags_addr = 0x00000100;
1540 g_boot_img_hdr.page_size = 2048;
1541
JP Abgrall7b8970c2013-03-07 17:06:41 -08001542 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001543 {"base", required_argument, 0, 0},
1544 {"cmdline", required_argument, 0, 0},
1545 {"disable-verification", no_argument, 0, 0},
1546 {"disable-verity", no_argument, 0, 0},
1547 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001548 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001549 {"kernel-offset", required_argument, 0, 0},
1550 {"os-patch-level", required_argument, 0, 0},
1551 {"os-version", required_argument, 0, 0},
1552 {"page-size", required_argument, 0, 0},
1553 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001554 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001555 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001556 {"skip-secondary", no_argument, 0, 0},
1557 {"slot", required_argument, 0, 0},
1558 {"tags-offset", required_argument, 0, 0},
1559 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001560 {"verbose", no_argument, 0, 'v'},
1561 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001562#if !defined(_WIN32)
1563 {"wipe-and-use-fbe", no_argument, 0, 0},
1564#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001565 {0, 0, 0, 0}
1566 };
Colin Cross8879f982012-05-22 17:53:34 -07001567
1568 serial = getenv("ANDROID_SERIAL");
1569
Elliott Hughes577e8b42018-04-05 16:12:47 -07001570 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001571 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001572 if (c == 0) {
1573 std::string name{longopts[longindex].name};
1574 if (name == "base") {
1575 g_base_addr = strtoul(optarg, 0, 16);
1576 } else if (name == "cmdline") {
1577 g_cmdline = optarg;
1578 } else if (name == "disable-verification") {
1579 g_disable_verification = true;
1580 } else if (name == "disable-verity") {
1581 g_disable_verity = true;
1582 } else if (name == "header-version") {
1583 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1584 } else if (name == "kernel-offset") {
1585 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1586 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001587 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001588 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001589 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001590 } else if (name == "page-size") {
1591 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1592 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1593 } else if (name == "ramdisk-offset") {
1594 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1595 } else if (name == "skip-reboot") {
1596 skip_reboot = true;
1597 } else if (name == "skip-secondary") {
1598 skip_secondary = true;
1599 } else if (name == "slot") {
1600 slot_override = optarg;
1601 } else if (name == "tags-offset") {
1602 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1603 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001604 setvbuf(stdout, nullptr, _IONBF, 0);
1605 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001606 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001607 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001608 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001609 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001610#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001611 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001612 wants_wipe = true;
1613 set_fbe_marker = true;
1614#endif
1615 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001616 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001617 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001618 } else {
1619 switch (c) {
1620 case 'a':
1621 wants_set_active = true;
1622 if (optarg) next_active = optarg;
1623 break;
1624 case 'h':
1625 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001626 case 'l':
1627 g_long_listing = true;
1628 break;
1629 case 's':
1630 serial = optarg;
1631 break;
1632 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001633 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1634 die("invalid sparse limit %s", optarg);
1635 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001636 break;
1637 case 'v':
1638 set_verbose();
1639 break;
1640 case 'w':
1641 wants_wipe = true;
1642 break;
1643 case '?':
1644 return 1;
1645 default:
1646 abort();
1647 }
Colin Cross8879f982012-05-22 17:53:34 -07001648 }
1649 }
1650
1651 argc -= optind;
1652 argv += optind;
1653
Elliott Hughesd6365a72017-05-08 18:04:49 -07001654 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001655
Colin Cross8fb6e062012-07-24 16:36:41 -07001656 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001657 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001658 return 0;
1659 }
1660
Colin Crossc7b75dc2012-08-29 18:17:06 -07001661 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001662 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001663 }
1664
David Pursell0b156632015-10-30 11:22:01 -07001665 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001666 if (transport == nullptr) {
1667 return 1;
1668 }
Tom Cherry9027af02018-09-24 15:48:09 -07001669 fastboot::DriverCallbacks driver_callbacks = {
1670 .prolog = Status,
1671 .epilog = Epilog,
1672 .info = InfoMessage,
1673 };
1674 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1675 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001676
Elliott Hughes5620d222018-03-28 08:20:00 -07001677 const double start = now();
1678
Aaron Wisnerdb511202018-06-26 15:38:35 -05001679 if (slot_override != "") slot_override = verify_slot(slot_override);
1680 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001681
1682 if (wants_set_active) {
1683 if (next_active == "") {
1684 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001685 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001686 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001687 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001688 } else {
1689 wants_set_active = false;
1690 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001691 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001692 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001693 }
1694 }
1695 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001696
Elliott Hughesd6365a72017-05-08 18:04:49 -07001697 std::vector<std::string> args(argv, argv + argc);
1698 while (!args.empty()) {
1699 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001700
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001701 if (command == FB_CMD_GETVAR) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001702 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001703 DisplayVarOrError(variable, variable);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001704 } else if (command == FB_CMD_ERASE) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001705 std::string partition = next_arg(&args);
1706 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001707 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001708 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001709 fs_get_generator(partition_type) != nullptr) {
1710 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1711 partition_type.c_str());
1712 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001713
Tom Cherry9027af02018-09-24 15:48:09 -07001714 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001715 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001716 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001717 } else if (android::base::StartsWith(command, "format")) {
1718 // Parsing for: "format[:[type][:[size]]]"
1719 // Some valid things:
1720 // - select only the size, and leave default fs type:
1721 // format::0x4000000 userdata
1722 // - default fs type and size:
1723 // format userdata
1724 // format:: userdata
1725 std::vector<std::string> pieces = android::base::Split(command, ":");
1726 std::string type_override;
1727 if (pieces.size() > 1) type_override = pieces[1].c_str();
1728 std::string size_override;
1729 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001730
Elliott Hughesd6365a72017-05-08 18:04:49 -07001731 std::string partition = next_arg(&args);
1732
1733 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001734 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001735 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001736 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001737 } else if (command == "signature") {
1738 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001739 std::vector<char> data;
1740 if (!ReadFileToVector(filename, &data)) {
1741 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1742 }
1743 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001744 fb->Download("signature", data);
1745 fb->RawCommand("signature", "installing signature");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001746 } else if (command == FB_CMD_REBOOT) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001747 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001748
1749 if (args.size() == 1) {
1750 std::string what = next_arg(&args);
1751 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001752 wants_reboot = false;
1753 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001754 } else if (what == "recovery") {
1755 wants_reboot = false;
1756 wants_reboot_recovery = true;
1757 } else if (what == "fastboot") {
1758 wants_reboot = false;
1759 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001760 } else {
1761 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001762 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001763
Elliott Hughesca85df02015-02-25 10:02:00 -08001764 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 if (!args.empty()) syntax_error("junk after reboot command");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001766 } else if (command == FB_CMD_REBOOT_BOOTLOADER) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001767 wants_reboot_bootloader = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001768 } else if (command == FB_CMD_REBOOT_RECOVERY) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001769 wants_reboot_recovery = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001770 } else if (command == FB_CMD_REBOOT_FASTBOOT) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001771 wants_reboot_fastboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001772 } else if (command == FB_CMD_CONTINUE) {
Tom Cherry9027af02018-09-24 15:48:09 -07001773 fb->Continue();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001774 } else if (command == FB_CMD_BOOT) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001775 std::string kernel = next_arg(&args);
1776 std::string ramdisk;
1777 if (!args.empty()) ramdisk = next_arg(&args);
1778 std::string second_stage;
1779 if (!args.empty()) second_stage = next_arg(&args);
1780
Tom Cherrydfd85df2018-09-20 14:45:05 -07001781 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001782 fb->Download("boot.img", data);
1783 fb->Boot();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001784 } else if (command == FB_CMD_FLASH) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001785 std::string pname = next_arg(&args);
1786
Elliott Hughes2810d002016-04-25 14:31:18 -07001787 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001788 if (!args.empty()) {
1789 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001790 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001791 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001792 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001793 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001794
1795 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001796 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001797 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001798 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001799 } else if (command == "flash:raw") {
1800 std::string partition = next_arg(&args);
1801 std::string kernel = next_arg(&args);
1802 std::string ramdisk;
1803 if (!args.empty()) ramdisk = next_arg(&args);
1804 std::string second_stage;
1805 if (!args.empty()) second_stage = next_arg(&args);
1806
Tom Cherrydfd85df2018-09-20 14:45:05 -07001807 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1808 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001809 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001810 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001811 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001812 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001813 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001814 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001815 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001816 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001817 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001818 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001819 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001820 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001821 bool slot_all = (slot_override == "all");
1822 if (slot_all) {
1823 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1824 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001825 std::string filename = "update.zip";
1826 if (!args.empty()) {
1827 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001828 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001829 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001830 wants_reboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001831 } else if (command == FB_CMD_SET_ACTIVE) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001832 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001833 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001834 } else if (command == "stage") {
1835 std::string filename = next_arg(&args);
1836
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001837 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001838 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001839 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001840 }
Tom Cherry9027af02018-09-24 15:48:09 -07001841 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001842 } else if (command == "get_staged") {
1843 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001844 fb->Upload(filename);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001845 } else if (command == FB_CMD_OEM) {
1846 do_oem_command(FB_CMD_OEM, &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001847 } else if (command == "flashing") {
1848 if (args.empty()) {
1849 syntax_error("missing 'flashing' command");
1850 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1851 args[0] == "unlock_critical" ||
1852 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001853 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001854 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001855 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001856 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001857 }
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001858 } else if (command == FB_CMD_CREATE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001859 std::string partition = next_arg(&args);
1860 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001861 fb->CreatePartition(partition, size);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001862 } else if (command == FB_CMD_DELETE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001863 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001864 fb->DeletePartition(partition);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001865 } else if (command == FB_CMD_RESIZE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001866 std::string partition = next_arg(&args);
1867 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001868 fb->ResizePartition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001869 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001870 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001871 }
1872 }
1873
1874 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001875 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1876 for (const auto& partition : partitions) {
1877 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001878 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1879 continue;
1880 }
Paul Crowley4d170062018-04-24 17:06:30 -07001881 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001882 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001883 if (partition == "userdata" && set_fbe_marker) {
1884 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1885 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001886 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001887 delete_fbemarker_tmpdir(initial_userdata_dir);
1888 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001889 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001890 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001891 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001892 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001893 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001894 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001895 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001896 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001897 fb->Reboot();
1898 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001899 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001900 fb->RebootTo("bootloader");
1901 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001902 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001903 fb->RebootTo("recovery");
1904 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001905 } else if (wants_reboot_fastboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001906 fb->RebootTo("fastboot");
1907 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001908 }
1909
Elliott Hughes5620d222018-03-28 08:20:00 -07001910 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001911
Tom Cherry9027af02018-09-24 15:48:09 -07001912 auto* old_transport = fb->set_transport(nullptr);
1913 delete old_transport;
1914
Tom Cherry11f12092018-08-29 21:36:28 -07001915 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001916}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001917
Aaron Wisnerdb511202018-06-26 15:38:35 -05001918void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001919 unsigned year, month, day;
1920 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1921 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1922 }
1923 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1924 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1925 hdr->SetOsPatchLevel(year, month);
1926}
1927
Aaron Wisnerdb511202018-06-26 15:38:35 -05001928void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001929 unsigned major = 0, minor = 0, patch = 0;
1930 std::vector<std::string> versions = android::base::Split(arg, ".");
1931 if (versions.size() < 1 || versions.size() > 3 ||
1932 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1933 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1934 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1935 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1936 syntax_error("bad OS version: %s", arg);
1937 }
1938 hdr->SetOsVersion(major, minor, patch);
1939}