blob: 625e047b6ec16dadb682da33cf9affff018d6440 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Tom Cherry9027af02018-09-24 15:48:09 -070029#include "fastboot.h"
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Tom Cherry4aa60b32018-09-05 15:11:44 -070048#include <regex>
Elliott Hughes290a2282016-11-14 17:08:47 -080049#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080050#include <utility>
51#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070052
Elliott Hughes82ff3152016-08-31 15:07:18 -070053#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070054#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080056#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070057#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080058#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070059#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050060#include <android-base/unique_fd.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070061#include <build/version.h>
62#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070063#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070064#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
Elliott Hughes253c18d2015-03-18 22:47:09 -070066#include "bootimg_utils.h"
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070067#include "constants.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080068#include "diagnose_usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070069#include "fastboot_driver.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070070#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080071#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070072#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080073#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070074#include "usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070075#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Tom Cherrydfd85df2018-09-20 14:45:05 -070077using android::base::ReadFully;
Tom Cherry4aa60b32018-09-05 15:11:44 -070078using android::base::Split;
79using android::base::Trim;
Chris Fries0ea946c2017-04-12 10:25:57 -050080using android::base::unique_fd;
81
David Pursell2ec418a2016-01-20 08:32:08 -080082static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070083
Elliott Hughes577e8b42018-04-05 16:12:47 -070084static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050085// Don't resparse files in too-big chunks.
86// libsparse will support INT_MAX, but this results in large allocations, so
87// let's keep it at 1GB to avoid memory pressure on the host.
88static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070089static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070090static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Elliott Hughes577e8b42018-04-05 16:12:47 -070092static unsigned g_base_addr = 0x10000000;
93static boot_img_hdr_v1 g_boot_img_hdr = {};
94static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080095
David Zeuthenb6ea4352017-08-07 14:29:26 -040096static bool g_disable_verity = false;
97static bool g_disable_verification = false;
98
Paul Crowley8f7f56e2015-11-27 09:29:37 +000099static const std::string convert_fbe_marker_filename("convert_fbe");
100
Tom Cherry9027af02018-09-24 15:48:09 -0700101fastboot::FastBootDriver* fb = nullptr;
102
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500104 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 FB_BUFFER_SPARSE,
106};
107
108struct fastboot_buffer {
109 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700110 void* data;
111 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500112 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700113 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700114};
115
David Anderson0debda02018-08-28 13:54:03 -0700116enum class ImageType {
117 // Must be flashed for device to boot into the kernel.
118 BootCritical,
119 // Normal partition to be flashed during "flashall".
120 Normal,
121 // Partition that is never flashed during "flashall".
122 Extra
123};
124
David Anderson32e376f2018-08-16 13:43:11 -0700125struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700126 const char* nickname;
127 const char* img_name;
128 const char* sig_name;
129 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700130 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700131 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700132 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700133};
134
135static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100136 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700137 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
138 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
139 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
140 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
141 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
142 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
143 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100144 { "product_services",
145 "product_services.img",
146 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700147 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700148 true, ImageType::Normal },
149 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
150 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
151 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
152 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
153 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
154 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson166bfef2018-10-15 14:46:59 -0700155 { "vbmeta_system",
156 "vbmeta_system.img",
157 "vbmeta_system.sig",
158 "vbmeta_system",
David Anderson1109c922018-09-17 17:32:54 -0700159 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700160 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
161 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100162 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700163};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700164
David Anderson0debda02018-08-28 13:54:03 -0700165static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700166 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700167 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700168 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700169 }
David Anderson0debda02018-08-28 13:54:03 -0700170 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700171}
172
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700173static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700174 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700175 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700176 return find_item_given_name(images[i].img_name);
177 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 }
179
Elliott Hughesd6365a72017-05-08 18:04:49 -0700180 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700181 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182}
183
Tom Cherry9027af02018-09-24 15:48:09 -0700184double last_start_time;
185
186static void Status(const std::string& message) {
187 static constexpr char kStatusFormat[] = "%-50s ";
188 fprintf(stderr, kStatusFormat, message.c_str());
189 last_start_time = now();
190}
191
192static void Epilog(int status) {
193 if (status) {
194 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
195 die("Command failed");
196 } else {
197 double split = now();
198 fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
199 }
200}
201
202static void InfoMessage(const std::string& info) {
203 fprintf(stderr, "(bootloader) %s\n", info.c_str());
204}
205
Elliott Hughesfc797672015-04-07 20:12:50 -0700206static int64_t get_file_size(int fd) {
207 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700208 if (fstat(fd, &sb) == -1) {
209 die("could not get file size");
210 }
211 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700212}
213
Tom Cherrydfd85df2018-09-20 14:45:05 -0700214bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
215 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
Tom Cherrydfd85df2018-09-20 14:45:05 -0700217 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
218 if (fd == -1) {
219 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221
Tom Cherrydfd85df2018-09-20 14:45:05 -0700222 out->resize(get_file_size(fd));
223 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700224}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225
Elliott Hughesfc797672015-04-07 20:12:50 -0700226static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700227 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
228 return -1;
229 }
230
Scott Anderson13081c62012-04-06 12:39:30 -0700231 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700233 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
234 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 return 0;
236}
237
Elliott Hughesfc797672015-04-07 20:12:50 -0700238static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800239 return match_fastboot_with_serial(info, serial);
240}
241
Elliott Hughesfc797672015-04-07 20:12:50 -0700242static int list_devices_callback(usb_ifc_info* info) {
243 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800244 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700245 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800246 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700247 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800248 if (!serial[0]) {
249 serial = "????????????";
250 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700251 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700252 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800253 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700254 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800255 printf("%-22s fastboot", serial.c_str());
256 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700257 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800258 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 }
260
261 return -1;
262}
263
David Pursell2ec418a2016-01-20 08:32:08 -0800264// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
265// a specific device, otherwise the first USB device found will be used.
266//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700267// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800268// Otherwise it blocks until the target is available.
269//
270// The returned Transport is a singleton, so multiple calls to this function will return the same
271// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700272static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800273 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274
David Pursell4601c972016-02-05 15:35:09 -0800275 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800276 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800277 int port = 0;
278 if (serial != nullptr) {
279 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800280
David Pursell4601c972016-02-05 15:35:09 -0800281 if (android::base::StartsWith(serial, "tcp:")) {
282 protocol = Socket::Protocol::kTcp;
283 port = tcp::kDefaultPort;
284 net_address = serial + strlen("tcp:");
285 } else if (android::base::StartsWith(serial, "udp:")) {
286 protocol = Socket::Protocol::kUdp;
287 port = udp::kDefaultPort;
288 net_address = serial + strlen("udp:");
289 }
290
291 if (net_address != nullptr) {
292 std::string error;
293 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700294 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800295 }
David Pursell2ec418a2016-01-20 08:32:08 -0800296 }
297 }
298
David Anderson1d887432018-08-27 16:47:32 -0700299 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800300 while (true) {
301 if (!host.empty()) {
302 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800303 if (protocol == Socket::Protocol::kTcp) {
304 transport = tcp::Connect(host, port, &error).release();
305 } else if (protocol == Socket::Protocol::kUdp) {
306 transport = udp::Connect(host, port, &error).release();
307 }
308
David Pursell2ec418a2016-01-20 08:32:08 -0800309 if (transport == nullptr && announce) {
310 fprintf(stderr, "error: %s\n", error.c_str());
311 }
312 } else {
313 transport = usb_open(match_fastboot);
314 }
315
316 if (transport != nullptr) {
317 return transport;
318 }
319
David Pursell0b156632015-10-30 11:22:01 -0700320 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800321 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700322 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800324 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 }
326}
327
Elliott Hughesfc797672015-04-07 20:12:50 -0700328static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 // We don't actually open a USB device here,
330 // just getting our callback called so we can
331 // list all the connected devices.
332 usb_open(list_devices_callback);
333}
334
Elliott Hughesd6365a72017-05-08 18:04:49 -0700335static void syntax_error(const char* fmt, ...) {
336 fprintf(stderr, "fastboot: usage: ");
337
338 va_list ap;
339 va_start(ap, fmt);
340 vfprintf(stderr, fmt, ap);
341 va_end(ap);
342
343 fprintf(stderr, "\n");
344 exit(1);
345}
346
347static int show_help() {
348 // clang-format off
349 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700350// 1 2 3 4 5 6 7 8
351// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700352 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800353 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700354 "flashing:\n"
355 " update ZIP Flash all partitions from an update.zip package.\n"
356 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
357 " On A/B devices, flashed slot is set as active.\n"
358 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700359 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
360 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700361 "\n"
362 "basics:\n"
363 " devices [-l] List devices in bootloader (-l: with device paths).\n"
364 " getvar NAME Display given bootloader variable.\n"
365 " reboot [bootloader] Reboot device.\n"
366 "\n"
367 "locking/unlocking:\n"
368 " flashing lock|unlock Lock/unlock partitions for flashing\n"
369 " flashing lock_critical|unlock_critical\n"
370 " Lock/unlock 'critical' bootloader partitions.\n"
371 " flashing get_unlock_ability\n"
372 " Check whether unlocking is allowed (1) or not(0).\n"
373 "\n"
374 "advanced:\n"
375 " erase PARTITION Erase a flash partition.\n"
376 " format[:FS_TYPE[:SIZE]] PARTITION\n"
377 " Format a flash partition.\n"
378 " set_active SLOT Set the active slot.\n"
379 " oem [COMMAND...] Execute OEM-specific command.\n"
380 "\n"
381 "boot image:\n"
382 " boot KERNEL [RAMDISK [SECOND]]\n"
383 " Download and boot kernel from RAM.\n"
384 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
385 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700386 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700387 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
388 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
389 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
390 " --tags-offset Set tags offset (default: 0x00000100).\n"
391 " --page-size BYTES Set flash page size (default: 2048).\n"
392 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700393 " --os-version MAJOR[.MINOR[.PATCH]]\n"
394 " Set boot image OS version (default: 0.0.0).\n"
395 " --os-patch-level YYYY-MM-DD\n"
396 " Set boot image OS security patch level.\n"
397 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700398 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700399 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700400 //" continue Continue with autoboot.\n"
401 //"\n"
402 "Android Things:\n"
403 " stage IN_FILE Sends given file to stage for the next command.\n"
404 " 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 -0800405 "\n"
406 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700407 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700408 " -s SERIAL Specify a USB device.\n"
409 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700410 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700411 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
412 " non-current slot (default: current active slot).\n"
413 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
414 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
415 " --skip-reboot Don't reboot device after flashing.\n"
416 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
417 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000418#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700419 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000420#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700421 // TODO: remove --unbuffered?
422 " --unbuffered Don't buffer input or output.\n"
423 " --verbose, -v Verbose output.\n"
424 " --version Display version.\n"
425 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700427 // clang-format off
428 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000430
Tom Cherrydfd85df2018-09-20 14:45:05 -0700431static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
432 const std::string& second_stage) {
433 std::vector<char> kernel_data;
434 if (!ReadFileToVector(kernel, &kernel_data)) {
435 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
436 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800437
Elliott Hughesfc797672015-04-07 20:12:50 -0700438 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700439 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800440 die("cannot load '%s': too short", kernel.c_str());
441 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700442 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700443 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700444 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700445 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800446
Elliott Hughes4089d342017-10-27 14:21:12 -0700447 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800448
Tom Cherrydfd85df2018-09-20 14:45:05 -0700449 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 }
451
Tom Cherrydfd85df2018-09-20 14:45:05 -0700452 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700453 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700454 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
455 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
456 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 }
458
Tom Cherrydfd85df2018-09-20 14:45:05 -0700459 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700460 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700461 if (!ReadFileToVector(second_stage, &second_stage_data)) {
462 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
463 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200464 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700466
Tom Cherrydfd85df2018-09-20 14:45:05 -0700467 std::vector<char> out;
468 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
469 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800470
Tom Cherrydfd85df2018-09-20 14:45:05 -0700471 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
472 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
473
474 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475}
476
Tom Cherrydfd85df2018-09-20 14:45:05 -0700477static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
478 std::vector<char>* out) {
479 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700480 ZipEntry zip_entry;
481 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700482 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
483 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484 }
485
Tom Cherrydfd85df2018-09-20 14:45:05 -0700486 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487
Tom Cherrydfd85df2018-09-20 14:45:05 -0700488 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
489 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Tom Cherrydfd85df2018-09-20 14:45:05 -0700491 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
492 out->size());
493 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000494
Tom Cherrydfd85df2018-09-20 14:45:05 -0700495 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496}
497
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700498#if defined(_WIN32)
499
500// TODO: move this to somewhere it can be shared.
501
502#include <windows.h>
503
504// Windows' tmpfile(3) requires administrator rights because
505// it creates temporary files in the root directory.
506static FILE* win32_tmpfile() {
507 char temp_path[PATH_MAX];
508 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
509 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700510 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700511 }
512
513 char filename[PATH_MAX];
514 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700515 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700516 }
517
518 return fopen(filename, "w+bTD");
519}
520
521#define tmpfile win32_tmpfile
522
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000523static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700524 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000525}
526
Elliott Hughes855cdf82018-04-02 14:24:03 -0700527static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700528 // TODO: reimplement to avoid leaking a FILE*.
529 return fileno(tmpfile());
530}
531
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000532#else
533
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700534static std::string make_temporary_template() {
535 const char* tmpdir = getenv("TMPDIR");
536 if (tmpdir == nullptr) tmpdir = P_tmpdir;
537 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
538}
539
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000540static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700541 std::string result(make_temporary_template());
542 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700543 die("unable to create temporary directory with template %s: %s",
544 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000545 }
546 return result;
547}
548
Elliott Hughes855cdf82018-04-02 14:24:03 -0700549static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700550 std::string path_template(make_temporary_template());
551 int fd = mkstemp(&path_template[0]);
552 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700553 die("failed to create temporary file for %s with template %s: %s\n",
554 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700555 }
556 unlink(path_template.c_str());
557 return fd;
558}
559
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700560#endif
561
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000562static std::string create_fbemarker_tmpdir() {
563 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000564 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
565 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
566 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700567 die("unable to create FBE marker file %s locally: %s",
568 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000569 }
570 close(fd);
571 return dir;
572}
573
574static void delete_fbemarker_tmpdir(const std::string& dir) {
575 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
576 if (unlink(marker_file.c_str()) == -1) {
577 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
578 marker_file.c_str(), errno, strerror(errno));
579 return;
580 }
581 if (rmdir(dir.c_str()) == -1) {
582 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
583 dir.c_str(), errno, strerror(errno));
584 return;
585 }
586}
587
Elliott Hughes45964a82017-05-03 22:43:23 -0700588static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700589 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700590
Yusuke Sato07447542015-06-25 14:39:19 -0700591 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700592 ZipEntry zip_entry;
593 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
594 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700595 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000596 return -1;
597 }
598
Elliott Hughes23af1122017-11-10 08:42:17 -0800599 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700600 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800601 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700602 int error = ExtractEntryToFile(zip, &zip_entry, fd);
603 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800604 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700605 }
606
Elliott Hughes4089d342017-10-27 14:21:12 -0700607 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800608 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700609 }
610
Elliott Hughes23af1122017-11-10 08:42:17 -0800611 fprintf(stderr, " took %.3fs\n", now() - start);
612
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700613 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700614}
615
Tom Cherry4aa60b32018-09-05 15:11:44 -0700616static void CheckRequirement(const std::string& cur_product, const std::string& var,
617 const std::string& product, bool invert,
618 const std::vector<std::string>& options) {
619 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700620
Tom Cherry4aa60b32018-09-05 15:11:44 -0700621 double start = now();
622
623 if (!product.empty()) {
624 if (product != cur_product) {
625 double split = now();
626 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
627 cur_product.c_str(), product.c_str(), (split - start));
628 return;
629 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700631
632 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700633 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700634 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700635 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
636 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700637 die("requirements not met!");
638 }
639
640 bool match = false;
641 for (const auto& option : options) {
642 if (option == var_value || (option.back() == '*' &&
643 !var_value.compare(0, option.length() - 1, option, 0,
644 option.length() - 1))) {
645 match = true;
646 break;
647 }
648 }
649
650 if (invert) {
651 match = !match;
652 }
653
654 if (match) {
655 double split = now();
656 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
657 return;
658 }
659
660 fprintf(stderr, "FAILED\n\n");
661 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
662 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
663 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
664 fprintf(stderr, " or '%s'", it->c_str());
665 }
666 fprintf(stderr, ".\n\n");
667 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668}
669
Tom Cherry4aa60b32018-09-05 15:11:44 -0700670bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
671 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700672 // "require product=alpha|beta|gamma"
673 // "require version-bootloader=1234"
674 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
675 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700676 *product = "";
677 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700678
Tom Cherry4aa60b32018-09-05 15:11:44 -0700679 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
680 auto require_product_regex =
681 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
682 std::smatch match_results;
683
684 if (std::regex_match(line, match_results, require_reject_regex)) {
685 *invert = Trim(match_results[1]) == "reject";
686 } else if (std::regex_match(line, match_results, require_product_regex)) {
687 *product = match_results[1];
688 } else {
689 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 }
691
Tom Cherry4aa60b32018-09-05 15:11:44 -0700692 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700693 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700694 if (*name == "board") {
695 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696 }
697
Tom Cherry4aa60b32018-09-05 15:11:44 -0700698 auto raw_options = Split(match_results[3], "|");
699 for (const auto& option : raw_options) {
700 auto trimmed_option = Trim(option);
701 options->emplace_back(trimmed_option);
702 }
703
704 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705}
706
Tom Cherry4aa60b32018-09-05 15:11:44 -0700707// "require partition-exists=x" is a special case, added because of the trouble we had when
708// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
709// missing out new partitions. A device with new partitions can use "partition-exists" to
710// override the fields `optional_if_no_image` in the `images` array.
711static void HandlePartitionExists(const std::vector<std::string>& options) {
712 const std::string& partition_name = options[0];
713 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700714 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700715 (has_slot != "yes" && has_slot != "no")) {
716 die("device doesn't have required partition %s!", partition_name.c_str());
717 }
718 bool known_partition = false;
719 for (size_t i = 0; i < arraysize(images); ++i) {
720 if (images[i].nickname && images[i].nickname == partition_name) {
721 images[i].optional_if_no_image = false;
722 known_partition = true;
723 }
724 }
725 if (!known_partition) {
726 die("device requires partition %s which is not known to this version of fastboot",
727 partition_name.c_str());
728 }
729}
730
731static void CheckRequirements(const std::string& data) {
732 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700733 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
734 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700735 }
736
737 auto lines = Split(data, "\n");
738 for (const auto& line : lines) {
739 if (line.empty()) {
740 continue;
741 }
742
743 std::string name;
744 std::string product;
745 bool invert;
746 std::vector<std::string> options;
747
748 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
749 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
750 continue;
751 }
752 if (name == "partition-exists") {
753 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800754 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700755 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800756 }
757 }
758}
759
Tom Cherry9027af02018-09-24 15:48:09 -0700760static void DisplayVarOrError(const std::string& label, const std::string& var) {
761 std::string value;
762
763 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
764 Status("getvar:" + var);
765 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
766 return;
767 }
768 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
769}
770
771static void DumpInfo() {
772 fprintf(stderr, "--------------------------------------------\n");
773 DisplayVarOrError("Bootloader Version...", "version-bootloader");
774 DisplayVarOrError("Baseband Version.....", "version-baseband");
775 DisplayVarOrError("Serial Number........", "serialno");
776 fprintf(stderr, "--------------------------------------------\n");
777
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778}
779
Tao Bao41cf35f2018-04-24 10:54:21 -0700780static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700781 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700782 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700783
Tao Bao41cf35f2018-04-24 10:54:21 -0700784 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
785 die("invalid max size %" PRId64, max_size);
786 }
787
Elliott Hughesfc797672015-04-07 20:12:50 -0700788 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700789 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700790
Elliott Hughes253c18d2015-03-18 22:47:09 -0700791 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700792 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700793
794 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700795 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700796
797 return out_s;
798}
799
Aaron Wisnerdb511202018-06-26 15:38:35 -0500800static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700801 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700802 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700803 max_download_size.empty()) {
804 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700805 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700806 }
807
Elliott Hughes77c0e662015-11-02 15:51:12 -0800808 // Some bootloaders (angler, for example) send spurious whitespace too.
809 max_download_size = android::base::Trim(max_download_size);
810
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700811 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700812 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800813 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700814 return 0;
815 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700816 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700817 return limit;
818}
819
Aaron Wisnerdb511202018-06-26 15:38:35 -0500820static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700821 int64_t limit = sparse_limit;
822 if (limit == 0) {
823 // Unlimited, so see what the target device's limit is.
824 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700825 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500826 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700827 }
828 if (target_sparse_limit > 0) {
829 limit = target_sparse_limit;
830 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700831 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700832 }
833 }
834
835 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500836 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700837 }
838
839 return 0;
840}
841
Aaron Wisnerdb511202018-06-26 15:38:35 -0500842static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700843 int64_t sz = get_file_size(fd);
844 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700845 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700847
David Anderson32e376f2018-08-16 13:43:11 -0700848 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
849 buf->image_size = sparse_file_len(s, false, false);
850 sparse_file_destroy(s);
851 } else {
852 buf->image_size = sz;
853 }
854
Elliott Hughesa56a7292018-10-26 10:34:53 -0700855 lseek(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500856 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700857 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700858 sparse_file** s = load_sparse_files(fd, limit);
859 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700860 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700861 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700862 buf->type = FB_BUFFER_SPARSE;
863 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700864 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500865 buf->type = FB_BUFFER_FD;
866 buf->data = nullptr;
867 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000868 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700869 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700870
Elliott Hughes53ec4952016-05-11 12:39:27 -0700871 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700872}
873
Aaron Wisnerdb511202018-06-26 15:38:35 -0500874static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500875 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
876
Elliott Hughes53ec4952016-05-11 12:39:27 -0700877 if (fd == -1) {
878 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700879 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500880
881 struct stat s;
882 if (fstat(fd, &s)) {
883 return false;
884 }
885 if (!S_ISREG(s.st_mode)) {
886 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
887 return false;
888 }
889
Aaron Wisnerdb511202018-06-26 15:38:35 -0500890 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700891}
892
David Zeuthenb6ea4352017-08-07 14:29:26 -0400893static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
894 // Buffer needs to be at least the size of the VBMeta struct which
895 // is 256 bytes.
896 if (buf->sz < 256) {
897 return;
898 }
899
Elliott Hughes855cdf82018-04-02 14:24:03 -0700900 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400901
902 std::string data;
903 if (!android::base::ReadFdToString(buf->fd, &data)) {
904 die("Failed reading from vbmeta");
905 }
906
907 // There's a 32-bit big endian |flags| field at offset 120 where
908 // bit 0 corresponds to disable-verity and bit 1 corresponds to
909 // disable-verification.
910 //
911 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
912 // the VBMeta struct.
913 if (g_disable_verity) {
914 data[123] |= 0x01;
915 }
916 if (g_disable_verification) {
917 data[123] |= 0x02;
918 }
919
920 if (!android::base::WriteStringToFd(data, fd)) {
921 die("Failed writing to modified vbmeta");
922 }
923 close(buf->fd);
924 buf->fd = fd;
925 lseek(fd, 0, SEEK_SET);
926}
927
Elliott Hughes5620d222018-03-28 08:20:00 -0700928static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700929{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700930 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700931
David Zeuthenb6ea4352017-08-07 14:29:26 -0400932 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
933 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700934 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400935 rewrite_vbmeta_buffer(buf);
936 }
937
Rom Lemarchand622810c2013-06-28 09:54:59 -0700938 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800939 case FB_BUFFER_SPARSE: {
940 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700941 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700942 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700943 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800944 sparse_files.emplace_back(*s, sz);
945 ++s;
946 }
947
948 for (size_t i = 0; i < sparse_files.size(); ++i) {
949 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700950 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700951 }
952 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800953 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500954 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700955 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700956 break;
957 default:
958 die("unknown buffer type: %d", buf->type);
959 }
960}
961
Aaron Wisnerdb511202018-06-26 15:38:35 -0500962static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700964 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700965 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700966}
967
Aaron Wisnerdb511202018-06-26 15:38:35 -0500968static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700969 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700970 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700971 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
972 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700973 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700974 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700975 return count;
976}
977
Aaron Wisnerdb511202018-06-26 15:38:35 -0500978static bool supports_AB() {
979 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700980}
981
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700982// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983static std::string get_other_slot(const std::string& current_slot, int count) {
984 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700985
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 char next = (current_slot[0] - 'a' + 1)%count + 'a';
987 return std::string(1, next);
988}
989
Aaron Wisnerdb511202018-06-26 15:38:35 -0500990static std::string get_other_slot(const std::string& current_slot) {
991 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992}
993
Aaron Wisnerdb511202018-06-26 15:38:35 -0500994static std::string get_other_slot(int count) {
995 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700996}
997
Aaron Wisnerdb511202018-06-26 15:38:35 -0500998static std::string get_other_slot() {
999 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -07001000}
1001
Aaron Wisnerdb511202018-06-26 15:38:35 -05001002static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001003 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001004 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001005 if (allow_all) {
1006 return "all";
1007 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001008 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001009 if (count > 0) {
1010 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001011 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001012 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001013 }
1014 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001016
Aaron Wisnerdb511202018-06-26 15:38:35 -05001017 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001018 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001019
Daniel Rosenberg80919472016-06-30 19:25:31 -07001020 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001021 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001022 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001023 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001024 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001025 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001026 }
1027
Daniel Rosenberg80919472016-06-30 19:25:31 -07001028 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1029
1030 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1031 for (int i=0; i<count; i++) {
1032 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001033 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001034
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001035 exit(1);
1036}
1037
Aaron Wisnerdb511202018-06-26 15:38:35 -05001038static std::string verify_slot(const std::string& slot) {
1039 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001040}
1041
Aaron Wisnerdb511202018-06-26 15:38:35 -05001042static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001043 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001044 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001045 std::string current_slot;
1046
Tom Cherry9027af02018-09-24 15:48:09 -07001047 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001048 /* If has-slot is not supported, the answer is no. */
1049 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001050 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001051 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001052 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001053 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001054 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001055 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001056 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001057 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001058 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001059 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001060 }
1061 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001062 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001063 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001064 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001065 }
1066 func(part);
1067 }
1068}
1069
David Pursell0b156632015-10-30 11:22:01 -07001070/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1071 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1072 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1073 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001074 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001075static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001076 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001077 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001078
Daniel Rosenberg80919472016-06-30 19:25:31 -07001079 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001080 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001081 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001082 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001083 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001084 for (int i=0; i < get_slot_count(); i++) {
1085 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086 }
1087 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001088 do_for_partition(part, "", 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, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001092 }
1093}
1094
David Anderson629e51c2018-10-25 12:51:25 -07001095static bool is_logical(const std::string& partition) {
1096 std::string value;
1097 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
1098}
1099
Aaron Wisnerdb511202018-06-26 15:38:35 -05001100static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001101 struct fastboot_buffer buf;
1102
Aaron Wisnerdb511202018-06-26 15:38:35 -05001103 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001104 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001105 }
David Anderson629e51c2018-10-25 12:51:25 -07001106 if (is_logical(pname)) {
1107 fb->ResizePartition(pname, std::to_string(buf.image_size));
1108 }
Rom Lemarchand622810c2013-06-28 09:54:59 -07001109 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001110}
1111
Daniel Rosenberg13454092016-06-27 19:43:11 -07001112// Sets slot_override as the active slot. If slot_override is blank,
1113// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001114static void set_active(const std::string& slot_override) {
1115 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001116
Daniel Rosenberg80919472016-06-30 19:25:31 -07001117 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001118 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001119 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001120 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001121 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001122 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001123 }
1124 }
1125}
1126
David Anderson32e376f2018-08-16 13:43:11 -07001127static bool is_userspace_fastboot() {
1128 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001129 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001130}
1131
David Anderson1d887432018-08-27 16:47:32 -07001132static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001133 fb->RebootTo("fastboot");
1134
1135 auto* old_transport = fb->set_transport(nullptr);
1136 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001137
1138 // Give the current connection time to close.
1139 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1140
Tom Cherry9027af02018-09-24 15:48:09 -07001141 fb->set_transport(open_device());
David Anderson1d887432018-08-27 16:47:32 -07001142}
1143
David Andersoncf444f32018-08-29 14:15:49 -07001144class ImageSource {
1145 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001146 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001147 virtual int OpenFile(const std::string& name) const = 0;
1148};
David Anderson32e376f2018-08-16 13:43:11 -07001149
David Andersoncf444f32018-08-29 14:15:49 -07001150class FlashAllTool {
1151 public:
1152 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001153
David Andersoncf444f32018-08-29 14:15:49 -07001154 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001155
David Andersoncf444f32018-08-29 14:15:49 -07001156 private:
1157 void CheckRequirements();
1158 void DetermineSecondarySlot();
1159 void CollectImages();
1160 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1161 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1162 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001163
David Andersoncf444f32018-08-29 14:15:49 -07001164 const ImageSource& source_;
1165 std::string slot_override_;
1166 bool skip_secondary_;
1167 bool wipe_;
1168 std::string secondary_slot_;
1169 std::vector<std::pair<const Image*, std::string>> boot_images_;
1170 std::vector<std::pair<const Image*, std::string>> os_images_;
1171};
1172
1173FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1174 : source_(source),
1175 slot_override_(slot_override),
1176 skip_secondary_(skip_secondary),
1177 wipe_(wipe)
1178{
David Anderson32e376f2018-08-16 13:43:11 -07001179}
1180
David Andersoncf444f32018-08-29 14:15:49 -07001181void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001182 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001183 CheckRequirements();
David Anderson96a9fd42018-11-05 15:21:44 -08001184
1185 // Change the slot first, so we boot into the correct recovery image when
1186 // using fastbootd.
1187 if (slot_override_ == "all") {
1188 set_active("a");
1189 } else {
1190 set_active(slot_override_);
1191 }
1192
David Andersoncf444f32018-08-29 14:15:49 -07001193 DetermineSecondarySlot();
1194 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001195
David Anderson95d40932018-08-28 12:14:33 -07001196 // First flash boot partitions. We allow this to happen either in userspace
1197 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001198 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001199
1200 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001201 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001202
1203 // Resize any logical partition to 0, so each partition is reset to 0
1204 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001205 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001206 auto resize_partition = [](const std::string& partition) -> void {
1207 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001208 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001209 }
1210 };
David Anderson95d40932018-08-28 12:14:33 -07001211 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001212 }
1213
David Anderson95d40932018-08-28 12:14:33 -07001214 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001215 FlashImages(os_images_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001216}
1217
David Andersoncf444f32018-08-29 14:15:49 -07001218void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001219 std::vector<char> contents;
1220 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001221 die("could not read android-info.txt");
1222 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001223 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001224}
1225
1226void FlashAllTool::DetermineSecondarySlot() {
1227 if (skip_secondary_) {
1228 return;
1229 }
David Anderson96a9fd42018-11-05 15:21:44 -08001230 if (slot_override_ != "" && slot_override_ != "all") {
David Andersoncf444f32018-08-29 14:15:49 -07001231 secondary_slot_ = get_other_slot(slot_override_);
1232 } else {
1233 secondary_slot_ = get_other_slot();
1234 }
1235 if (secondary_slot_ == "") {
1236 if (supports_AB()) {
1237 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1238 }
1239 skip_secondary_ = true;
1240 }
1241}
1242
1243void FlashAllTool::CollectImages() {
1244 for (size_t i = 0; i < arraysize(images); ++i) {
1245 std::string slot = slot_override_;
1246 if (images[i].IsSecondary()) {
1247 if (skip_secondary_) {
1248 continue;
1249 }
1250 slot = secondary_slot_;
1251 }
1252 if (images[i].type == ImageType::BootCritical) {
1253 boot_images_.emplace_back(&images[i], slot);
1254 } else if (images[i].type == ImageType::Normal) {
1255 os_images_.emplace_back(&images[i], slot);
1256 }
1257 }
1258}
1259
1260void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1261 for (const auto& [image, slot] : images) {
1262 fastboot_buffer buf;
1263 int fd = source_.OpenFile(image->img_name);
1264 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1265 if (image->optional_if_no_image) {
1266 continue;
1267 }
1268 die("could not load '%s': %s", image->img_name, strerror(errno));
1269 }
1270 FlashImage(*image, slot, &buf);
1271 }
1272}
1273
1274void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1275 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001276 std::vector<char> signature_data;
1277 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001278 fb->Download("signature", signature_data);
1279 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001280 }
1281
1282 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001283 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001284 }
1285 flash_buf(partition_name.c_str(), buf);
1286 };
1287 do_for_partitions(image.part_name, slot, flash, false);
1288}
1289
1290void FlashAllTool::UpdateSuperPartition() {
David Andersoncf444f32018-08-29 14:15:49 -07001291 int fd = source_.OpenFile("super_empty.img");
1292 if (fd < 0) {
1293 return;
1294 }
1295 if (!is_userspace_fastboot()) {
1296 reboot_to_userspace_fastboot();
1297 }
David Anderson0444a8c2018-10-25 12:01:17 -07001298 if (!is_userspace_fastboot()) {
1299 die("Failed to boot into userspace; one or more components might be unbootable.");
1300 }
David Andersoncf444f32018-08-29 14:15:49 -07001301
David Anderson90fe0a42018-11-05 18:01:32 -08001302 std::string super_name;
1303 if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
1304 super_name = "super";
1305 }
1306 fb->Download(super_name, fd, get_file_size(fd));
1307
1308 std::string command = "update-super:" + super_name;
David Andersoncf444f32018-08-29 14:15:49 -07001309 if (wipe_) {
1310 command += ":wipe";
1311 }
Tom Cherry9027af02018-09-24 15:48:09 -07001312 fb->RawCommand(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001313}
1314
1315class ZipImageSource final : public ImageSource {
1316 public:
1317 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001318 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001319 int OpenFile(const std::string& name) const override;
1320
1321 private:
1322 ZipArchiveHandle zip_;
1323};
1324
Tom Cherrydfd85df2018-09-20 14:45:05 -07001325bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1326 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001327}
1328
1329int ZipImageSource::OpenFile(const std::string& name) const {
1330 return unzip_to_file(zip_, name.c_str());
1331}
1332
1333static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001334 ZipArchiveHandle zip;
1335 int error = OpenArchive(filename, &zip);
1336 if (error != 0) {
1337 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1338 }
1339
1340 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1341 tool.Flash();
1342
1343 CloseArchive(zip);
1344}
1345
1346class LocalImageSource final : public ImageSource {
1347 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001348 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001349 int OpenFile(const std::string& name) const override;
1350};
1351
Tom Cherrydfd85df2018-09-20 14:45:05 -07001352bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001353 auto path = find_item_given_name(name);
1354 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001355 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001356 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001357 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001358}
1359
1360int LocalImageSource::OpenFile(const std::string& name) const {
1361 auto path = find_item_given_name(name);
1362 return open(path.c_str(), O_RDONLY);
1363}
1364
1365static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001366 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1367 tool.Flash();
1368}
1369
Elliott Hughesd6365a72017-05-08 18:04:49 -07001370static std::string next_arg(std::vector<std::string>* args) {
1371 if (args->empty()) syntax_error("expected argument");
1372 std::string result = args->front();
1373 args->erase(args->begin());
1374 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001375}
1376
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001377static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001378 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001379
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001380 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001381 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001382 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001383 }
Tom Cherry9027af02018-09-24 15:48:09 -07001384 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001385}
1386
Connor O'Brience16a8a2017-02-06 14:39:31 -08001387static std::string fb_fix_numeric_var(std::string var) {
1388 // Some bootloaders (angler, for example), send spurious leading whitespace.
1389 var = android::base::Trim(var);
1390 // Some bootloaders (hammerhead, for example) use implicit hex.
1391 // This code used to use strtol with base 16.
1392 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1393 return var;
1394}
1395
Aaron Wisnerdb511202018-06-26 15:38:35 -05001396static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001397 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001398 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001399 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001400 return 0;
1401 }
1402 sizeString = fb_fix_numeric_var(sizeString);
1403
1404 unsigned size;
1405 if (!android::base::ParseUint(sizeString, &size)) {
1406 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1407 return 0;
1408 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001409 if ((size & (size - 1)) != 0) {
1410 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001411 return 0;
1412 }
1413 return size;
1414}
1415
Aaron Wisnerdb511202018-06-26 15:38:35 -05001416static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001417 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001418 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001419 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001420 std::string partition_type, partition_size;
1421
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001422 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001423 const char* errMsg = nullptr;
1424 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001425 TemporaryFile output;
1426 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001427
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001428 unsigned int limit = INT_MAX;
1429 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001430 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001431 }
1432 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001433 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001434 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001435
Tom Cherry9027af02018-09-24 15:48:09 -07001436 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001437 errMsg = "Can't determine partition type.\n";
1438 goto failed;
1439 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001440 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001441 if (partition_type != type_override) {
1442 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001443 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001444 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001445 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001446 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001447
Tom Cherry9027af02018-09-24 15:48:09 -07001448 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001449 errMsg = "Unable to get partition size\n";
1450 goto failed;
1451 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001452 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001453 if (partition_size != size_override) {
1454 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001455 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001456 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001457 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001458 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001459 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001460
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001461 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001462 if (!gen) {
1463 if (skip_if_not_supported) {
1464 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001465 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001466 return;
1467 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001468 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1469 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001470 return;
1471 }
1472
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001473 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001474 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001475 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1476 return;
1477 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001478
Connor O'Brience16a8a2017-02-06 14:39:31 -08001479 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001480 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1481 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001482
Jin Qian4a335822017-04-18 16:23:18 -07001483 if (fs_generator_generate(gen, output.path, size, initial_dir,
1484 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001485 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001486 return;
1487 }
1488
Jin Qian4a335822017-04-18 16:23:18 -07001489 fd.reset(open(output.path, O_RDONLY));
1490 if (fd == -1) {
1491 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1492 return;
1493 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001494 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001495 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001496 return;
1497 }
1498 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001499 return;
1500
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001501failed:
1502 if (skip_if_not_supported) {
1503 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001504 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001505 }
Tom Cherry9027af02018-09-24 15:48:09 -07001506 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001507}
1508
Aaron Wisnerdb511202018-06-26 15:38:35 -05001509int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001510 bool wants_wipe = false;
1511 bool wants_reboot = false;
1512 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001513 bool wants_reboot_recovery = false;
1514 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001515 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001516 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001517 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001518 bool set_fbe_marker = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001519 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001520 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001521 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001522
Elliott Hughes577e8b42018-04-05 16:12:47 -07001523 g_boot_img_hdr.kernel_addr = 0x00008000;
1524 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1525 g_boot_img_hdr.second_addr = 0x00f00000;
1526 g_boot_img_hdr.tags_addr = 0x00000100;
1527 g_boot_img_hdr.page_size = 2048;
1528
JP Abgrall7b8970c2013-03-07 17:06:41 -08001529 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001530 {"base", required_argument, 0, 0},
1531 {"cmdline", required_argument, 0, 0},
1532 {"disable-verification", no_argument, 0, 0},
1533 {"disable-verity", no_argument, 0, 0},
1534 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001535 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001536 {"kernel-offset", required_argument, 0, 0},
1537 {"os-patch-level", required_argument, 0, 0},
1538 {"os-version", required_argument, 0, 0},
1539 {"page-size", required_argument, 0, 0},
1540 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001541 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001542 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001543 {"skip-secondary", no_argument, 0, 0},
1544 {"slot", required_argument, 0, 0},
1545 {"tags-offset", required_argument, 0, 0},
1546 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001547 {"verbose", no_argument, 0, 'v'},
1548 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001549#if !defined(_WIN32)
1550 {"wipe-and-use-fbe", no_argument, 0, 0},
1551#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001552 {0, 0, 0, 0}
1553 };
Colin Cross8879f982012-05-22 17:53:34 -07001554
1555 serial = getenv("ANDROID_SERIAL");
1556
Elliott Hughes577e8b42018-04-05 16:12:47 -07001557 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001558 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001559 if (c == 0) {
1560 std::string name{longopts[longindex].name};
1561 if (name == "base") {
1562 g_base_addr = strtoul(optarg, 0, 16);
1563 } else if (name == "cmdline") {
1564 g_cmdline = optarg;
1565 } else if (name == "disable-verification") {
1566 g_disable_verification = true;
1567 } else if (name == "disable-verity") {
1568 g_disable_verity = true;
1569 } else if (name == "header-version") {
1570 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1571 } else if (name == "kernel-offset") {
1572 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1573 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001574 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001575 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001576 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001577 } else if (name == "page-size") {
1578 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1579 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1580 } else if (name == "ramdisk-offset") {
1581 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1582 } else if (name == "skip-reboot") {
1583 skip_reboot = true;
1584 } else if (name == "skip-secondary") {
1585 skip_secondary = true;
1586 } else if (name == "slot") {
1587 slot_override = optarg;
1588 } else if (name == "tags-offset") {
1589 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1590 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001591 setvbuf(stdout, nullptr, _IONBF, 0);
1592 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001593 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001594 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001595 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001596 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001597#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001598 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001599 wants_wipe = true;
1600 set_fbe_marker = true;
1601#endif
1602 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001603 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001604 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001605 } else {
1606 switch (c) {
1607 case 'a':
1608 wants_set_active = true;
1609 if (optarg) next_active = optarg;
1610 break;
1611 case 'h':
1612 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001613 case 'l':
1614 g_long_listing = true;
1615 break;
1616 case 's':
1617 serial = optarg;
1618 break;
1619 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001620 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1621 die("invalid sparse limit %s", optarg);
1622 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001623 break;
1624 case 'v':
1625 set_verbose();
1626 break;
1627 case 'w':
1628 wants_wipe = true;
1629 break;
1630 case '?':
1631 return 1;
1632 default:
1633 abort();
1634 }
Colin Cross8879f982012-05-22 17:53:34 -07001635 }
1636 }
1637
1638 argc -= optind;
1639 argv += optind;
1640
Elliott Hughesd6365a72017-05-08 18:04:49 -07001641 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001642
Colin Cross8fb6e062012-07-24 16:36:41 -07001643 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001644 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001645 return 0;
1646 }
1647
Colin Crossc7b75dc2012-08-29 18:17:06 -07001648 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001649 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001650 }
1651
David Pursell0b156632015-10-30 11:22:01 -07001652 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001653 if (transport == nullptr) {
1654 return 1;
1655 }
Tom Cherry9027af02018-09-24 15:48:09 -07001656 fastboot::DriverCallbacks driver_callbacks = {
1657 .prolog = Status,
1658 .epilog = Epilog,
1659 .info = InfoMessage,
1660 };
1661 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1662 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001663
Elliott Hughes5620d222018-03-28 08:20:00 -07001664 const double start = now();
1665
Aaron Wisnerdb511202018-06-26 15:38:35 -05001666 if (slot_override != "") slot_override = verify_slot(slot_override);
1667 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001668
1669 if (wants_set_active) {
1670 if (next_active == "") {
1671 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001672 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001673 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001674 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001675 } else {
1676 wants_set_active = false;
1677 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001678 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001679 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680 }
1681 }
1682 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001683
Elliott Hughesd6365a72017-05-08 18:04:49 -07001684 std::vector<std::string> args(argv, argv + argc);
1685 while (!args.empty()) {
1686 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001687
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001688 if (command == FB_CMD_GETVAR) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001689 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001690 DisplayVarOrError(variable, variable);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001691 } else if (command == FB_CMD_ERASE) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001692 std::string partition = next_arg(&args);
1693 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001694 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001695 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001696 fs_get_generator(partition_type) != nullptr) {
1697 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1698 partition_type.c_str());
1699 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001700
Tom Cherry9027af02018-09-24 15:48:09 -07001701 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001702 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001703 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001704 } else if (android::base::StartsWith(command, "format")) {
1705 // Parsing for: "format[:[type][:[size]]]"
1706 // Some valid things:
1707 // - select only the size, and leave default fs type:
1708 // format::0x4000000 userdata
1709 // - default fs type and size:
1710 // format userdata
1711 // format:: userdata
1712 std::vector<std::string> pieces = android::base::Split(command, ":");
1713 std::string type_override;
1714 if (pieces.size() > 1) type_override = pieces[1].c_str();
1715 std::string size_override;
1716 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001717
Elliott Hughesd6365a72017-05-08 18:04:49 -07001718 std::string partition = next_arg(&args);
1719
1720 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001721 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001722 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001723 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 } else if (command == "signature") {
1725 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001726 std::vector<char> data;
1727 if (!ReadFileToVector(filename, &data)) {
1728 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1729 }
1730 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001731 fb->Download("signature", data);
1732 fb->RawCommand("signature", "installing signature");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001733 } else if (command == FB_CMD_REBOOT) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001734 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001735
1736 if (args.size() == 1) {
1737 std::string what = next_arg(&args);
1738 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001739 wants_reboot = false;
1740 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001741 } else if (what == "recovery") {
1742 wants_reboot = false;
1743 wants_reboot_recovery = true;
1744 } else if (what == "fastboot") {
1745 wants_reboot = false;
1746 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001747 } else {
1748 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001749 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001750
Elliott Hughesca85df02015-02-25 10:02:00 -08001751 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001752 if (!args.empty()) syntax_error("junk after reboot command");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001753 } else if (command == FB_CMD_REBOOT_BOOTLOADER) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001754 wants_reboot_bootloader = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001755 } else if (command == FB_CMD_REBOOT_RECOVERY) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001756 wants_reboot_recovery = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001757 } else if (command == FB_CMD_REBOOT_FASTBOOT) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001758 wants_reboot_fastboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001759 } else if (command == FB_CMD_CONTINUE) {
Tom Cherry9027af02018-09-24 15:48:09 -07001760 fb->Continue();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001761 } else if (command == FB_CMD_BOOT) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001762 std::string kernel = next_arg(&args);
1763 std::string ramdisk;
1764 if (!args.empty()) ramdisk = next_arg(&args);
1765 std::string second_stage;
1766 if (!args.empty()) second_stage = next_arg(&args);
1767
Tom Cherrydfd85df2018-09-20 14:45:05 -07001768 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001769 fb->Download("boot.img", data);
1770 fb->Boot();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001771 } else if (command == FB_CMD_FLASH) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001772 std::string pname = next_arg(&args);
1773
Elliott Hughes2810d002016-04-25 14:31:18 -07001774 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001775 if (!args.empty()) {
1776 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001777 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001778 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001779 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001780 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001781
1782 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001783 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001784 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001785 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001786 } else if (command == "flash:raw") {
1787 std::string partition = next_arg(&args);
1788 std::string kernel = next_arg(&args);
1789 std::string ramdisk;
1790 if (!args.empty()) ramdisk = next_arg(&args);
1791 std::string second_stage;
1792 if (!args.empty()) second_stage = next_arg(&args);
1793
Tom Cherrydfd85df2018-09-20 14:45:05 -07001794 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1795 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001796 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001797 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001798 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001799 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001800 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001801 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001802 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001803 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001804 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001805 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001806 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001807 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001808 bool slot_all = (slot_override == "all");
1809 if (slot_all) {
1810 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1811 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001812 std::string filename = "update.zip";
1813 if (!args.empty()) {
1814 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001815 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001816 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001817 wants_reboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001818 } else if (command == FB_CMD_SET_ACTIVE) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001819 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001820 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001821 } else if (command == "stage") {
1822 std::string filename = next_arg(&args);
1823
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001824 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001825 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001826 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001827 }
Tom Cherry9027af02018-09-24 15:48:09 -07001828 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001829 } else if (command == "get_staged") {
1830 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001831 fb->Upload(filename);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001832 } else if (command == FB_CMD_OEM) {
1833 do_oem_command(FB_CMD_OEM, &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001834 } else if (command == "flashing") {
1835 if (args.empty()) {
1836 syntax_error("missing 'flashing' command");
1837 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1838 args[0] == "unlock_critical" ||
1839 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001840 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001841 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001842 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001843 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001844 }
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001845 } else if (command == FB_CMD_CREATE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001846 std::string partition = next_arg(&args);
1847 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001848 fb->CreatePartition(partition, size);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001849 } else if (command == FB_CMD_DELETE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001850 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001851 fb->DeletePartition(partition);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001852 } else if (command == FB_CMD_RESIZE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001853 std::string partition = next_arg(&args);
1854 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001855 fb->ResizePartition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001856 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001857 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001858 }
1859 }
1860
1861 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001862 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1863 for (const auto& partition : partitions) {
1864 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001865 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1866 continue;
1867 }
Paul Crowley4d170062018-04-24 17:06:30 -07001868 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001869 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001870 if (partition == "userdata" && set_fbe_marker) {
1871 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1872 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001873 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001874 delete_fbemarker_tmpdir(initial_userdata_dir);
1875 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001876 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001877 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001878 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001879 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001880 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001881 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001882 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001883 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001884 fb->Reboot();
1885 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001886 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001887 fb->RebootTo("bootloader");
1888 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001889 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001890 fb->RebootTo("recovery");
1891 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001892 } else if (wants_reboot_fastboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001893 fb->RebootTo("fastboot");
1894 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001895 }
1896
Elliott Hughes5620d222018-03-28 08:20:00 -07001897 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001898
Tom Cherry9027af02018-09-24 15:48:09 -07001899 auto* old_transport = fb->set_transport(nullptr);
1900 delete old_transport;
1901
Tom Cherry11f12092018-08-29 21:36:28 -07001902 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001903}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001904
Aaron Wisnerdb511202018-06-26 15:38:35 -05001905void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001906 unsigned year, month, day;
1907 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1908 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1909 }
1910 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1911 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1912 hdr->SetOsPatchLevel(year, month);
1913}
1914
Aaron Wisnerdb511202018-06-26 15:38:35 -05001915void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001916 unsigned major = 0, minor = 0, patch = 0;
1917 std::vector<std::string> versions = android::base::Split(arg, ".");
1918 if (versions.size() < 1 || versions.size() > 3 ||
1919 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1920 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1921 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1922 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1923 syntax_error("bad OS version: %s", arg);
1924 }
1925 hdr->SetOsVersion(major, minor, patch);
1926}