blob: 59626507d1b2e09cca8c6641e8a21e2ababa0737 [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"
Elliott Hughes1b708d32015-12-11 19:07:01 -080067#include "diagnose_usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070068#include "fastboot_driver.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070069#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080070#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070071#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080072#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070073#include "usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070074#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Tom Cherrydfd85df2018-09-20 14:45:05 -070076using android::base::ReadFully;
Tom Cherry4aa60b32018-09-05 15:11:44 -070077using android::base::Split;
78using android::base::Trim;
Chris Fries0ea946c2017-04-12 10:25:57 -050079using android::base::unique_fd;
80
Colin Crossf8387882012-05-24 17:18:41 -070081#ifndef O_BINARY
82#define O_BINARY 0
83#endif
84
David Pursell2ec418a2016-01-20 08:32:08 -080085static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070086
Elliott Hughes577e8b42018-04-05 16:12:47 -070087static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050088// Don't resparse files in too-big chunks.
89// libsparse will support INT_MAX, but this results in large allocations, so
90// let's keep it at 1GB to avoid memory pressure on the host.
91static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070092static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070093static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094
Elliott Hughes577e8b42018-04-05 16:12:47 -070095static unsigned g_base_addr = 0x10000000;
96static boot_img_hdr_v1 g_boot_img_hdr = {};
97static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080098
David Zeuthenb6ea4352017-08-07 14:29:26 -040099static bool g_disable_verity = false;
100static bool g_disable_verification = false;
101
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000102static const std::string convert_fbe_marker_filename("convert_fbe");
103
Tom Cherry9027af02018-09-24 15:48:09 -0700104fastboot::FastBootDriver* fb = nullptr;
105
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500107 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108 FB_BUFFER_SPARSE,
109};
110
111struct fastboot_buffer {
112 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700113 void* data;
114 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500115 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700116 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700117};
118
David Anderson0debda02018-08-28 13:54:03 -0700119enum class ImageType {
120 // Must be flashed for device to boot into the kernel.
121 BootCritical,
122 // Normal partition to be flashed during "flashall".
123 Normal,
124 // Partition that is never flashed during "flashall".
125 Extra
126};
127
David Anderson32e376f2018-08-16 13:43:11 -0700128struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700129 const char* nickname;
130 const char* img_name;
131 const char* sig_name;
132 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700133 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700134 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700135 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700136};
137
138static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100139 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700140 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
141 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
142 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
143 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
144 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
145 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
146 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100147 { "product_services",
148 "product_services.img",
149 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700150 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700151 true, ImageType::Normal },
152 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
153 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
154 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
155 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
156 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
157 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson1109c922018-09-17 17:32:54 -0700158 { "vbmeta_mainline",
159 "vbmeta_mainline.img",
160 "vbmeta_mainline.sig",
161 "vbmeta_mainline",
162 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700163 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
164 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100165 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700166};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700167
David Anderson0debda02018-08-28 13:54:03 -0700168static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700169 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700170 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700171 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700172 }
David Anderson0debda02018-08-28 13:54:03 -0700173 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700174}
175
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700176static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700177 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700178 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700179 return find_item_given_name(images[i].img_name);
180 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 }
182
Elliott Hughesd6365a72017-05-08 18:04:49 -0700183 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700184 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185}
186
Tom Cherry9027af02018-09-24 15:48:09 -0700187double last_start_time;
188
189static void Status(const std::string& message) {
190 static constexpr char kStatusFormat[] = "%-50s ";
191 fprintf(stderr, kStatusFormat, message.c_str());
192 last_start_time = now();
193}
194
195static void Epilog(int status) {
196 if (status) {
197 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
198 die("Command failed");
199 } else {
200 double split = now();
201 fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
202 }
203}
204
205static void InfoMessage(const std::string& info) {
206 fprintf(stderr, "(bootloader) %s\n", info.c_str());
207}
208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int64_t get_file_size(int fd) {
210 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700211 if (fstat(fd, &sb) == -1) {
212 die("could not get file size");
213 }
214 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700215}
216
Tom Cherrydfd85df2018-09-20 14:45:05 -0700217bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
218 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219
Tom Cherrydfd85df2018-09-20 14:45:05 -0700220 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
221 if (fd == -1) {
222 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224
Tom Cherrydfd85df2018-09-20 14:45:05 -0700225 out->resize(get_file_size(fd));
226 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700227}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228
Elliott Hughesfc797672015-04-07 20:12:50 -0700229static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700230 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
231 return -1;
232 }
233
Scott Anderson13081c62012-04-06 12:39:30 -0700234 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700236 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
237 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 return 0;
239}
240
Elliott Hughesfc797672015-04-07 20:12:50 -0700241static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800242 return match_fastboot_with_serial(info, serial);
243}
244
Elliott Hughesfc797672015-04-07 20:12:50 -0700245static int list_devices_callback(usb_ifc_info* info) {
246 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800247 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700248 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800249 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700250 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 if (!serial[0]) {
252 serial = "????????????";
253 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700254 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700255 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800256 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700257 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800258 printf("%-22s fastboot", serial.c_str());
259 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700260 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800261 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262 }
263
264 return -1;
265}
266
David Pursell2ec418a2016-01-20 08:32:08 -0800267// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
268// a specific device, otherwise the first USB device found will be used.
269//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700270// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800271// Otherwise it blocks until the target is available.
272//
273// The returned Transport is a singleton, so multiple calls to this function will return the same
274// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700275static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800276 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277
David Pursell4601c972016-02-05 15:35:09 -0800278 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800279 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800280 int port = 0;
281 if (serial != nullptr) {
282 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800283
David Pursell4601c972016-02-05 15:35:09 -0800284 if (android::base::StartsWith(serial, "tcp:")) {
285 protocol = Socket::Protocol::kTcp;
286 port = tcp::kDefaultPort;
287 net_address = serial + strlen("tcp:");
288 } else if (android::base::StartsWith(serial, "udp:")) {
289 protocol = Socket::Protocol::kUdp;
290 port = udp::kDefaultPort;
291 net_address = serial + strlen("udp:");
292 }
293
294 if (net_address != nullptr) {
295 std::string error;
296 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700297 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800298 }
David Pursell2ec418a2016-01-20 08:32:08 -0800299 }
300 }
301
David Anderson1d887432018-08-27 16:47:32 -0700302 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800303 while (true) {
304 if (!host.empty()) {
305 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800306 if (protocol == Socket::Protocol::kTcp) {
307 transport = tcp::Connect(host, port, &error).release();
308 } else if (protocol == Socket::Protocol::kUdp) {
309 transport = udp::Connect(host, port, &error).release();
310 }
311
David Pursell2ec418a2016-01-20 08:32:08 -0800312 if (transport == nullptr && announce) {
313 fprintf(stderr, "error: %s\n", error.c_str());
314 }
315 } else {
316 transport = usb_open(match_fastboot);
317 }
318
319 if (transport != nullptr) {
320 return transport;
321 }
322
David Pursell0b156632015-10-30 11:22:01 -0700323 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800324 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700325 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800327 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 }
329}
330
Elliott Hughesfc797672015-04-07 20:12:50 -0700331static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 // We don't actually open a USB device here,
333 // just getting our callback called so we can
334 // list all the connected devices.
335 usb_open(list_devices_callback);
336}
337
Elliott Hughesd6365a72017-05-08 18:04:49 -0700338static void syntax_error(const char* fmt, ...) {
339 fprintf(stderr, "fastboot: usage: ");
340
341 va_list ap;
342 va_start(ap, fmt);
343 vfprintf(stderr, fmt, ap);
344 va_end(ap);
345
346 fprintf(stderr, "\n");
347 exit(1);
348}
349
350static int show_help() {
351 // clang-format off
352 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700353// 1 2 3 4 5 6 7 8
354// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700355 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700357 "flashing:\n"
358 " update ZIP Flash all partitions from an update.zip package.\n"
359 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
360 " On A/B devices, flashed slot is set as active.\n"
361 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700362 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
363 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700364 "\n"
365 "basics:\n"
366 " devices [-l] List devices in bootloader (-l: with device paths).\n"
367 " getvar NAME Display given bootloader variable.\n"
368 " reboot [bootloader] Reboot device.\n"
369 "\n"
370 "locking/unlocking:\n"
371 " flashing lock|unlock Lock/unlock partitions for flashing\n"
372 " flashing lock_critical|unlock_critical\n"
373 " Lock/unlock 'critical' bootloader partitions.\n"
374 " flashing get_unlock_ability\n"
375 " Check whether unlocking is allowed (1) or not(0).\n"
376 "\n"
377 "advanced:\n"
378 " erase PARTITION Erase a flash partition.\n"
379 " format[:FS_TYPE[:SIZE]] PARTITION\n"
380 " Format a flash partition.\n"
381 " set_active SLOT Set the active slot.\n"
382 " oem [COMMAND...] Execute OEM-specific command.\n"
383 "\n"
384 "boot image:\n"
385 " boot KERNEL [RAMDISK [SECOND]]\n"
386 " Download and boot kernel from RAM.\n"
387 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
388 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700389 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700390 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
391 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
392 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
393 " --tags-offset Set tags offset (default: 0x00000100).\n"
394 " --page-size BYTES Set flash page size (default: 2048).\n"
395 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700396 " --os-version MAJOR[.MINOR[.PATCH]]\n"
397 " Set boot image OS version (default: 0.0.0).\n"
398 " --os-patch-level YYYY-MM-DD\n"
399 " Set boot image OS security patch level.\n"
400 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700401 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700402 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700403 //" continue Continue with autoboot.\n"
404 //"\n"
405 "Android Things:\n"
406 " stage IN_FILE Sends given file to stage for the next command.\n"
407 " 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 -0800408 "\n"
409 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700410 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700411 " -s SERIAL Specify a USB device.\n"
412 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700413 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700414 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
415 " non-current slot (default: current active slot).\n"
416 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
417 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
418 " --skip-reboot Don't reboot device after flashing.\n"
419 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
420 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000421#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700422 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000423#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700424 // TODO: remove --unbuffered?
425 " --unbuffered Don't buffer input or output.\n"
426 " --verbose, -v Verbose output.\n"
427 " --version Display version.\n"
428 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700430 // clang-format off
431 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000433
Tom Cherrydfd85df2018-09-20 14:45:05 -0700434static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
435 const std::string& second_stage) {
436 std::vector<char> kernel_data;
437 if (!ReadFileToVector(kernel, &kernel_data)) {
438 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
439 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800440
Elliott Hughesfc797672015-04-07 20:12:50 -0700441 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700442 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800443 die("cannot load '%s': too short", kernel.c_str());
444 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700445 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700446 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700447 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700448 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800449
Elliott Hughes4089d342017-10-27 14:21:12 -0700450 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800451
Tom Cherrydfd85df2018-09-20 14:45:05 -0700452 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 }
454
Tom Cherrydfd85df2018-09-20 14:45:05 -0700455 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700456 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700457 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
458 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
459 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 }
461
Tom Cherrydfd85df2018-09-20 14:45:05 -0700462 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700463 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700464 if (!ReadFileToVector(second_stage, &second_stage_data)) {
465 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
466 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200467 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700469
Tom Cherrydfd85df2018-09-20 14:45:05 -0700470 std::vector<char> out;
471 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
472 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800473
Tom Cherrydfd85df2018-09-20 14:45:05 -0700474 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
475 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
476
477 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800478}
479
Tom Cherrydfd85df2018-09-20 14:45:05 -0700480static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
481 std::vector<char>* out) {
482 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700483 ZipEntry zip_entry;
484 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700485 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
486 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 }
488
Tom Cherrydfd85df2018-09-20 14:45:05 -0700489 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Tom Cherrydfd85df2018-09-20 14:45:05 -0700491 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
492 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493
Tom Cherrydfd85df2018-09-20 14:45:05 -0700494 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
495 out->size());
496 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000497
Tom Cherrydfd85df2018-09-20 14:45:05 -0700498 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499}
500
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700501#if defined(_WIN32)
502
503// TODO: move this to somewhere it can be shared.
504
505#include <windows.h>
506
507// Windows' tmpfile(3) requires administrator rights because
508// it creates temporary files in the root directory.
509static FILE* win32_tmpfile() {
510 char temp_path[PATH_MAX];
511 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
512 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700513 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700514 }
515
516 char filename[PATH_MAX];
517 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700518 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700519 }
520
521 return fopen(filename, "w+bTD");
522}
523
524#define tmpfile win32_tmpfile
525
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000526static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700527 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000528}
529
Elliott Hughes855cdf82018-04-02 14:24:03 -0700530static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700531 // TODO: reimplement to avoid leaking a FILE*.
532 return fileno(tmpfile());
533}
534
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000535#else
536
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700537static std::string make_temporary_template() {
538 const char* tmpdir = getenv("TMPDIR");
539 if (tmpdir == nullptr) tmpdir = P_tmpdir;
540 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
541}
542
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000543static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700544 std::string result(make_temporary_template());
545 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700546 die("unable to create temporary directory with template %s: %s",
547 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000548 }
549 return result;
550}
551
Elliott Hughes855cdf82018-04-02 14:24:03 -0700552static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700553 std::string path_template(make_temporary_template());
554 int fd = mkstemp(&path_template[0]);
555 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700556 die("failed to create temporary file for %s with template %s: %s\n",
557 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700558 }
559 unlink(path_template.c_str());
560 return fd;
561}
562
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700563#endif
564
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000565static std::string create_fbemarker_tmpdir() {
566 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000567 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
568 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
569 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700570 die("unable to create FBE marker file %s locally: %s",
571 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000572 }
573 close(fd);
574 return dir;
575}
576
577static void delete_fbemarker_tmpdir(const std::string& dir) {
578 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
579 if (unlink(marker_file.c_str()) == -1) {
580 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
581 marker_file.c_str(), errno, strerror(errno));
582 return;
583 }
584 if (rmdir(dir.c_str()) == -1) {
585 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
586 dir.c_str(), errno, strerror(errno));
587 return;
588 }
589}
590
Elliott Hughes45964a82017-05-03 22:43:23 -0700591static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700592 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700593
Yusuke Sato07447542015-06-25 14:39:19 -0700594 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700595 ZipEntry zip_entry;
596 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
597 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700598 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000599 return -1;
600 }
601
Elliott Hughes23af1122017-11-10 08:42:17 -0800602 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700603 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800604 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700605 int error = ExtractEntryToFile(zip, &zip_entry, fd);
606 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800607 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700608 }
609
Elliott Hughes4089d342017-10-27 14:21:12 -0700610 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800611 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700612 }
613
Elliott Hughes23af1122017-11-10 08:42:17 -0800614 fprintf(stderr, " took %.3fs\n", now() - start);
615
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700616 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700617}
618
Tom Cherry4aa60b32018-09-05 15:11:44 -0700619static void CheckRequirement(const std::string& cur_product, const std::string& var,
620 const std::string& product, bool invert,
621 const std::vector<std::string>& options) {
622 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700623
Tom Cherry4aa60b32018-09-05 15:11:44 -0700624 double start = now();
625
626 if (!product.empty()) {
627 if (product != cur_product) {
628 double split = now();
629 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
630 cur_product.c_str(), product.c_str(), (split - start));
631 return;
632 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700634
635 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700636 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700637 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700638 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
639 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700640 die("requirements not met!");
641 }
642
643 bool match = false;
644 for (const auto& option : options) {
645 if (option == var_value || (option.back() == '*' &&
646 !var_value.compare(0, option.length() - 1, option, 0,
647 option.length() - 1))) {
648 match = true;
649 break;
650 }
651 }
652
653 if (invert) {
654 match = !match;
655 }
656
657 if (match) {
658 double split = now();
659 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
660 return;
661 }
662
663 fprintf(stderr, "FAILED\n\n");
664 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
665 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
666 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
667 fprintf(stderr, " or '%s'", it->c_str());
668 }
669 fprintf(stderr, ".\n\n");
670 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671}
672
Tom Cherry4aa60b32018-09-05 15:11:44 -0700673bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
674 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700675 // "require product=alpha|beta|gamma"
676 // "require version-bootloader=1234"
677 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
678 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700679 *product = "";
680 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700681
Tom Cherry4aa60b32018-09-05 15:11:44 -0700682 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
683 auto require_product_regex =
684 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
685 std::smatch match_results;
686
687 if (std::regex_match(line, match_results, require_reject_regex)) {
688 *invert = Trim(match_results[1]) == "reject";
689 } else if (std::regex_match(line, match_results, require_product_regex)) {
690 *product = match_results[1];
691 } else {
692 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 }
694
Tom Cherry4aa60b32018-09-05 15:11:44 -0700695 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700696 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700697 if (*name == "board") {
698 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 }
700
Tom Cherry4aa60b32018-09-05 15:11:44 -0700701 auto raw_options = Split(match_results[3], "|");
702 for (const auto& option : raw_options) {
703 auto trimmed_option = Trim(option);
704 options->emplace_back(trimmed_option);
705 }
706
707 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800708}
709
Tom Cherry4aa60b32018-09-05 15:11:44 -0700710// "require partition-exists=x" is a special case, added because of the trouble we had when
711// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
712// missing out new partitions. A device with new partitions can use "partition-exists" to
713// override the fields `optional_if_no_image` in the `images` array.
714static void HandlePartitionExists(const std::vector<std::string>& options) {
715 const std::string& partition_name = options[0];
716 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700717 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700718 (has_slot != "yes" && has_slot != "no")) {
719 die("device doesn't have required partition %s!", partition_name.c_str());
720 }
721 bool known_partition = false;
722 for (size_t i = 0; i < arraysize(images); ++i) {
723 if (images[i].nickname && images[i].nickname == partition_name) {
724 images[i].optional_if_no_image = false;
725 known_partition = true;
726 }
727 }
728 if (!known_partition) {
729 die("device requires partition %s which is not known to this version of fastboot",
730 partition_name.c_str());
731 }
732}
733
734static void CheckRequirements(const std::string& data) {
735 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700736 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
737 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700738 }
739
740 auto lines = Split(data, "\n");
741 for (const auto& line : lines) {
742 if (line.empty()) {
743 continue;
744 }
745
746 std::string name;
747 std::string product;
748 bool invert;
749 std::vector<std::string> options;
750
751 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
752 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
753 continue;
754 }
755 if (name == "partition-exists") {
756 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800757 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700758 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800759 }
760 }
761}
762
Tom Cherry9027af02018-09-24 15:48:09 -0700763static void DisplayVarOrError(const std::string& label, const std::string& var) {
764 std::string value;
765
766 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
767 Status("getvar:" + var);
768 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
769 return;
770 }
771 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
772}
773
774static void DumpInfo() {
775 fprintf(stderr, "--------------------------------------------\n");
776 DisplayVarOrError("Bootloader Version...", "version-bootloader");
777 DisplayVarOrError("Baseband Version.....", "version-baseband");
778 DisplayVarOrError("Serial Number........", "serialno");
779 fprintf(stderr, "--------------------------------------------\n");
780
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800781}
782
Tao Bao41cf35f2018-04-24 10:54:21 -0700783static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700784 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700785 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700786
Tao Bao41cf35f2018-04-24 10:54:21 -0700787 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
788 die("invalid max size %" PRId64, max_size);
789 }
790
Elliott Hughesfc797672015-04-07 20:12:50 -0700791 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700792 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700793
Elliott Hughes253c18d2015-03-18 22:47:09 -0700794 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700795 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700796
797 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700798 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700799
800 return out_s;
801}
802
Aaron Wisnerdb511202018-06-26 15:38:35 -0500803static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700804 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700805 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700806 max_download_size.empty()) {
807 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700808 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700809 }
810
Elliott Hughes77c0e662015-11-02 15:51:12 -0800811 // Some bootloaders (angler, for example) send spurious whitespace too.
812 max_download_size = android::base::Trim(max_download_size);
813
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700814 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700815 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800816 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700817 return 0;
818 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700819 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700820 return limit;
821}
822
Aaron Wisnerdb511202018-06-26 15:38:35 -0500823static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700824 int64_t limit = sparse_limit;
825 if (limit == 0) {
826 // Unlimited, so see what the target device's limit is.
827 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700828 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500829 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700830 }
831 if (target_sparse_limit > 0) {
832 limit = target_sparse_limit;
833 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700834 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700835 }
836 }
837
838 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500839 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700840 }
841
842 return 0;
843}
844
Aaron Wisnerdb511202018-06-26 15:38:35 -0500845static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700846 int64_t sz = get_file_size(fd);
847 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700848 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700849 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700850
David Anderson32e376f2018-08-16 13:43:11 -0700851 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
852 buf->image_size = sparse_file_len(s, false, false);
853 sparse_file_destroy(s);
854 } else {
855 buf->image_size = sz;
856 }
857
Elliott Hughesfc797672015-04-07 20:12:50 -0700858 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500859 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700860 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700861 sparse_file** s = load_sparse_files(fd, limit);
862 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700863 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700864 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700865 buf->type = FB_BUFFER_SPARSE;
866 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700867 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500868 buf->type = FB_BUFFER_FD;
869 buf->data = nullptr;
870 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000871 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700872 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700873
Elliott Hughes53ec4952016-05-11 12:39:27 -0700874 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700875}
876
Aaron Wisnerdb511202018-06-26 15:38:35 -0500877static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500878 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
879
Elliott Hughes53ec4952016-05-11 12:39:27 -0700880 if (fd == -1) {
881 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500883
884 struct stat s;
885 if (fstat(fd, &s)) {
886 return false;
887 }
888 if (!S_ISREG(s.st_mode)) {
889 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
890 return false;
891 }
892
Aaron Wisnerdb511202018-06-26 15:38:35 -0500893 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700894}
895
David Zeuthenb6ea4352017-08-07 14:29:26 -0400896static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
897 // Buffer needs to be at least the size of the VBMeta struct which
898 // is 256 bytes.
899 if (buf->sz < 256) {
900 return;
901 }
902
Elliott Hughes855cdf82018-04-02 14:24:03 -0700903 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400904
905 std::string data;
906 if (!android::base::ReadFdToString(buf->fd, &data)) {
907 die("Failed reading from vbmeta");
908 }
909
910 // There's a 32-bit big endian |flags| field at offset 120 where
911 // bit 0 corresponds to disable-verity and bit 1 corresponds to
912 // disable-verification.
913 //
914 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
915 // the VBMeta struct.
916 if (g_disable_verity) {
917 data[123] |= 0x01;
918 }
919 if (g_disable_verification) {
920 data[123] |= 0x02;
921 }
922
923 if (!android::base::WriteStringToFd(data, fd)) {
924 die("Failed writing to modified vbmeta");
925 }
926 close(buf->fd);
927 buf->fd = fd;
928 lseek(fd, 0, SEEK_SET);
929}
930
Elliott Hughes5620d222018-03-28 08:20:00 -0700931static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700932{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700933 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700934
David Zeuthenb6ea4352017-08-07 14:29:26 -0400935 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
936 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700937 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400938 rewrite_vbmeta_buffer(buf);
939 }
940
Rom Lemarchand622810c2013-06-28 09:54:59 -0700941 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800942 case FB_BUFFER_SPARSE: {
943 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700944 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700945 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700946 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800947 sparse_files.emplace_back(*s, sz);
948 ++s;
949 }
950
951 for (size_t i = 0; i < sparse_files.size(); ++i) {
952 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700953 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700954 }
955 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800956 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500957 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700958 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700959 break;
960 default:
961 die("unknown buffer type: %d", buf->type);
962 }
963}
964
Aaron Wisnerdb511202018-06-26 15:38:35 -0500965static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700966 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700967 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700968 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700969}
970
Aaron Wisnerdb511202018-06-26 15:38:35 -0500971static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700973 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700974 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
975 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700976 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700978 return count;
979}
980
Aaron Wisnerdb511202018-06-26 15:38:35 -0500981static bool supports_AB() {
982 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700983}
984
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700985// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986static std::string get_other_slot(const std::string& current_slot, int count) {
987 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700988
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 char next = (current_slot[0] - 'a' + 1)%count + 'a';
990 return std::string(1, next);
991}
992
Aaron Wisnerdb511202018-06-26 15:38:35 -0500993static std::string get_other_slot(const std::string& current_slot) {
994 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700995}
996
Aaron Wisnerdb511202018-06-26 15:38:35 -0500997static std::string get_other_slot(int count) {
998 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999}
1000
Aaron Wisnerdb511202018-06-26 15:38:35 -05001001static std::string get_other_slot() {
1002 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -07001003}
1004
Aaron Wisnerdb511202018-06-26 15:38:35 -05001005static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001006 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001008 if (allow_all) {
1009 return "all";
1010 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001011 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 if (count > 0) {
1013 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001014 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001015 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001016 }
1017 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001018 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001019
Aaron Wisnerdb511202018-06-26 15:38:35 -05001020 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001021 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001022
Daniel Rosenberg80919472016-06-30 19:25:31 -07001023 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001024 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001025 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001026 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001027 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001028 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001029 }
1030
Daniel Rosenberg80919472016-06-30 19:25:31 -07001031 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1032
1033 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1034 for (int i=0; i<count; i++) {
1035 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001036 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001037
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001038 exit(1);
1039}
1040
Aaron Wisnerdb511202018-06-26 15:38:35 -05001041static std::string verify_slot(const std::string& slot) {
1042 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001043}
1044
Aaron Wisnerdb511202018-06-26 15:38:35 -05001045static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001046 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001047 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001048 std::string current_slot;
1049
Tom Cherry9027af02018-09-24 15:48:09 -07001050 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001051 /* If has-slot is not supported, the answer is no. */
1052 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001053 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001054 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001055 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001056 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001057 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001058 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001059 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001060 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001061 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001062 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001063 }
1064 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001065 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001066 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001067 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001068 }
1069 func(part);
1070 }
1071}
1072
David Pursell0b156632015-10-30 11:22:01 -07001073/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1074 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1075 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1076 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001077 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001078static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001079 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001080 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001081
Daniel Rosenberg80919472016-06-30 19:25:31 -07001082 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001083 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001084 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001085 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001086 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001087 for (int i=0; i < get_slot_count(); i++) {
1088 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001089 }
1090 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001091 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001092 }
1093 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001094 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001095 }
1096}
1097
Aaron Wisnerdb511202018-06-26 15:38:35 -05001098static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001099 struct fastboot_buffer buf;
1100
Aaron Wisnerdb511202018-06-26 15:38:35 -05001101 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001102 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001103 }
1104 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001105}
1106
Daniel Rosenberg13454092016-06-27 19:43:11 -07001107// Sets slot_override as the active slot. If slot_override is blank,
1108// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001109static void set_active(const std::string& slot_override) {
1110 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001111
Daniel Rosenberg80919472016-06-30 19:25:31 -07001112 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001113 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001114 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001115 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001116 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001117 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001118 }
1119 }
1120}
1121
David Anderson32e376f2018-08-16 13:43:11 -07001122static bool is_userspace_fastboot() {
1123 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001124 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001125}
1126
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001127static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1128 std::string has_slot;
1129 std::string partition_name = partition;
1130
Tom Cherry9027af02018-09-24 15:48:09 -07001131 if (fb->GetVar("has-slot:" + partition, &has_slot) == fastboot::SUCCESS && has_slot == "yes") {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001132 if (slot == "") {
1133 std::string current_slot = get_current_slot();
1134 if (current_slot == "") {
1135 die("Failed to identify current slot");
1136 }
1137 partition_name += "_" + current_slot;
1138 } else {
1139 partition_name += "_" + slot;
1140 }
1141 }
1142 std::string partition_size;
Tom Cherry9027af02018-09-24 15:48:09 -07001143 return fb->GetVar("partition-size:" + partition_name, &partition_size) == fastboot::SUCCESS;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001144}
1145
David Anderson32e376f2018-08-16 13:43:11 -07001146static bool is_logical(const std::string& partition) {
1147 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001148 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001149}
1150
David Anderson1d887432018-08-27 16:47:32 -07001151static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001152 fb->RebootTo("fastboot");
1153
1154 auto* old_transport = fb->set_transport(nullptr);
1155 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001156
1157 // Give the current connection time to close.
1158 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1159
Tom Cherry9027af02018-09-24 15:48:09 -07001160 fb->set_transport(open_device());
David Anderson1d887432018-08-27 16:47:32 -07001161}
1162
David Andersoncf444f32018-08-29 14:15:49 -07001163class ImageSource {
1164 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001165 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001166 virtual int OpenFile(const std::string& name) const = 0;
1167};
David Anderson32e376f2018-08-16 13:43:11 -07001168
David Andersoncf444f32018-08-29 14:15:49 -07001169class FlashAllTool {
1170 public:
1171 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001172
David Andersoncf444f32018-08-29 14:15:49 -07001173 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001174
David Andersoncf444f32018-08-29 14:15:49 -07001175 private:
1176 void CheckRequirements();
1177 void DetermineSecondarySlot();
1178 void CollectImages();
1179 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1180 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1181 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001182
David Andersoncf444f32018-08-29 14:15:49 -07001183 const ImageSource& source_;
1184 std::string slot_override_;
1185 bool skip_secondary_;
1186 bool wipe_;
1187 std::string secondary_slot_;
1188 std::vector<std::pair<const Image*, std::string>> boot_images_;
1189 std::vector<std::pair<const Image*, std::string>> os_images_;
1190};
1191
1192FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1193 : source_(source),
1194 slot_override_(slot_override),
1195 skip_secondary_(skip_secondary),
1196 wipe_(wipe)
1197{
David Anderson32e376f2018-08-16 13:43:11 -07001198}
1199
David Andersoncf444f32018-08-29 14:15:49 -07001200void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001201 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001202 CheckRequirements();
1203 DetermineSecondarySlot();
1204 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001205
David Anderson95d40932018-08-28 12:14:33 -07001206 // First flash boot partitions. We allow this to happen either in userspace
1207 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001208 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001209
1210 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001211 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001212
1213 // Resize any logical partition to 0, so each partition is reset to 0
1214 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001215 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001216 auto resize_partition = [](const std::string& partition) -> void {
1217 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001218 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001219 }
1220 };
David Anderson95d40932018-08-28 12:14:33 -07001221 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001222 }
1223
David Anderson95d40932018-08-28 12:14:33 -07001224 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001225 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001226
David Andersoncf444f32018-08-29 14:15:49 -07001227 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001228 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001229 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001230 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001232}
1233
David Andersoncf444f32018-08-29 14:15:49 -07001234void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001235 std::vector<char> contents;
1236 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001237 die("could not read android-info.txt");
1238 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001239 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001240}
1241
1242void FlashAllTool::DetermineSecondarySlot() {
1243 if (skip_secondary_) {
1244 return;
1245 }
1246 if (slot_override_ != "") {
1247 secondary_slot_ = get_other_slot(slot_override_);
1248 } else {
1249 secondary_slot_ = get_other_slot();
1250 }
1251 if (secondary_slot_ == "") {
1252 if (supports_AB()) {
1253 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1254 }
1255 skip_secondary_ = true;
1256 }
1257}
1258
1259void FlashAllTool::CollectImages() {
1260 for (size_t i = 0; i < arraysize(images); ++i) {
1261 std::string slot = slot_override_;
1262 if (images[i].IsSecondary()) {
1263 if (skip_secondary_) {
1264 continue;
1265 }
1266 slot = secondary_slot_;
1267 }
1268 if (images[i].type == ImageType::BootCritical) {
1269 boot_images_.emplace_back(&images[i], slot);
1270 } else if (images[i].type == ImageType::Normal) {
1271 os_images_.emplace_back(&images[i], slot);
1272 }
1273 }
1274}
1275
1276void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1277 for (const auto& [image, slot] : images) {
1278 fastboot_buffer buf;
1279 int fd = source_.OpenFile(image->img_name);
1280 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1281 if (image->optional_if_no_image) {
1282 continue;
1283 }
1284 die("could not load '%s': %s", image->img_name, strerror(errno));
1285 }
1286 FlashImage(*image, slot, &buf);
1287 }
1288}
1289
1290void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1291 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001292 std::vector<char> signature_data;
1293 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001294 fb->Download("signature", signature_data);
1295 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001296 }
1297
1298 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001299 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001300 }
1301 flash_buf(partition_name.c_str(), buf);
1302 };
1303 do_for_partitions(image.part_name, slot, flash, false);
1304}
1305
1306void FlashAllTool::UpdateSuperPartition() {
1307 if (!if_partition_exists("super", "")) {
1308 return;
1309 }
1310
1311 int fd = source_.OpenFile("super_empty.img");
1312 if (fd < 0) {
1313 return;
1314 }
1315 if (!is_userspace_fastboot()) {
1316 reboot_to_userspace_fastboot();
1317 }
Tom Cherry9027af02018-09-24 15:48:09 -07001318 fb->Download("super", fd, get_file_size(fd));
David Andersoncf444f32018-08-29 14:15:49 -07001319
1320 std::string command = "update-super:super";
1321 if (wipe_) {
1322 command += ":wipe";
1323 }
Tom Cherry9027af02018-09-24 15:48:09 -07001324 fb->RawCommand(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001325}
1326
1327class ZipImageSource final : public ImageSource {
1328 public:
1329 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001330 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001331 int OpenFile(const std::string& name) const override;
1332
1333 private:
1334 ZipArchiveHandle zip_;
1335};
1336
Tom Cherrydfd85df2018-09-20 14:45:05 -07001337bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1338 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001339}
1340
1341int ZipImageSource::OpenFile(const std::string& name) const {
1342 return unzip_to_file(zip_, name.c_str());
1343}
1344
1345static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001346 ZipArchiveHandle zip;
1347 int error = OpenArchive(filename, &zip);
1348 if (error != 0) {
1349 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1350 }
1351
1352 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1353 tool.Flash();
1354
1355 CloseArchive(zip);
1356}
1357
1358class LocalImageSource final : public ImageSource {
1359 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001360 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001361 int OpenFile(const std::string& name) const override;
1362};
1363
Tom Cherrydfd85df2018-09-20 14:45:05 -07001364bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001365 auto path = find_item_given_name(name);
1366 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001367 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001368 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001369 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001370}
1371
1372int LocalImageSource::OpenFile(const std::string& name) const {
1373 auto path = find_item_given_name(name);
1374 return open(path.c_str(), O_RDONLY);
1375}
1376
1377static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001378 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1379 tool.Flash();
1380}
1381
Elliott Hughesd6365a72017-05-08 18:04:49 -07001382static std::string next_arg(std::vector<std::string>* args) {
1383 if (args->empty()) syntax_error("expected argument");
1384 std::string result = args->front();
1385 args->erase(args->begin());
1386 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001387}
1388
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001389static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001390 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001391
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001392 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001393 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001394 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001395 }
Tom Cherry9027af02018-09-24 15:48:09 -07001396 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001397}
1398
Connor O'Brience16a8a2017-02-06 14:39:31 -08001399static std::string fb_fix_numeric_var(std::string var) {
1400 // Some bootloaders (angler, for example), send spurious leading whitespace.
1401 var = android::base::Trim(var);
1402 // Some bootloaders (hammerhead, for example) use implicit hex.
1403 // This code used to use strtol with base 16.
1404 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1405 return var;
1406}
1407
Aaron Wisnerdb511202018-06-26 15:38:35 -05001408static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001409 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001410 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001411 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001412 return 0;
1413 }
1414 sizeString = fb_fix_numeric_var(sizeString);
1415
1416 unsigned size;
1417 if (!android::base::ParseUint(sizeString, &size)) {
1418 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1419 return 0;
1420 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001421 if ((size & (size - 1)) != 0) {
1422 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001423 return 0;
1424 }
1425 return size;
1426}
1427
Aaron Wisnerdb511202018-06-26 15:38:35 -05001428static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001429 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001430 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001431 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001432 std::string partition_type, partition_size;
1433
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001434 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001435 const char* errMsg = nullptr;
1436 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001437 TemporaryFile output;
1438 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001439
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001440 unsigned int limit = INT_MAX;
1441 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001442 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001443 }
1444 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001445 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001446 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001447
Tom Cherry9027af02018-09-24 15:48:09 -07001448 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001449 errMsg = "Can't determine partition type.\n";
1450 goto failed;
1451 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001452 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001453 if (partition_type != type_override) {
1454 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001455 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001456 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001457 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001458 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001459
Tom Cherry9027af02018-09-24 15:48:09 -07001460 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001461 errMsg = "Unable to get partition size\n";
1462 goto failed;
1463 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001464 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001465 if (partition_size != size_override) {
1466 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001467 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001468 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001469 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001470 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001471 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001472
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001473 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001474 if (!gen) {
1475 if (skip_if_not_supported) {
1476 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001477 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001478 return;
1479 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001480 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1481 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001482 return;
1483 }
1484
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001485 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001486 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001487 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1488 return;
1489 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001490
Connor O'Brience16a8a2017-02-06 14:39:31 -08001491 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001492 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1493 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001494
Jin Qian4a335822017-04-18 16:23:18 -07001495 if (fs_generator_generate(gen, output.path, size, initial_dir,
1496 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001497 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001498 return;
1499 }
1500
Jin Qian4a335822017-04-18 16:23:18 -07001501 fd.reset(open(output.path, O_RDONLY));
1502 if (fd == -1) {
1503 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1504 return;
1505 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001506 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001507 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001508 return;
1509 }
1510 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001511 return;
1512
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001513failed:
1514 if (skip_if_not_supported) {
1515 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001516 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001517 }
Tom Cherry9027af02018-09-24 15:48:09 -07001518 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001519}
1520
Aaron Wisnerdb511202018-06-26 15:38:35 -05001521int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001522 bool wants_wipe = false;
1523 bool wants_reboot = false;
1524 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001525 bool wants_reboot_recovery = false;
1526 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001527 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001528 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001529 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001530 bool set_fbe_marker = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001531 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001532 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001533 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001534
Elliott Hughes577e8b42018-04-05 16:12:47 -07001535 g_boot_img_hdr.kernel_addr = 0x00008000;
1536 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1537 g_boot_img_hdr.second_addr = 0x00f00000;
1538 g_boot_img_hdr.tags_addr = 0x00000100;
1539 g_boot_img_hdr.page_size = 2048;
1540
JP Abgrall7b8970c2013-03-07 17:06:41 -08001541 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001542 {"base", required_argument, 0, 0},
1543 {"cmdline", required_argument, 0, 0},
1544 {"disable-verification", no_argument, 0, 0},
1545 {"disable-verity", no_argument, 0, 0},
1546 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001547 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001548 {"kernel-offset", required_argument, 0, 0},
1549 {"os-patch-level", required_argument, 0, 0},
1550 {"os-version", required_argument, 0, 0},
1551 {"page-size", required_argument, 0, 0},
1552 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001553 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001554 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001555 {"skip-secondary", no_argument, 0, 0},
1556 {"slot", required_argument, 0, 0},
1557 {"tags-offset", required_argument, 0, 0},
1558 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001559 {"verbose", no_argument, 0, 'v'},
1560 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001561#if !defined(_WIN32)
1562 {"wipe-and-use-fbe", no_argument, 0, 0},
1563#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001564 {0, 0, 0, 0}
1565 };
Colin Cross8879f982012-05-22 17:53:34 -07001566
1567 serial = getenv("ANDROID_SERIAL");
1568
Elliott Hughes577e8b42018-04-05 16:12:47 -07001569 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001570 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001571 if (c == 0) {
1572 std::string name{longopts[longindex].name};
1573 if (name == "base") {
1574 g_base_addr = strtoul(optarg, 0, 16);
1575 } else if (name == "cmdline") {
1576 g_cmdline = optarg;
1577 } else if (name == "disable-verification") {
1578 g_disable_verification = true;
1579 } else if (name == "disable-verity") {
1580 g_disable_verity = true;
1581 } else if (name == "header-version") {
1582 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1583 } else if (name == "kernel-offset") {
1584 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1585 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001586 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001587 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001588 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001589 } else if (name == "page-size") {
1590 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1591 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1592 } else if (name == "ramdisk-offset") {
1593 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1594 } else if (name == "skip-reboot") {
1595 skip_reboot = true;
1596 } else if (name == "skip-secondary") {
1597 skip_secondary = true;
1598 } else if (name == "slot") {
1599 slot_override = optarg;
1600 } else if (name == "tags-offset") {
1601 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1602 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001603 setvbuf(stdout, nullptr, _IONBF, 0);
1604 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001605 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001606 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001607 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001608 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001609#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001610 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001611 wants_wipe = true;
1612 set_fbe_marker = true;
1613#endif
1614 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001615 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001616 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001617 } else {
1618 switch (c) {
1619 case 'a':
1620 wants_set_active = true;
1621 if (optarg) next_active = optarg;
1622 break;
1623 case 'h':
1624 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001625 case 'l':
1626 g_long_listing = true;
1627 break;
1628 case 's':
1629 serial = optarg;
1630 break;
1631 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001632 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1633 die("invalid sparse limit %s", optarg);
1634 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001635 break;
1636 case 'v':
1637 set_verbose();
1638 break;
1639 case 'w':
1640 wants_wipe = true;
1641 break;
1642 case '?':
1643 return 1;
1644 default:
1645 abort();
1646 }
Colin Cross8879f982012-05-22 17:53:34 -07001647 }
1648 }
1649
1650 argc -= optind;
1651 argv += optind;
1652
Elliott Hughesd6365a72017-05-08 18:04:49 -07001653 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654
Colin Cross8fb6e062012-07-24 16:36:41 -07001655 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001656 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001657 return 0;
1658 }
1659
Colin Crossc7b75dc2012-08-29 18:17:06 -07001660 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001661 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001662 }
1663
David Pursell0b156632015-10-30 11:22:01 -07001664 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001665 if (transport == nullptr) {
1666 return 1;
1667 }
Tom Cherry9027af02018-09-24 15:48:09 -07001668 fastboot::DriverCallbacks driver_callbacks = {
1669 .prolog = Status,
1670 .epilog = Epilog,
1671 .info = InfoMessage,
1672 };
1673 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1674 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001675
Elliott Hughes5620d222018-03-28 08:20:00 -07001676 const double start = now();
1677
Aaron Wisnerdb511202018-06-26 15:38:35 -05001678 if (slot_override != "") slot_override = verify_slot(slot_override);
1679 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680
1681 if (wants_set_active) {
1682 if (next_active == "") {
1683 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001684 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001685 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001686 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001687 } else {
1688 wants_set_active = false;
1689 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001690 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001691 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001692 }
1693 }
1694 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001695
Elliott Hughesd6365a72017-05-08 18:04:49 -07001696 std::vector<std::string> args(argv, argv + argc);
1697 while (!args.empty()) {
1698 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001699
Elliott Hughesd6365a72017-05-08 18:04:49 -07001700 if (command == "getvar") {
1701 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001702 DisplayVarOrError(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 } else if (command == "erase") {
1704 std::string partition = next_arg(&args);
1705 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001706 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001707 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001708 fs_get_generator(partition_type) != nullptr) {
1709 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1710 partition_type.c_str());
1711 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001712
Tom Cherry9027af02018-09-24 15:48:09 -07001713 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001714 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001715 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001716 } else if (android::base::StartsWith(command, "format")) {
1717 // Parsing for: "format[:[type][:[size]]]"
1718 // Some valid things:
1719 // - select only the size, and leave default fs type:
1720 // format::0x4000000 userdata
1721 // - default fs type and size:
1722 // format userdata
1723 // format:: userdata
1724 std::vector<std::string> pieces = android::base::Split(command, ":");
1725 std::string type_override;
1726 if (pieces.size() > 1) type_override = pieces[1].c_str();
1727 std::string size_override;
1728 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001729
Elliott Hughesd6365a72017-05-08 18:04:49 -07001730 std::string partition = next_arg(&args);
1731
1732 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001733 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001734 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001735 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001736 } else if (command == "signature") {
1737 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001738 std::vector<char> data;
1739 if (!ReadFileToVector(filename, &data)) {
1740 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1741 }
1742 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001743 fb->Download("signature", data);
1744 fb->RawCommand("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001746 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001747
1748 if (args.size() == 1) {
1749 std::string what = next_arg(&args);
1750 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001751 wants_reboot = false;
1752 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001753 } else if (what == "recovery") {
1754 wants_reboot = false;
1755 wants_reboot_recovery = true;
1756 } else if (what == "fastboot") {
1757 wants_reboot = false;
1758 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001759 } else {
1760 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001761 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001762
Elliott Hughesca85df02015-02-25 10:02:00 -08001763 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001764 if (!args.empty()) syntax_error("junk after reboot command");
1765 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001766 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001767 } else if (command == "reboot-recovery") {
1768 wants_reboot_recovery = true;
1769 } else if (command == "reboot-fastboot") {
1770 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001771 } else if (command == "continue") {
Tom Cherry9027af02018-09-24 15:48:09 -07001772 fb->Continue();
Elliott Hughesd6365a72017-05-08 18:04:49 -07001773 } else if (command == "boot") {
1774 std::string kernel = next_arg(&args);
1775 std::string ramdisk;
1776 if (!args.empty()) ramdisk = next_arg(&args);
1777 std::string second_stage;
1778 if (!args.empty()) second_stage = next_arg(&args);
1779
Tom Cherrydfd85df2018-09-20 14:45:05 -07001780 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001781 fb->Download("boot.img", data);
1782 fb->Boot();
Elliott Hughesd6365a72017-05-08 18:04:49 -07001783 } else if (command == "flash") {
1784 std::string pname = next_arg(&args);
1785
Elliott Hughes2810d002016-04-25 14:31:18 -07001786 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001787 if (!args.empty()) {
1788 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001789 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001790 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001791 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001792 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001793
1794 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001795 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001796 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001797 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001798 } else if (command == "flash:raw") {
1799 std::string partition = next_arg(&args);
1800 std::string kernel = next_arg(&args);
1801 std::string ramdisk;
1802 if (!args.empty()) ramdisk = next_arg(&args);
1803 std::string second_stage;
1804 if (!args.empty()) second_stage = next_arg(&args);
1805
Tom Cherrydfd85df2018-09-20 14:45:05 -07001806 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1807 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001808 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001809 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001810 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001811 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001812 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001813 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001814 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001815 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001816 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001817 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001818 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001819 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001820 bool slot_all = (slot_override == "all");
1821 if (slot_all) {
1822 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1823 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001824 std::string filename = "update.zip";
1825 if (!args.empty()) {
1826 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001827 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001828 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001829 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001830 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001831 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001832 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001833 } else if (command == "stage") {
1834 std::string filename = next_arg(&args);
1835
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001836 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001837 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001838 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001839 }
Tom Cherry9027af02018-09-24 15:48:09 -07001840 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001841 } else if (command == "get_staged") {
1842 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001843 fb->Upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001844 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001845 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001846 } else if (command == "flashing") {
1847 if (args.empty()) {
1848 syntax_error("missing 'flashing' command");
1849 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1850 args[0] == "unlock_critical" ||
1851 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001852 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001853 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001854 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001855 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001856 }
David Anderson0d4277d2018-07-31 13:27:37 -07001857 } else if (command == "create-logical-partition") {
1858 std::string partition = next_arg(&args);
1859 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001860 fb->CreatePartition(partition, size);
David Anderson0d4277d2018-07-31 13:27:37 -07001861 } else if (command == "delete-logical-partition") {
1862 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001863 fb->DeletePartition(partition);
David Anderson0d4277d2018-07-31 13:27:37 -07001864 } else if (command == "resize-logical-partition") {
1865 std::string partition = next_arg(&args);
1866 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001867 fb->ResizePartition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001868 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001869 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001870 }
1871 }
1872
1873 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001874 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1875 for (const auto& partition : partitions) {
1876 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001877 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1878 continue;
1879 }
Paul Crowley4d170062018-04-24 17:06:30 -07001880 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001881 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001882 if (partition == "userdata" && set_fbe_marker) {
1883 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1884 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001885 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001886 delete_fbemarker_tmpdir(initial_userdata_dir);
1887 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001888 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001889 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001890 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001891 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001892 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001893 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001894 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001895 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001896 fb->Reboot();
1897 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001898 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001899 fb->RebootTo("bootloader");
1900 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001901 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001902 fb->RebootTo("recovery");
1903 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001904 } else if (wants_reboot_fastboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001905 fb->RebootTo("fastboot");
1906 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001907 }
1908
Elliott Hughes5620d222018-03-28 08:20:00 -07001909 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001910
Tom Cherry9027af02018-09-24 15:48:09 -07001911 auto* old_transport = fb->set_transport(nullptr);
1912 delete old_transport;
1913
Tom Cherry11f12092018-08-29 21:36:28 -07001914 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001915}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001916
Aaron Wisnerdb511202018-06-26 15:38:35 -05001917void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001918 unsigned year, month, day;
1919 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1920 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1921 }
1922 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1923 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1924 hdr->SetOsPatchLevel(year, month);
1925}
1926
Aaron Wisnerdb511202018-06-26 15:38:35 -05001927void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001928 unsigned major = 0, minor = 0, patch = 0;
1929 std::vector<std::string> versions = android::base::Split(arg, ".");
1930 if (versions.size() < 1 || versions.size() > 3 ||
1931 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1932 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1933 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1934 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1935 syntax_error("bad OS version: %s", arg);
1936 }
1937 hdr->SetOsVersion(major, minor, patch);
1938}