blob: 25df4516f9080a3601bfc5a19fe7fe7cb5e59df3 [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>
David Anderson0c732342018-11-28 17:10:42 -080049#include <string>
Elliott Hughes290a2282016-11-14 17:08:47 -080050#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080051#include <utility>
52#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070053
Elliott Hughes82ff3152016-08-31 15:07:18 -070054#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070055#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080056#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080057#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070058#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080059#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050060#include <android-base/unique_fd.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070061#include <build/version.h>
David Anderson89569642018-11-16 15:53:35 -080062#include <liblp/liblp.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070063#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070064#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070065#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Elliott Hughes253c18d2015-03-18 22:47:09 -070067#include "bootimg_utils.h"
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -070068#include "constants.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080069#include "diagnose_usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070070#include "fastboot_driver.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070071#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080072#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070073#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080074#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070075#include "usb.h"
Tom Cherry9027af02018-09-24 15:48:09 -070076#include "util.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
Tom Cherrydfd85df2018-09-20 14:45:05 -070078using android::base::ReadFully;
Tom Cherry4aa60b32018-09-05 15:11:44 -070079using android::base::Split;
80using android::base::Trim;
Chris Fries0ea946c2017-04-12 10:25:57 -050081using android::base::unique_fd;
David Anderson0c732342018-11-28 17:10:42 -080082using namespace std::string_literals;
Chris Fries0ea946c2017-04-12 10:25:57 -050083
David Pursell2ec418a2016-01-20 08:32:08 -080084static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070085
Elliott Hughes577e8b42018-04-05 16:12:47 -070086static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050087// Don't resparse files in too-big chunks.
88// libsparse will support INT_MAX, but this results in large allocations, so
89// let's keep it at 1GB to avoid memory pressure on the host.
90static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070091static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070092static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093
Elliott Hughes577e8b42018-04-05 16:12:47 -070094static unsigned g_base_addr = 0x10000000;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080095static boot_img_hdr_v2 g_boot_img_hdr = {};
Elliott Hughes577e8b42018-04-05 16:12:47 -070096static std::string g_cmdline;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -080097static std::string g_dtb_path;
JP Abgrall7b8970c2013-03-07 17:06:41 -080098
David Zeuthenb6ea4352017-08-07 14:29:26 -040099static bool g_disable_verity = false;
100static bool g_disable_verification = false;
101
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000102static const std::string convert_fbe_marker_filename("convert_fbe");
103
Tom Cherry9027af02018-09-24 15:48:09 -0700104fastboot::FastBootDriver* fb = nullptr;
105
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500107 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108 FB_BUFFER_SPARSE,
109};
110
111struct fastboot_buffer {
112 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700113 void* data;
114 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500115 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700116 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700117};
118
David Anderson0debda02018-08-28 13:54:03 -0700119enum class ImageType {
120 // Must be flashed for device to boot into the kernel.
121 BootCritical,
122 // Normal partition to be flashed during "flashall".
123 Normal,
124 // Partition that is never flashed during "flashall".
125 Extra
126};
127
David Anderson32e376f2018-08-16 13:43:11 -0700128struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700129 const char* nickname;
130 const char* img_name;
131 const char* sig_name;
132 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700133 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700134 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700135 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700136};
137
138static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100139 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700140 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
141 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
142 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
143 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
144 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
145 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
146 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100147 { "product_services",
148 "product_services.img",
149 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700150 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700151 true, ImageType::Normal },
152 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
153 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
154 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
155 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
156 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
157 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson166bfef2018-10-15 14:46:59 -0700158 { "vbmeta_system",
159 "vbmeta_system.img",
160 "vbmeta_system.sig",
161 "vbmeta_system",
David Anderson1109c922018-09-17 17:32:54 -0700162 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700163 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
164 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100165 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700166};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700167
David Andersone0e693c2018-11-27 20:19:26 -0800168static char* get_android_product_out() {
Elliott Hughes45964a82017-05-03 22:43:23 -0700169 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700170 if (dir == nullptr || dir[0] == '\0') {
David Andersone0e693c2018-11-27 20:19:26 -0800171 return nullptr;
172 }
173 return dir;
174}
175
176static std::string find_item_given_name(const std::string& img_name) {
177 char* dir = get_android_product_out();
178 if (!dir) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700179 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700180 }
David Anderson0debda02018-08-28 13:54:03 -0700181 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700182}
183
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700184static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700185 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700186 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700187 return find_item_given_name(images[i].img_name);
188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 }
190
Elliott Hughesd6365a72017-05-08 18:04:49 -0700191 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700192 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193}
194
Tom Cherry9027af02018-09-24 15:48:09 -0700195double last_start_time;
196
197static void Status(const std::string& message) {
198 static constexpr char kStatusFormat[] = "%-50s ";
199 fprintf(stderr, kStatusFormat, message.c_str());
200 last_start_time = now();
201}
202
203static void Epilog(int status) {
204 if (status) {
205 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
206 die("Command failed");
207 } else {
208 double split = now();
209 fprintf(stderr, "OKAY [%7.3fs]\n", (split - last_start_time));
210 }
211}
212
213static void InfoMessage(const std::string& info) {
214 fprintf(stderr, "(bootloader) %s\n", info.c_str());
215}
216
Elliott Hughesfc797672015-04-07 20:12:50 -0700217static int64_t get_file_size(int fd) {
218 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700219 if (fstat(fd, &sb) == -1) {
220 die("could not get file size");
221 }
222 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700223}
224
Tom Cherrydfd85df2018-09-20 14:45:05 -0700225bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
226 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227
Tom Cherrydfd85df2018-09-20 14:45:05 -0700228 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
229 if (fd == -1) {
230 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700231 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232
Tom Cherrydfd85df2018-09-20 14:45:05 -0700233 out->resize(get_file_size(fd));
234 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700235}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236
Elliott Hughesfc797672015-04-07 20:12:50 -0700237static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700238 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
239 return -1;
240 }
241
Scott Anderson13081c62012-04-06 12:39:30 -0700242 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700244 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
245 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 return 0;
247}
248
Elliott Hughesfc797672015-04-07 20:12:50 -0700249static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800250 return match_fastboot_with_serial(info, serial);
251}
252
Elliott Hughesfc797672015-04-07 20:12:50 -0700253static int list_devices_callback(usb_ifc_info* info) {
254 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800255 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700256 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800257 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700258 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259 if (!serial[0]) {
260 serial = "????????????";
261 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700262 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700263 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800264 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700265 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800266 printf("%-22s fastboot", serial.c_str());
267 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700268 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800269 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800270 }
271
272 return -1;
273}
274
David Pursell2ec418a2016-01-20 08:32:08 -0800275// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
276// a specific device, otherwise the first USB device found will be used.
277//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700278// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800279// Otherwise it blocks until the target is available.
280//
281// The returned Transport is a singleton, so multiple calls to this function will return the same
282// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700283static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800284 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285
David Pursell4601c972016-02-05 15:35:09 -0800286 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800287 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800288 int port = 0;
289 if (serial != nullptr) {
290 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800291
David Pursell4601c972016-02-05 15:35:09 -0800292 if (android::base::StartsWith(serial, "tcp:")) {
293 protocol = Socket::Protocol::kTcp;
294 port = tcp::kDefaultPort;
295 net_address = serial + strlen("tcp:");
296 } else if (android::base::StartsWith(serial, "udp:")) {
297 protocol = Socket::Protocol::kUdp;
298 port = udp::kDefaultPort;
299 net_address = serial + strlen("udp:");
300 }
301
302 if (net_address != nullptr) {
303 std::string error;
304 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700305 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800306 }
David Pursell2ec418a2016-01-20 08:32:08 -0800307 }
308 }
309
David Anderson1d887432018-08-27 16:47:32 -0700310 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800311 while (true) {
312 if (!host.empty()) {
313 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800314 if (protocol == Socket::Protocol::kTcp) {
315 transport = tcp::Connect(host, port, &error).release();
316 } else if (protocol == Socket::Protocol::kUdp) {
317 transport = udp::Connect(host, port, &error).release();
318 }
319
David Pursell2ec418a2016-01-20 08:32:08 -0800320 if (transport == nullptr && announce) {
321 fprintf(stderr, "error: %s\n", error.c_str());
322 }
323 } else {
324 transport = usb_open(match_fastboot);
325 }
326
327 if (transport != nullptr) {
328 return transport;
329 }
330
David Pursell0b156632015-10-30 11:22:01 -0700331 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800332 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700333 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800335 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 }
337}
338
Elliott Hughesfc797672015-04-07 20:12:50 -0700339static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800340 // We don't actually open a USB device here,
341 // just getting our callback called so we can
342 // list all the connected devices.
343 usb_open(list_devices_callback);
344}
345
Elliott Hughesd6365a72017-05-08 18:04:49 -0700346static void syntax_error(const char* fmt, ...) {
347 fprintf(stderr, "fastboot: usage: ");
348
349 va_list ap;
350 va_start(ap, fmt);
351 vfprintf(stderr, fmt, ap);
352 va_end(ap);
353
354 fprintf(stderr, "\n");
355 exit(1);
356}
357
358static int show_help() {
359 // clang-format off
360 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700361// 1 2 3 4 5 6 7 8
362// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700363 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700365 "flashing:\n"
366 " update ZIP Flash all partitions from an update.zip package.\n"
367 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
368 " On A/B devices, flashed slot is set as active.\n"
369 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700370 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
371 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700372 "\n"
373 "basics:\n"
374 " devices [-l] List devices in bootloader (-l: with device paths).\n"
375 " getvar NAME Display given bootloader variable.\n"
376 " reboot [bootloader] Reboot device.\n"
377 "\n"
378 "locking/unlocking:\n"
379 " flashing lock|unlock Lock/unlock partitions for flashing\n"
380 " flashing lock_critical|unlock_critical\n"
381 " Lock/unlock 'critical' bootloader partitions.\n"
382 " flashing get_unlock_ability\n"
383 " Check whether unlocking is allowed (1) or not(0).\n"
384 "\n"
385 "advanced:\n"
386 " erase PARTITION Erase a flash partition.\n"
387 " format[:FS_TYPE[:SIZE]] PARTITION\n"
388 " Format a flash partition.\n"
389 " set_active SLOT Set the active slot.\n"
390 " oem [COMMAND...] Execute OEM-specific command.\n"
David Anderson1d504e32019-01-15 14:38:20 -0800391 " gsi wipe|disable Wipe or disable a GSI installation (fastbootd only).\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700392 "\n"
393 "boot image:\n"
394 " boot KERNEL [RAMDISK [SECOND]]\n"
395 " Download and boot kernel from RAM.\n"
396 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
397 " Create boot image and flash it.\n"
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -0800398 " --dtb DTB Specify path to DTB for boot image header version 2.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700399 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700400 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
401 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
402 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
403 " --tags-offset Set tags offset (default: 0x00000100).\n"
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -0800404 " --dtb-offset Set dtb offset (default: 0x01100000).\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700405 " --page-size BYTES Set flash page size (default: 2048).\n"
406 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700407 " --os-version MAJOR[.MINOR[.PATCH]]\n"
408 " Set boot image OS version (default: 0.0.0).\n"
409 " --os-patch-level YYYY-MM-DD\n"
410 " Set boot image OS security patch level.\n"
411 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700412 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700413 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700414 //" continue Continue with autoboot.\n"
415 //"\n"
416 "Android Things:\n"
417 " stage IN_FILE Sends given file to stage for the next command.\n"
418 " 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 -0800419 "\n"
420 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700421 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700422 " -s SERIAL Specify a USB device.\n"
423 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700424 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
David Anderson89569642018-11-16 15:53:35 -0800425 " --force Force a flash operation that may be unsafe.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700426 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
427 " non-current slot (default: current active slot).\n"
428 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
429 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
430 " --skip-reboot Don't reboot device after flashing.\n"
431 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
432 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000433#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700434 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000435#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700436 // TODO: remove --unbuffered?
437 " --unbuffered Don't buffer input or output.\n"
438 " --verbose, -v Verbose output.\n"
439 " --version Display version.\n"
440 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700442 // clang-format off
443 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000445
Tom Cherrydfd85df2018-09-20 14:45:05 -0700446static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
447 const std::string& second_stage) {
448 std::vector<char> kernel_data;
449 if (!ReadFileToVector(kernel, &kernel_data)) {
450 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
451 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800452
Elliott Hughesfc797672015-04-07 20:12:50 -0700453 // Is this actually a boot image?
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -0800454 if (kernel_data.size() < sizeof(boot_img_hdr_v2)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800455 die("cannot load '%s': too short", kernel.c_str());
456 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700457 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700458 if (!g_cmdline.empty()) {
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -0800459 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v2*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700460 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800461
Elliott Hughes4089d342017-10-27 14:21:12 -0700462 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800463
Tom Cherrydfd85df2018-09-20 14:45:05 -0700464 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 }
466
Tom Cherrydfd85df2018-09-20 14:45:05 -0700467 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700468 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700469 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
470 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
471 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 }
473
Tom Cherrydfd85df2018-09-20 14:45:05 -0700474 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700475 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700476 if (!ReadFileToVector(second_stage, &second_stage_data)) {
477 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
478 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200479 }
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -0800480
481 std::vector<char> dtb_data;
482 if (!g_dtb_path.empty()) {
483 if (g_boot_img_hdr.header_version < 2) {
484 die("Argument dtb not supported for boot image header version %d\n",
485 g_boot_img_hdr.header_version);
486 }
487 if (!ReadFileToVector(g_dtb_path, &dtb_data)) {
488 die("cannot load '%s': %s", g_dtb_path.c_str(), strerror(errno));
489 }
490 }
491
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700493
Tom Cherrydfd85df2018-09-20 14:45:05 -0700494 std::vector<char> out;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -0800495 boot_img_hdr_v2* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
496 dtb_data, g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800497
Tom Cherrydfd85df2018-09-20 14:45:05 -0700498 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
499 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
Tom Cherrydfd85df2018-09-20 14:45:05 -0700500 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501}
502
Tom Cherrydfd85df2018-09-20 14:45:05 -0700503static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
504 std::vector<char>* out) {
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700505 ZipEntry zip_entry;
Elliott Hughesb17bf522019-05-03 22:38:44 -0700506 if (FindEntry(zip, entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700507 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
508 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 }
510
Tom Cherrydfd85df2018-09-20 14:45:05 -0700511 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512
Tom Cherrydfd85df2018-09-20 14:45:05 -0700513 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
514 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515
Tom Cherrydfd85df2018-09-20 14:45:05 -0700516 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
517 out->size());
518 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000519
Tom Cherrydfd85df2018-09-20 14:45:05 -0700520 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521}
522
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700523#if defined(_WIN32)
524
525// TODO: move this to somewhere it can be shared.
526
527#include <windows.h>
528
529// Windows' tmpfile(3) requires administrator rights because
530// it creates temporary files in the root directory.
531static FILE* win32_tmpfile() {
532 char temp_path[PATH_MAX];
533 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
534 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700535 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700536 }
537
538 char filename[PATH_MAX];
539 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700540 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700541 }
542
543 return fopen(filename, "w+bTD");
544}
545
546#define tmpfile win32_tmpfile
547
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000548static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700549 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000550}
551
Elliott Hughes855cdf82018-04-02 14:24:03 -0700552static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700553 // TODO: reimplement to avoid leaking a FILE*.
554 return fileno(tmpfile());
555}
556
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000557#else
558
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700559static std::string make_temporary_template() {
560 const char* tmpdir = getenv("TMPDIR");
561 if (tmpdir == nullptr) tmpdir = P_tmpdir;
562 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
563}
564
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000565static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700566 std::string result(make_temporary_template());
567 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700568 die("unable to create temporary directory with template %s: %s",
569 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000570 }
571 return result;
572}
573
Elliott Hughes855cdf82018-04-02 14:24:03 -0700574static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700575 std::string path_template(make_temporary_template());
576 int fd = mkstemp(&path_template[0]);
577 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700578 die("failed to create temporary file for %s with template %s: %s\n",
579 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700580 }
581 unlink(path_template.c_str());
582 return fd;
583}
584
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700585#endif
586
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000587static std::string create_fbemarker_tmpdir() {
588 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000589 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
590 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
591 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700592 die("unable to create FBE marker file %s locally: %s",
593 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000594 }
595 close(fd);
596 return dir;
597}
598
599static void delete_fbemarker_tmpdir(const std::string& dir) {
600 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
601 if (unlink(marker_file.c_str()) == -1) {
602 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
603 marker_file.c_str(), errno, strerror(errno));
604 return;
605 }
606 if (rmdir(dir.c_str()) == -1) {
607 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
608 dir.c_str(), errno, strerror(errno));
609 return;
610 }
611}
612
Elliott Hughes45964a82017-05-03 22:43:23 -0700613static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700614 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700615
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700616 ZipEntry zip_entry;
Elliott Hughesb17bf522019-05-03 22:38:44 -0700617 if (FindEntry(zip, entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700618 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700619 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000620 return -1;
621 }
622
Elliott Hughes23af1122017-11-10 08:42:17 -0800623 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700624 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800625 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700626 int error = ExtractEntryToFile(zip, &zip_entry, fd);
627 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800628 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700629 }
630
Elliott Hughes4089d342017-10-27 14:21:12 -0700631 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800632 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700633 }
634
Elliott Hughes23af1122017-11-10 08:42:17 -0800635 fprintf(stderr, " took %.3fs\n", now() - start);
636
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700637 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638}
639
Tom Cherry4aa60b32018-09-05 15:11:44 -0700640static void CheckRequirement(const std::string& cur_product, const std::string& var,
641 const std::string& product, bool invert,
642 const std::vector<std::string>& options) {
643 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700644
Tom Cherry4aa60b32018-09-05 15:11:44 -0700645 double start = now();
646
647 if (!product.empty()) {
648 if (product != cur_product) {
649 double split = now();
650 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
651 cur_product.c_str(), product.c_str(), (split - start));
652 return;
653 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700655
656 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700657 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700658 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700659 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
660 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700661 die("requirements not met!");
662 }
663
664 bool match = false;
665 for (const auto& option : options) {
666 if (option == var_value || (option.back() == '*' &&
667 !var_value.compare(0, option.length() - 1, option, 0,
668 option.length() - 1))) {
669 match = true;
670 break;
671 }
672 }
673
674 if (invert) {
675 match = !match;
676 }
677
678 if (match) {
679 double split = now();
680 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
681 return;
682 }
683
684 fprintf(stderr, "FAILED\n\n");
685 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
686 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
687 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
688 fprintf(stderr, " or '%s'", it->c_str());
689 }
690 fprintf(stderr, ".\n\n");
691 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692}
693
Tom Cherry4aa60b32018-09-05 15:11:44 -0700694bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
695 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700696 // "require product=alpha|beta|gamma"
697 // "require version-bootloader=1234"
698 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
699 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700700 *product = "";
701 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700702
Tom Cherry4aa60b32018-09-05 15:11:44 -0700703 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
704 auto require_product_regex =
705 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
706 std::smatch match_results;
707
708 if (std::regex_match(line, match_results, require_reject_regex)) {
709 *invert = Trim(match_results[1]) == "reject";
710 } else if (std::regex_match(line, match_results, require_product_regex)) {
711 *product = match_results[1];
712 } else {
713 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 }
715
Tom Cherry4aa60b32018-09-05 15:11:44 -0700716 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700717 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700718 if (*name == "board") {
719 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 }
721
Tom Cherry4aa60b32018-09-05 15:11:44 -0700722 auto raw_options = Split(match_results[3], "|");
723 for (const auto& option : raw_options) {
724 auto trimmed_option = Trim(option);
725 options->emplace_back(trimmed_option);
726 }
727
728 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729}
730
Tom Cherry4aa60b32018-09-05 15:11:44 -0700731// "require partition-exists=x" is a special case, added because of the trouble we had when
732// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
733// missing out new partitions. A device with new partitions can use "partition-exists" to
734// override the fields `optional_if_no_image` in the `images` array.
735static void HandlePartitionExists(const std::vector<std::string>& options) {
736 const std::string& partition_name = options[0];
737 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700738 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700739 (has_slot != "yes" && has_slot != "no")) {
740 die("device doesn't have required partition %s!", partition_name.c_str());
741 }
742 bool known_partition = false;
743 for (size_t i = 0; i < arraysize(images); ++i) {
744 if (images[i].nickname && images[i].nickname == partition_name) {
745 images[i].optional_if_no_image = false;
746 known_partition = true;
747 }
748 }
749 if (!known_partition) {
750 die("device requires partition %s which is not known to this version of fastboot",
751 partition_name.c_str());
752 }
753}
754
755static void CheckRequirements(const std::string& data) {
756 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700757 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
758 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700759 }
760
761 auto lines = Split(data, "\n");
762 for (const auto& line : lines) {
763 if (line.empty()) {
764 continue;
765 }
766
767 std::string name;
768 std::string product;
769 bool invert;
770 std::vector<std::string> options;
771
772 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
773 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
774 continue;
775 }
776 if (name == "partition-exists") {
777 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800778 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700779 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 }
781 }
782}
783
Tom Cherry9027af02018-09-24 15:48:09 -0700784static void DisplayVarOrError(const std::string& label, const std::string& var) {
785 std::string value;
786
787 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
788 Status("getvar:" + var);
789 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
790 return;
791 }
792 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
793}
794
795static void DumpInfo() {
796 fprintf(stderr, "--------------------------------------------\n");
797 DisplayVarOrError("Bootloader Version...", "version-bootloader");
798 DisplayVarOrError("Baseband Version.....", "version-baseband");
799 DisplayVarOrError("Serial Number........", "serialno");
800 fprintf(stderr, "--------------------------------------------\n");
801
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800802}
803
Tao Bao41cf35f2018-04-24 10:54:21 -0700804static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700805 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700806 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700807
Tao Bao41cf35f2018-04-24 10:54:21 -0700808 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
809 die("invalid max size %" PRId64, max_size);
810 }
811
Elliott Hughesfc797672015-04-07 20:12:50 -0700812 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700813 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700814
Elliott Hughes253c18d2015-03-18 22:47:09 -0700815 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700816 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700817
818 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700819 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700820
821 return out_s;
822}
823
Aaron Wisnerdb511202018-06-26 15:38:35 -0500824static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700825 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700826 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700827 max_download_size.empty()) {
828 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700829 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700830 }
831
Elliott Hughes77c0e662015-11-02 15:51:12 -0800832 // Some bootloaders (angler, for example) send spurious whitespace too.
833 max_download_size = android::base::Trim(max_download_size);
834
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700835 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700836 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800837 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700838 return 0;
839 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700840 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700841 return limit;
842}
843
Aaron Wisnerdb511202018-06-26 15:38:35 -0500844static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700845 int64_t limit = sparse_limit;
846 if (limit == 0) {
847 // Unlimited, so see what the target device's limit is.
848 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700849 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500850 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700851 }
852 if (target_sparse_limit > 0) {
853 limit = target_sparse_limit;
854 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700855 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700856 }
857 }
858
859 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500860 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700861 }
862
863 return 0;
864}
865
Aaron Wisnerdb511202018-06-26 15:38:35 -0500866static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700867 int64_t sz = get_file_size(fd);
868 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700869 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700870 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700871
David Anderson32e376f2018-08-16 13:43:11 -0700872 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
873 buf->image_size = sparse_file_len(s, false, false);
874 sparse_file_destroy(s);
875 } else {
876 buf->image_size = sz;
877 }
878
Elliott Hughesa56a7292018-10-26 10:34:53 -0700879 lseek(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500880 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700881 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700882 sparse_file** s = load_sparse_files(fd, limit);
883 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700884 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700885 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700886 buf->type = FB_BUFFER_SPARSE;
887 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700888 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500889 buf->type = FB_BUFFER_FD;
890 buf->data = nullptr;
891 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000892 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700893 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700894
Elliott Hughes53ec4952016-05-11 12:39:27 -0700895 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700896}
897
Aaron Wisnerdb511202018-06-26 15:38:35 -0500898static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500899 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
900
Elliott Hughes53ec4952016-05-11 12:39:27 -0700901 if (fd == -1) {
902 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700903 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500904
905 struct stat s;
906 if (fstat(fd, &s)) {
907 return false;
908 }
909 if (!S_ISREG(s.st_mode)) {
910 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
911 return false;
912 }
913
Aaron Wisnerdb511202018-06-26 15:38:35 -0500914 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700915}
916
David Zeuthenb6ea4352017-08-07 14:29:26 -0400917static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
918 // Buffer needs to be at least the size of the VBMeta struct which
919 // is 256 bytes.
920 if (buf->sz < 256) {
921 return;
922 }
923
Elliott Hughes855cdf82018-04-02 14:24:03 -0700924 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400925
926 std::string data;
927 if (!android::base::ReadFdToString(buf->fd, &data)) {
928 die("Failed reading from vbmeta");
929 }
930
931 // There's a 32-bit big endian |flags| field at offset 120 where
932 // bit 0 corresponds to disable-verity and bit 1 corresponds to
933 // disable-verification.
934 //
935 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
936 // the VBMeta struct.
937 if (g_disable_verity) {
938 data[123] |= 0x01;
939 }
940 if (g_disable_verification) {
941 data[123] |= 0x02;
942 }
943
944 if (!android::base::WriteStringToFd(data, fd)) {
945 die("Failed writing to modified vbmeta");
946 }
947 close(buf->fd);
948 buf->fd = fd;
949 lseek(fd, 0, SEEK_SET);
950}
951
Elliott Hughes5620d222018-03-28 08:20:00 -0700952static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700953{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700954 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700955
David Zeuthenb6ea4352017-08-07 14:29:26 -0400956 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
957 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700958 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400959 rewrite_vbmeta_buffer(buf);
960 }
961
Rom Lemarchand622810c2013-06-28 09:54:59 -0700962 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800963 case FB_BUFFER_SPARSE: {
964 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700965 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700966 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700967 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800968 sparse_files.emplace_back(*s, sz);
969 ++s;
970 }
971
972 for (size_t i = 0; i < sparse_files.size(); ++i) {
973 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700974 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700975 }
976 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800977 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500978 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700979 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700980 break;
981 default:
982 die("unknown buffer type: %d", buf->type);
983 }
984}
985
Aaron Wisnerdb511202018-06-26 15:38:35 -0500986static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700988 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700989 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990}
991
Aaron Wisnerdb511202018-06-26 15:38:35 -0500992static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700994 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700995 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
996 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700997 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700998 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700999 return count;
1000}
1001
Aaron Wisnerdb511202018-06-26 15:38:35 -05001002static bool supports_AB() {
1003 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001004}
1005
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001006// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007static std::string get_other_slot(const std::string& current_slot, int count) {
1008 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001009
Daniel Rosenberg80919472016-06-30 19:25:31 -07001010 char next = (current_slot[0] - 'a' + 1)%count + 'a';
1011 return std::string(1, next);
1012}
1013
Aaron Wisnerdb511202018-06-26 15:38:35 -05001014static std::string get_other_slot(const std::string& current_slot) {
1015 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -07001016}
1017
Aaron Wisnerdb511202018-06-26 15:38:35 -05001018static std::string get_other_slot(int count) {
1019 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001020}
1021
Aaron Wisnerdb511202018-06-26 15:38:35 -05001022static std::string get_other_slot() {
1023 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -07001024}
1025
Aaron Wisnerdb511202018-06-26 15:38:35 -05001026static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001027 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001028 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001029 if (allow_all) {
1030 return "all";
1031 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001032 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001033 if (count > 0) {
1034 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001035 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001036 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001037 }
1038 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001039 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001040
Aaron Wisnerdb511202018-06-26 15:38:35 -05001041 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001042 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001043
Daniel Rosenberg80919472016-06-30 19:25:31 -07001044 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001045 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001046 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001047 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001048 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001049 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001050 }
1051
Daniel Rosenberg80919472016-06-30 19:25:31 -07001052 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1053
1054 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1055 for (int i=0; i<count; i++) {
1056 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001057 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001058
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001059 exit(1);
1060}
1061
Aaron Wisnerdb511202018-06-26 15:38:35 -05001062static std::string verify_slot(const std::string& slot) {
1063 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001064}
1065
Aaron Wisnerdb511202018-06-26 15:38:35 -05001066static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001067 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001068 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001069 std::string current_slot;
1070
Tom Cherry9027af02018-09-24 15:48:09 -07001071 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001072 /* If has-slot is not supported, the answer is no. */
1073 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001074 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001075 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001076 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001077 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001078 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001079 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001080 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001081 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001082 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001083 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001084 }
1085 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001086 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001087 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001088 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001089 }
1090 func(part);
1091 }
1092}
1093
David Pursell0b156632015-10-30 11:22:01 -07001094/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1095 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1096 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1097 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001098 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001099static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001100 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001101 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001102
Daniel Rosenberg80919472016-06-30 19:25:31 -07001103 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001104 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001105 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001106 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001107 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001108 for (int i=0; i < get_slot_count(); i++) {
1109 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001110 }
1111 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001112 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001113 }
1114 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001115 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001116 }
1117}
1118
David Anderson629e51c2018-10-25 12:51:25 -07001119static bool is_logical(const std::string& partition) {
1120 std::string value;
1121 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
1122}
1123
David Anderson0c732342018-11-28 17:10:42 -08001124static bool is_retrofit_device() {
1125 std::string value;
1126 if (fb->GetVar("super-partition-name", &value) != fastboot::SUCCESS) {
1127 return false;
1128 }
1129 return android::base::StartsWith(value, "system_");
1130}
1131
Aaron Wisnerdb511202018-06-26 15:38:35 -05001132static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001133 struct fastboot_buffer buf;
1134
Aaron Wisnerdb511202018-06-26 15:38:35 -05001135 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001136 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001137 }
David Anderson629e51c2018-10-25 12:51:25 -07001138 if (is_logical(pname)) {
1139 fb->ResizePartition(pname, std::to_string(buf.image_size));
1140 }
Rom Lemarchand622810c2013-06-28 09:54:59 -07001141 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001142}
1143
Daniel Rosenberg13454092016-06-27 19:43:11 -07001144// Sets slot_override as the active slot. If slot_override is blank,
1145// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001146static void set_active(const std::string& slot_override) {
1147 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001148
Daniel Rosenberg80919472016-06-30 19:25:31 -07001149 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001150 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001151 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001152 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001153 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001154 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001155 }
1156 }
1157}
1158
David Anderson32e376f2018-08-16 13:43:11 -07001159static bool is_userspace_fastboot() {
1160 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001161 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001162}
1163
David Anderson1d887432018-08-27 16:47:32 -07001164static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001165 fb->RebootTo("fastboot");
1166
1167 auto* old_transport = fb->set_transport(nullptr);
1168 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001169
1170 // Give the current connection time to close.
1171 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1172
Tom Cherry9027af02018-09-24 15:48:09 -07001173 fb->set_transport(open_device());
David Anderson11d6d272018-12-07 16:39:07 -08001174
1175 if (!is_userspace_fastboot()) {
1176 die("Failed to boot into userspace fastboot; one or more components might be unbootable.");
1177 }
David Anderson1d887432018-08-27 16:47:32 -07001178}
1179
David Andersoncf444f32018-08-29 14:15:49 -07001180class ImageSource {
1181 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001182 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001183 virtual int OpenFile(const std::string& name) const = 0;
1184};
David Anderson32e376f2018-08-16 13:43:11 -07001185
David Andersoncf444f32018-08-29 14:15:49 -07001186class FlashAllTool {
1187 public:
1188 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001189
David Andersoncf444f32018-08-29 14:15:49 -07001190 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001191
David Andersoncf444f32018-08-29 14:15:49 -07001192 private:
1193 void CheckRequirements();
1194 void DetermineSecondarySlot();
1195 void CollectImages();
1196 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1197 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1198 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001199
David Andersoncf444f32018-08-29 14:15:49 -07001200 const ImageSource& source_;
1201 std::string slot_override_;
1202 bool skip_secondary_;
1203 bool wipe_;
1204 std::string secondary_slot_;
1205 std::vector<std::pair<const Image*, std::string>> boot_images_;
1206 std::vector<std::pair<const Image*, std::string>> os_images_;
1207};
1208
1209FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1210 : source_(source),
1211 slot_override_(slot_override),
1212 skip_secondary_(skip_secondary),
1213 wipe_(wipe)
1214{
David Anderson32e376f2018-08-16 13:43:11 -07001215}
1216
David Andersoncf444f32018-08-29 14:15:49 -07001217void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001218 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001219 CheckRequirements();
David Anderson96a9fd42018-11-05 15:21:44 -08001220
1221 // Change the slot first, so we boot into the correct recovery image when
1222 // using fastbootd.
1223 if (slot_override_ == "all") {
1224 set_active("a");
1225 } else {
1226 set_active(slot_override_);
1227 }
1228
David Andersoncf444f32018-08-29 14:15:49 -07001229 DetermineSecondarySlot();
1230 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001231
David Anderson95d40932018-08-28 12:14:33 -07001232 // First flash boot partitions. We allow this to happen either in userspace
1233 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001234 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001235
1236 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001237 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001238
1239 // Resize any logical partition to 0, so each partition is reset to 0
1240 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001241 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001242 auto resize_partition = [](const std::string& partition) -> void {
1243 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001244 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001245 }
1246 };
David Anderson95d40932018-08-28 12:14:33 -07001247 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001248 }
1249
David Anderson95d40932018-08-28 12:14:33 -07001250 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001251 FlashImages(os_images_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252}
1253
David Andersoncf444f32018-08-29 14:15:49 -07001254void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001255 std::vector<char> contents;
1256 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001257 die("could not read android-info.txt");
1258 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001259 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001260}
1261
1262void FlashAllTool::DetermineSecondarySlot() {
1263 if (skip_secondary_) {
1264 return;
1265 }
David Anderson96a9fd42018-11-05 15:21:44 -08001266 if (slot_override_ != "" && slot_override_ != "all") {
David Andersoncf444f32018-08-29 14:15:49 -07001267 secondary_slot_ = get_other_slot(slot_override_);
1268 } else {
1269 secondary_slot_ = get_other_slot();
1270 }
1271 if (secondary_slot_ == "") {
1272 if (supports_AB()) {
1273 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1274 }
1275 skip_secondary_ = true;
1276 }
1277}
1278
1279void FlashAllTool::CollectImages() {
1280 for (size_t i = 0; i < arraysize(images); ++i) {
1281 std::string slot = slot_override_;
1282 if (images[i].IsSecondary()) {
1283 if (skip_secondary_) {
1284 continue;
1285 }
1286 slot = secondary_slot_;
1287 }
1288 if (images[i].type == ImageType::BootCritical) {
1289 boot_images_.emplace_back(&images[i], slot);
1290 } else if (images[i].type == ImageType::Normal) {
1291 os_images_.emplace_back(&images[i], slot);
1292 }
1293 }
1294}
1295
1296void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1297 for (const auto& [image, slot] : images) {
1298 fastboot_buffer buf;
1299 int fd = source_.OpenFile(image->img_name);
1300 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1301 if (image->optional_if_no_image) {
1302 continue;
1303 }
1304 die("could not load '%s': %s", image->img_name, strerror(errno));
1305 }
1306 FlashImage(*image, slot, &buf);
1307 }
1308}
1309
1310void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1311 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001312 std::vector<char> signature_data;
1313 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001314 fb->Download("signature", signature_data);
1315 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001316 }
1317
1318 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001319 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001320 }
1321 flash_buf(partition_name.c_str(), buf);
1322 };
1323 do_for_partitions(image.part_name, slot, flash, false);
1324}
1325
1326void FlashAllTool::UpdateSuperPartition() {
David Andersoncf444f32018-08-29 14:15:49 -07001327 int fd = source_.OpenFile("super_empty.img");
1328 if (fd < 0) {
1329 return;
1330 }
1331 if (!is_userspace_fastboot()) {
1332 reboot_to_userspace_fastboot();
1333 }
David Andersoncf444f32018-08-29 14:15:49 -07001334
David Anderson90fe0a42018-11-05 18:01:32 -08001335 std::string super_name;
1336 if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
1337 super_name = "super";
1338 }
1339 fb->Download(super_name, fd, get_file_size(fd));
1340
1341 std::string command = "update-super:" + super_name;
David Andersoncf444f32018-08-29 14:15:49 -07001342 if (wipe_) {
1343 command += ":wipe";
1344 }
Tom Cherry9027af02018-09-24 15:48:09 -07001345 fb->RawCommand(command, "Updating super partition");
David Anderson0c732342018-11-28 17:10:42 -08001346
1347 // Retrofit devices have two super partitions, named super_a and super_b.
1348 // On these devices, secondary slots must be flashed as physical
1349 // partitions (otherwise they would not mount on first boot). To enforce
1350 // this, we delete any logical partitions for the "other" slot.
1351 if (is_retrofit_device()) {
1352 for (const auto& [image, slot] : os_images_) {
1353 std::string partition_name = image->part_name + "_"s + slot;
1354 if (image->IsSecondary() && is_logical(partition_name)) {
1355 fb->DeletePartition(partition_name);
1356 }
1357 }
1358 }
David Andersoncf444f32018-08-29 14:15:49 -07001359}
1360
1361class ZipImageSource final : public ImageSource {
1362 public:
1363 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001364 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001365 int OpenFile(const std::string& name) const override;
1366
1367 private:
1368 ZipArchiveHandle zip_;
1369};
1370
Tom Cherrydfd85df2018-09-20 14:45:05 -07001371bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1372 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001373}
1374
1375int ZipImageSource::OpenFile(const std::string& name) const {
1376 return unzip_to_file(zip_, name.c_str());
1377}
1378
1379static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001380 ZipArchiveHandle zip;
1381 int error = OpenArchive(filename, &zip);
1382 if (error != 0) {
1383 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1384 }
1385
1386 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1387 tool.Flash();
1388
1389 CloseArchive(zip);
1390}
1391
1392class LocalImageSource final : public ImageSource {
1393 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001394 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001395 int OpenFile(const std::string& name) const override;
1396};
1397
Tom Cherrydfd85df2018-09-20 14:45:05 -07001398bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001399 auto path = find_item_given_name(name);
1400 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001401 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001402 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001403 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001404}
1405
1406int LocalImageSource::OpenFile(const std::string& name) const {
1407 auto path = find_item_given_name(name);
Hridya Valsaraju1a6f6fe2019-04-03 17:57:00 -07001408 return open(path.c_str(), O_RDONLY | O_BINARY);
David Andersoncf444f32018-08-29 14:15:49 -07001409}
1410
1411static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001412 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1413 tool.Flash();
1414}
1415
Elliott Hughesd6365a72017-05-08 18:04:49 -07001416static std::string next_arg(std::vector<std::string>* args) {
1417 if (args->empty()) syntax_error("expected argument");
1418 std::string result = args->front();
1419 args->erase(args->begin());
1420 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001421}
1422
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001423static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001424 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001425
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001426 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001427 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001428 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001429 }
Tom Cherry9027af02018-09-24 15:48:09 -07001430 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001431}
1432
Connor O'Brience16a8a2017-02-06 14:39:31 -08001433static std::string fb_fix_numeric_var(std::string var) {
1434 // Some bootloaders (angler, for example), send spurious leading whitespace.
1435 var = android::base::Trim(var);
1436 // Some bootloaders (hammerhead, for example) use implicit hex.
1437 // This code used to use strtol with base 16.
1438 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1439 return var;
1440}
1441
Aaron Wisnerdb511202018-06-26 15:38:35 -05001442static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001443 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001444 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001445 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001446 return 0;
1447 }
1448 sizeString = fb_fix_numeric_var(sizeString);
1449
1450 unsigned size;
1451 if (!android::base::ParseUint(sizeString, &size)) {
1452 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1453 return 0;
1454 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001455 if ((size & (size - 1)) != 0) {
1456 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001457 return 0;
1458 }
1459 return size;
1460}
1461
Aaron Wisnerdb511202018-06-26 15:38:35 -05001462static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001463 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001464 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001465 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001466 std::string partition_type, partition_size;
1467
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001468 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001469 const char* errMsg = nullptr;
1470 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001471 TemporaryFile output;
1472 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001473
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001474 unsigned int limit = INT_MAX;
1475 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001476 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001477 }
1478 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001479 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001480 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001481
Tom Cherry9027af02018-09-24 15:48:09 -07001482 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001483 errMsg = "Can't determine partition type.\n";
1484 goto failed;
1485 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001486 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001487 if (partition_type != type_override) {
1488 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001489 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001490 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001491 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001492 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001493
Tom Cherry9027af02018-09-24 15:48:09 -07001494 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001495 errMsg = "Unable to get partition size\n";
1496 goto failed;
1497 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001498 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001499 if (partition_size != size_override) {
1500 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001501 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001502 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001503 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001504 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001505 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001506
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001507 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001508 if (!gen) {
1509 if (skip_if_not_supported) {
1510 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001511 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001512 return;
1513 }
Mark Salyzyna30b6962018-11-20 11:04:15 -08001514 die("Formatting is not supported for file system with type '%s'.",
1515 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001516 }
1517
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001518 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001519 if (!android::base::ParseInt(partition_size, &size)) {
Mark Salyzyna30b6962018-11-20 11:04:15 -08001520 die("Couldn't parse partition size '%s'.", partition_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001521 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001522
Connor O'Brience16a8a2017-02-06 14:39:31 -08001523 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001524 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1525 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001526
Jin Qian4a335822017-04-18 16:23:18 -07001527 if (fs_generator_generate(gen, output.path, size, initial_dir,
1528 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001529 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001530 }
1531
Jin Qian4a335822017-04-18 16:23:18 -07001532 fd.reset(open(output.path, O_RDONLY));
1533 if (fd == -1) {
Mark Salyzyna30b6962018-11-20 11:04:15 -08001534 die("Cannot open generated image: %s", strerror(errno));
Jin Qian4a335822017-04-18 16:23:18 -07001535 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001536 if (!load_buf_fd(fd.release(), &buf)) {
Mark Salyzyna30b6962018-11-20 11:04:15 -08001537 die("Cannot read image: %s", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001538 }
1539 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001540 return;
1541
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001542failed:
1543 if (skip_if_not_supported) {
1544 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001545 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001546 }
Tom Cherry9027af02018-09-24 15:48:09 -07001547 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Mark Salyzyna30b6962018-11-20 11:04:15 -08001548 if (!skip_if_not_supported) {
1549 die("Command failed");
1550 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001551}
1552
David Anderson89569642018-11-16 15:53:35 -08001553static bool should_flash_in_userspace(const std::string& partition_name) {
David Andersone0e693c2018-11-27 20:19:26 -08001554 if (!get_android_product_out()) {
1555 return false;
1556 }
David Anderson89569642018-11-16 15:53:35 -08001557 auto path = find_item_given_name("super_empty.img");
David Anderson00475802018-12-04 17:05:47 -08001558 if (path.empty() || access(path.c_str(), R_OK)) {
David Anderson89569642018-11-16 15:53:35 -08001559 return false;
1560 }
1561 auto metadata = android::fs_mgr::ReadFromImageFile(path);
1562 if (!metadata) {
1563 return false;
1564 }
1565 for (const auto& partition : metadata->partitions) {
1566 auto candidate = android::fs_mgr::GetPartitionName(partition);
1567 if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) {
1568 // On retrofit devices, we don't know if, or whether, the A or B
1569 // slot has been flashed for dynamic partitions. Instead we add
1570 // both names to the list as a conservative guess.
1571 if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) {
1572 return true;
1573 }
1574 } else if (candidate == partition_name) {
1575 return true;
1576 }
1577 }
1578 return false;
1579}
1580
Aaron Wisnerdb511202018-06-26 15:38:35 -05001581int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001582 bool wants_wipe = false;
1583 bool wants_reboot = false;
1584 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001585 bool wants_reboot_recovery = false;
1586 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001587 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001588 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001589 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001590 bool set_fbe_marker = false;
David Anderson89569642018-11-16 15:53:35 -08001591 bool force_flash = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001592 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001593 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001594 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001595
Elliott Hughes577e8b42018-04-05 16:12:47 -07001596 g_boot_img_hdr.kernel_addr = 0x00008000;
1597 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1598 g_boot_img_hdr.second_addr = 0x00f00000;
1599 g_boot_img_hdr.tags_addr = 0x00000100;
1600 g_boot_img_hdr.page_size = 2048;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001601 g_boot_img_hdr.dtb_addr = 0x01100000;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001602
JP Abgrall7b8970c2013-03-07 17:06:41 -08001603 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001604 {"base", required_argument, 0, 0},
1605 {"cmdline", required_argument, 0, 0},
1606 {"disable-verification", no_argument, 0, 0},
1607 {"disable-verity", no_argument, 0, 0},
David Anderson89569642018-11-16 15:53:35 -08001608 {"force", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001609 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001610 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001611 {"kernel-offset", required_argument, 0, 0},
1612 {"os-patch-level", required_argument, 0, 0},
1613 {"os-version", required_argument, 0, 0},
1614 {"page-size", required_argument, 0, 0},
1615 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001616 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001617 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001618 {"skip-secondary", no_argument, 0, 0},
1619 {"slot", required_argument, 0, 0},
1620 {"tags-offset", required_argument, 0, 0},
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001621 {"dtb", required_argument, 0, 0},
1622 {"dtb-offset", required_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001623 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001624 {"verbose", no_argument, 0, 'v'},
1625 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001626#if !defined(_WIN32)
1627 {"wipe-and-use-fbe", no_argument, 0, 0},
1628#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001629 {0, 0, 0, 0}
1630 };
Colin Cross8879f982012-05-22 17:53:34 -07001631
1632 serial = getenv("ANDROID_SERIAL");
1633
Elliott Hughes577e8b42018-04-05 16:12:47 -07001634 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001635 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001636 if (c == 0) {
1637 std::string name{longopts[longindex].name};
1638 if (name == "base") {
1639 g_base_addr = strtoul(optarg, 0, 16);
1640 } else if (name == "cmdline") {
1641 g_cmdline = optarg;
1642 } else if (name == "disable-verification") {
1643 g_disable_verification = true;
1644 } else if (name == "disable-verity") {
1645 g_disable_verity = true;
David Anderson89569642018-11-16 15:53:35 -08001646 } else if (name == "force") {
1647 force_flash = true;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001648 } else if (name == "header-version") {
1649 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001650 } else if (name == "dtb") {
1651 g_dtb_path = optarg;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001652 } else if (name == "kernel-offset") {
1653 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1654 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001655 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001656 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001657 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001658 } else if (name == "page-size") {
1659 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1660 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1661 } else if (name == "ramdisk-offset") {
1662 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1663 } else if (name == "skip-reboot") {
1664 skip_reboot = true;
1665 } else if (name == "skip-secondary") {
1666 skip_secondary = true;
1667 } else if (name == "slot") {
1668 slot_override = optarg;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001669 } else if (name == "dtb-offset") {
1670 g_boot_img_hdr.dtb_addr = strtoul(optarg, 0, 16);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001671 } else if (name == "tags-offset") {
1672 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1673 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001674 setvbuf(stdout, nullptr, _IONBF, 0);
1675 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001676 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001677 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001678 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001679 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001680#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001681 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001682 wants_wipe = true;
1683 set_fbe_marker = true;
1684#endif
1685 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001686 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001687 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001688 } else {
1689 switch (c) {
1690 case 'a':
1691 wants_set_active = true;
1692 if (optarg) next_active = optarg;
1693 break;
1694 case 'h':
1695 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001696 case 'l':
1697 g_long_listing = true;
1698 break;
1699 case 's':
1700 serial = optarg;
1701 break;
1702 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001703 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1704 die("invalid sparse limit %s", optarg);
1705 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001706 break;
1707 case 'v':
1708 set_verbose();
1709 break;
1710 case 'w':
1711 wants_wipe = true;
1712 break;
1713 case '?':
1714 return 1;
1715 default:
1716 abort();
1717 }
Colin Cross8879f982012-05-22 17:53:34 -07001718 }
1719 }
1720
1721 argc -= optind;
1722 argv += optind;
1723
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001725
Colin Cross8fb6e062012-07-24 16:36:41 -07001726 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001727 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001728 return 0;
1729 }
1730
Colin Crossc7b75dc2012-08-29 18:17:06 -07001731 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001732 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001733 }
1734
David Pursell0b156632015-10-30 11:22:01 -07001735 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001736 if (transport == nullptr) {
1737 return 1;
1738 }
Tom Cherry9027af02018-09-24 15:48:09 -07001739 fastboot::DriverCallbacks driver_callbacks = {
1740 .prolog = Status,
1741 .epilog = Epilog,
1742 .info = InfoMessage,
1743 };
1744 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1745 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001746
Elliott Hughes5620d222018-03-28 08:20:00 -07001747 const double start = now();
1748
Aaron Wisnerdb511202018-06-26 15:38:35 -05001749 if (slot_override != "") slot_override = verify_slot(slot_override);
1750 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001751
1752 if (wants_set_active) {
1753 if (next_active == "") {
1754 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001755 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001756 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001757 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001758 } else {
1759 wants_set_active = false;
1760 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001761 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001762 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001763 }
1764 }
1765 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001766
Elliott Hughesd6365a72017-05-08 18:04:49 -07001767 std::vector<std::string> args(argv, argv + argc);
1768 while (!args.empty()) {
1769 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001770
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001771 if (command == FB_CMD_GETVAR) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001772 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001773 DisplayVarOrError(variable, variable);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001774 } else if (command == FB_CMD_ERASE) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001775 std::string partition = next_arg(&args);
1776 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001777 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001778 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001779 fs_get_generator(partition_type) != nullptr) {
1780 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1781 partition_type.c_str());
1782 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001783
Tom Cherry9027af02018-09-24 15:48:09 -07001784 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001785 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001786 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001787 } else if (android::base::StartsWith(command, "format")) {
1788 // Parsing for: "format[:[type][:[size]]]"
1789 // Some valid things:
1790 // - select only the size, and leave default fs type:
1791 // format::0x4000000 userdata
1792 // - default fs type and size:
1793 // format userdata
1794 // format:: userdata
1795 std::vector<std::string> pieces = android::base::Split(command, ":");
1796 std::string type_override;
1797 if (pieces.size() > 1) type_override = pieces[1].c_str();
1798 std::string size_override;
1799 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001800
Elliott Hughesd6365a72017-05-08 18:04:49 -07001801 std::string partition = next_arg(&args);
1802
1803 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001804 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001805 };
Greg Kaiserb49d9e12019-03-26 12:04:05 -07001806 do_for_partitions(partition, slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001807 } else if (command == "signature") {
1808 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001809 std::vector<char> data;
1810 if (!ReadFileToVector(filename, &data)) {
1811 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1812 }
1813 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001814 fb->Download("signature", data);
1815 fb->RawCommand("signature", "installing signature");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001816 } else if (command == FB_CMD_REBOOT) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001817 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001818
1819 if (args.size() == 1) {
1820 std::string what = next_arg(&args);
1821 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001822 wants_reboot = false;
1823 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001824 } else if (what == "recovery") {
1825 wants_reboot = false;
1826 wants_reboot_recovery = true;
1827 } else if (what == "fastboot") {
1828 wants_reboot = false;
1829 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001830 } else {
1831 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001832 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001833
Elliott Hughesca85df02015-02-25 10:02:00 -08001834 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001835 if (!args.empty()) syntax_error("junk after reboot command");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001836 } else if (command == FB_CMD_REBOOT_BOOTLOADER) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001837 wants_reboot_bootloader = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001838 } else if (command == FB_CMD_REBOOT_RECOVERY) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001839 wants_reboot_recovery = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001840 } else if (command == FB_CMD_REBOOT_FASTBOOT) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001841 wants_reboot_fastboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001842 } else if (command == FB_CMD_CONTINUE) {
Tom Cherry9027af02018-09-24 15:48:09 -07001843 fb->Continue();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001844 } else if (command == FB_CMD_BOOT) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001845 std::string kernel = next_arg(&args);
1846 std::string ramdisk;
1847 if (!args.empty()) ramdisk = next_arg(&args);
1848 std::string second_stage;
1849 if (!args.empty()) second_stage = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001850 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001851 fb->Download("boot.img", data);
1852 fb->Boot();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001853 } else if (command == FB_CMD_FLASH) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001854 std::string pname = next_arg(&args);
1855
Elliott Hughes2810d002016-04-25 14:31:18 -07001856 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001857 if (!args.empty()) {
1858 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001859 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001860 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001861 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001862 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001863
1864 auto flash = [&](const std::string &partition) {
David Anderson89569642018-11-16 15:53:35 -08001865 if (should_flash_in_userspace(partition) && !is_userspace_fastboot() &&
1866 !force_flash) {
1867 die("The partition you are trying to flash is dynamic, and "
1868 "should be flashed via fastbootd. Please run:\n"
1869 "\n"
1870 " fastboot reboot fastboot\n"
1871 "\n"
1872 "And try again. If you are intentionally trying to "
1873 "overwrite a fixed partition, use --force.");
1874 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001875 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001876 };
Greg Kaiserb49d9e12019-03-26 12:04:05 -07001877 do_for_partitions(pname, slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001878 } else if (command == "flash:raw") {
1879 std::string partition = next_arg(&args);
1880 std::string kernel = next_arg(&args);
1881 std::string ramdisk;
1882 if (!args.empty()) ramdisk = next_arg(&args);
1883 std::string second_stage;
1884 if (!args.empty()) second_stage = next_arg(&args);
1885
Tom Cherrydfd85df2018-09-20 14:45:05 -07001886 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1887 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001888 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001889 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001890 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001891 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001892 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001893 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001894 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001895 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001896 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001897 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001898 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001899 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001900 bool slot_all = (slot_override == "all");
1901 if (slot_all) {
1902 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1903 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001904 std::string filename = "update.zip";
1905 if (!args.empty()) {
1906 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001907 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001908 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001909 wants_reboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001910 } else if (command == FB_CMD_SET_ACTIVE) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001911 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001912 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001913 } else if (command == "stage") {
1914 std::string filename = next_arg(&args);
1915
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001916 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001917 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001918 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001919 }
Tom Cherry9027af02018-09-24 15:48:09 -07001920 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001921 } else if (command == "get_staged") {
1922 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001923 fb->Upload(filename);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001924 } else if (command == FB_CMD_OEM) {
1925 do_oem_command(FB_CMD_OEM, &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001926 } else if (command == "flashing") {
1927 if (args.empty()) {
1928 syntax_error("missing 'flashing' command");
1929 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1930 args[0] == "unlock_critical" ||
1931 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001932 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001933 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001934 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001935 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001936 }
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001937 } else if (command == FB_CMD_CREATE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001938 std::string partition = next_arg(&args);
1939 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001940 fb->CreatePartition(partition, size);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001941 } else if (command == FB_CMD_DELETE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001942 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001943 fb->DeletePartition(partition);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001944 } else if (command == FB_CMD_RESIZE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001945 std::string partition = next_arg(&args);
1946 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001947 fb->ResizePartition(partition, size);
David Anderson1d504e32019-01-15 14:38:20 -08001948 } else if (command == "gsi") {
David Anderson3d782d52019-01-29 13:09:49 -08001949 std::string arg = next_arg(&args);
1950 if (arg == "wipe") {
David Anderson1d504e32019-01-15 14:38:20 -08001951 fb->RawCommand("gsi:wipe", "wiping GSI");
David Anderson3d782d52019-01-29 13:09:49 -08001952 } else if (arg == "disable") {
David Anderson1d504e32019-01-15 14:38:20 -08001953 fb->RawCommand("gsi:disable", "disabling GSI");
1954 } else {
1955 syntax_error("expected 'wipe' or 'disable'");
1956 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001957 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001958 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001959 }
1960 }
1961
1962 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001963 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1964 for (const auto& partition : partitions) {
1965 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001966 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1967 continue;
1968 }
Paul Crowley4d170062018-04-24 17:06:30 -07001969 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001970 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001971 if (partition == "userdata" && set_fbe_marker) {
1972 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1973 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001974 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001975 delete_fbemarker_tmpdir(initial_userdata_dir);
1976 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001977 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001978 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001979 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001980 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001981 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001982 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001983 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001984 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001985 fb->Reboot();
1986 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001987 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001988 fb->RebootTo("bootloader");
1989 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001990 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001991 fb->RebootTo("recovery");
1992 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001993 } else if (wants_reboot_fastboot) {
David Anderson11d6d272018-12-07 16:39:07 -08001994 reboot_to_userspace_fastboot();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001995 }
1996
Elliott Hughes5620d222018-03-28 08:20:00 -07001997 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001998
Tom Cherry9027af02018-09-24 15:48:09 -07001999 auto* old_transport = fb->set_transport(nullptr);
2000 delete old_transport;
2001
Tom Cherry11f12092018-08-29 21:36:28 -07002002 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002003}
Elliott Hughes6ebec932018-04-10 14:22:13 -07002004
Aaron Wisnerdb511202018-06-26 15:38:35 -05002005void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07002006 unsigned year, month, day;
2007 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
2008 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
2009 }
2010 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
2011 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
2012 hdr->SetOsPatchLevel(year, month);
2013}
2014
Aaron Wisnerdb511202018-06-26 15:38:35 -05002015void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07002016 unsigned major = 0, minor = 0, patch = 0;
2017 std::vector<std::string> versions = android::base::Split(arg, ".");
2018 if (versions.size() < 1 || versions.size() > 3 ||
2019 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
2020 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
2021 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
2022 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
2023 syntax_error("bad OS version: %s", arg);
2024 }
2025 hdr->SetOsVersion(major, minor, patch);
2026}