blob: 773c44b4d549ed4ffe0e550c95ee20b8f86f4223 [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 Anderson32e376f2018-08-16 13:43:11 -0700110struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700111 const char* nickname;
112 const char* img_name;
113 const char* sig_name;
114 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700115 bool optional_if_no_image;
116 bool optional_if_no_partition;
Mark Salyzyn8fb0fb82018-08-24 14:37:11 -0700117 bool flashall;
David Anderson95d40932018-08-28 12:14:33 -0700118 bool needed_for_fastbootd;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700119 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700120};
121
122static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100123 // clang-format off
David Anderson95d40932018-08-28 12:14:33 -0700124 { "boot", "boot.img", "boot.sig", "boot", false, false, true, true, },
125 { nullptr, "boot_other.img", "boot.sig", "boot", true, false, true, false, },
126 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false, true, true, },
127 { "dts", "dt.img", "dt.sig", "dts", true, false, true, true, },
128 { "odm", "odm.img", "odm.sig", "odm", true, false, true, false, },
129 { "product", "product.img", "product.sig", "product", true, false, true, false, },
Dario Freniab5583b2018-08-17 01:01:25 +0100130 { "product_services",
131 "product_services.img",
132 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700133 "product_services",
David Anderson95d40932018-08-28 12:14:33 -0700134 true, true, true, false, },
135 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false, true, true, },
136 { "system", "system.img", "system.sig", "system", false, true, true, false, },
137 { nullptr, "system_other.img", "system.sig", "system", true, false, true, false, },
138 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false, true, true, },
139 { "vendor", "vendor.img", "vendor.sig", "vendor", true, true, true, false, },
140 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, false, true, false, },
141 { "super", "super.img", "super.sig", "super", true, true, false, false, },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100142 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700143};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700144
Elliott Hughes45964a82017-05-03 22:43:23 -0700145static std::string find_item_given_name(const char* img_name) {
146 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700147 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700148 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700149 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700150 return android::base::StringPrintf("%s/%s", dir, img_name);
151}
152
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700153static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700154 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700155 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700156 return find_item_given_name(images[i].img_name);
157 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 }
159
Elliott Hughesd6365a72017-05-08 18:04:49 -0700160 if (item == "userdata") return find_item_given_name("userdata.img");
161 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700162
Elliott Hughesd6365a72017-05-08 18:04:49 -0700163 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700164 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165}
166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167static int64_t get_file_size(int fd) {
168 struct stat sb;
169 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700170}
171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800173 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700174 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176 *sz = get_file_size(fd);
177 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700178 goto oops;
179 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181 data = (char*) malloc(*sz);
182 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughesfc797672015-04-07 20:12:50 -0700184 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 close(fd);
186
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 return data;
188
189oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800190 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191 close(fd);
192 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800193 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194 return 0;
195}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Elliott Hughes2810d002016-04-25 14:31:18 -0700197static void* load_file(const std::string& path, int64_t* sz) {
198 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700199 if (fd == -1) return nullptr;
200 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700201}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800202
Elliott Hughesfc797672015-04-07 20:12:50 -0700203static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700204 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
205 return -1;
206 }
207
Scott Anderson13081c62012-04-06 12:39:30 -0700208 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700210 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
211 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 return 0;
213}
214
Elliott Hughesfc797672015-04-07 20:12:50 -0700215static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800216 return match_fastboot_with_serial(info, serial);
217}
218
Elliott Hughesfc797672015-04-07 20:12:50 -0700219static int list_devices_callback(usb_ifc_info* info) {
220 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700222 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700224 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225 if (!serial[0]) {
226 serial = "????????????";
227 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700228 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700229 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800230 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700231 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800232 printf("%-22s fastboot", serial.c_str());
233 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700234 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800235 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 }
237
238 return -1;
239}
240
David Pursell2ec418a2016-01-20 08:32:08 -0800241// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
242// a specific device, otherwise the first USB device found will be used.
243//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700244// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800245// Otherwise it blocks until the target is available.
246//
247// The returned Transport is a singleton, so multiple calls to this function will return the same
248// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700249static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800250 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251
David Pursell4601c972016-02-05 15:35:09 -0800252 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800253 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800254 int port = 0;
255 if (serial != nullptr) {
256 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800257
David Pursell4601c972016-02-05 15:35:09 -0800258 if (android::base::StartsWith(serial, "tcp:")) {
259 protocol = Socket::Protocol::kTcp;
260 port = tcp::kDefaultPort;
261 net_address = serial + strlen("tcp:");
262 } else if (android::base::StartsWith(serial, "udp:")) {
263 protocol = Socket::Protocol::kUdp;
264 port = udp::kDefaultPort;
265 net_address = serial + strlen("udp:");
266 }
267
268 if (net_address != nullptr) {
269 std::string error;
270 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700271 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800272 }
David Pursell2ec418a2016-01-20 08:32:08 -0800273 }
274 }
275
David Anderson1d887432018-08-27 16:47:32 -0700276 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800277 while (true) {
278 if (!host.empty()) {
279 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800280 if (protocol == Socket::Protocol::kTcp) {
281 transport = tcp::Connect(host, port, &error).release();
282 } else if (protocol == Socket::Protocol::kUdp) {
283 transport = udp::Connect(host, port, &error).release();
284 }
285
David Pursell2ec418a2016-01-20 08:32:08 -0800286 if (transport == nullptr && announce) {
287 fprintf(stderr, "error: %s\n", error.c_str());
288 }
289 } else {
290 transport = usb_open(match_fastboot);
291 }
292
293 if (transport != nullptr) {
294 return transport;
295 }
296
David Pursell0b156632015-10-30 11:22:01 -0700297 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800298 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700299 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800301 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 }
303}
304
Elliott Hughesfc797672015-04-07 20:12:50 -0700305static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 // We don't actually open a USB device here,
307 // just getting our callback called so we can
308 // list all the connected devices.
309 usb_open(list_devices_callback);
310}
311
Elliott Hughesd6365a72017-05-08 18:04:49 -0700312static void syntax_error(const char* fmt, ...) {
313 fprintf(stderr, "fastboot: usage: ");
314
315 va_list ap;
316 va_start(ap, fmt);
317 vfprintf(stderr, fmt, ap);
318 va_end(ap);
319
320 fprintf(stderr, "\n");
321 exit(1);
322}
323
324static int show_help() {
325 // clang-format off
326 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700327// 1 2 3 4 5 6 7 8
328// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700329 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700331 "flashing:\n"
332 " update ZIP Flash all partitions from an update.zip package.\n"
333 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
334 " On A/B devices, flashed slot is set as active.\n"
335 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700336 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
337 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700338 "\n"
339 "basics:\n"
340 " devices [-l] List devices in bootloader (-l: with device paths).\n"
341 " getvar NAME Display given bootloader variable.\n"
342 " reboot [bootloader] Reboot device.\n"
343 "\n"
344 "locking/unlocking:\n"
345 " flashing lock|unlock Lock/unlock partitions for flashing\n"
346 " flashing lock_critical|unlock_critical\n"
347 " Lock/unlock 'critical' bootloader partitions.\n"
348 " flashing get_unlock_ability\n"
349 " Check whether unlocking is allowed (1) or not(0).\n"
350 "\n"
351 "advanced:\n"
352 " erase PARTITION Erase a flash partition.\n"
353 " format[:FS_TYPE[:SIZE]] PARTITION\n"
354 " Format a flash partition.\n"
355 " set_active SLOT Set the active slot.\n"
356 " oem [COMMAND...] Execute OEM-specific command.\n"
357 "\n"
358 "boot image:\n"
359 " boot KERNEL [RAMDISK [SECOND]]\n"
360 " Download and boot kernel from RAM.\n"
361 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
362 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700363 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700364 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
365 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
366 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
367 " --tags-offset Set tags offset (default: 0x00000100).\n"
368 " --page-size BYTES Set flash page size (default: 2048).\n"
369 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700370 " --os-version MAJOR[.MINOR[.PATCH]]\n"
371 " Set boot image OS version (default: 0.0.0).\n"
372 " --os-patch-level YYYY-MM-DD\n"
373 " Set boot image OS security patch level.\n"
374 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700375 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700376 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700377 //" continue Continue with autoboot.\n"
378 //"\n"
379 "Android Things:\n"
380 " stage IN_FILE Sends given file to stage for the next command.\n"
381 " 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 -0800382 "\n"
383 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700384 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700385 " -s SERIAL Specify a USB device.\n"
386 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700387 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700388 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
389 " non-current slot (default: current active slot).\n"
390 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
391 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
392 " --skip-reboot Don't reboot device after flashing.\n"
393 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
394 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000395#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700396 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000397#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700398 // TODO: remove --unbuffered?
399 " --unbuffered Don't buffer input or output.\n"
400 " --verbose, -v Verbose output.\n"
401 " --version Display version.\n"
402 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700404 // clang-format off
405 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000407
Elliott Hughesd6365a72017-05-08 18:04:49 -0700408static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
Elliott Hughes577e8b42018-04-05 16:12:47 -0700409 const std::string& second_stage, int64_t* sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700410 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700411 void* kdata = load_file(kernel.c_str(), &ksize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700412 if (kdata == nullptr) die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800413
Elliott Hughesfc797672015-04-07 20:12:50 -0700414 // Is this actually a boot image?
Hridya Valsaraju7d824132018-03-30 16:15:33 -0700415 if (ksize < static_cast<int64_t>(sizeof(boot_img_hdr_v1))) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800416 die("cannot load '%s': too short", kernel.c_str());
417 }
Elliott Hughesd6365a72017-05-08 18:04:49 -0700418 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700419 if (!g_cmdline.empty()) {
420 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kdata), g_cmdline);
421 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800422
Elliott Hughes4089d342017-10-27 14:21:12 -0700423 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800424
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 *sz = ksize;
426 return kdata;
427 }
428
Elliott Hughesfc797672015-04-07 20:12:50 -0700429 void* rdata = nullptr;
430 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700431 if (!ramdisk.empty()) {
432 rdata = load_file(ramdisk.c_str(), &rsize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700433 if (rdata == nullptr) die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 }
435
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 void* sdata = nullptr;
437 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700438 if (!second_stage.empty()) {
439 sdata = load_file(second_stage.c_str(), &ssize);
Elliott Hughes4089d342017-10-27 14:21:12 -0700440 if (sdata == nullptr) die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200441 }
442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 fprintf(stderr,"creating boot image...\n");
Elliott Hughes577e8b42018-04-05 16:12:47 -0700444 boot_img_hdr_v1* bdata = mkbootimg(kdata, ksize, rdata, rsize, sdata, ssize,
445 g_base_addr, g_boot_img_hdr, sz);
Elliott Hughes4089d342017-10-27 14:21:12 -0700446 if (bdata == nullptr) die("failed to create boot.img");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700447
Elliott Hughes577e8b42018-04-05 16:12:47 -0700448 if (!g_cmdline.empty()) bootimg_set_cmdline(bdata, g_cmdline);
449 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", *sz);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800450
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 return bdata;
452}
453
Elliott Hughes23af1122017-11-10 08:42:17 -0800454static void* unzip_to_memory(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700455 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700456 ZipEntry zip_entry;
457 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700458 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Elliott Hughes4089d342017-10-27 14:21:12 -0700459 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 }
461
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700462 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463
Elliott Hughes23af1122017-11-10 08:42:17 -0800464 fprintf(stderr, "extracting %s (%" PRId64 " MB) to RAM...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700465 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughes4089d342017-10-27 14:21:12 -0700466 if (data == nullptr) die("failed to allocate %" PRId64 " bytes for '%s'", *sz, entry_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700468 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
Elliott Hughes4089d342017-10-27 14:21:12 -0700469 if (error != 0) die("failed to extract '%s': %s", entry_name, ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000470
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471 return data;
472}
473
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700474#if defined(_WIN32)
475
476// TODO: move this to somewhere it can be shared.
477
478#include <windows.h>
479
480// Windows' tmpfile(3) requires administrator rights because
481// it creates temporary files in the root directory.
482static FILE* win32_tmpfile() {
483 char temp_path[PATH_MAX];
484 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
485 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700486 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700487 }
488
489 char filename[PATH_MAX];
490 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700491 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700492 }
493
494 return fopen(filename, "w+bTD");
495}
496
497#define tmpfile win32_tmpfile
498
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000499static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700500 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000501}
502
Elliott Hughes855cdf82018-04-02 14:24:03 -0700503static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700504 // TODO: reimplement to avoid leaking a FILE*.
505 return fileno(tmpfile());
506}
507
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000508#else
509
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700510static std::string make_temporary_template() {
511 const char* tmpdir = getenv("TMPDIR");
512 if (tmpdir == nullptr) tmpdir = P_tmpdir;
513 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
514}
515
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000516static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700517 std::string result(make_temporary_template());
518 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700519 die("unable to create temporary directory with template %s: %s",
520 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000521 }
522 return result;
523}
524
Elliott Hughes855cdf82018-04-02 14:24:03 -0700525static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700526 std::string path_template(make_temporary_template());
527 int fd = mkstemp(&path_template[0]);
528 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700529 die("failed to create temporary file for %s with template %s: %s\n",
530 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700531 }
532 unlink(path_template.c_str());
533 return fd;
534}
535
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700536#endif
537
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000538static std::string create_fbemarker_tmpdir() {
539 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000540 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
541 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
542 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700543 die("unable to create FBE marker file %s locally: %s",
544 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000545 }
546 close(fd);
547 return dir;
548}
549
550static void delete_fbemarker_tmpdir(const std::string& dir) {
551 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
552 if (unlink(marker_file.c_str()) == -1) {
553 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
554 marker_file.c_str(), errno, strerror(errno));
555 return;
556 }
557 if (rmdir(dir.c_str()) == -1) {
558 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
559 dir.c_str(), errno, strerror(errno));
560 return;
561 }
562}
563
Elliott Hughes45964a82017-05-03 22:43:23 -0700564static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700565 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700566
Yusuke Sato07447542015-06-25 14:39:19 -0700567 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700568 ZipEntry zip_entry;
569 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
570 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000571 return -1;
572 }
573
Elliott Hughes23af1122017-11-10 08:42:17 -0800574 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700575 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800576 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700577 int error = ExtractEntryToFile(zip, &zip_entry, fd);
578 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800579 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700580 }
581
Elliott Hughes4089d342017-10-27 14:21:12 -0700582 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800583 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700584 }
585
Elliott Hughes23af1122017-11-10 08:42:17 -0800586 fprintf(stderr, " took %.3fs\n", now() - start);
587
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700588 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700589}
590
Elliott Hughes5620d222018-03-28 08:20:00 -0700591static char* strip(char* s) {
592 while (*s && isspace(*s)) s++;
593
594 int n = strlen(s);
595 while (n-- > 0) {
596 if (!isspace(s[n])) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 s[n] = 0;
598 }
599 return s;
600}
601
602#define MAX_OPTIONS 32
Aaron Wisnerdb511202018-06-26 15:38:35 -0500603static void check_requirement(char* line) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800604 char *val[MAX_OPTIONS];
Elliott Hughes5620d222018-03-28 08:20:00 -0700605 unsigned count;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 char *x;
607 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800608
Elliott Hughes5620d222018-03-28 08:20:00 -0700609 // "require product=alpha|beta|gamma"
610 // "require version-bootloader=1234"
611 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
612 // "require partition-exists=vendor"
613
614 char* name = line;
615 const char* product = "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 if (!strncmp(name, "reject ", 7)) {
617 name += 7;
618 invert = 1;
619 } else if (!strncmp(name, "require ", 8)) {
620 name += 8;
621 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700622 } else if (!strncmp(name, "require-for-product:", 20)) {
623 // Get the product and point name past it
Elliott Hughes5620d222018-03-28 08:20:00 -0700624 product = name + 20;
Wink Savilleb98762f2011-04-04 17:54:59 -0700625 name = strchr(name, ' ');
Elliott Hughes5620d222018-03-28 08:20:00 -0700626 if (!name) die("android-info.txt syntax error: %s", line);
Wink Savilleb98762f2011-04-04 17:54:59 -0700627 *name = 0;
628 name += 1;
629 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630 }
631
632 x = strchr(name, '=');
Elliott Hughes5620d222018-03-28 08:20:00 -0700633 if (x == 0) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 *x = 0;
635 val[0] = x + 1;
636
Elliott Hughes5620d222018-03-28 08:20:00 -0700637 name = strip(name);
638
639 // "require partition-exists=x" is a special case, added because of the trouble we had when
640 // Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
641 // missing out new partitions. A device with new partitions can use "partition-exists" to
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700642 // override the fields `optional_if_no_image` and 'optional_if_no_partition' in the `images`
643 // array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700644 if (!strcmp(name, "partition-exists")) {
645 const char* partition_name = val[0];
646 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500647 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700648 (has_slot != "yes" && has_slot != "no")) {
649 die("device doesn't have required partition %s!", partition_name);
650 }
651 bool known_partition = false;
652 for (size_t i = 0; i < arraysize(images); ++i) {
653 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700654 images[i].optional_if_no_image = false;
655 images[i].optional_if_no_partition = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700656 known_partition = true;
657 }
658 }
659 if (!known_partition) {
660 die("device requires partition %s which is not known to this version of fastboot",
661 partition_name);
662 }
663 return;
664 }
665
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 for(count = 1; count < MAX_OPTIONS; count++) {
667 x = strchr(val[count - 1],'|');
668 if (x == 0) break;
669 *x = 0;
670 val[count] = x + 1;
671 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800672
Elliott Hughes253c18d2015-03-18 22:47:09 -0700673 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700674 const char* var = name;
675 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676
Elliott Hughes253c18d2015-03-18 22:47:09 -0700677 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700678 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679
Elliott Hughes5620d222018-03-28 08:20:00 -0700680 for (size_t i = 0; i < count; ++i) {
681 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 }
683
Elliott Hughes5620d222018-03-28 08:20:00 -0700684 fb_queue_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685}
686
Aaron Wisnerdb511202018-06-26 15:38:35 -0500687static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700688 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700690 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500692 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 data = s;
694 } else {
695 s++;
696 }
697 }
Aaron Wisnerdb511202018-06-26 15:38:35 -0500698 if (fb_execute_queue()) die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699}
700
Elliott Hughesfc797672015-04-07 20:12:50 -0700701static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 fb_queue_notice("--------------------------------------------");
Elliott Hughes5620d222018-03-28 08:20:00 -0700703 fb_queue_display("Bootloader Version...", "version-bootloader");
704 fb_queue_display("Baseband Version.....", "version-baseband");
705 fb_queue_display("Serial Number........", "serialno");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800706 fb_queue_notice("--------------------------------------------");
707}
708
Tao Bao41cf35f2018-04-24 10:54:21 -0700709static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700710 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700711 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700712
Tao Bao41cf35f2018-04-24 10:54:21 -0700713 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
714 die("invalid max size %" PRId64, max_size);
715 }
716
Elliott Hughesfc797672015-04-07 20:12:50 -0700717 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700718 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700719
Elliott Hughes253c18d2015-03-18 22:47:09 -0700720 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700721 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700722
723 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700724 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700725
726 return out_s;
727}
728
Aaron Wisnerdb511202018-06-26 15:38:35 -0500729static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700730 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500731 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700732 max_download_size.empty()) {
733 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700734 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700735 }
736
Elliott Hughes77c0e662015-11-02 15:51:12 -0800737 // Some bootloaders (angler, for example) send spurious whitespace too.
738 max_download_size = android::base::Trim(max_download_size);
739
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700741 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800742 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700743 return 0;
744 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700745 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700746 return limit;
747}
748
Aaron Wisnerdb511202018-06-26 15:38:35 -0500749static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700750 int64_t limit = sparse_limit;
751 if (limit == 0) {
752 // Unlimited, so see what the target device's limit is.
753 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700754 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500755 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700756 }
757 if (target_sparse_limit > 0) {
758 limit = target_sparse_limit;
759 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700760 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762 }
763
764 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500765 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700766 }
767
768 return 0;
769}
770
Aaron Wisnerdb511202018-06-26 15:38:35 -0500771static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700772 int64_t sz = get_file_size(fd);
773 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700774 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700775 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700776
David Anderson32e376f2018-08-16 13:43:11 -0700777 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
778 buf->image_size = sparse_file_len(s, false, false);
779 sparse_file_destroy(s);
780 } else {
781 buf->image_size = sz;
782 }
783
Elliott Hughesfc797672015-04-07 20:12:50 -0700784 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500785 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700786 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700787 sparse_file** s = load_sparse_files(fd, limit);
788 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700789 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700790 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700791 buf->type = FB_BUFFER_SPARSE;
792 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700793 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500794 buf->type = FB_BUFFER_FD;
795 buf->data = nullptr;
796 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000797 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700798 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700799
Elliott Hughes53ec4952016-05-11 12:39:27 -0700800 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700801}
802
Aaron Wisnerdb511202018-06-26 15:38:35 -0500803static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500804 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
805
Elliott Hughes53ec4952016-05-11 12:39:27 -0700806 if (fd == -1) {
807 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700808 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500809
810 struct stat s;
811 if (fstat(fd, &s)) {
812 return false;
813 }
814 if (!S_ISREG(s.st_mode)) {
815 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
816 return false;
817 }
818
Aaron Wisnerdb511202018-06-26 15:38:35 -0500819 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700820}
821
David Zeuthenb6ea4352017-08-07 14:29:26 -0400822static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
823 // Buffer needs to be at least the size of the VBMeta struct which
824 // is 256 bytes.
825 if (buf->sz < 256) {
826 return;
827 }
828
Elliott Hughes855cdf82018-04-02 14:24:03 -0700829 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400830
831 std::string data;
832 if (!android::base::ReadFdToString(buf->fd, &data)) {
833 die("Failed reading from vbmeta");
834 }
835
836 // There's a 32-bit big endian |flags| field at offset 120 where
837 // bit 0 corresponds to disable-verity and bit 1 corresponds to
838 // disable-verification.
839 //
840 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
841 // the VBMeta struct.
842 if (g_disable_verity) {
843 data[123] |= 0x01;
844 }
845 if (g_disable_verification) {
846 data[123] |= 0x02;
847 }
848
849 if (!android::base::WriteStringToFd(data, fd)) {
850 die("Failed writing to modified vbmeta");
851 }
852 close(buf->fd);
853 buf->fd = fd;
854 lseek(fd, 0, SEEK_SET);
855}
856
Elliott Hughes5620d222018-03-28 08:20:00 -0700857static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700858{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700859 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700860
David Zeuthenb6ea4352017-08-07 14:29:26 -0400861 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
862 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700863 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400864 rewrite_vbmeta_buffer(buf);
865 }
866
Rom Lemarchand622810c2013-06-28 09:54:59 -0700867 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800868 case FB_BUFFER_SPARSE: {
869 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700870 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700871 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700872 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800873 sparse_files.emplace_back(*s, sz);
874 ++s;
875 }
876
877 for (size_t i = 0; i < sparse_files.size(); ++i) {
878 const auto& pair = sparse_files[i];
Elliott Hughes5620d222018-03-28 08:20:00 -0700879 fb_queue_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700880 }
881 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800882 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500883 case FB_BUFFER_FD:
Elliott Hughes5620d222018-03-28 08:20:00 -0700884 fb_queue_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700885 break;
886 default:
887 die("unknown buffer type: %d", buf->type);
888 }
889}
890
Aaron Wisnerdb511202018-06-26 15:38:35 -0500891static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700892 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500893 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700894 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700895}
896
Aaron Wisnerdb511202018-06-26 15:38:35 -0500897static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700898 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700899 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500900 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700901 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700902 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700903 return count;
904}
905
Aaron Wisnerdb511202018-06-26 15:38:35 -0500906static bool supports_AB() {
907 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700908}
909
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700910// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700911static std::string get_other_slot(const std::string& current_slot, int count) {
912 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700913
Daniel Rosenberg80919472016-06-30 19:25:31 -0700914 char next = (current_slot[0] - 'a' + 1)%count + 'a';
915 return std::string(1, next);
916}
917
Aaron Wisnerdb511202018-06-26 15:38:35 -0500918static std::string get_other_slot(const std::string& current_slot) {
919 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920}
921
Aaron Wisnerdb511202018-06-26 15:38:35 -0500922static std::string get_other_slot(int count) {
923 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700924}
925
Aaron Wisnerdb511202018-06-26 15:38:35 -0500926static std::string get_other_slot() {
927 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700928}
929
Aaron Wisnerdb511202018-06-26 15:38:35 -0500930static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700931 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700932 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800933 if (allow_all) {
934 return "all";
935 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500936 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700937 if (count > 0) {
938 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800939 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700940 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800941 }
942 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700943 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800944
Aaron Wisnerdb511202018-06-26 15:38:35 -0500945 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700946 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800947
Daniel Rosenberg80919472016-06-30 19:25:31 -0700948 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500949 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700950 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700951 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800952 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700953 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800954 }
955
Daniel Rosenberg80919472016-06-30 19:25:31 -0700956 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
957
958 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
959 for (int i=0; i<count; i++) {
960 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700961 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700962
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700963 exit(1);
964}
965
Aaron Wisnerdb511202018-06-26 15:38:35 -0500966static std::string verify_slot(const std::string& slot) {
967 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800968}
969
Aaron Wisnerdb511202018-06-26 15:38:35 -0500970static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700971 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800972 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700973 std::string current_slot;
974
Aaron Wisnerdb511202018-06-26 15:38:35 -0500975 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800976 /* If has-slot is not supported, the answer is no. */
977 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800979 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700980 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500981 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700983 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700984 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700986 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700988 }
989 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700991 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700992 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 }
994 func(part);
995 }
996}
997
David Pursell0b156632015-10-30 11:22:01 -0700998/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
999 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1000 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1001 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001002 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001003static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001004 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001005 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001006
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001008 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001009 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001010 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001011 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001012 for (int i=0; i < get_slot_count(); i++) {
1013 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001014 }
1015 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001016 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017 }
1018 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001019 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001020 }
1021}
1022
Aaron Wisnerdb511202018-06-26 15:38:35 -05001023static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001024 struct fastboot_buffer buf;
1025
Aaron Wisnerdb511202018-06-26 15:38:35 -05001026 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001027 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001028 }
1029 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001030}
1031
Elliott Hughes45964a82017-05-03 22:43:23 -07001032static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001033 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001034 void* data = unzip_to_memory(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001035 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001036 fb_queue_download("signature", data, sz);
1037 fb_queue_command("signature", "installing signature");
1038}
1039
Daniel Rosenberg13454092016-06-27 19:43:11 -07001040// Sets slot_override as the active slot. If slot_override is blank,
1041// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001042static void set_active(const std::string& slot_override) {
1043 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001044
Daniel Rosenberg80919472016-06-30 19:25:31 -07001045 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001046 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001047 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001048 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001049 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001050 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001051 }
1052 }
1053}
1054
David Anderson32e376f2018-08-16 13:43:11 -07001055static bool is_userspace_fastboot() {
1056 std::string value;
1057 return fb_getvar("is-userspace", &value) && value == "yes";
1058}
1059
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001060static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1061 std::string has_slot;
1062 std::string partition_name = partition;
1063
1064 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1065 if (slot == "") {
1066 std::string current_slot = get_current_slot();
1067 if (current_slot == "") {
1068 die("Failed to identify current slot");
1069 }
1070 partition_name += "_" + current_slot;
1071 } else {
1072 partition_name += "_" + slot;
1073 }
1074 }
1075 std::string partition_size;
1076 return fb_getvar("partition-size:" + partition_name, &partition_size);
1077}
1078
Aaron Wisnerdb511202018-06-26 15:38:35 -05001079static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 queue_info_dump();
1081
Wink Savilleb98762f2011-04-04 17:54:59 -07001082 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1083
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001084 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001085 int error = OpenArchive(filename, &zip);
1086 if (error != 0) {
1087 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001088 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001089
Elliott Hughesfc797672015-04-07 20:12:50 -07001090 int64_t sz;
Elliott Hughes23af1122017-11-10 08:42:17 -08001091 void* data = unzip_to_memory(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001092 if (data == nullptr) {
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001093 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 }
1095
Aaron Wisnerdb511202018-06-26 15:38:35 -05001096 check_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001097
Alex Lightbb9b8a52016-06-29 09:26:44 -07001098 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001099 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001100 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001101 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001102 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001103 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001104 }
1105 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001106 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001107 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1108 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001109 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001110 }
1111 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001112 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001113 const char* slot = slot_override.c_str();
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001114 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001115 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001116 slot = secondary.c_str();
1117 } else {
1118 continue;
1119 }
1120 }
1121
Elliott Hughes253c18d2015-03-18 22:47:09 -07001122 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001123 if (fd == -1) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001124 if (images[i].optional_if_no_image) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001125 continue; // An optional file is missing, so ignore it.
Elliott Hughesacdbe922015-06-02 21:37:05 -07001126 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001127 die("non-optional file %s missing", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001128 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001129
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001130 if (images[i].optional_if_no_partition &&
1131 !if_partition_exists(images[i].part_name, slot)) {
1132 continue;
1133 }
1134
Elliott Hughes253c18d2015-03-18 22:47:09 -07001135 fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001136 if (!load_buf_fd(fd, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001137 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1138 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001139
Elliott Hughes4089d342017-10-27 14:21:12 -07001140 auto update = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001141 do_update_signature(zip, images[i].sig_name);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001142 flash_buf(partition.c_str(), &buf);
1143 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001144 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001145 * program exits.
1146 */
1147 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001148 do_for_partitions(images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001149 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001150
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001151 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001152 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001153 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001154 set_active(slot_override);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001155 }
Elliott Hughes4089d342017-10-27 14:21:12 -07001156
1157 CloseArchive(zip);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158}
1159
Alex Lightbb9b8a52016-06-29 09:26:44 -07001160static void do_send_signature(const std::string& fn) {
1161 std::size_t extension_loc = fn.find(".img");
1162 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001163
Alex Lightbb9b8a52016-06-29 09:26:44 -07001164 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001165
1166 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001167 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001168 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001169
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001170 fb_queue_download("signature", data, sz);
1171 fb_queue_command("signature", "installing signature");
1172}
1173
David Anderson32e376f2018-08-16 13:43:11 -07001174static bool is_logical(const std::string& partition) {
1175 std::string value;
1176 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1177}
1178
David Anderson1d887432018-08-27 16:47:32 -07001179static void reboot_to_userspace_fastboot() {
1180 if (!fb_reboot_to_userspace()) {
1181 die("Must reboot to userspace fastboot to flash logical partitions");
1182 }
1183
1184 // Give the current connection time to close.
1185 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1186
1187 fb_reinit(open_device());
1188}
1189
David Anderson32e376f2018-08-16 13:43:11 -07001190static void update_super_partition(bool force_wipe) {
1191 if (!if_partition_exists("super", "")) {
1192 return;
1193 }
1194 std::string image = find_item_given_name("super_empty.img");
1195 if (access(image.c_str(), R_OK) < 0) {
1196 return;
1197 }
1198
1199 if (!is_userspace_fastboot()) {
David Anderson1d887432018-08-27 16:47:32 -07001200 reboot_to_userspace_fastboot();
David Anderson32e376f2018-08-16 13:43:11 -07001201 }
1202
1203 int fd = open(image.c_str(), O_RDONLY);
1204 if (fd < 0) {
1205 die("could not open '%s': %s", image.c_str(), strerror(errno));
1206 }
1207 fb_queue_download_fd("super", fd, get_file_size(fd));
1208
1209 std::string command = "update-super:super";
1210 if (force_wipe) {
1211 command += ":wipe";
1212 }
1213 fb_queue_command(command, "Updating super partition");
1214
1215 // We need these commands to have finished before proceeding, since
1216 // otherwise "getvar is-logical" may not return a correct answer below.
1217 fb_execute_queue();
1218}
1219
David Anderson95d40932018-08-28 12:14:33 -07001220static void flash_images(const std::vector<std::pair<const Image*, std::string>>& images) {
1221 // Flash each partition in the list if it has a corresponding image.
1222 for (const auto& [image, slot] : images) {
1223 auto fname = find_item_given_name(image->img_name);
1224 fastboot_buffer buf;
1225 if (!load_buf(fname.c_str(), &buf)) {
1226 if (image->optional_if_no_image) continue;
1227 die("could not load '%s': %s", image->img_name, strerror(errno));
1228 }
1229 if (image->optional_if_no_partition &&
1230 !if_partition_exists(image->part_name, slot)) {
1231 continue;
1232 }
1233 auto flashall = [&](const std::string &partition) {
1234 do_send_signature(fname.c_str());
1235 if (is_logical(partition)) {
1236 fb_queue_resize_partition(partition, std::to_string(buf.image_size));
1237 }
1238 flash_buf(partition.c_str(), &buf);
1239 };
1240 do_for_partitions(image->part_name, slot, flashall, false);
1241 }
1242}
1243
David Anderson32e376f2018-08-16 13:43:11 -07001244static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001245 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001246 queue_info_dump();
1247
Wink Savilleb98762f2011-04-04 17:54:59 -07001248 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1249
Elliott Hughes45964a82017-05-03 22:43:23 -07001250 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001251 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001252
Elliott Hughesfc797672015-04-07 20:12:50 -07001253 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001254 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001255 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001256
Aaron Wisnerdb511202018-06-26 15:38:35 -05001257 check_requirements(reinterpret_cast<char*>(data), sz);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001258
Alex Lightbb9b8a52016-06-29 09:26:44 -07001259 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001260 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001261 if (slot_override != "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001262 secondary = get_other_slot(slot_override);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001263 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001264 secondary = get_other_slot();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001265 }
1266 if (secondary == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001267 if (supports_AB()) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001268 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1269 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001270 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001271 }
1272 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001273
David Anderson32e376f2018-08-16 13:43:11 -07001274 // List of partitions to flash and their slots.
David Anderson95d40932018-08-28 12:14:33 -07001275 std::vector<std::pair<const Image*, std::string>> boot_images;
1276 std::vector<std::pair<const Image*, std::string>> os_images;
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001277 for (size_t i = 0; i < arraysize(images); i++) {
Mark Salyzyn8fb0fb82018-08-24 14:37:11 -07001278 if (!images[i].flashall) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001279 const char* slot = NULL;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001280 if (images[i].IsSecondary()) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001281 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001282 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001283 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001284 }
1285 if (!slot) continue;
David Anderson95d40932018-08-28 12:14:33 -07001286 if (images[i].needed_for_fastbootd) {
1287 boot_images.emplace_back(&images[i], slot);
1288 } else {
1289 os_images.emplace_back(&images[i], slot);
1290 }
1291 }
David Anderson32e376f2018-08-16 13:43:11 -07001292
David Anderson95d40932018-08-28 12:14:33 -07001293 // First flash boot partitions. We allow this to happen either in userspace
1294 // or in bootloader fastboot.
1295 flash_images(boot_images);
1296
1297 // Sync the super partition. This will reboot to userspace fastboot if needed.
1298 update_super_partition(wipe);
1299
1300 // Resize any logical partition to 0, so each partition is reset to 0
1301 // extents, and will achieve more optimal allocation.
1302 for (const auto& [image, slot] : os_images) {
David Anderson32e376f2018-08-16 13:43:11 -07001303 auto resize_partition = [](const std::string& partition) -> void {
1304 if (is_logical(partition)) {
1305 fb_queue_resize_partition(partition, "0");
1306 }
1307 };
David Anderson95d40932018-08-28 12:14:33 -07001308 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001309 }
1310
David Anderson95d40932018-08-28 12:14:33 -07001311 // Flash OS images, resizing logical partitions as needed.
1312 flash_images(os_images);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001313
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001314 if (slot_override == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001315 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001316 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001317 set_active(slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001318 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001319}
1320
Elliott Hughesd6365a72017-05-08 18:04:49 -07001321static std::string next_arg(std::vector<std::string>* args) {
1322 if (args->empty()) syntax_error("expected argument");
1323 std::string result = args->front();
1324 args->erase(args->begin());
1325 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001326}
1327
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001328static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001329 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001330
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001331 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001332 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001333 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001334 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001335 fb_queue_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001336}
1337
Connor O'Brience16a8a2017-02-06 14:39:31 -08001338static std::string fb_fix_numeric_var(std::string var) {
1339 // Some bootloaders (angler, for example), send spurious leading whitespace.
1340 var = android::base::Trim(var);
1341 // Some bootloaders (hammerhead, for example) use implicit hex.
1342 // This code used to use strtol with base 16.
1343 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1344 return var;
1345}
1346
Aaron Wisnerdb511202018-06-26 15:38:35 -05001347static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001348 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001349 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001350 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001351 return 0;
1352 }
1353 sizeString = fb_fix_numeric_var(sizeString);
1354
1355 unsigned size;
1356 if (!android::base::ParseUint(sizeString, &size)) {
1357 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1358 return 0;
1359 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001360 if ((size & (size - 1)) != 0) {
1361 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001362 return 0;
1363 }
1364 return size;
1365}
1366
Aaron Wisnerdb511202018-06-26 15:38:35 -05001367static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001368 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001369 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001370 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001371 std::string partition_type, partition_size;
1372
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001373 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001374 const char* errMsg = nullptr;
1375 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001376 TemporaryFile output;
1377 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001378
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001379 unsigned int limit = INT_MAX;
1380 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001381 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001382 }
1383 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001384 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001385 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001386
Aaron Wisnerdb511202018-06-26 15:38:35 -05001387 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001388 errMsg = "Can't determine partition type.\n";
1389 goto failed;
1390 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001391 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001392 if (partition_type != type_override) {
1393 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001394 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001395 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001397 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001398
Aaron Wisnerdb511202018-06-26 15:38:35 -05001399 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001400 errMsg = "Unable to get partition size\n";
1401 goto failed;
1402 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001403 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001404 if (partition_size != size_override) {
1405 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001406 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001407 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001409 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001410 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001412 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001413 if (!gen) {
1414 if (skip_if_not_supported) {
1415 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001416 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001417 return;
1418 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1420 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 return;
1422 }
1423
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001424 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001425 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001426 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1427 return;
1428 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001429
Connor O'Brience16a8a2017-02-06 14:39:31 -08001430 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001431 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1432 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001433
Jin Qian4a335822017-04-18 16:23:18 -07001434 if (fs_generator_generate(gen, output.path, size, initial_dir,
1435 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001436 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001437 return;
1438 }
1439
Jin Qian4a335822017-04-18 16:23:18 -07001440 fd.reset(open(output.path, O_RDONLY));
1441 if (fd == -1) {
1442 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1443 return;
1444 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001445 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001446 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001447 return;
1448 }
1449 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001450 return;
1451
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001452failed:
1453 if (skip_if_not_supported) {
1454 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001455 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001456 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001457 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001458}
1459
Aaron Wisnerdb511202018-06-26 15:38:35 -05001460int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001461 bool wants_wipe = false;
1462 bool wants_reboot = false;
1463 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001464 bool wants_reboot_recovery = false;
1465 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001466 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001467 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001468 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001469 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001470 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001471 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001472 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001473 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001474 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001475
Elliott Hughes577e8b42018-04-05 16:12:47 -07001476 g_boot_img_hdr.kernel_addr = 0x00008000;
1477 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1478 g_boot_img_hdr.second_addr = 0x00f00000;
1479 g_boot_img_hdr.tags_addr = 0x00000100;
1480 g_boot_img_hdr.page_size = 2048;
1481
JP Abgrall7b8970c2013-03-07 17:06:41 -08001482 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001483 {"base", required_argument, 0, 0},
1484 {"cmdline", required_argument, 0, 0},
1485 {"disable-verification", no_argument, 0, 0},
1486 {"disable-verity", no_argument, 0, 0},
1487 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001488 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001489 {"kernel-offset", required_argument, 0, 0},
1490 {"os-patch-level", required_argument, 0, 0},
1491 {"os-version", required_argument, 0, 0},
1492 {"page-size", required_argument, 0, 0},
1493 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001494 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001495 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001496 {"skip-secondary", no_argument, 0, 0},
1497 {"slot", required_argument, 0, 0},
1498 {"tags-offset", required_argument, 0, 0},
1499 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001500 {"verbose", no_argument, 0, 'v'},
1501 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001502#if !defined(_WIN32)
1503 {"wipe-and-use-fbe", no_argument, 0, 0},
1504#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001505 {0, 0, 0, 0}
1506 };
Colin Cross8879f982012-05-22 17:53:34 -07001507
1508 serial = getenv("ANDROID_SERIAL");
1509
Elliott Hughes577e8b42018-04-05 16:12:47 -07001510 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001511 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001512 if (c == 0) {
1513 std::string name{longopts[longindex].name};
1514 if (name == "base") {
1515 g_base_addr = strtoul(optarg, 0, 16);
1516 } else if (name == "cmdline") {
1517 g_cmdline = optarg;
1518 } else if (name == "disable-verification") {
1519 g_disable_verification = true;
1520 } else if (name == "disable-verity") {
1521 g_disable_verity = true;
1522 } else if (name == "header-version") {
1523 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1524 } else if (name == "kernel-offset") {
1525 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1526 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001527 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001528 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001529 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001530 } else if (name == "page-size") {
1531 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1532 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1533 } else if (name == "ramdisk-offset") {
1534 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1535 } else if (name == "skip-reboot") {
1536 skip_reboot = true;
1537 } else if (name == "skip-secondary") {
1538 skip_secondary = true;
1539 } else if (name == "slot") {
1540 slot_override = optarg;
1541 } else if (name == "tags-offset") {
1542 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1543 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001544 setvbuf(stdout, nullptr, _IONBF, 0);
1545 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001546 } else if (name == "version") {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001547 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001548 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001549 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001550#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001551 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001552 wants_wipe = true;
1553 set_fbe_marker = true;
1554#endif
1555 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001556 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001557 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001558 } else {
1559 switch (c) {
1560 case 'a':
1561 wants_set_active = true;
1562 if (optarg) next_active = optarg;
1563 break;
1564 case 'h':
1565 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001566 case 'l':
1567 g_long_listing = true;
1568 break;
1569 case 's':
1570 serial = optarg;
1571 break;
1572 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001573 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1574 die("invalid sparse limit %s", optarg);
1575 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001576 break;
1577 case 'v':
1578 set_verbose();
1579 break;
1580 case 'w':
1581 wants_wipe = true;
1582 break;
1583 case '?':
1584 return 1;
1585 default:
1586 abort();
1587 }
Colin Cross8879f982012-05-22 17:53:34 -07001588 }
1589 }
1590
1591 argc -= optind;
1592 argv += optind;
1593
Elliott Hughesd6365a72017-05-08 18:04:49 -07001594 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001595
Colin Cross8fb6e062012-07-24 16:36:41 -07001596 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001597 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001598 return 0;
1599 }
1600
Colin Crossc7b75dc2012-08-29 18:17:06 -07001601 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001602 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001603 }
1604
David Pursell0b156632015-10-30 11:22:01 -07001605 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001606 if (transport == nullptr) {
1607 return 1;
1608 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001609 fastboot::FastBootDriver fb(transport);
1610 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001611
Elliott Hughes5620d222018-03-28 08:20:00 -07001612 const double start = now();
1613
Aaron Wisnerdb511202018-06-26 15:38:35 -05001614 if (slot_override != "") slot_override = verify_slot(slot_override);
1615 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001616
1617 if (wants_set_active) {
1618 if (next_active == "") {
1619 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001620 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001621 if (fb_getvar("current-slot", &current_slot)) {
1622 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001623 } else {
1624 wants_set_active = false;
1625 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001626 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001627 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001628 }
1629 }
1630 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001631
Elliott Hughesd6365a72017-05-08 18:04:49 -07001632 std::vector<std::string> args(argv, argv + argc);
1633 while (!args.empty()) {
1634 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001635
Elliott Hughesd6365a72017-05-08 18:04:49 -07001636 if (command == "getvar") {
1637 std::string variable = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001638 fb_queue_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001639 } else if (command == "erase") {
1640 std::string partition = next_arg(&args);
1641 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001642 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001643 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001644 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001645 fs_get_generator(partition_type) != nullptr) {
1646 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1647 partition_type.c_str());
1648 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001649
Elliott Hughes5620d222018-03-28 08:20:00 -07001650 fb_queue_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001651 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001652 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001653 } else if (android::base::StartsWith(command, "format")) {
1654 // Parsing for: "format[:[type][:[size]]]"
1655 // Some valid things:
1656 // - select only the size, and leave default fs type:
1657 // format::0x4000000 userdata
1658 // - default fs type and size:
1659 // format userdata
1660 // format:: userdata
1661 std::vector<std::string> pieces = android::base::Split(command, ":");
1662 std::string type_override;
1663 if (pieces.size() > 1) type_override = pieces[1].c_str();
1664 std::string size_override;
1665 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001666
Elliott Hughesd6365a72017-05-08 18:04:49 -07001667 std::string partition = next_arg(&args);
1668
1669 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001670 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001671 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001672 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001673 } else if (command == "signature") {
1674 std::string filename = next_arg(&args);
1675 data = load_file(filename.c_str(), &sz);
1676 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -07001677 if (sz != 256) die("signature must be 256 bytes (got %" PRId64 ")", sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001678 fb_queue_download("signature", data, sz);
1679 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001680 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001681 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001682
1683 if (args.size() == 1) {
1684 std::string what = next_arg(&args);
1685 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001686 wants_reboot = false;
1687 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001688 } else if (what == "recovery") {
1689 wants_reboot = false;
1690 wants_reboot_recovery = true;
1691 } else if (what == "fastboot") {
1692 wants_reboot = false;
1693 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001694 } else {
1695 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001696 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001697
Elliott Hughesca85df02015-02-25 10:02:00 -08001698 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001699 if (!args.empty()) syntax_error("junk after reboot command");
1700 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001701 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001702 } else if (command == "reboot-recovery") {
1703 wants_reboot_recovery = true;
1704 } else if (command == "reboot-fastboot") {
1705 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001706 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001708 } else if (command == "boot") {
1709 std::string kernel = next_arg(&args);
1710 std::string ramdisk;
1711 if (!args.empty()) ramdisk = next_arg(&args);
1712 std::string second_stage;
1713 if (!args.empty()) second_stage = next_arg(&args);
1714
Elliott Hughes577e8b42018-04-05 16:12:47 -07001715 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001716 fb_queue_download("boot.img", data, sz);
1717 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001718 } else if (command == "flash") {
1719 std::string pname = next_arg(&args);
1720
Elliott Hughes2810d002016-04-25 14:31:18 -07001721 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001722 if (!args.empty()) {
1723 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001724 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001725 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001726 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001727 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001728
1729 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001730 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001731 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001732 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001733 } else if (command == "flash:raw") {
1734 std::string partition = next_arg(&args);
1735 std::string kernel = next_arg(&args);
1736 std::string ramdisk;
1737 if (!args.empty()) ramdisk = next_arg(&args);
1738 std::string second_stage;
1739 if (!args.empty()) second_stage = next_arg(&args);
1740
Elliott Hughes577e8b42018-04-05 16:12:47 -07001741 data = load_bootable_image(kernel, ramdisk, second_stage, &sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001742 auto flashraw = [&](const std::string& partition) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001743 fb_queue_flash(partition, data, sz);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001744 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001745 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001746 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001747 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001748 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001749 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001750 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001751 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001752 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001753 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001754 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001755 bool slot_all = (slot_override == "all");
1756 if (slot_all) {
1757 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1758 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001759 std::string filename = "update.zip";
1760 if (!args.empty()) {
1761 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001762 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001763 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001764 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001765 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001766 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001767 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001768 } else if (command == "stage") {
1769 std::string filename = next_arg(&args);
1770
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001771 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001772 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001773 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001774 }
Elliott Hughes5620d222018-03-28 08:20:00 -07001775 fb_queue_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001776 } else if (command == "get_staged") {
1777 std::string filename = next_arg(&args);
Elliott Hughes5620d222018-03-28 08:20:00 -07001778 fb_queue_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001779 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001780 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001781 } else if (command == "flashing") {
1782 if (args.empty()) {
1783 syntax_error("missing 'flashing' command");
1784 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1785 args[0] == "unlock_critical" ||
1786 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001787 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001788 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001789 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001790 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001791 }
David Anderson0d4277d2018-07-31 13:27:37 -07001792 } else if (command == "create-logical-partition") {
1793 std::string partition = next_arg(&args);
1794 std::string size = next_arg(&args);
1795 fb_queue_create_partition(partition, size);
1796 } else if (command == "delete-logical-partition") {
1797 std::string partition = next_arg(&args);
1798 fb_queue_delete_partition(partition);
1799 } else if (command == "resize-logical-partition") {
1800 std::string partition = next_arg(&args);
1801 std::string size = next_arg(&args);
1802 fb_queue_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001803 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001804 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001805 }
1806 }
1807
1808 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001809 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1810 for (const auto& partition : partitions) {
1811 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001812 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001813 if (partition_type.empty()) continue;
1814 fb_queue_erase(partition);
1815 if (partition == "userdata" && set_fbe_marker) {
1816 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1817 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001818 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001819 delete_fbemarker_tmpdir(initial_userdata_dir);
1820 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001821 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001822 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001823 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001824 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001825 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001826 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001827 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001828 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001829 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001830 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001831 } else if (wants_reboot_bootloader) {
1832 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001833 fb_queue_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001834 } else if (wants_reboot_recovery) {
1835 fb_queue_command("reboot-recovery", "rebooting into recovery");
1836 fb_queue_wait_for_disconnect();
1837 } else if (wants_reboot_fastboot) {
1838 fb_queue_command("reboot-fastboot", "rebooting into fastboot");
1839 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001840 }
1841
Aaron Wisnerdb511202018-06-26 15:38:35 -05001842 int status = fb_execute_queue() ? EXIT_FAILURE : EXIT_SUCCESS;
Elliott Hughes5620d222018-03-28 08:20:00 -07001843 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
1844 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001845}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001846
Aaron Wisnerdb511202018-06-26 15:38:35 -05001847void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001848 unsigned year, month, day;
1849 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1850 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1851 }
1852 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1853 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1854 hdr->SetOsPatchLevel(year, month);
1855}
1856
Aaron Wisnerdb511202018-06-26 15:38:35 -05001857void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001858 unsigned major = 0, minor = 0, patch = 0;
1859 std::vector<std::string> versions = android::base::Split(arg, ".");
1860 if (versions.size() < 1 || versions.size() > 3 ||
1861 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1862 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1863 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1864 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1865 syntax_error("bad OS version: %s", arg);
1866 }
1867 hdr->SetOsVersion(major, minor, patch);
1868}