blob: 331b0c805288f240c88876d137c12d37f54367e8 [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
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070056#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050057#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050063#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Chris Fries0ea946c2017-04-12 10:25:57 -050070using android::base::unique_fd;
71
Colin Crossf8387882012-05-24 17:18:41 -070072#ifndef O_BINARY
73#define O_BINARY 0
74#endif
75
Wink Savilleb98762f2011-04-04 17:54:59 -070076char cur_product[FB_RESPONSE_SZ + 1];
77
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070079
Elliott Hughes577e8b42018-04-05 16:12:47 -070080static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070085static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070086static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes577e8b42018-04-05 16:12:47 -070088static unsigned g_base_addr = 0x10000000;
89static boot_img_hdr_v1 g_boot_img_hdr = {};
90static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080091
David Zeuthenb6ea4352017-08-07 14:29:26 -040092static bool g_disable_verity = false;
93static bool g_disable_verification = false;
94
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700107 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700108};
109
David Anderson0debda02018-08-28 13:54:03 -0700110enum class ImageType {
111 // Must be flashed for device to boot into the kernel.
112 BootCritical,
113 // Normal partition to be flashed during "flashall".
114 Normal,
115 // Partition that is never flashed during "flashall".
116 Extra
117};
118
David Anderson32e376f2018-08-16 13:43:11 -0700119struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700120 const char* nickname;
121 const char* img_name;
122 const char* sig_name;
123 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700124 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700125 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700126 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700127};
128
129static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100130 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700131 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
132 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
133 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
134 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
135 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
136 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
137 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100138 { "product_services",
139 "product_services.img",
140 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700141 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700142 true, ImageType::Normal },
143 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
144 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
145 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
146 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
147 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
148 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
149 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
150 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100151 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700152};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700153
David Anderson0debda02018-08-28 13:54:03 -0700154static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700155 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700156 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700157 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700158 }
David Anderson0debda02018-08-28 13:54:03 -0700159 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700160}
161
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700162static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700163 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700164 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700165 return find_item_given_name(images[i].img_name);
166 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168
Elliott Hughesd6365a72017-05-08 18:04:49 -0700169 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700170 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171}
172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173static int64_t get_file_size(int fd) {
174 struct stat sb;
175 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700176}
177
Elliott Hughesfc797672015-04-07 20:12:50 -0700178static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700180 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 *sz = get_file_size(fd);
183 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700184 goto oops;
185 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186
Elliott Hughesfc797672015-04-07 20:12:50 -0700187 data = (char*) malloc(*sz);
188 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfc797672015-04-07 20:12:50 -0700190 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 close(fd);
192
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 return data;
194
195oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800196 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 close(fd);
198 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800199 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 return 0;
201}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
Elliott Hughes2810d002016-04-25 14:31:18 -0700203static void* load_file(const std::string& path, int64_t* sz) {
204 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700205 if (fd == -1) return nullptr;
206 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700207}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208
Elliott Hughesfc797672015-04-07 20:12:50 -0700209static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700210 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
211 return -1;
212 }
213
Scott Anderson13081c62012-04-06 12:39:30 -0700214 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
217 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 return 0;
219}
220
Elliott Hughesfc797672015-04-07 20:12:50 -0700221static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800222 return match_fastboot_with_serial(info, serial);
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static int list_devices_callback(usb_ifc_info* info) {
226 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700228 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700230 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if (!serial[0]) {
232 serial = "????????????";
233 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700234 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700235 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800236 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700237 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800238 printf("%-22s fastboot", serial.c_str());
239 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700240 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800241 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
244 return -1;
245}
246
David Pursell2ec418a2016-01-20 08:32:08 -0800247// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
248// a specific device, otherwise the first USB device found will be used.
249//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700250// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800251// Otherwise it blocks until the target is available.
252//
253// The returned Transport is a singleton, so multiple calls to this function will return the same
254// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700255static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800256 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257
David Pursell4601c972016-02-05 15:35:09 -0800258 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800259 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800260 int port = 0;
261 if (serial != nullptr) {
262 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800263
David Pursell4601c972016-02-05 15:35:09 -0800264 if (android::base::StartsWith(serial, "tcp:")) {
265 protocol = Socket::Protocol::kTcp;
266 port = tcp::kDefaultPort;
267 net_address = serial + strlen("tcp:");
268 } else if (android::base::StartsWith(serial, "udp:")) {
269 protocol = Socket::Protocol::kUdp;
270 port = udp::kDefaultPort;
271 net_address = serial + strlen("udp:");
272 }
273
274 if (net_address != nullptr) {
275 std::string error;
276 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700277 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800278 }
David Pursell2ec418a2016-01-20 08:32:08 -0800279 }
280 }
281
David Anderson1d887432018-08-27 16:47:32 -0700282 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800283 while (true) {
284 if (!host.empty()) {
285 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800286 if (protocol == Socket::Protocol::kTcp) {
287 transport = tcp::Connect(host, port, &error).release();
288 } else if (protocol == Socket::Protocol::kUdp) {
289 transport = udp::Connect(host, port, &error).release();
290 }
291
David Pursell2ec418a2016-01-20 08:32:08 -0800292 if (transport == nullptr && announce) {
293 fprintf(stderr, "error: %s\n", error.c_str());
294 }
295 } else {
296 transport = usb_open(match_fastboot);
297 }
298
299 if (transport != nullptr) {
300 return transport;
301 }
302
David Pursell0b156632015-10-30 11:22:01 -0700303 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800304 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700305 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800307 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 }
309}
310
Elliott Hughesfc797672015-04-07 20:12:50 -0700311static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 // We don't actually open a USB device here,
313 // just getting our callback called so we can
314 // list all the connected devices.
315 usb_open(list_devices_callback);
316}
317
Elliott Hughesd6365a72017-05-08 18:04:49 -0700318static void syntax_error(const char* fmt, ...) {
319 fprintf(stderr, "fastboot: usage: ");
320
321 va_list ap;
322 va_start(ap, fmt);
323 vfprintf(stderr, fmt, ap);
324 va_end(ap);
325
326 fprintf(stderr, "\n");
327 exit(1);
328}
329
330static int show_help() {
331 // clang-format off
332 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700333// 1 2 3 4 5 6 7 8
334// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700335 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800336 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700337 "flashing:\n"
338 " update ZIP Flash all partitions from an update.zip package.\n"
339 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
340 " On A/B devices, flashed slot is set as active.\n"
341 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700342 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
343 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700344 "\n"
345 "basics:\n"
346 " devices [-l] List devices in bootloader (-l: with device paths).\n"
347 " getvar NAME Display given bootloader variable.\n"
348 " reboot [bootloader] Reboot device.\n"
349 "\n"
350 "locking/unlocking:\n"
351 " flashing lock|unlock Lock/unlock partitions for flashing\n"
352 " flashing lock_critical|unlock_critical\n"
353 " Lock/unlock 'critical' bootloader partitions.\n"
354 " flashing get_unlock_ability\n"
355 " Check whether unlocking is allowed (1) or not(0).\n"
356 "\n"
357 "advanced:\n"
358 " erase PARTITION Erase a flash partition.\n"
359 " format[:FS_TYPE[:SIZE]] PARTITION\n"
360 " Format a flash partition.\n"
361 " set_active SLOT Set the active slot.\n"
362 " oem [COMMAND...] Execute OEM-specific command.\n"
363 "\n"
364 "boot image:\n"
365 " boot KERNEL [RAMDISK [SECOND]]\n"
366 " Download and boot kernel from RAM.\n"
367 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
368 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700369 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700370 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
371 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
372 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
373 " --tags-offset Set tags offset (default: 0x00000100).\n"
374 " --page-size BYTES Set flash page size (default: 2048).\n"
375 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700376 " --os-version MAJOR[.MINOR[.PATCH]]\n"
377 " Set boot image OS version (default: 0.0.0).\n"
378 " --os-patch-level YYYY-MM-DD\n"
379 " Set boot image OS security patch level.\n"
380 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700381 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700382 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 //" continue Continue with autoboot.\n"
384 //"\n"
385 "Android Things:\n"
386 " stage IN_FILE Sends given file to stage for the next command.\n"
387 " 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 -0800388 "\n"
389 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700390 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700391 " -s SERIAL Specify a USB device.\n"
392 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700393 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700394 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
395 " non-current slot (default: current active slot).\n"
396 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
397 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
398 " --skip-reboot Don't reboot device after flashing.\n"
399 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
400 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000401#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700402 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000403#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700404 // TODO: remove --unbuffered?
405 " --unbuffered Don't buffer input or output.\n"
406 " --verbose, -v Verbose output.\n"
407 " --version Display version.\n"
408 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700410 // clang-format off
411 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000413
Elliott Hughesd6365a72017-05-08 18:04:49 -0700414static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700415 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700416 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700417 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700418 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800419
Elliott Hughesfc797672015-04-07 20:12:50 -0700420 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700421 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800422 die("cannot load '%s': too short", kernel.c_str());
423 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700424 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700425 if (!g_cmdline.empty()) {
426 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
427 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800428
Elliott Hughes4089d342017-10-27 14:21:12 -0700429 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800430
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 *sz = ksize;
432 return kdata;
433 }
434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 void* rdata = nullptr;
436 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700437 if (!ramdisk.empty()) {
438 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700439 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 }
441
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 void* sdata = nullptr;
443 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700444 if (!second_stage.empty()) {
445 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700446 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200447 }
448
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700450 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
451 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700452 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700453
Elliott Hughes577e8b42018-04-05 16:12:47 -0700454 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
455 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800456
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800457 return bdata;
458}
459
Elliott Hughes23af1122017-11-10 08:42:17 -0800460static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700461 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700462 ZipEntry zip_entry;
463 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700464 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700465 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466 }
467
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700468 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Elliott Hughes23af1122017-11-10 08:42:17 -0800470 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700471 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700472 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700474 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700475 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 return data;
478}
479
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700480#if defined(_WIN32)
481
482// TODO: move this to somewhere it can be shared.
483
484#include <windows.h>
485
486// Windows' tmpfile(3) requires administrator rights because
487// it creates temporary files in the root directory.
488static FILE* win32_tmpfile() {
489 char temp_path[PATH_MAX];
490 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
491 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700492 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700493 }
494
495 char filename[PATH_MAX];
496 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700497 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700498 }
499
500 return fopen(filename, "w+bTD");
501}
502
503#define tmpfile win32_tmpfile
504
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000505static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700506 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507}
508
Elliott Hughes855cdf82018-04-02 14:24:03 -0700509static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700510 // TODO: reimplement to avoid leaking a FILE*.
511 return fileno(tmpfile());
512}
513
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000514#else
515
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700516static std::string make_temporary_template() {
517 const char* tmpdir = getenv("TMPDIR");
518 if (tmpdir == nullptr) tmpdir = P_tmpdir;
519 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
520}
521
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000522static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700523 std::string result(make_temporary_template());
524 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700525 die("unable to create temporary directory with template %s: %s",
526 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000527 }
528 return result;
529}
530
Elliott Hughes855cdf82018-04-02 14:24:03 -0700531static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700532 std::string path_template(make_temporary_template());
533 int fd = mkstemp(&path_template[0]);
534 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700535 die("failed to create temporary file for %s with template %s: %s\n",
536 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700537 }
538 unlink(path_template.c_str());
539 return fd;
540}
541
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700542#endif
543
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544static std::string create_fbemarker_tmpdir() {
545 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000546 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
547 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
548 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700549 die("unable to create FBE marker file %s locally: %s",
550 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000551 }
552 close(fd);
553 return dir;
554}
555
556static void delete_fbemarker_tmpdir(const std::string& dir) {
557 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
558 if (unlink(marker_file.c_str()) == -1) {
559 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
560 marker_file.c_str(), errno, strerror(errno));
561 return;
562 }
563 if (rmdir(dir.c_str()) == -1) {
564 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
565 dir.c_str(), errno, strerror(errno));
566 return;
567 }
568}
569
Elliott Hughes45964a82017-05-03 22:43:23 -0700570static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700571 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700572
Yusuke Sato07447542015-06-25 14:39:19 -0700573 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700574 ZipEntry zip_entry;
575 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
576 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000577 return -1;
578 }
579
Elliott Hughes23af1122017-11-10 08:42:17 -0800580 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700581 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800582 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700583 int error = ExtractEntryToFile(zip, &zip_entry, fd);
584 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800585 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700586 }
587
Elliott Hughes4089d342017-10-27 14:21:12 -0700588 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800589 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700590 }
591
Elliott Hughes23af1122017-11-10 08:42:17 -0800592 fprintf(stderr, " took %.3fs\n", now() - start);
593
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700594 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700595}
596
Elliott Hughes5620d222018-03-28 08:20:00 -0700597static char* strip(char* s) {
598 while (*s && isspace(*s)) s++;
599
600 int n = strlen(s);
601 while (n-- > 0) {
602 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800603 s[n] = 0;
604 }
605 return s;
606}
607
608#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500609static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800610 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700611 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800612 char *x;
613 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800614
Elliott Hughes5620d222018-03-28 08:20:00 -0700615 // "require product=alpha|beta|gamma"
616 // "require version-bootloader=1234"
617 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
618 // "require partition-exists=vendor"
619
620 char* name = line;
621 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800622 if (!strncmp(name, "reject ", 7)) {
623 name += 7;
624 invert = 1;
625 } else if (!strncmp(name, "require ", 8)) {
626 name += 8;
627 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700628 } else if (!strncmp(name, "require-for-product:", 20)) {
629 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700630 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700631 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700632 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700633 *name = 0;
634 name += 1;
635 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800636 }
637
638 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700639 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800640 *x = 0;
641 val[0] = x + 1;
642
Elliott Hughes5620d222018-03-28 08:20:00 -0700643 name = strip(name);
644
645 // "require partition-exists=x" is a special case, added because of the trouble we had when
646 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
647 // missing out new partitions. A device with new partitions can use "partition-exists" to
David Anderson0debda02018-08-28 13:54:03 -0700648 // override the fields `optional_if_no_image` in the `images` array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700649 if (!strcmp(name, "partition-exists")) {
650 const char* partition_name = val[0];
651 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500652 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700653 (has_slot != "yes" && has_slot != "no")) {
654 die("device doesn't have required partition %s!", partition_name);
655 }
656 bool known_partition = false;
657 for (size_t i = 0; i < arraysize(images); ++i) {
658 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700659 images[i].optional_if_no_image = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700660 known_partition = true;
661 }
662 }
663 if (!known_partition) {
664 die("device requires partition %s which is not known to this version of fastboot",
665 partition_name);
666 }
667 return;
668 }
669
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 for(count = 1; count < MAX_OPTIONS; count++) {
671 x = strchr(val[count - 1],'|');
672 if (x == 0) break;
673 *x = 0;
674 val[count] = x + 1;
675 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800676
Elliott Hughes253c18d2015-03-18 22:47:09 -0700677 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700678 const char* var = name;
679 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680
Elliott Hughes253c18d2015-03-18 22:47:09 -0700681 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700682 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683
Elliott Hughes5620d222018-03-28 08:20:00 -0700684 for (size_t i = 0; i < count; ++i) {
685 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800686 }
687
Elliott Hughes5620d222018-03-28 08:20:00 -0700688 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689}
690
Aaron Wisnerdb511202018-06-26 15:38:35 -0500691static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700692 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700694 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500696 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800697 data = s;
698 } else {
699 s++;
700 }
701 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500702 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800703}
704
Elliott Hughesfc797672015-04-07 20:12:50 -0700705static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700707 fb_queue_display("Bootloader Version...", "version-bootloader");
708 fb_queue_display("Baseband Version.....", "version-baseband");
709 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800710 fb_queue_notice("--------------------------------------------");
711}
712
Tao Bao41cf35f2018-04-24 10:54:21 -0700713static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700714 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700715 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700716
Tao Bao41cf35f2018-04-24 10:54:21 -0700717 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
718 die("invalid max size %" PRId64, max_size);
719 }
720
Elliott Hughesfc797672015-04-07 20:12:50 -0700721 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700722 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700723
Elliott Hughes253c18d2015-03-18 22:47:09 -0700724 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700725 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700726
727 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700728 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700729
730 return out_s;
731}
732
Aaron Wisnerdb511202018-06-26 15:38:35 -0500733static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700734 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500735 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700736 max_download_size.empty()) {
737 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700738 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700739 }
740
Elliott Hughes77c0e662015-11-02 15:51:12 -0800741 // Some bootloaders (angler, for example) send spurious whitespace too.
742 max_download_size = android::base::Trim(max_download_size);
743
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700744 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700745 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800746 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700747 return 0;
748 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700749 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700750 return limit;
751}
752
Aaron Wisnerdb511202018-06-26 15:38:35 -0500753static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700754 int64_t limit = sparse_limit;
755 if (limit == 0) {
756 // Unlimited, so see what the target device's limit is.
757 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700758 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500759 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700760 }
761 if (target_sparse_limit > 0) {
762 limit = target_sparse_limit;
763 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700764 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700765 }
766 }
767
768 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500769 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700770 }
771
772 return 0;
773}
774
Aaron Wisnerdb511202018-06-26 15:38:35 -0500775static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700776 int64_t sz = get_file_size(fd);
777 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700778 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700779 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700780
David Anderson32e376f2018-08-16 13:43:11 -0700781 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
782 buf->image_size = sparse_file_len(s, false, false);
783 sparse_file_destroy(s);
784 } else {
785 buf->image_size = sz;
786 }
787
Elliott Hughesfc797672015-04-07 20:12:50 -0700788 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500789 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700790 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700791 sparse_file** s = load_sparse_files(fd, limit);
792 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700793 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700794 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700795 buf->type = FB_BUFFER_SPARSE;
796 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700797 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500798 buf->type = FB_BUFFER_FD;
799 buf->data = nullptr;
800 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000801 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700802 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700803
Elliott Hughes53ec4952016-05-11 12:39:27 -0700804 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700805}
806
Aaron Wisnerdb511202018-06-26 15:38:35 -0500807static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500808 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
809
Elliott Hughes53ec4952016-05-11 12:39:27 -0700810 if (fd == -1) {
811 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500813
814 struct stat s;
815 if (fstat(fd, &s)) {
816 return false;
817 }
818 if (!S_ISREG(s.st_mode)) {
819 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
820 return false;
821 }
822
Aaron Wisnerdb511202018-06-26 15:38:35 -0500823 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700824}
825
David Zeuthenb6ea4352017-08-07 14:29:26 -0400826static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
827 // Buffer needs to be at least the size of the VBMeta struct which
828 // is 256 bytes.
829 if (buf->sz < 256) {
830 return;
831 }
832
Elliott Hughes855cdf82018-04-02 14:24:03 -0700833 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400834
835 std::string data;
836 if (!android::base::ReadFdToString(buf->fd, &data)) {
837 die("Failed reading from vbmeta");
838 }
839
840 // There's a 32-bit big endian |flags| field at offset 120 where
841 // bit 0 corresponds to disable-verity and bit 1 corresponds to
842 // disable-verification.
843 //
844 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
845 // the VBMeta struct.
846 if (g_disable_verity) {
847 data[123] |= 0x01;
848 }
849 if (g_disable_verification) {
850 data[123] |= 0x02;
851 }
852
853 if (!android::base::WriteStringToFd(data, fd)) {
854 die("Failed writing to modified vbmeta");
855 }
856 close(buf->fd);
857 buf->fd = fd;
858 lseek(fd, 0, SEEK_SET);
859}
860
Elliott Hughes5620d222018-03-28 08:20:00 -0700861static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700862{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700863 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864
David Zeuthenb6ea4352017-08-07 14:29:26 -0400865 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
866 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700867 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400868 rewrite_vbmeta_buffer(buf);
869 }
870
Rom Lemarchand622810c2013-06-28 09:54:59 -0700871 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800872 case FB_BUFFER_SPARSE: {
873 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700874 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700875 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700876 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800877 sparse_files.emplace_back(*s, sz);
878 ++s;
879 }
880
881 for (size_t i = 0; i < sparse_files.size(); ++i) {
882 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700883 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700884 }
885 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800886 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500887 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700888 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700889 break;
890 default:
891 die("unknown buffer type: %d", buf->type);
892 }
893}
894
Aaron Wisnerdb511202018-06-26 15:38:35 -0500895static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700896 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500897 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700898 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700899}
900
Aaron Wisnerdb511202018-06-26 15:38:35 -0500901static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700902 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700903 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500904 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700905 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700906 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700907 return count;
908}
909
Aaron Wisnerdb511202018-06-26 15:38:35 -0500910static bool supports_AB() {
911 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700912}
913
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700914// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700915static std::string get_other_slot(const std::string& current_slot, int count) {
916 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700917
Daniel Rosenberg80919472016-06-30 19:25:31 -0700918 char next = (current_slot[0] - 'a' + 1)%count + 'a';
919 return std::string(1, next);
920}
921
Aaron Wisnerdb511202018-06-26 15:38:35 -0500922static std::string get_other_slot(const std::string& current_slot) {
923 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700924}
925
Aaron Wisnerdb511202018-06-26 15:38:35 -0500926static std::string get_other_slot(int count) {
927 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700928}
929
Aaron Wisnerdb511202018-06-26 15:38:35 -0500930static std::string get_other_slot() {
931 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700932}
933
Aaron Wisnerdb511202018-06-26 15:38:35 -0500934static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700935 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800937 if (allow_all) {
938 return "all";
939 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500940 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700941 if (count > 0) {
942 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800943 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700944 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800945 }
946 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700947 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800948
Aaron Wisnerdb511202018-06-26 15:38:35 -0500949 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700950 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800951
Daniel Rosenberg80919472016-06-30 19:25:31 -0700952 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500953 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700954 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700955 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800956 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700957 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800958 }
959
Daniel Rosenberg80919472016-06-30 19:25:31 -0700960 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
961
962 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
963 for (int i=0; i<count; i++) {
964 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700965 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700966
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967 exit(1);
968}
969
Aaron Wisnerdb511202018-06-26 15:38:35 -0500970static std::string verify_slot(const std::string& slot) {
971 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800972}
973
Aaron Wisnerdb511202018-06-26 15:38:35 -0500974static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700975 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800976 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700977 std::string current_slot;
978
Aaron Wisnerdb511202018-06-26 15:38:35 -0500979 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800980 /* If has-slot is not supported, the answer is no. */
981 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700982 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800983 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700984 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500985 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700987 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700988 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700992 }
993 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700994 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700995 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 }
998 func(part);
999 }
1000}
1001
David Pursell0b156632015-10-30 11:22:01 -07001002/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1003 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1004 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1005 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001006 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001007static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001008 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001009 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001010
Daniel Rosenberg80919472016-06-30 19:25:31 -07001011 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001012 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001013 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001014 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001015 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001016 for (int i=0; i < get_slot_count(); i++) {
1017 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001018 }
1019 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001020 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 }
1022 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001023 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001024 }
1025}
1026
Aaron Wisnerdb511202018-06-26 15:38:35 -05001027static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001028 struct fastboot_buffer buf;
1029
Aaron Wisnerdb511202018-06-26 15:38:35 -05001030 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001031 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001032 }
1033 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001034}
1035
Elliott Hughes45964a82017-05-03 22:43:23 -07001036static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001037 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001038 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001039 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001040 fb_queue_download("signature", data, sz);
1041 fb_queue_command("signature", "installing signature");
1042}
1043
Daniel Rosenberg13454092016-06-27 19:43:11 -07001044// Sets slot_override as the active slot. If slot_override is blank,
1045// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001046static void set_active(const std::string& slot_override) {
1047 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001048
Daniel Rosenberg80919472016-06-30 19:25:31 -07001049 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001050 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001051 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001052 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001053 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001054 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001055 }
1056 }
1057}
1058
David Anderson32e376f2018-08-16 13:43:11 -07001059static bool is_userspace_fastboot() {
1060 std::string value;
1061 return fb_getvar("is-userspace", &value) && value == "yes";
1062}
1063
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001064static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1065 std::string has_slot;
1066 std::string partition_name = partition;
1067
1068 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1069 if (slot == "") {
1070 std::string current_slot = get_current_slot();
1071 if (current_slot == "") {
1072 die("Failed to identify current slot");
1073 }
1074 partition_name += "_" + current_slot;
1075 } else {
1076 partition_name += "_" + slot;
1077 }
1078 }
1079 std::string partition_size;
1080 return fb_getvar("partition-size:" + partition_name, &partition_size);
1081}
1082
Aaron Wisnerdb511202018-06-26 15:38:35 -05001083static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084 queue_info_dump();
1085
Wink Savilleb98762f2011-04-04 17:54:59 -07001086 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1087
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001088 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001089 int error = OpenArchive(filename, &zip);
1090 if (error != 0) {
1091 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001092 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001093
Elliott Hughesfc797672015-04-07 20:12:50 -07001094 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001095 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001096 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001097 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001098 }
1099
Aaron Wisnerdb511202018-06-26 15:38:35 -05001100 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001101
Alex Lightbb9b8a52016-06-29 09:26:44 -07001102 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001103 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001104 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001105 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001106 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001107 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001108 }
1109 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001110 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001111 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1112 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001113 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001114 }
1115 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001116 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001117 const char* slot = slot_override.c_str();
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001118 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001119 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001120 slot = secondary.c_str();
1121 } else {
1122 continue;
1123 }
1124 }
1125
Elliott Hughes253c18d2015-03-18 22:47:09 -07001126 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001127 if (fd == -1) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001128 if (images[i].optional_if_no_image) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001129 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001130 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001131 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001132 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001133
Elliott Hughes253c18d2015-03-18 22:47:09 -07001134 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001135 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001136 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1137 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001138
Elliott Hughes4089d342017-10-27 14:21:12 -07001139 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001140 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001141 flash_buf(partition.c_str(), &buf);
1142 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001143 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001144 * program exits.
1145 */
1146 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001147 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001148 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001149
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001150 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001151 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001152 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001153 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001154 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001155
1156 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157}
1158
Alex Lightbb9b8a52016-06-29 09:26:44 -07001159static void do_send_signature(const std::string& fn) {
1160 std::size_t extension_loc = fn.find(".img");
1161 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001162
Alex Lightbb9b8a52016-06-29 09:26:44 -07001163 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001164
1165 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001166 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001167 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001168
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169 fb_queue_download("signature", data, sz);
1170 fb_queue_command("signature", "installing signature");
1171}
1172
David Anderson32e376f2018-08-16 13:43:11 -07001173static bool is_logical(const std::string& partition) {
1174 std::string value;
1175 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1176}
1177
David Anderson1d887432018-08-27 16:47:32 -07001178static void reboot_to_userspace_fastboot() {
1179 if (!fb_reboot_to_userspace()) {
1180 die("Must reboot to userspace fastboot to flash logical partitions");
1181 }
1182
1183 // Give the current connection time to close.
1184 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1185
1186 fb_reinit(open_device());
1187}
1188
David Anderson32e376f2018-08-16 13:43:11 -07001189static void update_super_partition(bool force_wipe) {
1190 if (!if_partition_exists("super", "")) {
1191 return;
1192 }
1193 std::string image = find_item_given_name("super_empty.img");
1194 if (access(image.c_str(), R_OK) < 0) {
1195 return;
1196 }
1197
1198 if (!is_userspace_fastboot()) {
David Anderson1d887432018-08-27 16:47:32 -07001199 reboot_to_userspace_fastboot();
David Anderson32e376f2018-08-16 13:43:11 -07001200 }
1201
1202 int fd = open(image.c_str(), O_RDONLY);
1203 if (fd < 0) {
1204 die("could not open '%s': %s", image.c_str(), strerror(errno));
1205 }
1206 fb_queue_download_fd("super", fd, get_file_size(fd));
1207
1208 std::string command = "update-super:super";
1209 if (force_wipe) {
1210 command += ":wipe";
1211 }
1212 fb_queue_command(command, "Updating super partition");
1213
1214 // We need these commands to have finished before proceeding, since
1215 // otherwise "getvar is-logical" may not return a correct answer below.
1216 fb_execute_queue();
1217}
1218
David Anderson95d40932018-08-28 12:14:33 -07001219static void flash_images(const std::vector<std::pair<const Image*, std::string>>& images) {
1220 // Flash each partition in the list if it has a corresponding image.
1221 for (const auto& [image, slot] : images) {
1222 auto fname = find_item_given_name(image->img_name);
1223 fastboot_buffer buf;
1224 if (!load_buf(fname.c_str(), &buf)) {
1225 if (image->optional_if_no_image) continue;
1226 die("could not load '%s': %s", image->img_name, strerror(errno));
1227 }
David Anderson95d40932018-08-28 12:14:33 -07001228 auto flashall = [&](const std::string &partition) {
1229 do_send_signature(fname.c_str());
1230 if (is_logical(partition)) {
1231 fb_queue_resize_partition(partition, std::to_string(buf.image_size));
1232 }
1233 flash_buf(partition.c_str(), &buf);
1234 };
1235 do_for_partitions(image->part_name, slot, flashall, false);
1236 }
1237}
1238
David Anderson32e376f2018-08-16 13:43:11 -07001239static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001240 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001241 queue_info_dump();
1242
Wink Savilleb98762f2011-04-04 17:54:59 -07001243 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1244
Elliott Hughes45964a82017-05-03 22:43:23 -07001245 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001246 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247
Elliott Hughesfc797672015-04-07 20:12:50 -07001248 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001249 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001250 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001251
Aaron Wisnerdb511202018-06-26 15:38:35 -05001252 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001253
Alex Lightbb9b8a52016-06-29 09:26:44 -07001254 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001255 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001256 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001257 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001258 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001259 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001260 }
1261 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001262 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001263 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1264 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001265 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001266 }
1267 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001268
David Anderson32e376f2018-08-16 13:43:11 -07001269 // List of partitions to flash and their slots.
David Anderson95d40932018-08-28 12:14:33 -07001270 std::vector<std::pair<const Image*, std::string>> boot_images;
1271 std::vector<std::pair<const Image*, std::string>> os_images;
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001272 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001273 const char* slot = NULL;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001274 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001275 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001276 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001277 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001278 }
1279 if (!slot) continue;
David Anderson0debda02018-08-28 13:54:03 -07001280 if (images[i].type == ImageType::BootCritical) {
David Anderson95d40932018-08-28 12:14:33 -07001281 boot_images.emplace_back(&images[i], slot);
David Anderson0debda02018-08-28 13:54:03 -07001282 } else if (images[i].type == ImageType::Normal) {
David Anderson95d40932018-08-28 12:14:33 -07001283 os_images.emplace_back(&images[i], slot);
1284 }
1285 }
David Anderson32e376f2018-08-16 13:43:11 -07001286
David Anderson95d40932018-08-28 12:14:33 -07001287 // First flash boot partitions. We allow this to happen either in userspace
1288 // or in bootloader fastboot.
1289 flash_images(boot_images);
1290
1291 // Sync the super partition. This will reboot to userspace fastboot if needed.
1292 update_super_partition(wipe);
1293
1294 // Resize any logical partition to 0, so each partition is reset to 0
1295 // extents, and will achieve more optimal allocation.
1296 for (const auto& [image, slot] : os_images) {
David Anderson32e376f2018-08-16 13:43:11 -07001297 auto resize_partition = [](const std::string& partition) -> void {
1298 if (is_logical(partition)) {
1299 fb_queue_resize_partition(partition, "0");
1300 }
1301 };
David Anderson95d40932018-08-28 12:14:33 -07001302 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001303 }
1304
David Anderson95d40932018-08-28 12:14:33 -07001305 // Flash OS images, resizing logical partitions as needed.
1306 flash_images(os_images);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001307
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001308 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001309 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001310 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001311 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001312 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001313}
1314
Elliott Hughesd6365a72017-05-08 18:04:49 -07001315static std::string next_arg(std::vector<std::string>* args) {
1316 if (args->empty()) syntax_error("expected argument");
1317 std::string result = args->front();
1318 args->erase(args->begin());
1319 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001320}
1321
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001322static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001323 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001324
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001325 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001326 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001327 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001328 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001329 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001330}
1331
Connor O'Brience16a8a2017-02-06 14:39:31 -08001332static std::string fb_fix_numeric_var(std::string var) {
1333 // Some bootloaders (angler, for example), send spurious leading whitespace.
1334 var = android::base::Trim(var);
1335 // Some bootloaders (hammerhead, for example) use implicit hex.
1336 // This code used to use strtol with base 16.
1337 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1338 return var;
1339}
1340
Aaron Wisnerdb511202018-06-26 15:38:35 -05001341static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001342 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001343 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001344 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001345 return 0;
1346 }
1347 sizeString = fb_fix_numeric_var(sizeString);
1348
1349 unsigned size;
1350 if (!android::base::ParseUint(sizeString, &size)) {
1351 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1352 return 0;
1353 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001354 if ((size & (size - 1)) != 0) {
1355 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001356 return 0;
1357 }
1358 return size;
1359}
1360
Aaron Wisnerdb511202018-06-26 15:38:35 -05001361static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001362 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001363 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001364 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001365 std::string partition_type, partition_size;
1366
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001367 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001368 const char* errMsg = nullptr;
1369 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001370 TemporaryFile output;
1371 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001372
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001373 unsigned int limit = INT_MAX;
1374 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001375 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001376 }
1377 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001378 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001379 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001380
Aaron Wisnerdb511202018-06-26 15:38:35 -05001381 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001382 errMsg = "Can't determine partition type.\n";
1383 goto failed;
1384 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001385 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001386 if (partition_type != type_override) {
1387 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001388 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001389 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001390 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001391 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392
Aaron Wisnerdb511202018-06-26 15:38:35 -05001393 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001394 errMsg = "Unable to get partition size\n";
1395 goto failed;
1396 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001397 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 if (partition_size != size_override) {
1399 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001400 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001401 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001402 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001403 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001404 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001405
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001406 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001407 if (!gen) {
1408 if (skip_if_not_supported) {
1409 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001410 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411 return;
1412 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001413 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1414 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001415 return;
1416 }
1417
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001418 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001419 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001420 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1421 return;
1422 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001423
Connor O'Brience16a8a2017-02-06 14:39:31 -08001424 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001425 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1426 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001427
Jin Qian4a335822017-04-18 16:23:18 -07001428 if (fs_generator_generate(gen, output.path, size, initial_dir,
1429 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001430 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001431 return;
1432 }
1433
Jin Qian4a335822017-04-18 16:23:18 -07001434 fd.reset(open(output.path, O_RDONLY));
1435 if (fd == -1) {
1436 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1437 return;
1438 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001439 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001440 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001441 return;
1442 }
1443 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001444 return;
1445
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446failed:
1447 if (skip_if_not_supported) {
1448 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001449 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001450 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001451 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001452}
1453
Aaron Wisnerdb511202018-06-26 15:38:35 -05001454int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001455 bool wants_wipe = false;
1456 bool wants_reboot = false;
1457 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001458 bool wants_reboot_recovery = false;
1459 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001460 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001461 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001462 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001463 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001464 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001465 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001466 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001467 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001468 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001469
Elliott Hughes577e8b42018-04-05 16:12:47 -07001470 g_boot_img_hdr.kernel_addr = 0x00008000;
1471 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1472 g_boot_img_hdr.second_addr = 0x00f00000;
1473 g_boot_img_hdr.tags_addr = 0x00000100;
1474 g_boot_img_hdr.page_size = 2048;
1475
JP Abgrall7b8970c2013-03-07 17:06:41 -08001476 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001477 {"base", required_argument, 0, 0},
1478 {"cmdline", required_argument, 0, 0},
1479 {"disable-verification", no_argument, 0, 0},
1480 {"disable-verity", no_argument, 0, 0},
1481 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001482 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001483 {"kernel-offset", required_argument, 0, 0},
1484 {"os-patch-level", required_argument, 0, 0},
1485 {"os-version", required_argument, 0, 0},
1486 {"page-size", required_argument, 0, 0},
1487 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001488 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001489 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001490 {"skip-secondary", no_argument, 0, 0},
1491 {"slot", required_argument, 0, 0},
1492 {"tags-offset", required_argument, 0, 0},
1493 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001494 {"verbose", no_argument, 0, 'v'},
1495 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001496#if !defined(_WIN32)
1497 {"wipe-and-use-fbe", no_argument, 0, 0},
1498#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001499 {0, 0, 0, 0}
1500 };
Colin Cross8879f982012-05-22 17:53:34 -07001501
1502 serial = getenv("ANDROID_SERIAL");
1503
Elliott Hughes577e8b42018-04-05 16:12:47 -07001504 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001505 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001506 if (c == 0) {
1507 std::string name{longopts[longindex].name};
1508 if (name == "base") {
1509 g_base_addr = strtoul(optarg, 0, 16);
1510 } else if (name == "cmdline") {
1511 g_cmdline = optarg;
1512 } else if (name == "disable-verification") {
1513 g_disable_verification = true;
1514 } else if (name == "disable-verity") {
1515 g_disable_verity = true;
1516 } else if (name == "header-version") {
1517 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1518 } else if (name == "kernel-offset") {
1519 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1520 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001521 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001522 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001523 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001524 } else if (name == "page-size") {
1525 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1526 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1527 } else if (name == "ramdisk-offset") {
1528 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1529 } else if (name == "skip-reboot") {
1530 skip_reboot = true;
1531 } else if (name == "skip-secondary") {
1532 skip_secondary = true;
1533 } else if (name == "slot") {
1534 slot_override = optarg;
1535 } else if (name == "tags-offset") {
1536 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1537 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001538 setvbuf(stdout, nullptr, _IONBF, 0);
1539 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001540 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001541 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001542 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001543 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001544#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001545 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001546 wants_wipe = true;
1547 set_fbe_marker = true;
1548#endif
1549 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001550 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001551 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001552 } else {
1553 switch (c) {
1554 case 'a':
1555 wants_set_active = true;
1556 if (optarg) next_active = optarg;
1557 break;
1558 case 'h':
1559 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001560 case 'l':
1561 g_long_listing = true;
1562 break;
1563 case 's':
1564 serial = optarg;
1565 break;
1566 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001567 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1568 die("invalid sparse limit %s", optarg);
1569 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001570 break;
1571 case 'v':
1572 set_verbose();
1573 break;
1574 case 'w':
1575 wants_wipe = true;
1576 break;
1577 case '?':
1578 return 1;
1579 default:
1580 abort();
1581 }
Colin Cross8879f982012-05-22 17:53:34 -07001582 }
1583 }
1584
1585 argc -= optind;
1586 argv += optind;
1587
Elliott Hughesd6365a72017-05-08 18:04:49 -07001588 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001589
Colin Cross8fb6e062012-07-24 16:36:41 -07001590 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001591 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001592 return 0;
1593 }
1594
Colin Crossc7b75dc2012-08-29 18:17:06 -07001595 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001596 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001597 }
1598
David Pursell0b156632015-10-30 11:22:01 -07001599 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001600 if (transport == nullptr) {
1601 return 1;
1602 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001603 fastboot::FastBootDriver fb(transport);
1604 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001605
Elliott Hughes5620d222018-03-28 08:20:00 -07001606 const double start = now();
1607
Aaron Wisnerdb511202018-06-26 15:38:35 -05001608 if (slot_override != "") slot_override = verify_slot(slot_override);
1609 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001610
1611 if (wants_set_active) {
1612 if (next_active == "") {
1613 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001614 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001615 if (fb_getvar("current-slot", &current_slot)) {
1616 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001617 } else {
1618 wants_set_active = false;
1619 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001620 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001621 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001622 }
1623 }
1624 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001625
Elliott Hughesd6365a72017-05-08 18:04:49 -07001626 std::vector<std::string> args(argv, argv + argc);
1627 while (!args.empty()) {
1628 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001629
Elliott Hughesd6365a72017-05-08 18:04:49 -07001630 if (command == "getvar") {
1631 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001632 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001633 } else if (command == "erase") {
1634 std::string partition = next_arg(&args);
1635 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001636 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001637 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001638 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001639 fs_get_generator(partition_type) != nullptr) {
1640 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1641 partition_type.c_str());
1642 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001643
Elliott Hughes5620d222018-03-28 08:20:00 -07001644 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001645 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001646 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001647 } else if (android::base::StartsWith(command, "format")) {
1648 // Parsing for: "format[:[type][:[size]]]"
1649 // Some valid things:
1650 // - select only the size, and leave default fs type:
1651 // format::0x4000000 userdata
1652 // - default fs type and size:
1653 // format userdata
1654 // format:: userdata
1655 std::vector<std::string> pieces = android::base::Split(command, ":");
1656 std::string type_override;
1657 if (pieces.size() > 1) type_override = pieces[1].c_str();
1658 std::string size_override;
1659 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001660
Elliott Hughesd6365a72017-05-08 18:04:49 -07001661 std::string partition = next_arg(&args);
1662
1663 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001664 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001665 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001666 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001667 } else if (command == "signature") {
1668 std::string filename = next_arg(&args);
1669 data = load_file(filename.c_str(), &sz);
1670 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001671 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001672 fb_queue_download("signature", data, sz);
1673 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001674 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001675 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001676
1677 if (args.size() == 1) {
1678 std::string what = next_arg(&args);
1679 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680 wants_reboot = false;
1681 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001682 } else if (what == "recovery") {
1683 wants_reboot = false;
1684 wants_reboot_recovery = true;
1685 } else if (what == "fastboot") {
1686 wants_reboot = false;
1687 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001688 } else {
1689 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001690 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001691
Elliott Hughesca85df02015-02-25 10:02:00 -08001692 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001693 if (!args.empty()) syntax_error("junk after reboot command");
1694 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001695 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001696 } else if (command == "reboot-recovery") {
1697 wants_reboot_recovery = true;
1698 } else if (command == "reboot-fastboot") {
1699 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001700 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001701 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001702 } else if (command == "boot") {
1703 std::string kernel = next_arg(&args);
1704 std::string ramdisk;
1705 if (!args.empty()) ramdisk = next_arg(&args);
1706 std::string second_stage;
1707 if (!args.empty()) second_stage = next_arg(&args);
1708
Elliott Hughes577e8b42018-04-05 16:12:47 -07001709 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001710 fb_queue_download("boot.img", data, sz);
1711 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001712 } else if (command == "flash") {
1713 std::string pname = next_arg(&args);
1714
Elliott Hughes2810d002016-04-25 14:31:18 -07001715 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001716 if (!args.empty()) {
1717 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001718 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001719 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001720 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001721 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001722
1723 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001724 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001725 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001726 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001727 } else if (command == "flash:raw") {
1728 std::string partition = next_arg(&args);
1729 std::string kernel = next_arg(&args);
1730 std::string ramdisk;
1731 if (!args.empty()) ramdisk = next_arg(&args);
1732 std::string second_stage;
1733 if (!args.empty()) second_stage = next_arg(&args);
1734
Elliott Hughes577e8b42018-04-05 16:12:47 -07001735 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001736 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001737 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001738 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001739 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001740 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001741 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001742 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001743 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001744 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001745 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001746 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001747 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001748 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001749 bool slot_all = (slot_override == "all");
1750 if (slot_all) {
1751 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1752 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001753 std::string filename = "update.zip";
1754 if (!args.empty()) {
1755 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001756 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001757 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001758 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001759 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001760 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001761 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001762 } else if (command == "stage") {
1763 std::string filename = next_arg(&args);
1764
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001765 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001766 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001767 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001768 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001769 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001770 } else if (command == "get_staged") {
1771 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001772 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001773 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001774 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001775 } else if (command == "flashing") {
1776 if (args.empty()) {
1777 syntax_error("missing 'flashing' command");
1778 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1779 args[0] == "unlock_critical" ||
1780 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001781 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001782 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001783 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001784 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001785 }
David Anderson0d4277d2018-07-31 13:27:37 -07001786 } else if (command == "create-logical-partition") {
1787 std::string partition = next_arg(&args);
1788 std::string size = next_arg(&args);
1789 fb_queue_create_partition(partition, size);
1790 } else if (command == "delete-logical-partition") {
1791 std::string partition = next_arg(&args);
1792 fb_queue_delete_partition(partition);
1793 } else if (command == "resize-logical-partition") {
1794 std::string partition = next_arg(&args);
1795 std::string size = next_arg(&args);
1796 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001797 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001798 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001799 }
1800 }
1801
1802 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001803 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1804 for (const auto& partition : partitions) {
1805 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001806 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001807 if (partition_type.empty()) continue;
1808 fb_queue_erase(partition);
1809 if (partition == "userdata" && set_fbe_marker) {
1810 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1811 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001812 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001813 delete_fbemarker_tmpdir(initial_userdata_dir);
1814 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001815 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001816 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001817 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001818 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001819 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001820 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001821 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001822 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001823 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001824 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001825 } else if (wants_reboot_bootloader) {
1826 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001827 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001828 } else if (wants_reboot_recovery) {
1829 fb_queue_command("reboot-recovery", "rebooting into recovery");
1830 fb_queue_wait_for_disconnect();
1831 } else if (wants_reboot_fastboot) {
1832 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1833 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001834 }
1835
Aaron Wisnerdb511202018-06-26 15:38:35 -05001836 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001837 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1838 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001839}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001840
Aaron Wisnerdb511202018-06-26 15:38:35 -05001841void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001842 unsigned year, month, day;
1843 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1844 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1845 }
1846 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1847 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1848 hdr->SetOsPatchLevel(year, month);
1849}
1850
Aaron Wisnerdb511202018-06-26 15:38:35 -05001851void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001852 unsigned major = 0, minor = 0, patch = 0;
1853 std::vector<std::string> versions = android::base::Split(arg, ".");
1854 if (versions.size() < 1 || versions.size() > 3 ||
1855 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1856 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1857 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1858 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1859 syntax_error("bad OS version: %s", arg);
1860 }
1861 hdr->SetOsVersion(major, minor, patch);
1862}