blob: d5e88a79274b35773e5292fee8551f60ab0f8a5b [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Tom Cherry9027af02018-09-24 15:48:09 -070029#include "fastboot.h"
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Tom Cherry4aa60b32018-09-05 15:11:44 -070048#include <regex>
Elliott Hughes290a2282016-11-14 17:08:47 -080049#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080050#include <utility>
51#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070052
Elliott Hughes82ff3152016-08-31 15:07:18 -070053#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070054#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080056#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070057#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080058#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050059#include <android-base/unique_fd.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070060#include <build/version.h>
David Anderson89569642018-11-16 15:53:35 -080061#include <liblp/liblp.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070062#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 Andersone0e693c2018-11-27 20:19:26 -0800165static char* get_android_product_out() {
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') {
David Andersone0e693c2018-11-27 20:19:26 -0800168 return nullptr;
169 }
170 return dir;
171}
172
173static std::string find_item_given_name(const std::string& img_name) {
174 char* dir = get_android_product_out();
175 if (!dir) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700176 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700177 }
David Anderson0debda02018-08-28 13:54:03 -0700178 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700179}
180
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700181static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700182 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700183 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700184 return find_item_given_name(images[i].img_name);
185 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 }
187
Elliott Hughesd6365a72017-05-08 18:04:49 -0700188 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700189 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190}
191
Tom Cherry9027af02018-09-24 15:48:09 -0700192double last_start_time;
193
194static void Status(const std::string& message) {
195 static constexpr char kStatusFormat[] = "%-50s ";
196 fprintf(stderr, kStatusFormat, message.c_str());
197 last_start_time = now();
198}
199
200static void Epilog(int status) {
201 if (status) {
202 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
203 die("Command failed");
204 } else {
205 double split = now();
206 fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
207 }
208}
209
210static void InfoMessage(const std::string& info) {
211 fprintf(stderr, "(bootloader) %s\n", info.c_str());
212}
213
Elliott Hughesfc797672015-04-07 20:12:50 -0700214static int64_t get_file_size(int fd) {
215 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700216 if (fstat(fd, &sb) == -1) {
217 die("could not get file size");
218 }
219 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700220}
221
Tom Cherrydfd85df2018-09-20 14:45:05 -0700222bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
223 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224
Tom Cherrydfd85df2018-09-20 14:45:05 -0700225 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
226 if (fd == -1) {
227 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700228 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800229
Tom Cherrydfd85df2018-09-20 14:45:05 -0700230 out->resize(get_file_size(fd));
231 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700232}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233
Elliott Hughesfc797672015-04-07 20:12:50 -0700234static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700235 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
236 return -1;
237 }
238
Scott Anderson13081c62012-04-06 12:39:30 -0700239 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700241 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
242 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 return 0;
244}
245
Elliott Hughesfc797672015-04-07 20:12:50 -0700246static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800247 return match_fastboot_with_serial(info, serial);
248}
249
Elliott Hughesfc797672015-04-07 20:12:50 -0700250static int list_devices_callback(usb_ifc_info* info) {
251 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800252 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700253 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800254 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700255 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 if (!serial[0]) {
257 serial = "????????????";
258 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700259 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700260 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800261 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700262 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800263 printf("%-22s fastboot", serial.c_str());
264 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700265 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800266 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267 }
268
269 return -1;
270}
271
David Pursell2ec418a2016-01-20 08:32:08 -0800272// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
273// a specific device, otherwise the first USB device found will be used.
274//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700275// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800276// Otherwise it blocks until the target is available.
277//
278// The returned Transport is a singleton, so multiple calls to this function will return the same
279// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700280static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800281 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282
David Pursell4601c972016-02-05 15:35:09 -0800283 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800284 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800285 int port = 0;
286 if (serial != nullptr) {
287 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800288
David Pursell4601c972016-02-05 15:35:09 -0800289 if (android::base::StartsWith(serial, "tcp:")) {
290 protocol = Socket::Protocol::kTcp;
291 port = tcp::kDefaultPort;
292 net_address = serial + strlen("tcp:");
293 } else if (android::base::StartsWith(serial, "udp:")) {
294 protocol = Socket::Protocol::kUdp;
295 port = udp::kDefaultPort;
296 net_address = serial + strlen("udp:");
297 }
298
299 if (net_address != nullptr) {
300 std::string error;
301 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700302 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800303 }
David Pursell2ec418a2016-01-20 08:32:08 -0800304 }
305 }
306
David Anderson1d887432018-08-27 16:47:32 -0700307 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800308 while (true) {
309 if (!host.empty()) {
310 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800311 if (protocol == Socket::Protocol::kTcp) {
312 transport = tcp::Connect(host, port, &error).release();
313 } else if (protocol == Socket::Protocol::kUdp) {
314 transport = udp::Connect(host, port, &error).release();
315 }
316
David Pursell2ec418a2016-01-20 08:32:08 -0800317 if (transport == nullptr && announce) {
318 fprintf(stderr, "error: %s\n", error.c_str());
319 }
320 } else {
321 transport = usb_open(match_fastboot);
322 }
323
324 if (transport != nullptr) {
325 return transport;
326 }
327
David Pursell0b156632015-10-30 11:22:01 -0700328 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800329 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700330 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800332 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 }
334}
335
Elliott Hughesfc797672015-04-07 20:12:50 -0700336static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 // We don't actually open a USB device here,
338 // just getting our callback called so we can
339 // list all the connected devices.
340 usb_open(list_devices_callback);
341}
342
Elliott Hughesd6365a72017-05-08 18:04:49 -0700343static void syntax_error(const char* fmt, ...) {
344 fprintf(stderr, "fastboot: usage: ");
345
346 va_list ap;
347 va_start(ap, fmt);
348 vfprintf(stderr, fmt, ap);
349 va_end(ap);
350
351 fprintf(stderr, "\n");
352 exit(1);
353}
354
355static int show_help() {
356 // clang-format off
357 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700358// 1 2 3 4 5 6 7 8
359// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700360 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700362 "flashing:\n"
363 " update ZIP Flash all partitions from an update.zip package.\n"
364 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
365 " On A/B devices, flashed slot is set as active.\n"
366 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700367 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
368 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700369 "\n"
370 "basics:\n"
371 " devices [-l] List devices in bootloader (-l: with device paths).\n"
372 " getvar NAME Display given bootloader variable.\n"
373 " reboot [bootloader] Reboot device.\n"
374 "\n"
375 "locking/unlocking:\n"
376 " flashing lock|unlock Lock/unlock partitions for flashing\n"
377 " flashing lock_critical|unlock_critical\n"
378 " Lock/unlock 'critical' bootloader partitions.\n"
379 " flashing get_unlock_ability\n"
380 " Check whether unlocking is allowed (1) or not(0).\n"
381 "\n"
382 "advanced:\n"
383 " erase PARTITION Erase a flash partition.\n"
384 " format[:FS_TYPE[:SIZE]] PARTITION\n"
385 " Format a flash partition.\n"
386 " set_active SLOT Set the active slot.\n"
387 " oem [COMMAND...] Execute OEM-specific command.\n"
388 "\n"
389 "boot image:\n"
390 " boot KERNEL [RAMDISK [SECOND]]\n"
391 " Download and boot kernel from RAM.\n"
392 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
393 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700394 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
396 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
397 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
398 " --tags-offset Set tags offset (default: 0x00000100).\n"
399 " --page-size BYTES Set flash page size (default: 2048).\n"
400 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700401 " --os-version MAJOR[.MINOR[.PATCH]]\n"
402 " Set boot image OS version (default: 0.0.0).\n"
403 " --os-patch-level YYYY-MM-DD\n"
404 " Set boot image OS security patch level.\n"
405 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700406 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700407 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700408 //" continue Continue with autoboot.\n"
409 //"\n"
410 "Android Things:\n"
411 " stage IN_FILE Sends given file to stage for the next command.\n"
412 " 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 -0800413 "\n"
414 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700415 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700416 " -s SERIAL Specify a USB device.\n"
417 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700418 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
David Anderson89569642018-11-16 15:53:35 -0800419 " --force Force a flash operation that may be unsafe.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700420 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
421 " non-current slot (default: current active slot).\n"
422 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
423 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
424 " --skip-reboot Don't reboot device after flashing.\n"
425 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
426 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000427#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700428 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000429#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700430 // TODO: remove --unbuffered?
431 " --unbuffered Don't buffer input or output.\n"
432 " --verbose, -v Verbose output.\n"
433 " --version Display version.\n"
434 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700436 // clang-format off
437 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000439
Tom Cherrydfd85df2018-09-20 14:45:05 -0700440static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
441 const std::string& second_stage) {
442 std::vector<char> kernel_data;
443 if (!ReadFileToVector(kernel, &kernel_data)) {
444 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
445 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800446
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700448 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800449 die("cannot load '%s': too short", kernel.c_str());
450 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700451 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700452 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700453 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700454 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800455
Elliott Hughes4089d342017-10-27 14:21:12 -0700456 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800457
Tom Cherrydfd85df2018-09-20 14:45:05 -0700458 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 }
460
Tom Cherrydfd85df2018-09-20 14:45:05 -0700461 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700462 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700463 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
464 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
465 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 }
467
Tom Cherrydfd85df2018-09-20 14:45:05 -0700468 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700469 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700470 if (!ReadFileToVector(second_stage, &second_stage_data)) {
471 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
472 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200473 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700475
Tom Cherrydfd85df2018-09-20 14:45:05 -0700476 std::vector<char> out;
477 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
478 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800479
Tom Cherrydfd85df2018-09-20 14:45:05 -0700480 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
481 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
482
483 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800484}
485
Tom Cherrydfd85df2018-09-20 14:45:05 -0700486static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
487 std::vector<char>* out) {
488 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700489 ZipEntry zip_entry;
490 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700491 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
492 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493 }
494
Tom Cherrydfd85df2018-09-20 14:45:05 -0700495 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496
Tom Cherrydfd85df2018-09-20 14:45:05 -0700497 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
498 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499
Tom Cherrydfd85df2018-09-20 14:45:05 -0700500 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
501 out->size());
502 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000503
Tom Cherrydfd85df2018-09-20 14:45:05 -0700504 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505}
506
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700507#if defined(_WIN32)
508
509// TODO: move this to somewhere it can be shared.
510
511#include <windows.h>
512
513// Windows' tmpfile(3) requires administrator rights because
514// it creates temporary files in the root directory.
515static FILE* win32_tmpfile() {
516 char temp_path[PATH_MAX];
517 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
518 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700519 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700520 }
521
522 char filename[PATH_MAX];
523 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700524 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700525 }
526
527 return fopen(filename, "w+bTD");
528}
529
530#define tmpfile win32_tmpfile
531
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000532static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700533 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000534}
535
Elliott Hughes855cdf82018-04-02 14:24:03 -0700536static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700537 // TODO: reimplement to avoid leaking a FILE*.
538 return fileno(tmpfile());
539}
540
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000541#else
542
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700543static std::string make_temporary_template() {
544 const char* tmpdir = getenv("TMPDIR");
545 if (tmpdir == nullptr) tmpdir = P_tmpdir;
546 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
547}
548
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000549static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700550 std::string result(make_temporary_template());
551 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700552 die("unable to create temporary directory with template %s: %s",
553 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000554 }
555 return result;
556}
557
Elliott Hughes855cdf82018-04-02 14:24:03 -0700558static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700559 std::string path_template(make_temporary_template());
560 int fd = mkstemp(&path_template[0]);
561 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700562 die("failed to create temporary file for %s with template %s: %s\n",
563 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700564 }
565 unlink(path_template.c_str());
566 return fd;
567}
568
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700569#endif
570
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000571static std::string create_fbemarker_tmpdir() {
572 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000573 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
574 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
575 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700576 die("unable to create FBE marker file %s locally: %s",
577 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000578 }
579 close(fd);
580 return dir;
581}
582
583static void delete_fbemarker_tmpdir(const std::string& dir) {
584 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
585 if (unlink(marker_file.c_str()) == -1) {
586 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
587 marker_file.c_str(), errno, strerror(errno));
588 return;
589 }
590 if (rmdir(dir.c_str()) == -1) {
591 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
592 dir.c_str(), errno, strerror(errno));
593 return;
594 }
595}
596
Elliott Hughes45964a82017-05-03 22:43:23 -0700597static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700598 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700599
Yusuke Sato07447542015-06-25 14:39:19 -0700600 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700601 ZipEntry zip_entry;
602 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
603 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700604 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000605 return -1;
606 }
607
Elliott Hughes23af1122017-11-10 08:42:17 -0800608 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700609 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800610 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700611 int error = ExtractEntryToFile(zip, &zip_entry, fd);
612 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800613 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700614 }
615
Elliott Hughes4089d342017-10-27 14:21:12 -0700616 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800617 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700618 }
619
Elliott Hughes23af1122017-11-10 08:42:17 -0800620 fprintf(stderr, " took %.3fs\n", now() - start);
621
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700622 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700623}
624
Tom Cherry4aa60b32018-09-05 15:11:44 -0700625static void CheckRequirement(const std::string& cur_product, const std::string& var,
626 const std::string& product, bool invert,
627 const std::vector<std::string>& options) {
628 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700629
Tom Cherry4aa60b32018-09-05 15:11:44 -0700630 double start = now();
631
632 if (!product.empty()) {
633 if (product != cur_product) {
634 double split = now();
635 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
636 cur_product.c_str(), product.c_str(), (split - start));
637 return;
638 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800639 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700640
641 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700642 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700643 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700644 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
645 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700646 die("requirements not met!");
647 }
648
649 bool match = false;
650 for (const auto& option : options) {
651 if (option == var_value || (option.back() == '*' &&
652 !var_value.compare(0, option.length() - 1, option, 0,
653 option.length() - 1))) {
654 match = true;
655 break;
656 }
657 }
658
659 if (invert) {
660 match = !match;
661 }
662
663 if (match) {
664 double split = now();
665 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
666 return;
667 }
668
669 fprintf(stderr, "FAILED\n\n");
670 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
671 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
672 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
673 fprintf(stderr, " or '%s'", it->c_str());
674 }
675 fprintf(stderr, ".\n\n");
676 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677}
678
Tom Cherry4aa60b32018-09-05 15:11:44 -0700679bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
680 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700681 // "require product=alpha|beta|gamma"
682 // "require version-bootloader=1234"
683 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
684 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700685 *product = "";
686 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700687
Tom Cherry4aa60b32018-09-05 15:11:44 -0700688 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
689 auto require_product_regex =
690 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
691 std::smatch match_results;
692
693 if (std::regex_match(line, match_results, require_reject_regex)) {
694 *invert = Trim(match_results[1]) == "reject";
695 } else if (std::regex_match(line, match_results, require_product_regex)) {
696 *product = match_results[1];
697 } else {
698 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699 }
700
Tom Cherry4aa60b32018-09-05 15:11:44 -0700701 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700702 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700703 if (*name == "board") {
704 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800705 }
706
Tom Cherry4aa60b32018-09-05 15:11:44 -0700707 auto raw_options = Split(match_results[3], "|");
708 for (const auto& option : raw_options) {
709 auto trimmed_option = Trim(option);
710 options->emplace_back(trimmed_option);
711 }
712
713 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714}
715
Tom Cherry4aa60b32018-09-05 15:11:44 -0700716// "require partition-exists=x" is a special case, added because of the trouble we had when
717// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
718// missing out new partitions. A device with new partitions can use "partition-exists" to
719// override the fields `optional_if_no_image` in the `images` array.
720static void HandlePartitionExists(const std::vector<std::string>& options) {
721 const std::string& partition_name = options[0];
722 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700723 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700724 (has_slot != "yes" && has_slot != "no")) {
725 die("device doesn't have required partition %s!", partition_name.c_str());
726 }
727 bool known_partition = false;
728 for (size_t i = 0; i < arraysize(images); ++i) {
729 if (images[i].nickname && images[i].nickname == partition_name) {
730 images[i].optional_if_no_image = false;
731 known_partition = true;
732 }
733 }
734 if (!known_partition) {
735 die("device requires partition %s which is not known to this version of fastboot",
736 partition_name.c_str());
737 }
738}
739
740static void CheckRequirements(const std::string& data) {
741 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700742 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
743 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700744 }
745
746 auto lines = Split(data, "\n");
747 for (const auto& line : lines) {
748 if (line.empty()) {
749 continue;
750 }
751
752 std::string name;
753 std::string product;
754 bool invert;
755 std::vector<std::string> options;
756
757 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
758 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
759 continue;
760 }
761 if (name == "partition-exists") {
762 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800763 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700764 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800765 }
766 }
767}
768
Tom Cherry9027af02018-09-24 15:48:09 -0700769static void DisplayVarOrError(const std::string& label, const std::string& var) {
770 std::string value;
771
772 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
773 Status("getvar:" + var);
774 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
775 return;
776 }
777 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
778}
779
780static void DumpInfo() {
781 fprintf(stderr, "--------------------------------------------\n");
782 DisplayVarOrError("Bootloader Version...", "version-bootloader");
783 DisplayVarOrError("Baseband Version.....", "version-baseband");
784 DisplayVarOrError("Serial Number........", "serialno");
785 fprintf(stderr, "--------------------------------------------\n");
786
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800787}
788
Tao Bao41cf35f2018-04-24 10:54:21 -0700789static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700790 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700791 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700792
Tao Bao41cf35f2018-04-24 10:54:21 -0700793 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
794 die("invalid max size %" PRId64, max_size);
795 }
796
Elliott Hughesfc797672015-04-07 20:12:50 -0700797 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700798 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700799
Elliott Hughes253c18d2015-03-18 22:47:09 -0700800 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700801 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700802
803 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700804 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700805
806 return out_s;
807}
808
Aaron Wisnerdb511202018-06-26 15:38:35 -0500809static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700810 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700811 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700812 max_download_size.empty()) {
813 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700814 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700815 }
816
Elliott Hughes77c0e662015-11-02 15:51:12 -0800817 // Some bootloaders (angler, for example) send spurious whitespace too.
818 max_download_size = android::base::Trim(max_download_size);
819
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700820 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700821 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800822 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700823 return 0;
824 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700825 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700826 return limit;
827}
828
Aaron Wisnerdb511202018-06-26 15:38:35 -0500829static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700830 int64_t limit = sparse_limit;
831 if (limit == 0) {
832 // Unlimited, so see what the target device's limit is.
833 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700834 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500835 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700836 }
837 if (target_sparse_limit > 0) {
838 limit = target_sparse_limit;
839 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700840 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700841 }
842 }
843
844 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500845 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700846 }
847
848 return 0;
849}
850
Aaron Wisnerdb511202018-06-26 15:38:35 -0500851static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700852 int64_t sz = get_file_size(fd);
853 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700854 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700856
David Anderson32e376f2018-08-16 13:43:11 -0700857 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
858 buf->image_size = sparse_file_len(s, false, false);
859 sparse_file_destroy(s);
860 } else {
861 buf->image_size = sz;
862 }
863
Elliott Hughesa56a7292018-10-26 10:34:53 -0700864 lseek(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500865 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700866 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700867 sparse_file** s = load_sparse_files(fd, limit);
868 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700869 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700870 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700871 buf->type = FB_BUFFER_SPARSE;
872 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700873 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500874 buf->type = FB_BUFFER_FD;
875 buf->data = nullptr;
876 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000877 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700878 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700879
Elliott Hughes53ec4952016-05-11 12:39:27 -0700880 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700881}
882
Aaron Wisnerdb511202018-06-26 15:38:35 -0500883static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500884 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
885
Elliott Hughes53ec4952016-05-11 12:39:27 -0700886 if (fd == -1) {
887 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700888 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500889
890 struct stat s;
891 if (fstat(fd, &s)) {
892 return false;
893 }
894 if (!S_ISREG(s.st_mode)) {
895 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
896 return false;
897 }
898
Aaron Wisnerdb511202018-06-26 15:38:35 -0500899 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700900}
901
David Zeuthenb6ea4352017-08-07 14:29:26 -0400902static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
903 // Buffer needs to be at least the size of the VBMeta struct which
904 // is 256 bytes.
905 if (buf->sz < 256) {
906 return;
907 }
908
Elliott Hughes855cdf82018-04-02 14:24:03 -0700909 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400910
911 std::string data;
912 if (!android::base::ReadFdToString(buf->fd, &data)) {
913 die("Failed reading from vbmeta");
914 }
915
916 // There's a 32-bit big endian |flags| field at offset 120 where
917 // bit 0 corresponds to disable-verity and bit 1 corresponds to
918 // disable-verification.
919 //
920 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
921 // the VBMeta struct.
922 if (g_disable_verity) {
923 data[123] |= 0x01;
924 }
925 if (g_disable_verification) {
926 data[123] |= 0x02;
927 }
928
929 if (!android::base::WriteStringToFd(data, fd)) {
930 die("Failed writing to modified vbmeta");
931 }
932 close(buf->fd);
933 buf->fd = fd;
934 lseek(fd, 0, SEEK_SET);
935}
936
Elliott Hughes5620d222018-03-28 08:20:00 -0700937static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700938{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700939 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700940
David Zeuthenb6ea4352017-08-07 14:29:26 -0400941 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
942 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700943 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400944 rewrite_vbmeta_buffer(buf);
945 }
946
Rom Lemarchand622810c2013-06-28 09:54:59 -0700947 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800948 case FB_BUFFER_SPARSE: {
949 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700950 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700951 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700952 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800953 sparse_files.emplace_back(*s, sz);
954 ++s;
955 }
956
957 for (size_t i = 0; i < sparse_files.size(); ++i) {
958 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700959 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700960 }
961 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800962 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500963 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700964 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700965 break;
966 default:
967 die("unknown buffer type: %d", buf->type);
968 }
969}
970
Aaron Wisnerdb511202018-06-26 15:38:35 -0500971static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700973 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700974 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700975}
976
Aaron Wisnerdb511202018-06-26 15:38:35 -0500977static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700978 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700979 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700980 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
981 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700982 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700984 return count;
985}
986
Aaron Wisnerdb511202018-06-26 15:38:35 -0500987static bool supports_AB() {
988 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700989}
990
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700991// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992static std::string get_other_slot(const std::string& current_slot, int count) {
993 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700994
Daniel Rosenberg80919472016-06-30 19:25:31 -0700995 char next = (current_slot[0] - 'a' + 1)%count + 'a';
996 return std::string(1, next);
997}
998
Aaron Wisnerdb511202018-06-26 15:38:35 -0500999static std::string get_other_slot(const std::string& current_slot) {
1000 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001}
1002
Aaron Wisnerdb511202018-06-26 15:38:35 -05001003static std::string get_other_slot(int count) {
1004 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005}
1006
Aaron Wisnerdb511202018-06-26 15:38:35 -05001007static std::string get_other_slot() {
1008 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -07001009}
1010
Aaron Wisnerdb511202018-06-26 15:38:35 -05001011static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001014 if (allow_all) {
1015 return "all";
1016 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001017 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018 if (count > 0) {
1019 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001020 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001021 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001022 }
1023 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001024 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001025
Aaron Wisnerdb511202018-06-26 15:38:35 -05001026 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001027 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001028
Daniel Rosenberg80919472016-06-30 19:25:31 -07001029 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001030 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001031 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001032 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001033 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001034 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001035 }
1036
Daniel Rosenberg80919472016-06-30 19:25:31 -07001037 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1038
1039 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1040 for (int i=0; i<count; i++) {
1041 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001042 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001043
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001044 exit(1);
1045}
1046
Aaron Wisnerdb511202018-06-26 15:38:35 -05001047static std::string verify_slot(const std::string& slot) {
1048 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001049}
1050
Aaron Wisnerdb511202018-06-26 15:38:35 -05001051static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001052 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001053 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001054 std::string current_slot;
1055
Tom Cherry9027af02018-09-24 15:48:09 -07001056 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001057 /* If has-slot is not supported, the answer is no. */
1058 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001059 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001060 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001061 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001062 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001063 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001064 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001065 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001066 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001067 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001068 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001069 }
1070 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001071 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001072 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001073 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001074 }
1075 func(part);
1076 }
1077}
1078
David Pursell0b156632015-10-30 11:22:01 -07001079/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1080 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1081 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1082 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001083 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001084static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001085 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001086 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001087
Daniel Rosenberg80919472016-06-30 19:25:31 -07001088 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001089 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001090 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001091 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001092 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001093 for (int i=0; i < get_slot_count(); i++) {
1094 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001095 }
1096 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001097 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001098 }
1099 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001100 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001101 }
1102}
1103
David Anderson629e51c2018-10-25 12:51:25 -07001104static bool is_logical(const std::string& partition) {
1105 std::string value;
1106 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
1107}
1108
Aaron Wisnerdb511202018-06-26 15:38:35 -05001109static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001110 struct fastboot_buffer buf;
1111
Aaron Wisnerdb511202018-06-26 15:38:35 -05001112 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001113 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001114 }
David Anderson629e51c2018-10-25 12:51:25 -07001115 if (is_logical(pname)) {
1116 fb->ResizePartition(pname, std::to_string(buf.image_size));
1117 }
Rom Lemarchand622810c2013-06-28 09:54:59 -07001118 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001119}
1120
Daniel Rosenberg13454092016-06-27 19:43:11 -07001121// Sets slot_override as the active slot. If slot_override is blank,
1122// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001123static void set_active(const std::string& slot_override) {
1124 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001125
Daniel Rosenberg80919472016-06-30 19:25:31 -07001126 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001127 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001128 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001129 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001130 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001131 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001132 }
1133 }
1134}
1135
David Anderson32e376f2018-08-16 13:43:11 -07001136static bool is_userspace_fastboot() {
1137 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001138 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001139}
1140
David Anderson1d887432018-08-27 16:47:32 -07001141static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001142 fb->RebootTo("fastboot");
1143
1144 auto* old_transport = fb->set_transport(nullptr);
1145 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001146
1147 // Give the current connection time to close.
1148 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1149
Tom Cherry9027af02018-09-24 15:48:09 -07001150 fb->set_transport(open_device());
David Anderson1d887432018-08-27 16:47:32 -07001151}
1152
David Andersoncf444f32018-08-29 14:15:49 -07001153class ImageSource {
1154 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001155 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001156 virtual int OpenFile(const std::string& name) const = 0;
1157};
David Anderson32e376f2018-08-16 13:43:11 -07001158
David Andersoncf444f32018-08-29 14:15:49 -07001159class FlashAllTool {
1160 public:
1161 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001162
David Andersoncf444f32018-08-29 14:15:49 -07001163 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001164
David Andersoncf444f32018-08-29 14:15:49 -07001165 private:
1166 void CheckRequirements();
1167 void DetermineSecondarySlot();
1168 void CollectImages();
1169 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1170 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1171 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001172
David Andersoncf444f32018-08-29 14:15:49 -07001173 const ImageSource& source_;
1174 std::string slot_override_;
1175 bool skip_secondary_;
1176 bool wipe_;
1177 std::string secondary_slot_;
1178 std::vector<std::pair<const Image*, std::string>> boot_images_;
1179 std::vector<std::pair<const Image*, std::string>> os_images_;
1180};
1181
1182FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1183 : source_(source),
1184 slot_override_(slot_override),
1185 skip_secondary_(skip_secondary),
1186 wipe_(wipe)
1187{
David Anderson32e376f2018-08-16 13:43:11 -07001188}
1189
David Andersoncf444f32018-08-29 14:15:49 -07001190void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001191 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001192 CheckRequirements();
David Anderson96a9fd42018-11-05 15:21:44 -08001193
1194 // Change the slot first, so we boot into the correct recovery image when
1195 // using fastbootd.
1196 if (slot_override_ == "all") {
1197 set_active("a");
1198 } else {
1199 set_active(slot_override_);
1200 }
1201
David Andersoncf444f32018-08-29 14:15:49 -07001202 DetermineSecondarySlot();
1203 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001204
David Anderson95d40932018-08-28 12:14:33 -07001205 // First flash boot partitions. We allow this to happen either in userspace
1206 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001207 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001208
1209 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001210 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001211
1212 // Resize any logical partition to 0, so each partition is reset to 0
1213 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001214 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001215 auto resize_partition = [](const std::string& partition) -> void {
1216 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001217 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001218 }
1219 };
David Anderson95d40932018-08-28 12:14:33 -07001220 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001221 }
1222
David Anderson95d40932018-08-28 12:14:33 -07001223 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001224 FlashImages(os_images_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001225}
1226
David Andersoncf444f32018-08-29 14:15:49 -07001227void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001228 std::vector<char> contents;
1229 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001230 die("could not read android-info.txt");
1231 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001232 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001233}
1234
1235void FlashAllTool::DetermineSecondarySlot() {
1236 if (skip_secondary_) {
1237 return;
1238 }
David Anderson96a9fd42018-11-05 15:21:44 -08001239 if (slot_override_ != "" && slot_override_ != "all") {
David Andersoncf444f32018-08-29 14:15:49 -07001240 secondary_slot_ = get_other_slot(slot_override_);
1241 } else {
1242 secondary_slot_ = get_other_slot();
1243 }
1244 if (secondary_slot_ == "") {
1245 if (supports_AB()) {
1246 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1247 }
1248 skip_secondary_ = true;
1249 }
1250}
1251
1252void FlashAllTool::CollectImages() {
1253 for (size_t i = 0; i < arraysize(images); ++i) {
1254 std::string slot = slot_override_;
1255 if (images[i].IsSecondary()) {
1256 if (skip_secondary_) {
1257 continue;
1258 }
1259 slot = secondary_slot_;
1260 }
1261 if (images[i].type == ImageType::BootCritical) {
1262 boot_images_.emplace_back(&images[i], slot);
1263 } else if (images[i].type == ImageType::Normal) {
1264 os_images_.emplace_back(&images[i], slot);
1265 }
1266 }
1267}
1268
1269void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1270 for (const auto& [image, slot] : images) {
1271 fastboot_buffer buf;
1272 int fd = source_.OpenFile(image->img_name);
1273 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1274 if (image->optional_if_no_image) {
1275 continue;
1276 }
1277 die("could not load '%s': %s", image->img_name, strerror(errno));
1278 }
1279 FlashImage(*image, slot, &buf);
1280 }
1281}
1282
1283void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1284 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001285 std::vector<char> signature_data;
1286 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001287 fb->Download("signature", signature_data);
1288 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001289 }
1290
1291 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001292 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001293 }
1294 flash_buf(partition_name.c_str(), buf);
1295 };
1296 do_for_partitions(image.part_name, slot, flash, false);
1297}
1298
1299void FlashAllTool::UpdateSuperPartition() {
David Andersoncf444f32018-08-29 14:15:49 -07001300 int fd = source_.OpenFile("super_empty.img");
1301 if (fd < 0) {
1302 return;
1303 }
1304 if (!is_userspace_fastboot()) {
1305 reboot_to_userspace_fastboot();
1306 }
David Anderson0444a8c2018-10-25 12:01:17 -07001307 if (!is_userspace_fastboot()) {
1308 die("Failed to boot into userspace; one or more components might be unbootable.");
1309 }
David Andersoncf444f32018-08-29 14:15:49 -07001310
David Anderson90fe0a42018-11-05 18:01:32 -08001311 std::string super_name;
1312 if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
1313 super_name = "super";
1314 }
1315 fb->Download(super_name, fd, get_file_size(fd));
1316
1317 std::string command = "update-super:" + super_name;
David Andersoncf444f32018-08-29 14:15:49 -07001318 if (wipe_) {
1319 command += ":wipe";
1320 }
Tom Cherry9027af02018-09-24 15:48:09 -07001321 fb->RawCommand(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001322}
1323
1324class ZipImageSource final : public ImageSource {
1325 public:
1326 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001327 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001328 int OpenFile(const std::string& name) const override;
1329
1330 private:
1331 ZipArchiveHandle zip_;
1332};
1333
Tom Cherrydfd85df2018-09-20 14:45:05 -07001334bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1335 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001336}
1337
1338int ZipImageSource::OpenFile(const std::string& name) const {
1339 return unzip_to_file(zip_, name.c_str());
1340}
1341
1342static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001343 ZipArchiveHandle zip;
1344 int error = OpenArchive(filename, &zip);
1345 if (error != 0) {
1346 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1347 }
1348
1349 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1350 tool.Flash();
1351
1352 CloseArchive(zip);
1353}
1354
1355class LocalImageSource final : public ImageSource {
1356 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001357 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001358 int OpenFile(const std::string& name) const override;
1359};
1360
Tom Cherrydfd85df2018-09-20 14:45:05 -07001361bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001362 auto path = find_item_given_name(name);
1363 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001364 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001365 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001366 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001367}
1368
1369int LocalImageSource::OpenFile(const std::string& name) const {
1370 auto path = find_item_given_name(name);
1371 return open(path.c_str(), O_RDONLY);
1372}
1373
1374static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001375 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1376 tool.Flash();
1377}
1378
Elliott Hughesd6365a72017-05-08 18:04:49 -07001379static std::string next_arg(std::vector<std::string>* args) {
1380 if (args->empty()) syntax_error("expected argument");
1381 std::string result = args->front();
1382 args->erase(args->begin());
1383 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001384}
1385
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001386static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001387 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001388
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001389 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001390 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001391 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001392 }
Tom Cherry9027af02018-09-24 15:48:09 -07001393 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001394}
1395
Connor O'Brience16a8a2017-02-06 14:39:31 -08001396static std::string fb_fix_numeric_var(std::string var) {
1397 // Some bootloaders (angler, for example), send spurious leading whitespace.
1398 var = android::base::Trim(var);
1399 // Some bootloaders (hammerhead, for example) use implicit hex.
1400 // This code used to use strtol with base 16.
1401 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1402 return var;
1403}
1404
Aaron Wisnerdb511202018-06-26 15:38:35 -05001405static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001406 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001407 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001408 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001409 return 0;
1410 }
1411 sizeString = fb_fix_numeric_var(sizeString);
1412
1413 unsigned size;
1414 if (!android::base::ParseUint(sizeString, &size)) {
1415 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1416 return 0;
1417 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001418 if ((size & (size - 1)) != 0) {
1419 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001420 return 0;
1421 }
1422 return size;
1423}
1424
Aaron Wisnerdb511202018-06-26 15:38:35 -05001425static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001426 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001427 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001428 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001429 std::string partition_type, partition_size;
1430
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001431 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001432 const char* errMsg = nullptr;
1433 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001434 TemporaryFile output;
1435 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001436
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001437 unsigned int limit = INT_MAX;
1438 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001439 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001440 }
1441 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001442 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001443 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001444
Tom Cherry9027af02018-09-24 15:48:09 -07001445 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446 errMsg = "Can't determine partition type.\n";
1447 goto failed;
1448 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001449 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001450 if (partition_type != type_override) {
1451 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001452 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001453 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001454 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001455 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456
Tom Cherry9027af02018-09-24 15:48:09 -07001457 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001458 errMsg = "Unable to get partition size\n";
1459 goto failed;
1460 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001461 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001462 if (partition_size != size_override) {
1463 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001464 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001465 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001466 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001467 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001468 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001469
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001470 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001471 if (!gen) {
1472 if (skip_if_not_supported) {
1473 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001474 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001475 return;
1476 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001477 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1478 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001479 return;
1480 }
1481
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001482 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001483 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001484 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1485 return;
1486 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001487
Connor O'Brience16a8a2017-02-06 14:39:31 -08001488 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001489 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1490 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001491
Jin Qian4a335822017-04-18 16:23:18 -07001492 if (fs_generator_generate(gen, output.path, size, initial_dir,
1493 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001494 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001495 return;
1496 }
1497
Jin Qian4a335822017-04-18 16:23:18 -07001498 fd.reset(open(output.path, O_RDONLY));
1499 if (fd == -1) {
1500 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1501 return;
1502 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001503 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001504 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001505 return;
1506 }
1507 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001508 return;
1509
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001510failed:
1511 if (skip_if_not_supported) {
1512 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001513 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001514 }
Tom Cherry9027af02018-09-24 15:48:09 -07001515 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001516}
1517
David Anderson89569642018-11-16 15:53:35 -08001518static bool should_flash_in_userspace(const std::string& partition_name) {
David Andersone0e693c2018-11-27 20:19:26 -08001519 if (!get_android_product_out()) {
1520 return false;
1521 }
David Anderson89569642018-11-16 15:53:35 -08001522 auto path = find_item_given_name("super_empty.img");
1523 if (path.empty()) {
1524 return false;
1525 }
1526 auto metadata = android::fs_mgr::ReadFromImageFile(path);
1527 if (!metadata) {
1528 return false;
1529 }
1530 for (const auto& partition : metadata->partitions) {
1531 auto candidate = android::fs_mgr::GetPartitionName(partition);
1532 if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) {
1533 // On retrofit devices, we don't know if, or whether, the A or B
1534 // slot has been flashed for dynamic partitions. Instead we add
1535 // both names to the list as a conservative guess.
1536 if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) {
1537 return true;
1538 }
1539 } else if (candidate == partition_name) {
1540 return true;
1541 }
1542 }
1543 return false;
1544}
1545
Aaron Wisnerdb511202018-06-26 15:38:35 -05001546int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001547 bool wants_wipe = false;
1548 bool wants_reboot = false;
1549 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001550 bool wants_reboot_recovery = false;
1551 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001552 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001553 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001554 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001555 bool set_fbe_marker = false;
David Anderson89569642018-11-16 15:53:35 -08001556 bool force_flash = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001557 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001558 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001559 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001560
Elliott Hughes577e8b42018-04-05 16:12:47 -07001561 g_boot_img_hdr.kernel_addr = 0x00008000;
1562 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1563 g_boot_img_hdr.second_addr = 0x00f00000;
1564 g_boot_img_hdr.tags_addr = 0x00000100;
1565 g_boot_img_hdr.page_size = 2048;
1566
JP Abgrall7b8970c2013-03-07 17:06:41 -08001567 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001568 {"base", required_argument, 0, 0},
1569 {"cmdline", required_argument, 0, 0},
1570 {"disable-verification", no_argument, 0, 0},
1571 {"disable-verity", no_argument, 0, 0},
David Anderson89569642018-11-16 15:53:35 -08001572 {"force", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001573 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001574 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001575 {"kernel-offset", required_argument, 0, 0},
1576 {"os-patch-level", required_argument, 0, 0},
1577 {"os-version", required_argument, 0, 0},
1578 {"page-size", required_argument, 0, 0},
1579 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001580 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001581 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001582 {"skip-secondary", no_argument, 0, 0},
1583 {"slot", required_argument, 0, 0},
1584 {"tags-offset", required_argument, 0, 0},
1585 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001586 {"verbose", no_argument, 0, 'v'},
1587 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001588#if !defined(_WIN32)
1589 {"wipe-and-use-fbe", no_argument, 0, 0},
1590#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001591 {0, 0, 0, 0}
1592 };
Colin Cross8879f982012-05-22 17:53:34 -07001593
1594 serial = getenv("ANDROID_SERIAL");
1595
Elliott Hughes577e8b42018-04-05 16:12:47 -07001596 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001597 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001598 if (c == 0) {
1599 std::string name{longopts[longindex].name};
1600 if (name == "base") {
1601 g_base_addr = strtoul(optarg, 0, 16);
1602 } else if (name == "cmdline") {
1603 g_cmdline = optarg;
1604 } else if (name == "disable-verification") {
1605 g_disable_verification = true;
1606 } else if (name == "disable-verity") {
1607 g_disable_verity = true;
David Anderson89569642018-11-16 15:53:35 -08001608 } else if (name == "force") {
1609 force_flash = true;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001610 } else if (name == "header-version") {
1611 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1612 } else if (name == "kernel-offset") {
1613 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1614 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001615 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001616 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001617 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001618 } else if (name == "page-size") {
1619 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1620 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1621 } else if (name == "ramdisk-offset") {
1622 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1623 } else if (name == "skip-reboot") {
1624 skip_reboot = true;
1625 } else if (name == "skip-secondary") {
1626 skip_secondary = true;
1627 } else if (name == "slot") {
1628 slot_override = optarg;
1629 } else if (name == "tags-offset") {
1630 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1631 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001632 setvbuf(stdout, nullptr, _IONBF, 0);
1633 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001634 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001635 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001636 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001637 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001638#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001639 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001640 wants_wipe = true;
1641 set_fbe_marker = true;
1642#endif
1643 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001644 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001645 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001646 } else {
1647 switch (c) {
1648 case 'a':
1649 wants_set_active = true;
1650 if (optarg) next_active = optarg;
1651 break;
1652 case 'h':
1653 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001654 case 'l':
1655 g_long_listing = true;
1656 break;
1657 case 's':
1658 serial = optarg;
1659 break;
1660 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001661 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1662 die("invalid sparse limit %s", optarg);
1663 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001664 break;
1665 case 'v':
1666 set_verbose();
1667 break;
1668 case 'w':
1669 wants_wipe = true;
1670 break;
1671 case '?':
1672 return 1;
1673 default:
1674 abort();
1675 }
Colin Cross8879f982012-05-22 17:53:34 -07001676 }
1677 }
1678
1679 argc -= optind;
1680 argv += optind;
1681
Elliott Hughesd6365a72017-05-08 18:04:49 -07001682 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001683
Colin Cross8fb6e062012-07-24 16:36:41 -07001684 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001685 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001686 return 0;
1687 }
1688
Colin Crossc7b75dc2012-08-29 18:17:06 -07001689 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001690 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001691 }
1692
David Pursell0b156632015-10-30 11:22:01 -07001693 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001694 if (transport == nullptr) {
1695 return 1;
1696 }
Tom Cherry9027af02018-09-24 15:48:09 -07001697 fastboot::DriverCallbacks driver_callbacks = {
1698 .prolog = Status,
1699 .epilog = Epilog,
1700 .info = InfoMessage,
1701 };
1702 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1703 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001704
Elliott Hughes5620d222018-03-28 08:20:00 -07001705 const double start = now();
1706
Aaron Wisnerdb511202018-06-26 15:38:35 -05001707 if (slot_override != "") slot_override = verify_slot(slot_override);
1708 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001709
1710 if (wants_set_active) {
1711 if (next_active == "") {
1712 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001713 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001714 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001715 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001716 } else {
1717 wants_set_active = false;
1718 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001719 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001720 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001721 }
1722 }
1723 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001724
Elliott Hughesd6365a72017-05-08 18:04:49 -07001725 std::vector<std::string> args(argv, argv + argc);
1726 while (!args.empty()) {
1727 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001728
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001729 if (command == FB_CMD_GETVAR) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001730 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001731 DisplayVarOrError(variable, variable);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001732 } else if (command == FB_CMD_ERASE) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001733 std::string partition = next_arg(&args);
1734 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001735 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001736 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001737 fs_get_generator(partition_type) != nullptr) {
1738 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1739 partition_type.c_str());
1740 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001741
Tom Cherry9027af02018-09-24 15:48:09 -07001742 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001743 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001744 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 } else if (android::base::StartsWith(command, "format")) {
1746 // Parsing for: "format[:[type][:[size]]]"
1747 // Some valid things:
1748 // - select only the size, and leave default fs type:
1749 // format::0x4000000 userdata
1750 // - default fs type and size:
1751 // format userdata
1752 // format:: userdata
1753 std::vector<std::string> pieces = android::base::Split(command, ":");
1754 std::string type_override;
1755 if (pieces.size() > 1) type_override = pieces[1].c_str();
1756 std::string size_override;
1757 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001758
Elliott Hughesd6365a72017-05-08 18:04:49 -07001759 std::string partition = next_arg(&args);
1760
1761 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001762 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001763 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001764 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 } else if (command == "signature") {
1766 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001767 std::vector<char> data;
1768 if (!ReadFileToVector(filename, &data)) {
1769 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1770 }
1771 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001772 fb->Download("signature", data);
1773 fb->RawCommand("signature", "installing signature");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001774 } else if (command == FB_CMD_REBOOT) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001775 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001776
1777 if (args.size() == 1) {
1778 std::string what = next_arg(&args);
1779 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001780 wants_reboot = false;
1781 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001782 } else if (what == "recovery") {
1783 wants_reboot = false;
1784 wants_reboot_recovery = true;
1785 } else if (what == "fastboot") {
1786 wants_reboot = false;
1787 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001788 } else {
1789 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001790 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001791
Elliott Hughesca85df02015-02-25 10:02:00 -08001792 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001793 if (!args.empty()) syntax_error("junk after reboot command");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001794 } else if (command == FB_CMD_REBOOT_BOOTLOADER) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001795 wants_reboot_bootloader = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001796 } else if (command == FB_CMD_REBOOT_RECOVERY) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001797 wants_reboot_recovery = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001798 } else if (command == FB_CMD_REBOOT_FASTBOOT) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001799 wants_reboot_fastboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001800 } else if (command == FB_CMD_CONTINUE) {
Tom Cherry9027af02018-09-24 15:48:09 -07001801 fb->Continue();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001802 } else if (command == FB_CMD_BOOT) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001803 std::string kernel = next_arg(&args);
1804 std::string ramdisk;
1805 if (!args.empty()) ramdisk = next_arg(&args);
1806 std::string second_stage;
1807 if (!args.empty()) second_stage = next_arg(&args);
1808
Tom Cherrydfd85df2018-09-20 14:45:05 -07001809 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001810 fb->Download("boot.img", data);
1811 fb->Boot();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001812 } else if (command == FB_CMD_FLASH) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001813 std::string pname = next_arg(&args);
1814
Elliott Hughes2810d002016-04-25 14:31:18 -07001815 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001816 if (!args.empty()) {
1817 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001818 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001819 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001820 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001821 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001822
1823 auto flash = [&](const std::string &partition) {
David Anderson89569642018-11-16 15:53:35 -08001824 if (should_flash_in_userspace(partition) && !is_userspace_fastboot() &&
1825 !force_flash) {
1826 die("The partition you are trying to flash is dynamic, and "
1827 "should be flashed via fastbootd. Please run:\n"
1828 "\n"
1829 " fastboot reboot fastboot\n"
1830 "\n"
1831 "And try again. If you are intentionally trying to "
1832 "overwrite a fixed partition, use --force.");
1833 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001834 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001835 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001836 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001837 } else if (command == "flash:raw") {
1838 std::string partition = next_arg(&args);
1839 std::string kernel = next_arg(&args);
1840 std::string ramdisk;
1841 if (!args.empty()) ramdisk = next_arg(&args);
1842 std::string second_stage;
1843 if (!args.empty()) second_stage = next_arg(&args);
1844
Tom Cherrydfd85df2018-09-20 14:45:05 -07001845 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1846 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001847 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001848 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001849 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001850 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001851 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001852 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001853 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001854 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001855 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001856 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001857 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001858 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001859 bool slot_all = (slot_override == "all");
1860 if (slot_all) {
1861 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1862 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001863 std::string filename = "update.zip";
1864 if (!args.empty()) {
1865 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001866 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001867 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001868 wants_reboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001869 } else if (command == FB_CMD_SET_ACTIVE) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001870 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001871 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001872 } else if (command == "stage") {
1873 std::string filename = next_arg(&args);
1874
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001875 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001876 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001877 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001878 }
Tom Cherry9027af02018-09-24 15:48:09 -07001879 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001880 } else if (command == "get_staged") {
1881 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001882 fb->Upload(filename);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001883 } else if (command == FB_CMD_OEM) {
1884 do_oem_command(FB_CMD_OEM, &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001885 } else if (command == "flashing") {
1886 if (args.empty()) {
1887 syntax_error("missing 'flashing' command");
1888 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1889 args[0] == "unlock_critical" ||
1890 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001891 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001892 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001893 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001894 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001895 }
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001896 } else if (command == FB_CMD_CREATE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001897 std::string partition = next_arg(&args);
1898 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001899 fb->CreatePartition(partition, size);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001900 } else if (command == FB_CMD_DELETE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001901 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001902 fb->DeletePartition(partition);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001903 } else if (command == FB_CMD_RESIZE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001904 std::string partition = next_arg(&args);
1905 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001906 fb->ResizePartition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001907 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001908 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001909 }
1910 }
1911
1912 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001913 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1914 for (const auto& partition : partitions) {
1915 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001916 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1917 continue;
1918 }
Paul Crowley4d170062018-04-24 17:06:30 -07001919 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001920 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001921 if (partition == "userdata" && set_fbe_marker) {
1922 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1923 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001924 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001925 delete_fbemarker_tmpdir(initial_userdata_dir);
1926 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001927 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001928 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001929 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001930 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001931 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001932 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001933 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001934 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001935 fb->Reboot();
1936 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001937 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001938 fb->RebootTo("bootloader");
1939 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001940 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001941 fb->RebootTo("recovery");
1942 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001943 } else if (wants_reboot_fastboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001944 fb->RebootTo("fastboot");
1945 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001946 }
1947
Elliott Hughes5620d222018-03-28 08:20:00 -07001948 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001949
Tom Cherry9027af02018-09-24 15:48:09 -07001950 auto* old_transport = fb->set_transport(nullptr);
1951 delete old_transport;
1952
Tom Cherry11f12092018-08-29 21:36:28 -07001953 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001954}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001955
Aaron Wisnerdb511202018-06-26 15:38:35 -05001956void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001957 unsigned year, month, day;
1958 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1959 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1960 }
1961 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1962 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1963 hdr->SetOsPatchLevel(year, month);
1964}
1965
Aaron Wisnerdb511202018-06-26 15:38:35 -05001966void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001967 unsigned major = 0, minor = 0, patch = 0;
1968 std::vector<std::string> versions = android::base::Split(arg, ".");
1969 if (versions.size() < 1 || versions.size() > 3 ||
1970 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1971 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1972 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1973 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1974 syntax_error("bad OS version: %s", arg);
1975 }
1976 hdr->SetOsVersion(major, minor, patch);
1977}