blob: 4cdd8bcd9e9d8a926b1ab942d738e280bdd5d90f [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) {
505 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700506 ZipEntry zip_entry;
507 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700508 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
509 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510 }
511
Tom Cherrydfd85df2018-09-20 14:45:05 -0700512 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513
Tom Cherrydfd85df2018-09-20 14:45:05 -0700514 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
515 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800516
Tom Cherrydfd85df2018-09-20 14:45:05 -0700517 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
518 out->size());
519 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000520
Tom Cherrydfd85df2018-09-20 14:45:05 -0700521 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522}
523
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700524#if defined(_WIN32)
525
526// TODO: move this to somewhere it can be shared.
527
528#include <windows.h>
529
530// Windows' tmpfile(3) requires administrator rights because
531// it creates temporary files in the root directory.
532static FILE* win32_tmpfile() {
533 char temp_path[PATH_MAX];
534 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
535 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700536 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700537 }
538
539 char filename[PATH_MAX];
540 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700541 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700542 }
543
544 return fopen(filename, "w+bTD");
545}
546
547#define tmpfile win32_tmpfile
548
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000549static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700550 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551}
552
Elliott Hughes855cdf82018-04-02 14:24:03 -0700553static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700554 // TODO: reimplement to avoid leaking a FILE*.
555 return fileno(tmpfile());
556}
557
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000558#else
559
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700560static std::string make_temporary_template() {
561 const char* tmpdir = getenv("TMPDIR");
562 if (tmpdir == nullptr) tmpdir = P_tmpdir;
563 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
564}
565
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000566static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700567 std::string result(make_temporary_template());
568 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700569 die("unable to create temporary directory with template %s: %s",
570 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000571 }
572 return result;
573}
574
Elliott Hughes855cdf82018-04-02 14:24:03 -0700575static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700576 std::string path_template(make_temporary_template());
577 int fd = mkstemp(&path_template[0]);
578 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700579 die("failed to create temporary file for %s with template %s: %s\n",
580 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700581 }
582 unlink(path_template.c_str());
583 return fd;
584}
585
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700586#endif
587
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000588static std::string create_fbemarker_tmpdir() {
589 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000590 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
591 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
592 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700593 die("unable to create FBE marker file %s locally: %s",
594 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000595 }
596 close(fd);
597 return dir;
598}
599
600static void delete_fbemarker_tmpdir(const std::string& dir) {
601 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
602 if (unlink(marker_file.c_str()) == -1) {
603 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
604 marker_file.c_str(), errno, strerror(errno));
605 return;
606 }
607 if (rmdir(dir.c_str()) == -1) {
608 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
609 dir.c_str(), errno, strerror(errno));
610 return;
611 }
612}
613
Elliott Hughes45964a82017-05-03 22:43:23 -0700614static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700615 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700616
Yusuke Sato07447542015-06-25 14:39:19 -0700617 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700618 ZipEntry zip_entry;
619 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
620 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700621 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000622 return -1;
623 }
624
Elliott Hughes23af1122017-11-10 08:42:17 -0800625 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700626 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800627 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700628 int error = ExtractEntryToFile(zip, &zip_entry, fd);
629 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800630 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700631 }
632
Elliott Hughes4089d342017-10-27 14:21:12 -0700633 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800634 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700635 }
636
Elliott Hughes23af1122017-11-10 08:42:17 -0800637 fprintf(stderr, " took %.3fs\n", now() - start);
638
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700639 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700640}
641
Tom Cherry4aa60b32018-09-05 15:11:44 -0700642static void CheckRequirement(const std::string& cur_product, const std::string& var,
643 const std::string& product, bool invert,
644 const std::vector<std::string>& options) {
645 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700646
Tom Cherry4aa60b32018-09-05 15:11:44 -0700647 double start = now();
648
649 if (!product.empty()) {
650 if (product != cur_product) {
651 double split = now();
652 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
653 cur_product.c_str(), product.c_str(), (split - start));
654 return;
655 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700657
658 std::string var_value;
Tom Cherry9027af02018-09-24 15:48:09 -0700659 if (fb->GetVar(var, &var_value) != fastboot::SUCCESS) {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700660 fprintf(stderr, "FAILED\n\n");
Tom Cherry9027af02018-09-24 15:48:09 -0700661 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(),
662 fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700663 die("requirements not met!");
664 }
665
666 bool match = false;
667 for (const auto& option : options) {
668 if (option == var_value || (option.back() == '*' &&
669 !var_value.compare(0, option.length() - 1, option, 0,
670 option.length() - 1))) {
671 match = true;
672 break;
673 }
674 }
675
676 if (invert) {
677 match = !match;
678 }
679
680 if (match) {
681 double split = now();
682 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
683 return;
684 }
685
686 fprintf(stderr, "FAILED\n\n");
687 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
688 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
689 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
690 fprintf(stderr, " or '%s'", it->c_str());
691 }
692 fprintf(stderr, ".\n\n");
693 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800694}
695
Tom Cherry4aa60b32018-09-05 15:11:44 -0700696bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
697 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700698 // "require product=alpha|beta|gamma"
699 // "require version-bootloader=1234"
700 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
701 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700702 *product = "";
703 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700704
Tom Cherry4aa60b32018-09-05 15:11:44 -0700705 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
706 auto require_product_regex =
707 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
708 std::smatch match_results;
709
710 if (std::regex_match(line, match_results, require_reject_regex)) {
711 *invert = Trim(match_results[1]) == "reject";
712 } else if (std::regex_match(line, match_results, require_product_regex)) {
713 *product = match_results[1];
714 } else {
715 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800716 }
717
Tom Cherry4aa60b32018-09-05 15:11:44 -0700718 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700719 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700720 if (*name == "board") {
721 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 }
723
Tom Cherry4aa60b32018-09-05 15:11:44 -0700724 auto raw_options = Split(match_results[3], "|");
725 for (const auto& option : raw_options) {
726 auto trimmed_option = Trim(option);
727 options->emplace_back(trimmed_option);
728 }
729
730 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731}
732
Tom Cherry4aa60b32018-09-05 15:11:44 -0700733// "require partition-exists=x" is a special case, added because of the trouble we had when
734// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
735// missing out new partitions. A device with new partitions can use "partition-exists" to
736// override the fields `optional_if_no_image` in the `images` array.
737static void HandlePartitionExists(const std::vector<std::string>& options) {
738 const std::string& partition_name = options[0];
739 std::string has_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700740 if (fb->GetVar("has-slot:" + partition_name, &has_slot) != fastboot::SUCCESS ||
Tom Cherry4aa60b32018-09-05 15:11:44 -0700741 (has_slot != "yes" && has_slot != "no")) {
742 die("device doesn't have required partition %s!", partition_name.c_str());
743 }
744 bool known_partition = false;
745 for (size_t i = 0; i < arraysize(images); ++i) {
746 if (images[i].nickname && images[i].nickname == partition_name) {
747 images[i].optional_if_no_image = false;
748 known_partition = true;
749 }
750 }
751 if (!known_partition) {
752 die("device requires partition %s which is not known to this version of fastboot",
753 partition_name.c_str());
754 }
755}
756
757static void CheckRequirements(const std::string& data) {
758 std::string cur_product;
Tom Cherry9027af02018-09-24 15:48:09 -0700759 if (fb->GetVar("product", &cur_product) != fastboot::SUCCESS) {
760 fprintf(stderr, "getvar:product FAILED (%s)\n", fb->Error().c_str());
Tom Cherry4aa60b32018-09-05 15:11:44 -0700761 }
762
763 auto lines = Split(data, "\n");
764 for (const auto& line : lines) {
765 if (line.empty()) {
766 continue;
767 }
768
769 std::string name;
770 std::string product;
771 bool invert;
772 std::vector<std::string> options;
773
774 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
775 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
776 continue;
777 }
778 if (name == "partition-exists") {
779 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800780 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700781 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800782 }
783 }
784}
785
Tom Cherry9027af02018-09-24 15:48:09 -0700786static void DisplayVarOrError(const std::string& label, const std::string& var) {
787 std::string value;
788
789 if (fb->GetVar(var, &value) != fastboot::SUCCESS) {
790 Status("getvar:" + var);
791 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
792 return;
793 }
794 fprintf(stderr, "%s: %s\n", label.c_str(), value.c_str());
795}
796
797static void DumpInfo() {
798 fprintf(stderr, "--------------------------------------------\n");
799 DisplayVarOrError("Bootloader Version...", "version-bootloader");
800 DisplayVarOrError("Baseband Version.....", "version-baseband");
801 DisplayVarOrError("Serial Number........", "serialno");
802 fprintf(stderr, "--------------------------------------------\n");
803
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800804}
805
Tao Bao41cf35f2018-04-24 10:54:21 -0700806static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700807 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700808 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700809
Tao Bao41cf35f2018-04-24 10:54:21 -0700810 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
811 die("invalid max size %" PRId64, max_size);
812 }
813
Elliott Hughesfc797672015-04-07 20:12:50 -0700814 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700815 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700816
Elliott Hughes253c18d2015-03-18 22:47:09 -0700817 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700818 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700819
820 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700821 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700822
823 return out_s;
824}
825
Aaron Wisnerdb511202018-06-26 15:38:35 -0500826static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700827 std::string max_download_size;
Tom Cherry9027af02018-09-24 15:48:09 -0700828 if (fb->GetVar("max-download-size", &max_download_size) != fastboot::SUCCESS ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700829 max_download_size.empty()) {
830 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700831 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700832 }
833
Elliott Hughes77c0e662015-11-02 15:51:12 -0800834 // Some bootloaders (angler, for example) send spurious whitespace too.
835 max_download_size = android::base::Trim(max_download_size);
836
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700837 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700838 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800839 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700840 return 0;
841 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700842 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700843 return limit;
844}
845
Aaron Wisnerdb511202018-06-26 15:38:35 -0500846static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700847 int64_t limit = sparse_limit;
848 if (limit == 0) {
849 // Unlimited, so see what the target device's limit is.
850 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700851 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500852 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700853 }
854 if (target_sparse_limit > 0) {
855 limit = target_sparse_limit;
856 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700857 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700858 }
859 }
860
861 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500862 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700863 }
864
865 return 0;
866}
867
Aaron Wisnerdb511202018-06-26 15:38:35 -0500868static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700869 int64_t sz = get_file_size(fd);
870 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700871 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700872 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700873
David Anderson32e376f2018-08-16 13:43:11 -0700874 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
875 buf->image_size = sparse_file_len(s, false, false);
876 sparse_file_destroy(s);
877 } else {
878 buf->image_size = sz;
879 }
880
Elliott Hughesa56a7292018-10-26 10:34:53 -0700881 lseek(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500882 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700883 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700884 sparse_file** s = load_sparse_files(fd, limit);
885 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700886 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700887 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700888 buf->type = FB_BUFFER_SPARSE;
889 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700890 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500891 buf->type = FB_BUFFER_FD;
892 buf->data = nullptr;
893 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000894 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700895 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700896
Elliott Hughes53ec4952016-05-11 12:39:27 -0700897 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700898}
899
Aaron Wisnerdb511202018-06-26 15:38:35 -0500900static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500901 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
902
Elliott Hughes53ec4952016-05-11 12:39:27 -0700903 if (fd == -1) {
904 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700905 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500906
907 struct stat s;
908 if (fstat(fd, &s)) {
909 return false;
910 }
911 if (!S_ISREG(s.st_mode)) {
912 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
913 return false;
914 }
915
Aaron Wisnerdb511202018-06-26 15:38:35 -0500916 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700917}
918
David Zeuthenb6ea4352017-08-07 14:29:26 -0400919static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
920 // Buffer needs to be at least the size of the VBMeta struct which
921 // is 256 bytes.
922 if (buf->sz < 256) {
923 return;
924 }
925
Elliott Hughes855cdf82018-04-02 14:24:03 -0700926 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400927
928 std::string data;
929 if (!android::base::ReadFdToString(buf->fd, &data)) {
930 die("Failed reading from vbmeta");
931 }
932
933 // There's a 32-bit big endian |flags| field at offset 120 where
934 // bit 0 corresponds to disable-verity and bit 1 corresponds to
935 // disable-verification.
936 //
937 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
938 // the VBMeta struct.
939 if (g_disable_verity) {
940 data[123] |= 0x01;
941 }
942 if (g_disable_verification) {
943 data[123] |= 0x02;
944 }
945
946 if (!android::base::WriteStringToFd(data, fd)) {
947 die("Failed writing to modified vbmeta");
948 }
949 close(buf->fd);
950 buf->fd = fd;
951 lseek(fd, 0, SEEK_SET);
952}
953
Elliott Hughes5620d222018-03-28 08:20:00 -0700954static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700955{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700956 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700957
David Zeuthenb6ea4352017-08-07 14:29:26 -0400958 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
959 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700960 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400961 rewrite_vbmeta_buffer(buf);
962 }
963
Rom Lemarchand622810c2013-06-28 09:54:59 -0700964 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800965 case FB_BUFFER_SPARSE: {
966 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700967 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700968 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700969 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800970 sparse_files.emplace_back(*s, sz);
971 ++s;
972 }
973
974 for (size_t i = 0; i < sparse_files.size(); ++i) {
975 const auto& pair = sparse_files[i];
Tom Cherry9027af02018-09-24 15:48:09 -0700976 fb->FlashPartition(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700977 }
978 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800979 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500980 case FB_BUFFER_FD:
Tom Cherry9027af02018-09-24 15:48:09 -0700981 fb->FlashPartition(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700982 break;
983 default:
984 die("unknown buffer type: %d", buf->type);
985 }
986}
987
Aaron Wisnerdb511202018-06-26 15:38:35 -0500988static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -0700990 if (fb->GetVar("current-slot", &current_slot) != fastboot::SUCCESS) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700991 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992}
993
Aaron Wisnerdb511202018-06-26 15:38:35 -0500994static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700995 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700996 int count = 0;
Tom Cherry9027af02018-09-24 15:48:09 -0700997 if (fb->GetVar("slot-count", &var) != fastboot::SUCCESS ||
998 !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700999 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001000 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001 return count;
1002}
1003
Aaron Wisnerdb511202018-06-26 15:38:35 -05001004static bool supports_AB() {
1005 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001006}
1007
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001008// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -07001009static std::string get_other_slot(const std::string& current_slot, int count) {
1010 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001011
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 char next = (current_slot[0] - 'a' + 1)%count + 'a';
1013 return std::string(1, next);
1014}
1015
Aaron Wisnerdb511202018-06-26 15:38:35 -05001016static std::string get_other_slot(const std::string& current_slot) {
1017 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018}
1019
Aaron Wisnerdb511202018-06-26 15:38:35 -05001020static std::string get_other_slot(int count) {
1021 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001022}
1023
Aaron Wisnerdb511202018-06-26 15:38:35 -05001024static std::string get_other_slot() {
1025 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -07001026}
1027
Aaron Wisnerdb511202018-06-26 15:38:35 -05001028static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001029 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -07001030 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001031 if (allow_all) {
1032 return "all";
1033 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001034 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001035 if (count > 0) {
1036 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001037 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -07001038 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001039 }
1040 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001041 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001042
Aaron Wisnerdb511202018-06-26 15:38:35 -05001043 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -07001044 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001045
Daniel Rosenberg80919472016-06-30 19:25:31 -07001046 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001047 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001048 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001049 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001050 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -07001051 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -08001052 }
1053
Daniel Rosenberg80919472016-06-30 19:25:31 -07001054 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
1055
1056 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
1057 for (int i=0; i<count; i++) {
1058 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001059 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001060
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001061 exit(1);
1062}
1063
Aaron Wisnerdb511202018-06-26 15:38:35 -05001064static std::string verify_slot(const std::string& slot) {
1065 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001066}
1067
Aaron Wisnerdb511202018-06-26 15:38:35 -05001068static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001069 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001070 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001071 std::string current_slot;
1072
Tom Cherry9027af02018-09-24 15:48:09 -07001073 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001074 /* If has-slot is not supported, the answer is no. */
1075 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001076 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001077 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001078 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001079 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001080 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001081 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001082 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001083 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001084 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001085 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001086 }
1087 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001088 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001089 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001090 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001091 }
1092 func(part);
1093 }
1094}
1095
David Pursell0b156632015-10-30 11:22:01 -07001096/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1097 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1098 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1099 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001100 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001101static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001102 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001103 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001104
Daniel Rosenberg80919472016-06-30 19:25:31 -07001105 if (slot == "all") {
Tom Cherry9027af02018-09-24 15:48:09 -07001106 if (fb->GetVar("has-slot:" + part, &has_slot) != fastboot::SUCCESS) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001107 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001108 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001109 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001110 for (int i=0; i < get_slot_count(); i++) {
1111 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001112 }
1113 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001114 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001115 }
1116 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001117 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001118 }
1119}
1120
David Anderson629e51c2018-10-25 12:51:25 -07001121static bool is_logical(const std::string& partition) {
1122 std::string value;
1123 return fb->GetVar("is-logical:" + partition, &value) == fastboot::SUCCESS && value == "yes";
1124}
1125
David Anderson0c732342018-11-28 17:10:42 -08001126static bool is_retrofit_device() {
1127 std::string value;
1128 if (fb->GetVar("super-partition-name", &value) != fastboot::SUCCESS) {
1129 return false;
1130 }
1131 return android::base::StartsWith(value, "system_");
1132}
1133
Aaron Wisnerdb511202018-06-26 15:38:35 -05001134static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001135 struct fastboot_buffer buf;
1136
Aaron Wisnerdb511202018-06-26 15:38:35 -05001137 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001138 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001139 }
David Anderson629e51c2018-10-25 12:51:25 -07001140 if (is_logical(pname)) {
1141 fb->ResizePartition(pname, std::to_string(buf.image_size));
1142 }
Rom Lemarchand622810c2013-06-28 09:54:59 -07001143 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001144}
1145
Daniel Rosenberg13454092016-06-27 19:43:11 -07001146// Sets slot_override as the active slot. If slot_override is blank,
1147// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001148static void set_active(const std::string& slot_override) {
1149 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001150
Daniel Rosenberg80919472016-06-30 19:25:31 -07001151 if (slot_override != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001152 fb->SetActive(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001153 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001154 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001155 if (current_slot != "") {
Tom Cherry9027af02018-09-24 15:48:09 -07001156 fb->SetActive(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001157 }
1158 }
1159}
1160
David Anderson32e376f2018-08-16 13:43:11 -07001161static bool is_userspace_fastboot() {
1162 std::string value;
Tom Cherry9027af02018-09-24 15:48:09 -07001163 return fb->GetVar("is-userspace", &value) == fastboot::SUCCESS && value == "yes";
David Anderson32e376f2018-08-16 13:43:11 -07001164}
1165
David Anderson1d887432018-08-27 16:47:32 -07001166static void reboot_to_userspace_fastboot() {
Tom Cherry9027af02018-09-24 15:48:09 -07001167 fb->RebootTo("fastboot");
1168
1169 auto* old_transport = fb->set_transport(nullptr);
1170 delete old_transport;
David Anderson1d887432018-08-27 16:47:32 -07001171
1172 // Give the current connection time to close.
1173 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1174
Tom Cherry9027af02018-09-24 15:48:09 -07001175 fb->set_transport(open_device());
David Anderson11d6d272018-12-07 16:39:07 -08001176
1177 if (!is_userspace_fastboot()) {
1178 die("Failed to boot into userspace fastboot; one or more components might be unbootable.");
1179 }
David Anderson1d887432018-08-27 16:47:32 -07001180}
1181
David Andersoncf444f32018-08-29 14:15:49 -07001182class ImageSource {
1183 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001184 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001185 virtual int OpenFile(const std::string& name) const = 0;
1186};
David Anderson32e376f2018-08-16 13:43:11 -07001187
David Andersoncf444f32018-08-29 14:15:49 -07001188class FlashAllTool {
1189 public:
1190 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001191
David Andersoncf444f32018-08-29 14:15:49 -07001192 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001193
David Andersoncf444f32018-08-29 14:15:49 -07001194 private:
1195 void CheckRequirements();
1196 void DetermineSecondarySlot();
1197 void CollectImages();
1198 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1199 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1200 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001201
David Andersoncf444f32018-08-29 14:15:49 -07001202 const ImageSource& source_;
1203 std::string slot_override_;
1204 bool skip_secondary_;
1205 bool wipe_;
1206 std::string secondary_slot_;
1207 std::vector<std::pair<const Image*, std::string>> boot_images_;
1208 std::vector<std::pair<const Image*, std::string>> os_images_;
1209};
1210
1211FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1212 : source_(source),
1213 slot_override_(slot_override),
1214 skip_secondary_(skip_secondary),
1215 wipe_(wipe)
1216{
David Anderson32e376f2018-08-16 13:43:11 -07001217}
1218
David Andersoncf444f32018-08-29 14:15:49 -07001219void FlashAllTool::Flash() {
Tom Cherry9027af02018-09-24 15:48:09 -07001220 DumpInfo();
David Andersoncf444f32018-08-29 14:15:49 -07001221 CheckRequirements();
David Anderson96a9fd42018-11-05 15:21:44 -08001222
1223 // Change the slot first, so we boot into the correct recovery image when
1224 // using fastbootd.
1225 if (slot_override_ == "all") {
1226 set_active("a");
1227 } else {
1228 set_active(slot_override_);
1229 }
1230
David Andersoncf444f32018-08-29 14:15:49 -07001231 DetermineSecondarySlot();
1232 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001233
David Anderson95d40932018-08-28 12:14:33 -07001234 // First flash boot partitions. We allow this to happen either in userspace
1235 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001236 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001237
1238 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001239 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001240
1241 // Resize any logical partition to 0, so each partition is reset to 0
1242 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001243 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001244 auto resize_partition = [](const std::string& partition) -> void {
1245 if (is_logical(partition)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001246 fb->ResizePartition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001247 }
1248 };
David Anderson95d40932018-08-28 12:14:33 -07001249 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001250 }
1251
David Anderson95d40932018-08-28 12:14:33 -07001252 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001253 FlashImages(os_images_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254}
1255
David Andersoncf444f32018-08-29 14:15:49 -07001256void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001257 std::vector<char> contents;
1258 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001259 die("could not read android-info.txt");
1260 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001261 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001262}
1263
1264void FlashAllTool::DetermineSecondarySlot() {
1265 if (skip_secondary_) {
1266 return;
1267 }
David Anderson96a9fd42018-11-05 15:21:44 -08001268 if (slot_override_ != "" && slot_override_ != "all") {
David Andersoncf444f32018-08-29 14:15:49 -07001269 secondary_slot_ = get_other_slot(slot_override_);
1270 } else {
1271 secondary_slot_ = get_other_slot();
1272 }
1273 if (secondary_slot_ == "") {
1274 if (supports_AB()) {
1275 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1276 }
1277 skip_secondary_ = true;
1278 }
1279}
1280
1281void FlashAllTool::CollectImages() {
1282 for (size_t i = 0; i < arraysize(images); ++i) {
1283 std::string slot = slot_override_;
1284 if (images[i].IsSecondary()) {
1285 if (skip_secondary_) {
1286 continue;
1287 }
1288 slot = secondary_slot_;
1289 }
1290 if (images[i].type == ImageType::BootCritical) {
1291 boot_images_.emplace_back(&images[i], slot);
1292 } else if (images[i].type == ImageType::Normal) {
1293 os_images_.emplace_back(&images[i], slot);
1294 }
1295 }
1296}
1297
1298void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1299 for (const auto& [image, slot] : images) {
1300 fastboot_buffer buf;
1301 int fd = source_.OpenFile(image->img_name);
1302 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1303 if (image->optional_if_no_image) {
1304 continue;
1305 }
1306 die("could not load '%s': %s", image->img_name, strerror(errno));
1307 }
1308 FlashImage(*image, slot, &buf);
1309 }
1310}
1311
1312void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1313 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001314 std::vector<char> signature_data;
1315 if (source_.ReadFile(image.sig_name, &signature_data)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001316 fb->Download("signature", signature_data);
1317 fb->RawCommand("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001318 }
1319
1320 if (is_logical(partition_name)) {
Tom Cherry9027af02018-09-24 15:48:09 -07001321 fb->ResizePartition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001322 }
1323 flash_buf(partition_name.c_str(), buf);
1324 };
1325 do_for_partitions(image.part_name, slot, flash, false);
1326}
1327
1328void FlashAllTool::UpdateSuperPartition() {
David Andersoncf444f32018-08-29 14:15:49 -07001329 int fd = source_.OpenFile("super_empty.img");
1330 if (fd < 0) {
1331 return;
1332 }
1333 if (!is_userspace_fastboot()) {
1334 reboot_to_userspace_fastboot();
1335 }
David Andersoncf444f32018-08-29 14:15:49 -07001336
David Anderson90fe0a42018-11-05 18:01:32 -08001337 std::string super_name;
1338 if (fb->GetVar("super-partition-name", &super_name) != fastboot::RetCode::SUCCESS) {
1339 super_name = "super";
1340 }
1341 fb->Download(super_name, fd, get_file_size(fd));
1342
1343 std::string command = "update-super:" + super_name;
David Andersoncf444f32018-08-29 14:15:49 -07001344 if (wipe_) {
1345 command += ":wipe";
1346 }
Tom Cherry9027af02018-09-24 15:48:09 -07001347 fb->RawCommand(command, "Updating super partition");
David Anderson0c732342018-11-28 17:10:42 -08001348
1349 // Retrofit devices have two super partitions, named super_a and super_b.
1350 // On these devices, secondary slots must be flashed as physical
1351 // partitions (otherwise they would not mount on first boot). To enforce
1352 // this, we delete any logical partitions for the "other" slot.
1353 if (is_retrofit_device()) {
1354 for (const auto& [image, slot] : os_images_) {
1355 std::string partition_name = image->part_name + "_"s + slot;
1356 if (image->IsSecondary() && is_logical(partition_name)) {
1357 fb->DeletePartition(partition_name);
1358 }
1359 }
1360 }
David Andersoncf444f32018-08-29 14:15:49 -07001361}
1362
1363class ZipImageSource final : public ImageSource {
1364 public:
1365 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001366 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001367 int OpenFile(const std::string& name) const override;
1368
1369 private:
1370 ZipArchiveHandle zip_;
1371};
1372
Tom Cherrydfd85df2018-09-20 14:45:05 -07001373bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1374 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001375}
1376
1377int ZipImageSource::OpenFile(const std::string& name) const {
1378 return unzip_to_file(zip_, name.c_str());
1379}
1380
1381static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
David Andersoncf444f32018-08-29 14:15:49 -07001382 ZipArchiveHandle zip;
1383 int error = OpenArchive(filename, &zip);
1384 if (error != 0) {
1385 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1386 }
1387
1388 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1389 tool.Flash();
1390
1391 CloseArchive(zip);
1392}
1393
1394class LocalImageSource final : public ImageSource {
1395 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001396 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001397 int OpenFile(const std::string& name) const override;
1398};
1399
Tom Cherrydfd85df2018-09-20 14:45:05 -07001400bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001401 auto path = find_item_given_name(name);
1402 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001403 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001404 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001405 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001406}
1407
1408int LocalImageSource::OpenFile(const std::string& name) const {
1409 auto path = find_item_given_name(name);
1410 return open(path.c_str(), O_RDONLY);
1411}
1412
1413static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
David Andersoncf444f32018-08-29 14:15:49 -07001414 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1415 tool.Flash();
1416}
1417
Elliott Hughesd6365a72017-05-08 18:04:49 -07001418static std::string next_arg(std::vector<std::string>* args) {
1419 if (args->empty()) syntax_error("expected argument");
1420 std::string result = args->front();
1421 args->erase(args->begin());
1422 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001423}
1424
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001425static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001426 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001427
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001428 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001429 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001430 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001431 }
Tom Cherry9027af02018-09-24 15:48:09 -07001432 fb->RawCommand(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001433}
1434
Connor O'Brience16a8a2017-02-06 14:39:31 -08001435static std::string fb_fix_numeric_var(std::string var) {
1436 // Some bootloaders (angler, for example), send spurious leading whitespace.
1437 var = android::base::Trim(var);
1438 // Some bootloaders (hammerhead, for example) use implicit hex.
1439 // This code used to use strtol with base 16.
1440 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1441 return var;
1442}
1443
Aaron Wisnerdb511202018-06-26 15:38:35 -05001444static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001445 std::string sizeString;
Tom Cherry9027af02018-09-24 15:48:09 -07001446 if (fb->GetVar(name, &sizeString) != fastboot::SUCCESS || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001447 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001448 return 0;
1449 }
1450 sizeString = fb_fix_numeric_var(sizeString);
1451
1452 unsigned size;
1453 if (!android::base::ParseUint(sizeString, &size)) {
1454 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1455 return 0;
1456 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001457 if ((size & (size - 1)) != 0) {
1458 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001459 return 0;
1460 }
1461 return size;
1462}
1463
Aaron Wisnerdb511202018-06-26 15:38:35 -05001464static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001465 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001466 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001467 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001468 std::string partition_type, partition_size;
1469
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001470 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001471 const char* errMsg = nullptr;
1472 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001473 TemporaryFile output;
1474 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001475
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001476 unsigned int limit = INT_MAX;
1477 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001478 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001479 }
1480 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001481 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001482 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001483
Tom Cherry9027af02018-09-24 15:48:09 -07001484 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001485 errMsg = "Can't determine partition type.\n";
1486 goto failed;
1487 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001488 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001489 if (partition_type != type_override) {
1490 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001491 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001492 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001493 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001494 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001495
Tom Cherry9027af02018-09-24 15:48:09 -07001496 if (fb->GetVar("partition-size:" + partition, &partition_size) != fastboot::SUCCESS) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001497 errMsg = "Unable to get partition size\n";
1498 goto failed;
1499 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001500 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001501 if (partition_size != size_override) {
1502 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001503 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001504 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001505 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001506 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001507 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001508
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001509 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001510 if (!gen) {
1511 if (skip_if_not_supported) {
1512 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001513 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001514 return;
1515 }
Mark Salyzyna30b6962018-11-20 11:04:15 -08001516 die("Formatting is not supported for file system with type '%s'.",
1517 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001518 }
1519
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001520 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001521 if (!android::base::ParseInt(partition_size, &size)) {
Mark Salyzyna30b6962018-11-20 11:04:15 -08001522 die("Couldn't parse partition size '%s'.", partition_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001523 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001524
Connor O'Brience16a8a2017-02-06 14:39:31 -08001525 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001526 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1527 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001528
Jin Qian4a335822017-04-18 16:23:18 -07001529 if (fs_generator_generate(gen, output.path, size, initial_dir,
1530 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001531 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001532 }
1533
Jin Qian4a335822017-04-18 16:23:18 -07001534 fd.reset(open(output.path, O_RDONLY));
1535 if (fd == -1) {
Mark Salyzyna30b6962018-11-20 11:04:15 -08001536 die("Cannot open generated image: %s", strerror(errno));
Jin Qian4a335822017-04-18 16:23:18 -07001537 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001538 if (!load_buf_fd(fd.release(), &buf)) {
Mark Salyzyna30b6962018-11-20 11:04:15 -08001539 die("Cannot read image: %s", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001540 }
1541 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001542 return;
1543
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001544failed:
1545 if (skip_if_not_supported) {
1546 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001547 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001548 }
Tom Cherry9027af02018-09-24 15:48:09 -07001549 fprintf(stderr, "FAILED (%s)\n", fb->Error().c_str());
Mark Salyzyna30b6962018-11-20 11:04:15 -08001550 if (!skip_if_not_supported) {
1551 die("Command failed");
1552 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001553}
1554
David Anderson89569642018-11-16 15:53:35 -08001555static bool should_flash_in_userspace(const std::string& partition_name) {
David Andersone0e693c2018-11-27 20:19:26 -08001556 if (!get_android_product_out()) {
1557 return false;
1558 }
David Anderson89569642018-11-16 15:53:35 -08001559 auto path = find_item_given_name("super_empty.img");
David Anderson00475802018-12-04 17:05:47 -08001560 if (path.empty() || access(path.c_str(), R_OK)) {
David Anderson89569642018-11-16 15:53:35 -08001561 return false;
1562 }
1563 auto metadata = android::fs_mgr::ReadFromImageFile(path);
1564 if (!metadata) {
1565 return false;
1566 }
1567 for (const auto& partition : metadata->partitions) {
1568 auto candidate = android::fs_mgr::GetPartitionName(partition);
1569 if (partition.attributes & LP_PARTITION_ATTR_SLOT_SUFFIXED) {
1570 // On retrofit devices, we don't know if, or whether, the A or B
1571 // slot has been flashed for dynamic partitions. Instead we add
1572 // both names to the list as a conservative guess.
1573 if (candidate + "_a" == partition_name || candidate + "_b" == partition_name) {
1574 return true;
1575 }
1576 } else if (candidate == partition_name) {
1577 return true;
1578 }
1579 }
1580 return false;
1581}
1582
Aaron Wisnerdb511202018-06-26 15:38:35 -05001583int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001584 bool wants_wipe = false;
1585 bool wants_reboot = false;
1586 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001587 bool wants_reboot_recovery = false;
1588 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001589 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001590 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001591 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001592 bool set_fbe_marker = false;
David Anderson89569642018-11-16 15:53:35 -08001593 bool force_flash = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001594 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001595 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001596 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001597
Elliott Hughes577e8b42018-04-05 16:12:47 -07001598 g_boot_img_hdr.kernel_addr = 0x00008000;
1599 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1600 g_boot_img_hdr.second_addr = 0x00f00000;
1601 g_boot_img_hdr.tags_addr = 0x00000100;
1602 g_boot_img_hdr.page_size = 2048;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001603 g_boot_img_hdr.dtb_addr = 0x01100000;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001604
JP Abgrall7b8970c2013-03-07 17:06:41 -08001605 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001606 {"base", required_argument, 0, 0},
1607 {"cmdline", required_argument, 0, 0},
1608 {"disable-verification", no_argument, 0, 0},
1609 {"disable-verity", no_argument, 0, 0},
David Anderson89569642018-11-16 15:53:35 -08001610 {"force", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001611 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001612 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001613 {"kernel-offset", required_argument, 0, 0},
1614 {"os-patch-level", required_argument, 0, 0},
1615 {"os-version", required_argument, 0, 0},
1616 {"page-size", required_argument, 0, 0},
1617 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001618 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001619 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001620 {"skip-secondary", no_argument, 0, 0},
1621 {"slot", required_argument, 0, 0},
1622 {"tags-offset", required_argument, 0, 0},
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001623 {"dtb", required_argument, 0, 0},
1624 {"dtb-offset", required_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001625 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001626 {"verbose", no_argument, 0, 'v'},
1627 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001628#if !defined(_WIN32)
1629 {"wipe-and-use-fbe", no_argument, 0, 0},
1630#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001631 {0, 0, 0, 0}
1632 };
Colin Cross8879f982012-05-22 17:53:34 -07001633
1634 serial = getenv("ANDROID_SERIAL");
1635
Elliott Hughes577e8b42018-04-05 16:12:47 -07001636 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001637 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001638 if (c == 0) {
1639 std::string name{longopts[longindex].name};
1640 if (name == "base") {
1641 g_base_addr = strtoul(optarg, 0, 16);
1642 } else if (name == "cmdline") {
1643 g_cmdline = optarg;
1644 } else if (name == "disable-verification") {
1645 g_disable_verification = true;
1646 } else if (name == "disable-verity") {
1647 g_disable_verity = true;
David Anderson89569642018-11-16 15:53:35 -08001648 } else if (name == "force") {
1649 force_flash = true;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001650 } else if (name == "header-version") {
1651 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001652 } else if (name == "dtb") {
1653 g_dtb_path = optarg;
Elliott Hughes577e8b42018-04-05 16:12:47 -07001654 } else if (name == "kernel-offset") {
1655 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1656 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001657 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001658 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001659 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001660 } else if (name == "page-size") {
1661 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1662 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1663 } else if (name == "ramdisk-offset") {
1664 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1665 } else if (name == "skip-reboot") {
1666 skip_reboot = true;
1667 } else if (name == "skip-secondary") {
1668 skip_secondary = true;
1669 } else if (name == "slot") {
1670 slot_override = optarg;
Hridya Valsaraju6c8fca62019-01-24 12:47:08 -08001671 } else if (name == "dtb-offset") {
1672 g_boot_img_hdr.dtb_addr = strtoul(optarg, 0, 16);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001673 } else if (name == "tags-offset") {
1674 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1675 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001676 setvbuf(stdout, nullptr, _IONBF, 0);
1677 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001678 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001679 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001680 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001681 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001682#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001683 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001684 wants_wipe = true;
1685 set_fbe_marker = true;
1686#endif
1687 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001688 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001689 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001690 } else {
1691 switch (c) {
1692 case 'a':
1693 wants_set_active = true;
1694 if (optarg) next_active = optarg;
1695 break;
1696 case 'h':
1697 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001698 case 'l':
1699 g_long_listing = true;
1700 break;
1701 case 's':
1702 serial = optarg;
1703 break;
1704 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001705 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1706 die("invalid sparse limit %s", optarg);
1707 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001708 break;
1709 case 'v':
1710 set_verbose();
1711 break;
1712 case 'w':
1713 wants_wipe = true;
1714 break;
1715 case '?':
1716 return 1;
1717 default:
1718 abort();
1719 }
Colin Cross8879f982012-05-22 17:53:34 -07001720 }
1721 }
1722
1723 argc -= optind;
1724 argv += optind;
1725
Elliott Hughesd6365a72017-05-08 18:04:49 -07001726 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001727
Colin Cross8fb6e062012-07-24 16:36:41 -07001728 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001729 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001730 return 0;
1731 }
1732
Colin Crossc7b75dc2012-08-29 18:17:06 -07001733 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001734 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001735 }
1736
David Pursell0b156632015-10-30 11:22:01 -07001737 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001738 if (transport == nullptr) {
1739 return 1;
1740 }
Tom Cherry9027af02018-09-24 15:48:09 -07001741 fastboot::DriverCallbacks driver_callbacks = {
1742 .prolog = Status,
1743 .epilog = Epilog,
1744 .info = InfoMessage,
1745 };
1746 fastboot::FastBootDriver fastboot_driver(transport, driver_callbacks, false);
1747 fb = &fastboot_driver;
David Pursell2ec418a2016-01-20 08:32:08 -08001748
Elliott Hughes5620d222018-03-28 08:20:00 -07001749 const double start = now();
1750
Aaron Wisnerdb511202018-06-26 15:38:35 -05001751 if (slot_override != "") slot_override = verify_slot(slot_override);
1752 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001753
1754 if (wants_set_active) {
1755 if (next_active == "") {
1756 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001757 std::string current_slot;
Tom Cherry9027af02018-09-24 15:48:09 -07001758 if (fb->GetVar("current-slot", &current_slot) == fastboot::SUCCESS) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001759 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001760 } else {
1761 wants_set_active = false;
1762 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001763 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001764 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001765 }
1766 }
1767 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001768
Elliott Hughesd6365a72017-05-08 18:04:49 -07001769 std::vector<std::string> args(argv, argv + argc);
1770 while (!args.empty()) {
1771 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001772
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001773 if (command == FB_CMD_GETVAR) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001774 std::string variable = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001775 DisplayVarOrError(variable, variable);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001776 } else if (command == FB_CMD_ERASE) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001777 std::string partition = next_arg(&args);
1778 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001779 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001780 if (fb->GetVar("partition-type:" + partition, &partition_type) == fastboot::SUCCESS &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001781 fs_get_generator(partition_type) != nullptr) {
1782 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1783 partition_type.c_str());
1784 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001785
Tom Cherry9027af02018-09-24 15:48:09 -07001786 fb->Erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001787 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001788 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001789 } else if (android::base::StartsWith(command, "format")) {
1790 // Parsing for: "format[:[type][:[size]]]"
1791 // Some valid things:
1792 // - select only the size, and leave default fs type:
1793 // format::0x4000000 userdata
1794 // - default fs type and size:
1795 // format userdata
1796 // format:: userdata
1797 std::vector<std::string> pieces = android::base::Split(command, ":");
1798 std::string type_override;
1799 if (pieces.size() > 1) type_override = pieces[1].c_str();
1800 std::string size_override;
1801 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001802
Elliott Hughesd6365a72017-05-08 18:04:49 -07001803 std::string partition = next_arg(&args);
1804
1805 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001806 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001807 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001808 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001809 } else if (command == "signature") {
1810 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001811 std::vector<char> data;
1812 if (!ReadFileToVector(filename, &data)) {
1813 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1814 }
1815 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
Tom Cherry9027af02018-09-24 15:48:09 -07001816 fb->Download("signature", data);
1817 fb->RawCommand("signature", "installing signature");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001818 } else if (command == FB_CMD_REBOOT) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001819 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001820
1821 if (args.size() == 1) {
1822 std::string what = next_arg(&args);
1823 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001824 wants_reboot = false;
1825 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001826 } else if (what == "recovery") {
1827 wants_reboot = false;
1828 wants_reboot_recovery = true;
1829 } else if (what == "fastboot") {
1830 wants_reboot = false;
1831 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001832 } else {
1833 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001834 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001835
Elliott Hughesca85df02015-02-25 10:02:00 -08001836 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001837 if (!args.empty()) syntax_error("junk after reboot command");
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001838 } else if (command == FB_CMD_REBOOT_BOOTLOADER) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001839 wants_reboot_bootloader = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001840 } else if (command == FB_CMD_REBOOT_RECOVERY) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001841 wants_reboot_recovery = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001842 } else if (command == FB_CMD_REBOOT_FASTBOOT) {
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001843 wants_reboot_fastboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001844 } else if (command == FB_CMD_CONTINUE) {
Tom Cherry9027af02018-09-24 15:48:09 -07001845 fb->Continue();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001846 } else if (command == FB_CMD_BOOT) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001847 std::string kernel = next_arg(&args);
1848 std::string ramdisk;
1849 if (!args.empty()) ramdisk = next_arg(&args);
1850 std::string second_stage;
1851 if (!args.empty()) second_stage = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001852 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
Tom Cherry9027af02018-09-24 15:48:09 -07001853 fb->Download("boot.img", data);
1854 fb->Boot();
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001855 } else if (command == FB_CMD_FLASH) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001856 std::string pname = next_arg(&args);
1857
Elliott Hughes2810d002016-04-25 14:31:18 -07001858 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001859 if (!args.empty()) {
1860 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001861 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001862 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001863 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001864 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001865
1866 auto flash = [&](const std::string &partition) {
David Anderson89569642018-11-16 15:53:35 -08001867 if (should_flash_in_userspace(partition) && !is_userspace_fastboot() &&
1868 !force_flash) {
1869 die("The partition you are trying to flash is dynamic, and "
1870 "should be flashed via fastbootd. Please run:\n"
1871 "\n"
1872 " fastboot reboot fastboot\n"
1873 "\n"
1874 "And try again. If you are intentionally trying to "
1875 "overwrite a fixed partition, use --force.");
1876 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001877 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001878 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001879 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001880 } else if (command == "flash:raw") {
1881 std::string partition = next_arg(&args);
1882 std::string kernel = next_arg(&args);
1883 std::string ramdisk;
1884 if (!args.empty()) ramdisk = next_arg(&args);
1885 std::string second_stage;
1886 if (!args.empty()) second_stage = next_arg(&args);
1887
Tom Cherrydfd85df2018-09-20 14:45:05 -07001888 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1889 auto flashraw = [&data](const std::string& partition) {
Tom Cherry9027af02018-09-24 15:48:09 -07001890 fb->FlashPartition(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001891 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001892 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001893 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001894 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001895 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001896 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001897 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001898 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001899 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001900 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001901 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001902 bool slot_all = (slot_override == "all");
1903 if (slot_all) {
1904 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1905 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001906 std::string filename = "update.zip";
1907 if (!args.empty()) {
1908 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001909 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001910 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001911 wants_reboot = true;
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001912 } else if (command == FB_CMD_SET_ACTIVE) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001913 std::string slot = verify_slot(next_arg(&args), false);
Tom Cherry9027af02018-09-24 15:48:09 -07001914 fb->SetActive(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001915 } else if (command == "stage") {
1916 std::string filename = next_arg(&args);
1917
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001918 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001919 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001920 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001921 }
Tom Cherry9027af02018-09-24 15:48:09 -07001922 fb->Download(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001923 } else if (command == "get_staged") {
1924 std::string filename = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001925 fb->Upload(filename);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001926 } else if (command == FB_CMD_OEM) {
1927 do_oem_command(FB_CMD_OEM, &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001928 } else if (command == "flashing") {
1929 if (args.empty()) {
1930 syntax_error("missing 'flashing' command");
1931 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1932 args[0] == "unlock_critical" ||
1933 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001934 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001935 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001936 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001937 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001938 }
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001939 } else if (command == FB_CMD_CREATE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001940 std::string partition = next_arg(&args);
1941 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001942 fb->CreatePartition(partition, size);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001943 } else if (command == FB_CMD_DELETE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001944 std::string partition = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001945 fb->DeletePartition(partition);
Mark Salyzyn8e7e9cb2018-08-29 10:44:33 -07001946 } else if (command == FB_CMD_RESIZE_PARTITION) {
David Anderson0d4277d2018-07-31 13:27:37 -07001947 std::string partition = next_arg(&args);
1948 std::string size = next_arg(&args);
Tom Cherry9027af02018-09-24 15:48:09 -07001949 fb->ResizePartition(partition, size);
David Anderson1d504e32019-01-15 14:38:20 -08001950 } else if (command == "gsi") {
David Anderson3d782d52019-01-29 13:09:49 -08001951 std::string arg = next_arg(&args);
1952 if (arg == "wipe") {
David Anderson1d504e32019-01-15 14:38:20 -08001953 fb->RawCommand("gsi:wipe", "wiping GSI");
David Anderson3d782d52019-01-29 13:09:49 -08001954 } else if (arg == "disable") {
David Anderson1d504e32019-01-15 14:38:20 -08001955 fb->RawCommand("gsi:disable", "disabling GSI");
1956 } else {
1957 syntax_error("expected 'wipe' or 'disable'");
1958 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001959 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001960 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001961 }
1962 }
1963
1964 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001965 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1966 for (const auto& partition : partitions) {
1967 std::string partition_type;
Tom Cherry9027af02018-09-24 15:48:09 -07001968 if (fb->GetVar("partition-type:" + partition, &partition_type) != fastboot::SUCCESS) {
1969 continue;
1970 }
Paul Crowley4d170062018-04-24 17:06:30 -07001971 if (partition_type.empty()) continue;
Tom Cherry9027af02018-09-24 15:48:09 -07001972 fb->Erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001973 if (partition == "userdata" && set_fbe_marker) {
1974 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1975 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001976 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001977 delete_fbemarker_tmpdir(initial_userdata_dir);
1978 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001979 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001980 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001981 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001982 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001983 if (wants_set_active) {
Tom Cherry9027af02018-09-24 15:48:09 -07001984 fb->SetActive(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001985 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001986 if (wants_reboot && !skip_reboot) {
Tom Cherry9027af02018-09-24 15:48:09 -07001987 fb->Reboot();
1988 fb->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001989 } else if (wants_reboot_bootloader) {
Tom Cherry9027af02018-09-24 15:48:09 -07001990 fb->RebootTo("bootloader");
1991 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001992 } else if (wants_reboot_recovery) {
Tom Cherry9027af02018-09-24 15:48:09 -07001993 fb->RebootTo("recovery");
1994 fb->WaitForDisconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001995 } else if (wants_reboot_fastboot) {
David Anderson11d6d272018-12-07 16:39:07 -08001996 reboot_to_userspace_fastboot();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001997 }
1998
Elliott Hughes5620d222018-03-28 08:20:00 -07001999 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07002000
Tom Cherry9027af02018-09-24 15:48:09 -07002001 auto* old_transport = fb->set_transport(nullptr);
2002 delete old_transport;
2003
Tom Cherry11f12092018-08-29 21:36:28 -07002004 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08002005}
Elliott Hughes6ebec932018-04-10 14:22:13 -07002006
Aaron Wisnerdb511202018-06-26 15:38:35 -05002007void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07002008 unsigned year, month, day;
2009 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
2010 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
2011 }
2012 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
2013 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
2014 hdr->SetOsPatchLevel(year, month);
2015}
2016
Aaron Wisnerdb511202018-06-26 15:38:35 -05002017void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07002018 unsigned major = 0, minor = 0, patch = 0;
2019 std::vector<std::string> versions = android::base::Split(arg, ".");
2020 if (versions.size() < 1 || versions.size() > 3 ||
2021 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
2022 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
2023 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
2024 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
2025 syntax_error("bad OS version: %s", arg);
2026 }
2027 hdr->SetOsVersion(major, minor, patch);
2028}