blob: fd7b233229ccf2262dff94b3c39b59ed0054b70b [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>
Dan Willemsenab971b52018-08-29 14:58:02 -070058#include <build/version.h>
59#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070060#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070061#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
Elliott Hughes253c18d2015-03-18 22:47:09 -070063#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080064#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050065#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070066#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080067#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080069#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070070#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Tom Cherrydfd85df2018-09-20 14:45:05 -070072using android::base::ReadFully;
Chris Fries0ea946c2017-04-12 10:25:57 -050073using android::base::unique_fd;
74
Colin Crossf8387882012-05-24 17:18:41 -070075#ifndef O_BINARY
76#define O_BINARY 0
77#endif
78
Wink Savilleb98762f2011-04-04 17:54:59 -070079char cur_product[FB_RESPONSE_SZ + 1];
80
David Pursell2ec418a2016-01-20 08:32:08 -080081static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070082
Elliott Hughes577e8b42018-04-05 16:12:47 -070083static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050084// Don't resparse files in too-big chunks.
85// libsparse will support INT_MAX, but this results in large allocations, so
86// let's keep it at 1GB to avoid memory pressure on the host.
87static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070088static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070089static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughes577e8b42018-04-05 16:12:47 -070091static unsigned g_base_addr = 0x10000000;
92static boot_img_hdr_v1 g_boot_img_hdr = {};
93static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080094
David Zeuthenb6ea4352017-08-07 14:29:26 -040095static bool g_disable_verity = false;
96static bool g_disable_verification = false;
97
Paul Crowley8f7f56e2015-11-27 09:29:37 +000098static const std::string convert_fbe_marker_filename("convert_fbe");
99
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500101 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 FB_BUFFER_SPARSE,
103};
104
105struct fastboot_buffer {
106 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700107 void* data;
108 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500109 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700110 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
112
David Anderson0debda02018-08-28 13:54:03 -0700113enum class ImageType {
114 // Must be flashed for device to boot into the kernel.
115 BootCritical,
116 // Normal partition to be flashed during "flashall".
117 Normal,
118 // Partition that is never flashed during "flashall".
119 Extra
120};
121
David Anderson32e376f2018-08-16 13:43:11 -0700122struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700123 const char* nickname;
124 const char* img_name;
125 const char* sig_name;
126 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700127 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700128 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700129 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700130};
131
132static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100133 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700134 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
135 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
136 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
137 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
138 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
139 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
140 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100141 { "product_services",
142 "product_services.img",
143 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700144 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700145 true, ImageType::Normal },
146 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
147 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
148 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
149 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
150 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
151 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson1109c922018-09-17 17:32:54 -0700152 { "vbmeta_mainline",
153 "vbmeta_mainline.img",
154 "vbmeta_mainline.sig",
155 "vbmeta_mainline",
156 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700157 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
158 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100159 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700161
David Anderson0debda02018-08-28 13:54:03 -0700162static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700163 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700164 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700165 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700166 }
David Anderson0debda02018-08-28 13:54:03 -0700167 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700168}
169
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700170static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700171 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700172 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700173 return find_item_given_name(images[i].img_name);
174 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 }
176
Elliott Hughesd6365a72017-05-08 18:04:49 -0700177 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700178 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179}
180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181static int64_t get_file_size(int fd) {
182 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700183 if (fstat(fd, &sb) == -1) {
184 die("could not get file size");
185 }
186 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700187}
188
Tom Cherrydfd85df2018-09-20 14:45:05 -0700189bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
190 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
Tom Cherrydfd85df2018-09-20 14:45:05 -0700192 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
193 if (fd == -1) {
194 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700195 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Tom Cherrydfd85df2018-09-20 14:45:05 -0700197 out->resize(get_file_size(fd));
198 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700199}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200
Elliott Hughesfc797672015-04-07 20:12:50 -0700201static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700202 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
203 return -1;
204 }
205
Scott Anderson13081c62012-04-06 12:39:30 -0700206 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700208 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
209 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210 return 0;
211}
212
Elliott Hughesfc797672015-04-07 20:12:50 -0700213static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800214 return match_fastboot_with_serial(info, serial);
215}
216
Elliott Hughesfc797672015-04-07 20:12:50 -0700217static int list_devices_callback(usb_ifc_info* info) {
218 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700220 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700222 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 if (!serial[0]) {
224 serial = "????????????";
225 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700226 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700227 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800228 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700229 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800230 printf("%-22s fastboot", serial.c_str());
231 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700232 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800233 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 }
235
236 return -1;
237}
238
David Pursell2ec418a2016-01-20 08:32:08 -0800239// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
240// a specific device, otherwise the first USB device found will be used.
241//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700242// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800243// Otherwise it blocks until the target is available.
244//
245// The returned Transport is a singleton, so multiple calls to this function will return the same
246// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700247static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800248 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
David Pursell4601c972016-02-05 15:35:09 -0800250 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800251 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800252 int port = 0;
253 if (serial != nullptr) {
254 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800255
David Pursell4601c972016-02-05 15:35:09 -0800256 if (android::base::StartsWith(serial, "tcp:")) {
257 protocol = Socket::Protocol::kTcp;
258 port = tcp::kDefaultPort;
259 net_address = serial + strlen("tcp:");
260 } else if (android::base::StartsWith(serial, "udp:")) {
261 protocol = Socket::Protocol::kUdp;
262 port = udp::kDefaultPort;
263 net_address = serial + strlen("udp:");
264 }
265
266 if (net_address != nullptr) {
267 std::string error;
268 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700269 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800270 }
David Pursell2ec418a2016-01-20 08:32:08 -0800271 }
272 }
273
David Anderson1d887432018-08-27 16:47:32 -0700274 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800275 while (true) {
276 if (!host.empty()) {
277 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800278 if (protocol == Socket::Protocol::kTcp) {
279 transport = tcp::Connect(host, port, &error).release();
280 } else if (protocol == Socket::Protocol::kUdp) {
281 transport = udp::Connect(host, port, &error).release();
282 }
283
David Pursell2ec418a2016-01-20 08:32:08 -0800284 if (transport == nullptr && announce) {
285 fprintf(stderr, "error: %s\n", error.c_str());
286 }
287 } else {
288 transport = usb_open(match_fastboot);
289 }
290
291 if (transport != nullptr) {
292 return transport;
293 }
294
David Pursell0b156632015-10-30 11:22:01 -0700295 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800296 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700297 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800299 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 // We don't actually open a USB device here,
305 // just getting our callback called so we can
306 // list all the connected devices.
307 usb_open(list_devices_callback);
308}
309
Elliott Hughesd6365a72017-05-08 18:04:49 -0700310static void syntax_error(const char* fmt, ...) {
311 fprintf(stderr, "fastboot: usage: ");
312
313 va_list ap;
314 va_start(ap, fmt);
315 vfprintf(stderr, fmt, ap);
316 va_end(ap);
317
318 fprintf(stderr, "\n");
319 exit(1);
320}
321
322static int show_help() {
323 // clang-format off
324 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700325// 1 2 3 4 5 6 7 8
326// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700327 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700329 "flashing:\n"
330 " update ZIP Flash all partitions from an update.zip package.\n"
331 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
332 " On A/B devices, flashed slot is set as active.\n"
333 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700334 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
335 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700336 "\n"
337 "basics:\n"
338 " devices [-l] List devices in bootloader (-l: with device paths).\n"
339 " getvar NAME Display given bootloader variable.\n"
340 " reboot [bootloader] Reboot device.\n"
341 "\n"
342 "locking/unlocking:\n"
343 " flashing lock|unlock Lock/unlock partitions for flashing\n"
344 " flashing lock_critical|unlock_critical\n"
345 " Lock/unlock 'critical' bootloader partitions.\n"
346 " flashing get_unlock_ability\n"
347 " Check whether unlocking is allowed (1) or not(0).\n"
348 "\n"
349 "advanced:\n"
350 " erase PARTITION Erase a flash partition.\n"
351 " format[:FS_TYPE[:SIZE]] PARTITION\n"
352 " Format a flash partition.\n"
353 " set_active SLOT Set the active slot.\n"
354 " oem [COMMAND...] Execute OEM-specific command.\n"
355 "\n"
356 "boot image:\n"
357 " boot KERNEL [RAMDISK [SECOND]]\n"
358 " Download and boot kernel from RAM.\n"
359 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
360 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700361 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700362 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
363 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
364 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
365 " --tags-offset Set tags offset (default: 0x00000100).\n"
366 " --page-size BYTES Set flash page size (default: 2048).\n"
367 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700368 " --os-version MAJOR[.MINOR[.PATCH]]\n"
369 " Set boot image OS version (default: 0.0.0).\n"
370 " --os-patch-level YYYY-MM-DD\n"
371 " Set boot image OS security patch level.\n"
372 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700373 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700374 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700375 //" continue Continue with autoboot.\n"
376 //"\n"
377 "Android Things:\n"
378 " stage IN_FILE Sends given file to stage for the next command.\n"
379 " 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 -0800380 "\n"
381 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700382 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 " -s SERIAL Specify a USB device.\n"
384 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700385 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700386 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
387 " non-current slot (default: current active slot).\n"
388 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
389 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
390 " --skip-reboot Don't reboot device after flashing.\n"
391 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
392 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000393#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700394 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000395#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700396 // TODO: remove --unbuffered?
397 " --unbuffered Don't buffer input or output.\n"
398 " --verbose, -v Verbose output.\n"
399 " --version Display version.\n"
400 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700402 // clang-format off
403 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000405
Tom Cherrydfd85df2018-09-20 14:45:05 -0700406static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
407 const std::string& second_stage) {
408 std::vector<char> kernel_data;
409 if (!ReadFileToVector(kernel, &kernel_data)) {
410 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
411 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800412
Elliott Hughesfc797672015-04-07 20:12:50 -0700413 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700414 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800415 die("cannot load '%s': too short", kernel.c_str());
416 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700417 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700418 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700419 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700420 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800421
Elliott Hughes4089d342017-10-27 14:21:12 -0700422 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800423
Tom Cherrydfd85df2018-09-20 14:45:05 -0700424 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 }
426
Tom Cherrydfd85df2018-09-20 14:45:05 -0700427 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700428 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700429 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
430 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
431 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 }
433
Tom Cherrydfd85df2018-09-20 14:45:05 -0700434 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700435 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700436 if (!ReadFileToVector(second_stage, &second_stage_data)) {
437 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
438 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200439 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700441
Tom Cherrydfd85df2018-09-20 14:45:05 -0700442 std::vector<char> out;
443 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
444 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800445
Tom Cherrydfd85df2018-09-20 14:45:05 -0700446 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
447 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
448
449 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450}
451
Tom Cherrydfd85df2018-09-20 14:45:05 -0700452static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
453 std::vector<char>* out) {
454 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700455 ZipEntry zip_entry;
456 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700457 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
458 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 }
460
Tom Cherrydfd85df2018-09-20 14:45:05 -0700461 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462
Tom Cherrydfd85df2018-09-20 14:45:05 -0700463 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
464 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465
Tom Cherrydfd85df2018-09-20 14:45:05 -0700466 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
467 out->size());
468 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000469
Tom Cherrydfd85df2018-09-20 14:45:05 -0700470 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471}
472
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700473#if defined(_WIN32)
474
475// TODO: move this to somewhere it can be shared.
476
477#include <windows.h>
478
479// Windows' tmpfile(3) requires administrator rights because
480// it creates temporary files in the root directory.
481static FILE* win32_tmpfile() {
482 char temp_path[PATH_MAX];
483 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
484 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700485 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700486 }
487
488 char filename[PATH_MAX];
489 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700490 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700491 }
492
493 return fopen(filename, "w+bTD");
494}
495
496#define tmpfile win32_tmpfile
497
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000498static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700499 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000500}
501
Elliott Hughes855cdf82018-04-02 14:24:03 -0700502static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700503 // TODO: reimplement to avoid leaking a FILE*.
504 return fileno(tmpfile());
505}
506
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000507#else
508
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700509static std::string make_temporary_template() {
510 const char* tmpdir = getenv("TMPDIR");
511 if (tmpdir == nullptr) tmpdir = P_tmpdir;
512 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
513}
514
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000515static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700516 std::string result(make_temporary_template());
517 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700518 die("unable to create temporary directory with template %s: %s",
519 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000520 }
521 return result;
522}
523
Elliott Hughes855cdf82018-04-02 14:24:03 -0700524static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700525 std::string path_template(make_temporary_template());
526 int fd = mkstemp(&path_template[0]);
527 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700528 die("failed to create temporary file for %s with template %s: %s\n",
529 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700530 }
531 unlink(path_template.c_str());
532 return fd;
533}
534
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700535#endif
536
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000537static std::string create_fbemarker_tmpdir() {
538 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000539 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
540 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
541 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700542 die("unable to create FBE marker file %s locally: %s",
543 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544 }
545 close(fd);
546 return dir;
547}
548
549static void delete_fbemarker_tmpdir(const std::string& dir) {
550 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
551 if (unlink(marker_file.c_str()) == -1) {
552 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
553 marker_file.c_str(), errno, strerror(errno));
554 return;
555 }
556 if (rmdir(dir.c_str()) == -1) {
557 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
558 dir.c_str(), errno, strerror(errno));
559 return;
560 }
561}
562
Elliott Hughes45964a82017-05-03 22:43:23 -0700563static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700564 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700565
Yusuke Sato07447542015-06-25 14:39:19 -0700566 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700567 ZipEntry zip_entry;
568 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
569 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700570 errno = ENOENT;
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
David Anderson0debda02018-08-28 13:54:03 -0700642 // override the fields `optional_if_no_image` in the `images` array.
Elliott Hughes5620d222018-03-28 08:20:00 -0700643 if (!strcmp(name, "partition-exists")) {
644 const char* partition_name = val[0];
645 std::string has_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500646 if (!fb_getvar(std::string("has-slot:") + partition_name, &has_slot) ||
Elliott Hughes5620d222018-03-28 08:20:00 -0700647 (has_slot != "yes" && has_slot != "no")) {
648 die("device doesn't have required partition %s!", partition_name);
649 }
650 bool known_partition = false;
651 for (size_t i = 0; i < arraysize(images); ++i) {
652 if (images[i].nickname && !strcmp(images[i].nickname, partition_name)) {
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700653 images[i].optional_if_no_image = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700654 known_partition = true;
655 }
656 }
657 if (!known_partition) {
658 die("device requires partition %s which is not known to this version of fastboot",
659 partition_name);
660 }
661 return;
662 }
663
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800664 for(count = 1; count < MAX_OPTIONS; count++) {
665 x = strchr(val[count - 1],'|');
666 if (x == 0) break;
667 *x = 0;
668 val[count] = x + 1;
669 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800670
Elliott Hughes253c18d2015-03-18 22:47:09 -0700671 // Work around an unfortunate name mismatch.
Elliott Hughes5620d222018-03-28 08:20:00 -0700672 const char* var = name;
673 if (!strcmp(name, "board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674
Elliott Hughes253c18d2015-03-18 22:47:09 -0700675 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
Elliott Hughes5620d222018-03-28 08:20:00 -0700676 if (out == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800677
Elliott Hughes5620d222018-03-28 08:20:00 -0700678 for (size_t i = 0; i < count; ++i) {
679 out[i] = xstrdup(strip(val[i]));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 }
681
Tom Cherry11f12092018-08-29 21:36:28 -0700682 fb_require(product, var, invert, count, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800683}
684
Aaron Wisnerdb511202018-06-26 15:38:35 -0500685static void check_requirements(char* data, int64_t sz) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700686 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700688 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800689 *s++ = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500690 check_requirement(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691 data = s;
692 } else {
693 s++;
694 }
695 }
696}
697
Tom Cherry11f12092018-08-29 21:36:28 -0700698static void dump_info() {
699 fb_notice("--------------------------------------------");
700 fb_display("Bootloader Version...", "version-bootloader");
701 fb_display("Baseband Version.....", "version-baseband");
702 fb_display("Serial Number........", "serialno");
703 fb_notice("--------------------------------------------");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704}
705
Tao Bao41cf35f2018-04-24 10:54:21 -0700706static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700707 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700708 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700709
Tao Bao41cf35f2018-04-24 10:54:21 -0700710 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
711 die("invalid max size %" PRId64, max_size);
712 }
713
Elliott Hughesfc797672015-04-07 20:12:50 -0700714 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700715 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700716
Elliott Hughes253c18d2015-03-18 22:47:09 -0700717 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700718 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700719
720 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700721 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700722
723 return out_s;
724}
725
Aaron Wisnerdb511202018-06-26 15:38:35 -0500726static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700727 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500728 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700729 max_download_size.empty()) {
730 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700731 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700732 }
733
Elliott Hughes77c0e662015-11-02 15:51:12 -0800734 // Some bootloaders (angler, for example) send spurious whitespace too.
735 max_download_size = android::base::Trim(max_download_size);
736
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700737 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700738 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800739 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 return 0;
741 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700742 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700743 return limit;
744}
745
Aaron Wisnerdb511202018-06-26 15:38:35 -0500746static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700747 int64_t limit = sparse_limit;
748 if (limit == 0) {
749 // Unlimited, so see what the target device's limit is.
750 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700751 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500752 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700753 }
754 if (target_sparse_limit > 0) {
755 limit = target_sparse_limit;
756 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700757 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700758 }
759 }
760
761 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500762 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700763 }
764
765 return 0;
766}
767
Aaron Wisnerdb511202018-06-26 15:38:35 -0500768static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700769 int64_t sz = get_file_size(fd);
770 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700771 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700772 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700773
David Anderson32e376f2018-08-16 13:43:11 -0700774 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
775 buf->image_size = sparse_file_len(s, false, false);
776 sparse_file_destroy(s);
777 } else {
778 buf->image_size = sz;
779 }
780
Elliott Hughesfc797672015-04-07 20:12:50 -0700781 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500782 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700783 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700784 sparse_file** s = load_sparse_files(fd, limit);
785 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700786 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700787 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700788 buf->type = FB_BUFFER_SPARSE;
789 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700790 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500791 buf->type = FB_BUFFER_FD;
792 buf->data = nullptr;
793 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000794 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700795 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700796
Elliott Hughes53ec4952016-05-11 12:39:27 -0700797 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700798}
799
Aaron Wisnerdb511202018-06-26 15:38:35 -0500800static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500801 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
802
Elliott Hughes53ec4952016-05-11 12:39:27 -0700803 if (fd == -1) {
804 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700805 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500806
807 struct stat s;
808 if (fstat(fd, &s)) {
809 return false;
810 }
811 if (!S_ISREG(s.st_mode)) {
812 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
813 return false;
814 }
815
Aaron Wisnerdb511202018-06-26 15:38:35 -0500816 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817}
818
David Zeuthenb6ea4352017-08-07 14:29:26 -0400819static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
820 // Buffer needs to be at least the size of the VBMeta struct which
821 // is 256 bytes.
822 if (buf->sz < 256) {
823 return;
824 }
825
Elliott Hughes855cdf82018-04-02 14:24:03 -0700826 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400827
828 std::string data;
829 if (!android::base::ReadFdToString(buf->fd, &data)) {
830 die("Failed reading from vbmeta");
831 }
832
833 // There's a 32-bit big endian |flags| field at offset 120 where
834 // bit 0 corresponds to disable-verity and bit 1 corresponds to
835 // disable-verification.
836 //
837 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
838 // the VBMeta struct.
839 if (g_disable_verity) {
840 data[123] |= 0x01;
841 }
842 if (g_disable_verification) {
843 data[123] |= 0x02;
844 }
845
846 if (!android::base::WriteStringToFd(data, fd)) {
847 die("Failed writing to modified vbmeta");
848 }
849 close(buf->fd);
850 buf->fd = fd;
851 lseek(fd, 0, SEEK_SET);
852}
853
Elliott Hughes5620d222018-03-28 08:20:00 -0700854static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700856 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700857
David Zeuthenb6ea4352017-08-07 14:29:26 -0400858 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
859 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700860 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400861 rewrite_vbmeta_buffer(buf);
862 }
863
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800865 case FB_BUFFER_SPARSE: {
866 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700867 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700869 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800870 sparse_files.emplace_back(*s, sz);
871 ++s;
872 }
873
874 for (size_t i = 0; i < sparse_files.size(); ++i) {
875 const auto& pair = sparse_files[i];
Tom Cherry11f12092018-08-29 21:36:28 -0700876 fb_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700877 }
878 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800879 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500880 case FB_BUFFER_FD:
Tom Cherry11f12092018-08-29 21:36:28 -0700881 fb_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882 break;
883 default:
884 die("unknown buffer type: %d", buf->type);
885 }
886}
887
Aaron Wisnerdb511202018-06-26 15:38:35 -0500888static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700889 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500890 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700891 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700892}
893
Aaron Wisnerdb511202018-06-26 15:38:35 -0500894static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700895 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700896 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500897 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700898 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700899 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700900 return count;
901}
902
Aaron Wisnerdb511202018-06-26 15:38:35 -0500903static bool supports_AB() {
904 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700905}
906
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700907// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700908static std::string get_other_slot(const std::string& current_slot, int count) {
909 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700910
Daniel Rosenberg80919472016-06-30 19:25:31 -0700911 char next = (current_slot[0] - 'a' + 1)%count + 'a';
912 return std::string(1, next);
913}
914
Aaron Wisnerdb511202018-06-26 15:38:35 -0500915static std::string get_other_slot(const std::string& current_slot) {
916 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700917}
918
Aaron Wisnerdb511202018-06-26 15:38:35 -0500919static std::string get_other_slot(int count) {
920 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700921}
922
Aaron Wisnerdb511202018-06-26 15:38:35 -0500923static std::string get_other_slot() {
924 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700925}
926
Aaron Wisnerdb511202018-06-26 15:38:35 -0500927static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700928 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700929 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800930 if (allow_all) {
931 return "all";
932 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500933 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700934 if (count > 0) {
935 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800936 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700937 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800938 }
939 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700940 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800941
Aaron Wisnerdb511202018-06-26 15:38:35 -0500942 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700943 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800944
Daniel Rosenberg80919472016-06-30 19:25:31 -0700945 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500946 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700947 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700948 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800949 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700950 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800951 }
952
Daniel Rosenberg80919472016-06-30 19:25:31 -0700953 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
954
955 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
956 for (int i=0; i<count; i++) {
957 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700958 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700959
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700960 exit(1);
961}
962
Aaron Wisnerdb511202018-06-26 15:38:35 -0500963static std::string verify_slot(const std::string& slot) {
964 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800965}
966
Aaron Wisnerdb511202018-06-26 15:38:35 -0500967static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700968 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800969 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700970 std::string current_slot;
971
Aaron Wisnerdb511202018-06-26 15:38:35 -0500972 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800973 /* If has-slot is not supported, the answer is no. */
974 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700975 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800976 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500978 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700979 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700980 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700981 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700983 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700984 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 }
986 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700987 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700988 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 }
991 func(part);
992 }
993}
994
David Pursell0b156632015-10-30 11:22:01 -0700995/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
996 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
997 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
998 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001000static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001001 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001002 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001003
Daniel Rosenberg80919472016-06-30 19:25:31 -07001004 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001005 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001006 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001008 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001009 for (int i=0; i < get_slot_count(); i++) {
1010 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001011 }
1012 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001013 do_for_partition(part, "", 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, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017 }
1018}
1019
Aaron Wisnerdb511202018-06-26 15:38:35 -05001020static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001021 struct fastboot_buffer buf;
1022
Aaron Wisnerdb511202018-06-26 15:38:35 -05001023 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001024 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001025 }
1026 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001027}
1028
Daniel Rosenberg13454092016-06-27 19:43:11 -07001029// Sets slot_override as the active slot. If slot_override is blank,
1030// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001031static void set_active(const std::string& slot_override) {
1032 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001033
Daniel Rosenberg80919472016-06-30 19:25:31 -07001034 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001035 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001036 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001037 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001039 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001040 }
1041 }
1042}
1043
David Anderson32e376f2018-08-16 13:43:11 -07001044static bool is_userspace_fastboot() {
1045 std::string value;
1046 return fb_getvar("is-userspace", &value) && value == "yes";
1047}
1048
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001049static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1050 std::string has_slot;
1051 std::string partition_name = partition;
1052
1053 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1054 if (slot == "") {
1055 std::string current_slot = get_current_slot();
1056 if (current_slot == "") {
1057 die("Failed to identify current slot");
1058 }
1059 partition_name += "_" + current_slot;
1060 } else {
1061 partition_name += "_" + slot;
1062 }
1063 }
1064 std::string partition_size;
1065 return fb_getvar("partition-size:" + partition_name, &partition_size);
1066}
1067
David Anderson32e376f2018-08-16 13:43:11 -07001068static bool is_logical(const std::string& partition) {
1069 std::string value;
1070 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1071}
1072
David Anderson1d887432018-08-27 16:47:32 -07001073static void reboot_to_userspace_fastboot() {
1074 if (!fb_reboot_to_userspace()) {
1075 die("Must reboot to userspace fastboot to flash logical partitions");
1076 }
1077
1078 // Give the current connection time to close.
1079 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1080
1081 fb_reinit(open_device());
1082}
1083
David Andersoncf444f32018-08-29 14:15:49 -07001084class ImageSource {
1085 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001086 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001087 virtual int OpenFile(const std::string& name) const = 0;
1088};
David Anderson32e376f2018-08-16 13:43:11 -07001089
David Andersoncf444f32018-08-29 14:15:49 -07001090class FlashAllTool {
1091 public:
1092 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001093
David Andersoncf444f32018-08-29 14:15:49 -07001094 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001095
David Andersoncf444f32018-08-29 14:15:49 -07001096 private:
1097 void CheckRequirements();
1098 void DetermineSecondarySlot();
1099 void CollectImages();
1100 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1101 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1102 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001103
David Andersoncf444f32018-08-29 14:15:49 -07001104 const ImageSource& source_;
1105 std::string slot_override_;
1106 bool skip_secondary_;
1107 bool wipe_;
1108 std::string secondary_slot_;
1109 std::vector<std::pair<const Image*, std::string>> boot_images_;
1110 std::vector<std::pair<const Image*, std::string>> os_images_;
1111};
1112
1113FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1114 : source_(source),
1115 slot_override_(slot_override),
1116 skip_secondary_(skip_secondary),
1117 wipe_(wipe)
1118{
David Anderson32e376f2018-08-16 13:43:11 -07001119}
1120
David Andersoncf444f32018-08-29 14:15:49 -07001121void FlashAllTool::Flash() {
1122 CheckRequirements();
1123 DetermineSecondarySlot();
1124 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001125
David Anderson95d40932018-08-28 12:14:33 -07001126 // First flash boot partitions. We allow this to happen either in userspace
1127 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001128 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001129
1130 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001131 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001132
1133 // Resize any logical partition to 0, so each partition is reset to 0
1134 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001135 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001136 auto resize_partition = [](const std::string& partition) -> void {
1137 if (is_logical(partition)) {
Tom Cherry11f12092018-08-29 21:36:28 -07001138 fb_resize_partition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001139 }
1140 };
David Anderson95d40932018-08-28 12:14:33 -07001141 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001142 }
1143
David Anderson95d40932018-08-28 12:14:33 -07001144 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001145 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001146
David Andersoncf444f32018-08-29 14:15:49 -07001147 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001148 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001149 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001150 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001152}
1153
David Andersoncf444f32018-08-29 14:15:49 -07001154void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001155 std::vector<char> contents;
1156 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001157 die("could not read android-info.txt");
1158 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001159 check_requirements(reinterpret_cast<char*>(contents.data()), contents.size());
David Andersoncf444f32018-08-29 14:15:49 -07001160}
1161
1162void FlashAllTool::DetermineSecondarySlot() {
1163 if (skip_secondary_) {
1164 return;
1165 }
1166 if (slot_override_ != "") {
1167 secondary_slot_ = get_other_slot(slot_override_);
1168 } else {
1169 secondary_slot_ = get_other_slot();
1170 }
1171 if (secondary_slot_ == "") {
1172 if (supports_AB()) {
1173 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1174 }
1175 skip_secondary_ = true;
1176 }
1177}
1178
1179void FlashAllTool::CollectImages() {
1180 for (size_t i = 0; i < arraysize(images); ++i) {
1181 std::string slot = slot_override_;
1182 if (images[i].IsSecondary()) {
1183 if (skip_secondary_) {
1184 continue;
1185 }
1186 slot = secondary_slot_;
1187 }
1188 if (images[i].type == ImageType::BootCritical) {
1189 boot_images_.emplace_back(&images[i], slot);
1190 } else if (images[i].type == ImageType::Normal) {
1191 os_images_.emplace_back(&images[i], slot);
1192 }
1193 }
1194}
1195
1196void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1197 for (const auto& [image, slot] : images) {
1198 fastboot_buffer buf;
1199 int fd = source_.OpenFile(image->img_name);
1200 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1201 if (image->optional_if_no_image) {
1202 continue;
1203 }
1204 die("could not load '%s': %s", image->img_name, strerror(errno));
1205 }
1206 FlashImage(*image, slot, &buf);
1207 }
1208}
1209
1210void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1211 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001212 std::vector<char> signature_data;
1213 if (source_.ReadFile(image.sig_name, &signature_data)) {
1214 fb_download("signature", signature_data);
Tom Cherry11f12092018-08-29 21:36:28 -07001215 fb_command("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001216 }
1217
1218 if (is_logical(partition_name)) {
Tom Cherry11f12092018-08-29 21:36:28 -07001219 fb_resize_partition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001220 }
1221 flash_buf(partition_name.c_str(), buf);
1222 };
1223 do_for_partitions(image.part_name, slot, flash, false);
1224}
1225
1226void FlashAllTool::UpdateSuperPartition() {
1227 if (!if_partition_exists("super", "")) {
1228 return;
1229 }
1230
1231 int fd = source_.OpenFile("super_empty.img");
1232 if (fd < 0) {
1233 return;
1234 }
1235 if (!is_userspace_fastboot()) {
1236 reboot_to_userspace_fastboot();
1237 }
Tom Cherry11f12092018-08-29 21:36:28 -07001238 fb_download_fd("super", fd, get_file_size(fd));
David Andersoncf444f32018-08-29 14:15:49 -07001239
1240 std::string command = "update-super:super";
1241 if (wipe_) {
1242 command += ":wipe";
1243 }
Tom Cherry11f12092018-08-29 21:36:28 -07001244 fb_command(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001245}
1246
1247class ZipImageSource final : public ImageSource {
1248 public:
1249 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001250 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001251 int OpenFile(const std::string& name) const override;
1252
1253 private:
1254 ZipArchiveHandle zip_;
1255};
1256
Tom Cherrydfd85df2018-09-20 14:45:05 -07001257bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1258 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001259}
1260
1261int ZipImageSource::OpenFile(const std::string& name) const {
1262 return unzip_to_file(zip_, name.c_str());
1263}
1264
1265static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
Tom Cherry11f12092018-08-29 21:36:28 -07001266 dump_info();
David Andersoncf444f32018-08-29 14:15:49 -07001267
Tom Cherry11f12092018-08-29 21:36:28 -07001268 fb_query_save("product", cur_product, sizeof(cur_product));
David Andersoncf444f32018-08-29 14:15:49 -07001269
1270 ZipArchiveHandle zip;
1271 int error = OpenArchive(filename, &zip);
1272 if (error != 0) {
1273 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1274 }
1275
1276 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1277 tool.Flash();
1278
1279 CloseArchive(zip);
1280}
1281
1282class LocalImageSource final : public ImageSource {
1283 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001284 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001285 int OpenFile(const std::string& name) const override;
1286};
1287
Tom Cherrydfd85df2018-09-20 14:45:05 -07001288bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001289 auto path = find_item_given_name(name);
1290 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001291 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001292 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001293 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001294}
1295
1296int LocalImageSource::OpenFile(const std::string& name) const {
1297 auto path = find_item_given_name(name);
1298 return open(path.c_str(), O_RDONLY);
1299}
1300
1301static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
1302 std::string fname;
Tom Cherry11f12092018-08-29 21:36:28 -07001303 dump_info();
David Andersoncf444f32018-08-29 14:15:49 -07001304
Tom Cherry11f12092018-08-29 21:36:28 -07001305 fb_query_save("product", cur_product, sizeof(cur_product));
David Andersoncf444f32018-08-29 14:15:49 -07001306
1307 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1308 tool.Flash();
1309}
1310
Elliott Hughesd6365a72017-05-08 18:04:49 -07001311static std::string next_arg(std::vector<std::string>* args) {
1312 if (args->empty()) syntax_error("expected argument");
1313 std::string result = args->front();
1314 args->erase(args->begin());
1315 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001316}
1317
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001318static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001319 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001320
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001321 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001322 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001323 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001324 }
Tom Cherry11f12092018-08-29 21:36:28 -07001325 fb_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001326}
1327
Connor O'Brience16a8a2017-02-06 14:39:31 -08001328static std::string fb_fix_numeric_var(std::string var) {
1329 // Some bootloaders (angler, for example), send spurious leading whitespace.
1330 var = android::base::Trim(var);
1331 // Some bootloaders (hammerhead, for example) use implicit hex.
1332 // This code used to use strtol with base 16.
1333 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1334 return var;
1335}
1336
Aaron Wisnerdb511202018-06-26 15:38:35 -05001337static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001338 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001339 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001340 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001341 return 0;
1342 }
1343 sizeString = fb_fix_numeric_var(sizeString);
1344
1345 unsigned size;
1346 if (!android::base::ParseUint(sizeString, &size)) {
1347 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1348 return 0;
1349 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001350 if ((size & (size - 1)) != 0) {
1351 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001352 return 0;
1353 }
1354 return size;
1355}
1356
Aaron Wisnerdb511202018-06-26 15:38:35 -05001357static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001358 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001359 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001360 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001361 std::string partition_type, partition_size;
1362
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001363 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 const char* errMsg = nullptr;
1365 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001366 TemporaryFile output;
1367 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001368
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001369 unsigned int limit = INT_MAX;
1370 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001371 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001372 }
1373 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001374 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001375 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001376
Aaron Wisnerdb511202018-06-26 15:38:35 -05001377 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001378 errMsg = "Can't determine partition type.\n";
1379 goto failed;
1380 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001381 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001382 if (partition_type != type_override) {
1383 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001384 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001385 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001386 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001387 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001388
Aaron Wisnerdb511202018-06-26 15:38:35 -05001389 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390 errMsg = "Unable to get partition size\n";
1391 goto failed;
1392 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001393 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001394 if (partition_size != size_override) {
1395 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001396 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001397 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001399 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001400 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001401
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001402 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001403 if (!gen) {
1404 if (skip_if_not_supported) {
1405 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001406 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001407 return;
1408 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001409 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1410 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411 return;
1412 }
1413
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001415 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001416 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1417 return;
1418 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001419
Connor O'Brience16a8a2017-02-06 14:39:31 -08001420 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001421 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1422 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001423
Jin Qian4a335822017-04-18 16:23:18 -07001424 if (fs_generator_generate(gen, output.path, size, initial_dir,
1425 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001426 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001427 return;
1428 }
1429
Jin Qian4a335822017-04-18 16:23:18 -07001430 fd.reset(open(output.path, O_RDONLY));
1431 if (fd == -1) {
1432 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1433 return;
1434 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001435 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001436 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001437 return;
1438 }
1439 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440 return;
1441
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001442failed:
1443 if (skip_if_not_supported) {
1444 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001445 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001447 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001448}
1449
Aaron Wisnerdb511202018-06-26 15:38:35 -05001450int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001451 bool wants_wipe = false;
1452 bool wants_reboot = false;
1453 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001454 bool wants_reboot_recovery = false;
1455 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001456 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001457 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001458 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001459 bool set_fbe_marker = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001460 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001461 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001462 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001463
Elliott Hughes577e8b42018-04-05 16:12:47 -07001464 g_boot_img_hdr.kernel_addr = 0x00008000;
1465 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1466 g_boot_img_hdr.second_addr = 0x00f00000;
1467 g_boot_img_hdr.tags_addr = 0x00000100;
1468 g_boot_img_hdr.page_size = 2048;
1469
JP Abgrall7b8970c2013-03-07 17:06:41 -08001470 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001471 {"base", required_argument, 0, 0},
1472 {"cmdline", required_argument, 0, 0},
1473 {"disable-verification", no_argument, 0, 0},
1474 {"disable-verity", no_argument, 0, 0},
1475 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001476 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001477 {"kernel-offset", required_argument, 0, 0},
1478 {"os-patch-level", required_argument, 0, 0},
1479 {"os-version", required_argument, 0, 0},
1480 {"page-size", required_argument, 0, 0},
1481 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001482 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001483 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001484 {"skip-secondary", no_argument, 0, 0},
1485 {"slot", required_argument, 0, 0},
1486 {"tags-offset", required_argument, 0, 0},
1487 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001488 {"verbose", no_argument, 0, 'v'},
1489 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001490#if !defined(_WIN32)
1491 {"wipe-and-use-fbe", no_argument, 0, 0},
1492#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001493 {0, 0, 0, 0}
1494 };
Colin Cross8879f982012-05-22 17:53:34 -07001495
1496 serial = getenv("ANDROID_SERIAL");
1497
Elliott Hughes577e8b42018-04-05 16:12:47 -07001498 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001499 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001500 if (c == 0) {
1501 std::string name{longopts[longindex].name};
1502 if (name == "base") {
1503 g_base_addr = strtoul(optarg, 0, 16);
1504 } else if (name == "cmdline") {
1505 g_cmdline = optarg;
1506 } else if (name == "disable-verification") {
1507 g_disable_verification = true;
1508 } else if (name == "disable-verity") {
1509 g_disable_verity = true;
1510 } else if (name == "header-version") {
1511 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1512 } else if (name == "kernel-offset") {
1513 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1514 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001515 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001516 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001517 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001518 } else if (name == "page-size") {
1519 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1520 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1521 } else if (name == "ramdisk-offset") {
1522 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1523 } else if (name == "skip-reboot") {
1524 skip_reboot = true;
1525 } else if (name == "skip-secondary") {
1526 skip_secondary = true;
1527 } else if (name == "slot") {
1528 slot_override = optarg;
1529 } else if (name == "tags-offset") {
1530 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1531 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001532 setvbuf(stdout, nullptr, _IONBF, 0);
1533 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001534 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001535 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001536 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001537 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001538#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001539 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001540 wants_wipe = true;
1541 set_fbe_marker = true;
1542#endif
1543 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001544 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001545 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001546 } else {
1547 switch (c) {
1548 case 'a':
1549 wants_set_active = true;
1550 if (optarg) next_active = optarg;
1551 break;
1552 case 'h':
1553 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001554 case 'l':
1555 g_long_listing = true;
1556 break;
1557 case 's':
1558 serial = optarg;
1559 break;
1560 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001561 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1562 die("invalid sparse limit %s", optarg);
1563 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001564 break;
1565 case 'v':
1566 set_verbose();
1567 break;
1568 case 'w':
1569 wants_wipe = true;
1570 break;
1571 case '?':
1572 return 1;
1573 default:
1574 abort();
1575 }
Colin Cross8879f982012-05-22 17:53:34 -07001576 }
1577 }
1578
1579 argc -= optind;
1580 argv += optind;
1581
Elliott Hughesd6365a72017-05-08 18:04:49 -07001582 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001583
Colin Cross8fb6e062012-07-24 16:36:41 -07001584 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001585 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001586 return 0;
1587 }
1588
Colin Crossc7b75dc2012-08-29 18:17:06 -07001589 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001590 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001591 }
1592
David Pursell0b156632015-10-30 11:22:01 -07001593 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001594 if (transport == nullptr) {
1595 return 1;
1596 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001597 fastboot::FastBootDriver fb(transport);
1598 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001599
Elliott Hughes5620d222018-03-28 08:20:00 -07001600 const double start = now();
1601
Aaron Wisnerdb511202018-06-26 15:38:35 -05001602 if (slot_override != "") slot_override = verify_slot(slot_override);
1603 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001604
1605 if (wants_set_active) {
1606 if (next_active == "") {
1607 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001608 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001609 if (fb_getvar("current-slot", &current_slot)) {
1610 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001611 } else {
1612 wants_set_active = false;
1613 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001614 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001615 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001616 }
1617 }
1618 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001619
Elliott Hughesd6365a72017-05-08 18:04:49 -07001620 std::vector<std::string> args(argv, argv + argc);
1621 while (!args.empty()) {
1622 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001623
Elliott Hughesd6365a72017-05-08 18:04:49 -07001624 if (command == "getvar") {
1625 std::string variable = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001626 fb_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001627 } else if (command == "erase") {
1628 std::string partition = next_arg(&args);
1629 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001630 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001631 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001632 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001633 fs_get_generator(partition_type) != nullptr) {
1634 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1635 partition_type.c_str());
1636 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001637
Tom Cherry11f12092018-08-29 21:36:28 -07001638 fb_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001639 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001640 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001641 } else if (android::base::StartsWith(command, "format")) {
1642 // Parsing for: "format[:[type][:[size]]]"
1643 // Some valid things:
1644 // - select only the size, and leave default fs type:
1645 // format::0x4000000 userdata
1646 // - default fs type and size:
1647 // format userdata
1648 // format:: userdata
1649 std::vector<std::string> pieces = android::base::Split(command, ":");
1650 std::string type_override;
1651 if (pieces.size() > 1) type_override = pieces[1].c_str();
1652 std::string size_override;
1653 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001654
Elliott Hughesd6365a72017-05-08 18:04:49 -07001655 std::string partition = next_arg(&args);
1656
1657 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001658 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001659 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001660 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001661 } else if (command == "signature") {
1662 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001663 std::vector<char> data;
1664 if (!ReadFileToVector(filename, &data)) {
1665 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1666 }
1667 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
1668 fb_download("signature", data);
Tom Cherry11f12092018-08-29 21:36:28 -07001669 fb_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001670 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001671 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001672
1673 if (args.size() == 1) {
1674 std::string what = next_arg(&args);
1675 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001676 wants_reboot = false;
1677 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001678 } else if (what == "recovery") {
1679 wants_reboot = false;
1680 wants_reboot_recovery = true;
1681 } else if (what == "fastboot") {
1682 wants_reboot = false;
1683 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001684 } else {
1685 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001686 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001687
Elliott Hughesca85df02015-02-25 10:02:00 -08001688 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001689 if (!args.empty()) syntax_error("junk after reboot command");
1690 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001691 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001692 } else if (command == "reboot-recovery") {
1693 wants_reboot_recovery = true;
1694 } else if (command == "reboot-fastboot") {
1695 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001696 } else if (command == "continue") {
Tom Cherry11f12092018-08-29 21:36:28 -07001697 fb_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001698 } else if (command == "boot") {
1699 std::string kernel = next_arg(&args);
1700 std::string ramdisk;
1701 if (!args.empty()) ramdisk = next_arg(&args);
1702 std::string second_stage;
1703 if (!args.empty()) second_stage = next_arg(&args);
1704
Tom Cherrydfd85df2018-09-20 14:45:05 -07001705 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1706 fb_download("boot.img", data);
Tom Cherry11f12092018-08-29 21:36:28 -07001707 fb_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001708 } else if (command == "flash") {
1709 std::string pname = next_arg(&args);
1710
Elliott Hughes2810d002016-04-25 14:31:18 -07001711 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001712 if (!args.empty()) {
1713 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001714 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001715 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001716 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001717 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001718
1719 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001720 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001721 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001722 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001723 } else if (command == "flash:raw") {
1724 std::string partition = next_arg(&args);
1725 std::string kernel = next_arg(&args);
1726 std::string ramdisk;
1727 if (!args.empty()) ramdisk = next_arg(&args);
1728 std::string second_stage;
1729 if (!args.empty()) second_stage = next_arg(&args);
1730
Tom Cherrydfd85df2018-09-20 14:45:05 -07001731 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1732 auto flashraw = [&data](const std::string& partition) {
1733 fb_flash(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001734 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001735 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001736 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001737 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001738 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001739 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001740 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001741 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001742 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001743 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001744 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001745 bool slot_all = (slot_override == "all");
1746 if (slot_all) {
1747 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1748 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001749 std::string filename = "update.zip";
1750 if (!args.empty()) {
1751 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001752 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001753 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001754 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001755 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001756 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001757 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001758 } else if (command == "stage") {
1759 std::string filename = next_arg(&args);
1760
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001761 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001762 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001763 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001764 }
Tom Cherry11f12092018-08-29 21:36:28 -07001765 fb_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001766 } else if (command == "get_staged") {
1767 std::string filename = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001768 fb_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001769 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001770 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001771 } else if (command == "flashing") {
1772 if (args.empty()) {
1773 syntax_error("missing 'flashing' command");
1774 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1775 args[0] == "unlock_critical" ||
1776 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001777 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001778 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001779 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001780 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001781 }
David Anderson0d4277d2018-07-31 13:27:37 -07001782 } else if (command == "create-logical-partition") {
1783 std::string partition = next_arg(&args);
1784 std::string size = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001785 fb_create_partition(partition, size);
David Anderson0d4277d2018-07-31 13:27:37 -07001786 } else if (command == "delete-logical-partition") {
1787 std::string partition = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001788 fb_delete_partition(partition);
David Anderson0d4277d2018-07-31 13:27:37 -07001789 } else if (command == "resize-logical-partition") {
1790 std::string partition = next_arg(&args);
1791 std::string size = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001792 fb_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001793 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001794 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001795 }
1796 }
1797
1798 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001799 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1800 for (const auto& partition : partitions) {
1801 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001802 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001803 if (partition_type.empty()) continue;
Tom Cherry11f12092018-08-29 21:36:28 -07001804 fb_erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001805 if (partition == "userdata" && set_fbe_marker) {
1806 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1807 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001808 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001809 delete_fbemarker_tmpdir(initial_userdata_dir);
1810 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001811 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001812 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001813 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001814 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001815 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001816 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001817 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001818 if (wants_reboot && !skip_reboot) {
Tom Cherry11f12092018-08-29 21:36:28 -07001819 fb_reboot();
1820 fb_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001821 } else if (wants_reboot_bootloader) {
Tom Cherry11f12092018-08-29 21:36:28 -07001822 fb_command("reboot-bootloader", "rebooting into bootloader");
1823 fb_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001824 } else if (wants_reboot_recovery) {
Tom Cherry11f12092018-08-29 21:36:28 -07001825 fb_command("reboot-recovery", "rebooting into recovery");
1826 fb_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001827 } else if (wants_reboot_fastboot) {
Tom Cherry11f12092018-08-29 21:36:28 -07001828 fb_command("reboot-fastboot", "rebooting into fastboot");
1829 fb_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001830 }
1831
Elliott Hughes5620d222018-03-28 08:20:00 -07001832 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001833
1834 if (Transport* old_transport = fb.set_transport(nullptr)) {
1835 delete old_transport;
1836 }
Tom Cherry11f12092018-08-29 21:36:28 -07001837 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001838}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001839
Aaron Wisnerdb511202018-06-26 15:38:35 -05001840void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001841 unsigned year, month, day;
1842 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1843 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1844 }
1845 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1846 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1847 hdr->SetOsPatchLevel(year, month);
1848}
1849
Aaron Wisnerdb511202018-06-26 15:38:35 -05001850void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001851 unsigned major = 0, minor = 0, patch = 0;
1852 std::vector<std::string> versions = android::base::Split(arg, ".");
1853 if (versions.size() < 1 || versions.size() > 3 ||
1854 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1855 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1856 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1857 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1858 syntax_error("bad OS version: %s", arg);
1859 }
1860 hdr->SetOsVersion(major, minor, patch);
1861}