blob: 5173babebcc2f248fae253c1afdba5d66c94ae89 [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>
Tom Cherry4aa60b32018-09-05 15:11:44 -070046#include <regex>
Elliott Hughes290a2282016-11-14 17:08:47 -080047#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080048#include <utility>
49#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070050
Elliott Hughes82ff3152016-08-31 15:07:18 -070051#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070052#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080053#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080054#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070055#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080056#include <android-base/strings.h>
Jin Qian4a335822017-04-18 16:23:18 -070057#include <android-base/test_utils.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050058#include <android-base/unique_fd.h>
Dan Willemsenab971b52018-08-29 14:58:02 -070059#include <build/version.h>
60#include <platform_tools_version.h>
Colin Crossf8387882012-05-24 17:18:41 -070061#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070062#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063
Elliott Hughes253c18d2015-03-18 22:47:09 -070064#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080065#include "diagnose_usb.h"
Aaron Wisnerdb511202018-06-26 15:38:35 -050066#include "engine.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070067#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080068#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070069#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080070#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070071#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072
Tom Cherrydfd85df2018-09-20 14:45:05 -070073using android::base::ReadFully;
Tom Cherry4aa60b32018-09-05 15:11:44 -070074using android::base::Split;
75using android::base::Trim;
Chris Fries0ea946c2017-04-12 10:25:57 -050076using android::base::unique_fd;
77
Colin Crossf8387882012-05-24 17:18:41 -070078#ifndef O_BINARY
79#define O_BINARY 0
80#endif
81
David Pursell2ec418a2016-01-20 08:32:08 -080082static const char* serial = nullptr;
Elliott Hughes577e8b42018-04-05 16:12:47 -070083
Elliott Hughes577e8b42018-04-05 16:12:47 -070084static bool g_long_listing = false;
Chris Fries6a999712017-04-04 09:52:47 -050085// Don't resparse files in too-big chunks.
86// libsparse will support INT_MAX, but this results in large allocations, so
87// let's keep it at 1GB to avoid memory pressure on the host.
88static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Elliott Hughes542370d2018-04-19 19:49:44 -070089static uint64_t sparse_limit = 0;
Colin Crossf8387882012-05-24 17:18:41 -070090static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Elliott Hughes577e8b42018-04-05 16:12:47 -070092static unsigned g_base_addr = 0x10000000;
93static boot_img_hdr_v1 g_boot_img_hdr = {};
94static std::string g_cmdline;
JP Abgrall7b8970c2013-03-07 17:06:41 -080095
David Zeuthenb6ea4352017-08-07 14:29:26 -040096static bool g_disable_verity = false;
97static bool g_disable_verification = false;
98
Paul Crowley8f7f56e2015-11-27 09:29:37 +000099static const std::string convert_fbe_marker_filename("convert_fbe");
100
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500102 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700103 FB_BUFFER_SPARSE,
104};
105
106struct fastboot_buffer {
107 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700108 void* data;
109 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500110 int fd;
David Anderson32e376f2018-08-16 13:43:11 -0700111 int64_t image_size;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700112};
113
David Anderson0debda02018-08-28 13:54:03 -0700114enum class ImageType {
115 // Must be flashed for device to boot into the kernel.
116 BootCritical,
117 // Normal partition to be flashed during "flashall".
118 Normal,
119 // Partition that is never flashed during "flashall".
120 Extra
121};
122
David Anderson32e376f2018-08-16 13:43:11 -0700123struct Image {
Elliott Hughes45964a82017-05-03 22:43:23 -0700124 const char* nickname;
125 const char* img_name;
126 const char* sig_name;
127 const char* part_name;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700128 bool optional_if_no_image;
David Anderson0debda02018-08-28 13:54:03 -0700129 ImageType type;
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -0700130 bool IsSecondary() const { return nickname == nullptr; }
David Anderson32e376f2018-08-16 13:43:11 -0700131};
132
133static Image images[] = {
Dario Frenic7ea1af2018-05-25 16:07:19 +0100134 // clang-format off
David Anderson0debda02018-08-28 13:54:03 -0700135 { "boot", "boot.img", "boot.sig", "boot", false, ImageType::BootCritical },
136 { nullptr, "boot_other.img", "boot.sig", "boot", true, ImageType::Normal },
137 { "cache", "cache.img", "cache.sig", "cache", true, ImageType::Extra },
138 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, ImageType::BootCritical },
139 { "dts", "dt.img", "dt.sig", "dts", true, ImageType::BootCritical },
140 { "odm", "odm.img", "odm.sig", "odm", true, ImageType::Normal },
141 { "product", "product.img", "product.sig", "product", true, ImageType::Normal },
Dario Freniab5583b2018-08-17 01:01:25 +0100142 { "product_services",
143 "product_services.img",
144 "product_services.sig",
David Anderson8cdea7f2018-08-06 15:36:00 -0700145 "product_services",
David Anderson0debda02018-08-28 13:54:03 -0700146 true, ImageType::Normal },
147 { "recovery", "recovery.img", "recovery.sig", "recovery", true, ImageType::BootCritical },
148 { "super", "super.img", "super.sig", "super", true, ImageType::Extra },
149 { "system", "system.img", "system.sig", "system", false, ImageType::Normal },
150 { nullptr, "system_other.img", "system.sig", "system", true, ImageType::Normal },
151 { "userdata", "userdata.img", "userdata.sig", "userdata", true, ImageType::Extra },
152 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, ImageType::BootCritical },
David Anderson1109c922018-09-17 17:32:54 -0700153 { "vbmeta_mainline",
154 "vbmeta_mainline.img",
155 "vbmeta_mainline.sig",
156 "vbmeta_mainline",
157 true, ImageType::BootCritical },
David Anderson0debda02018-08-28 13:54:03 -0700158 { "vendor", "vendor.img", "vendor.sig", "vendor", true, ImageType::Normal },
159 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, ImageType::Normal },
Dario Frenic7ea1af2018-05-25 16:07:19 +0100160 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700161};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700162
David Anderson0debda02018-08-28 13:54:03 -0700163static std::string find_item_given_name(const std::string& img_name) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700164 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700165 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700166 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700167 }
David Anderson0debda02018-08-28 13:54:03 -0700168 return std::string(dir) + "/" + img_name;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700169}
170
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700171static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700172 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700173 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700174 return find_item_given_name(images[i].img_name);
175 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 }
177
Elliott Hughesd6365a72017-05-08 18:04:49 -0700178 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700179 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180}
181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182static int64_t get_file_size(int fd) {
183 struct stat sb;
Tom Cherrydfd85df2018-09-20 14:45:05 -0700184 if (fstat(fd, &sb) == -1) {
185 die("could not get file size");
186 }
187 return sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700188}
189
Tom Cherrydfd85df2018-09-20 14:45:05 -0700190bool ReadFileToVector(const std::string& file, std::vector<char>* out) {
191 out->clear();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Tom Cherrydfd85df2018-09-20 14:45:05 -0700193 unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC | O_BINARY)));
194 if (fd == -1) {
195 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700196 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197
Tom Cherrydfd85df2018-09-20 14:45:05 -0700198 out->resize(get_file_size(fd));
199 return ReadFully(fd, out->data(), out->size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700200}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Elliott Hughesfc797672015-04-07 20:12:50 -0700202static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700203 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
204 return -1;
205 }
206
Scott Anderson13081c62012-04-06 12:39:30 -0700207 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700209 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
210 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 return 0;
212}
213
Elliott Hughesfc797672015-04-07 20:12:50 -0700214static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800215 return match_fastboot_with_serial(info, serial);
216}
217
Elliott Hughesfc797672015-04-07 20:12:50 -0700218static int list_devices_callback(usb_ifc_info* info) {
219 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700223 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 if (!serial[0]) {
225 serial = "????????????";
226 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700227 // output compatible with "adb devices"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700228 if (!g_long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700230 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 printf("%-22s fastboot", serial.c_str());
232 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700233 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800234 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 }
236
237 return -1;
238}
239
David Pursell2ec418a2016-01-20 08:32:08 -0800240// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
241// a specific device, otherwise the first USB device found will be used.
242//
Elliott Hughes855cdf82018-04-02 14:24:03 -0700243// If |serial| is non-null but invalid, this exits.
David Pursell2ec418a2016-01-20 08:32:08 -0800244// Otherwise it blocks until the target is available.
245//
246// The returned Transport is a singleton, so multiple calls to this function will return the same
247// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700248static Transport* open_device() {
David Pursell2ec418a2016-01-20 08:32:08 -0800249 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800250
David Pursell4601c972016-02-05 15:35:09 -0800251 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800252 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800253 int port = 0;
254 if (serial != nullptr) {
255 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800256
David Pursell4601c972016-02-05 15:35:09 -0800257 if (android::base::StartsWith(serial, "tcp:")) {
258 protocol = Socket::Protocol::kTcp;
259 port = tcp::kDefaultPort;
260 net_address = serial + strlen("tcp:");
261 } else if (android::base::StartsWith(serial, "udp:")) {
262 protocol = Socket::Protocol::kUdp;
263 port = udp::kDefaultPort;
264 net_address = serial + strlen("udp:");
265 }
266
267 if (net_address != nullptr) {
268 std::string error;
269 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700270 die("invalid network address '%s': %s\n", net_address, error.c_str());
David Pursell4601c972016-02-05 15:35:09 -0800271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
David Anderson1d887432018-08-27 16:47:32 -0700275 Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800276 while (true) {
277 if (!host.empty()) {
278 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800279 if (protocol == Socket::Protocol::kTcp) {
280 transport = tcp::Connect(host, port, &error).release();
281 } else if (protocol == Socket::Protocol::kUdp) {
282 transport = udp::Connect(host, port, &error).release();
283 }
284
David Pursell2ec418a2016-01-20 08:32:08 -0800285 if (transport == nullptr && announce) {
286 fprintf(stderr, "error: %s\n", error.c_str());
287 }
288 } else {
289 transport = usb_open(match_fastboot);
290 }
291
292 if (transport != nullptr) {
293 return transport;
294 }
295
David Pursell0b156632015-10-30 11:22:01 -0700296 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800297 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700298 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800300 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301 }
302}
303
Elliott Hughesfc797672015-04-07 20:12:50 -0700304static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 // We don't actually open a USB device here,
306 // just getting our callback called so we can
307 // list all the connected devices.
308 usb_open(list_devices_callback);
309}
310
Elliott Hughesd6365a72017-05-08 18:04:49 -0700311static void syntax_error(const char* fmt, ...) {
312 fprintf(stderr, "fastboot: usage: ");
313
314 va_list ap;
315 va_start(ap, fmt);
316 vfprintf(stderr, fmt, ap);
317 va_end(ap);
318
319 fprintf(stderr, "\n");
320 exit(1);
321}
322
323static int show_help() {
324 // clang-format off
325 fprintf(stdout,
Elliott Hughes07fc6822018-05-24 16:36:05 -0700326// 1 2 3 4 5 6 7 8
327// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
Elliott Hughesb76188f2018-04-03 13:30:18 -0700328 "usage: fastboot [OPTION...] COMMAND...\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 "\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700330 "flashing:\n"
331 " update ZIP Flash all partitions from an update.zip package.\n"
332 " flashall Flash all partitions from $ANDROID_PRODUCT_OUT.\n"
333 " On A/B devices, flashed slot is set as active.\n"
334 " Secondary images may be flashed to inactive slot.\n"
Elliott Hughes07fc6822018-05-24 16:36:05 -0700335 " flash PARTITION [FILENAME] Flash given partition, using the image from\n"
336 " $ANDROID_PRODUCT_OUT if no filename is given.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700337 "\n"
338 "basics:\n"
339 " devices [-l] List devices in bootloader (-l: with device paths).\n"
340 " getvar NAME Display given bootloader variable.\n"
341 " reboot [bootloader] Reboot device.\n"
342 "\n"
343 "locking/unlocking:\n"
344 " flashing lock|unlock Lock/unlock partitions for flashing\n"
345 " flashing lock_critical|unlock_critical\n"
346 " Lock/unlock 'critical' bootloader partitions.\n"
347 " flashing get_unlock_ability\n"
348 " Check whether unlocking is allowed (1) or not(0).\n"
349 "\n"
350 "advanced:\n"
351 " erase PARTITION Erase a flash partition.\n"
352 " format[:FS_TYPE[:SIZE]] PARTITION\n"
353 " Format a flash partition.\n"
354 " set_active SLOT Set the active slot.\n"
355 " oem [COMMAND...] Execute OEM-specific command.\n"
356 "\n"
357 "boot image:\n"
358 " boot KERNEL [RAMDISK [SECOND]]\n"
359 " Download and boot kernel from RAM.\n"
360 " flash:raw PARTITION KERNEL [RAMDISK [SECOND]]\n"
361 " Create boot image and flash it.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700362 " --cmdline CMDLINE Override kernel command line.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700363 " --base ADDRESS Set kernel base address (default: 0x10000000).\n"
364 " --kernel-offset Set kernel offset (default: 0x00008000).\n"
365 " --ramdisk-offset Set ramdisk offset (default: 0x01000000).\n"
366 " --tags-offset Set tags offset (default: 0x00000100).\n"
367 " --page-size BYTES Set flash page size (default: 2048).\n"
368 " --header-version VERSION Set boot image header version.\n"
Elliott Hughes577e8b42018-04-05 16:12:47 -0700369 " --os-version MAJOR[.MINOR[.PATCH]]\n"
370 " Set boot image OS version (default: 0.0.0).\n"
371 " --os-patch-level YYYY-MM-DD\n"
372 " Set boot image OS security patch level.\n"
373 // TODO: still missing: `second_addr`, `name`, `id`, `recovery_dtbo_*`.
Elliott Hughesb76188f2018-04-03 13:30:18 -0700374 "\n"
Elliott Hughes2e9b7792018-04-04 13:36:44 -0700375 // TODO: what device(s) used this? is there any documentation?
Elliott Hughesb76188f2018-04-03 13:30:18 -0700376 //" continue Continue with autoboot.\n"
377 //"\n"
378 "Android Things:\n"
379 " stage IN_FILE Sends given file to stage for the next command.\n"
380 " 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 -0800381 "\n"
382 "options:\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700383 " -w Wipe userdata.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700384 " -s SERIAL Specify a USB device.\n"
385 " -s tcp|udp:HOST[:PORT] Specify a network device.\n"
Elliott Hughes542370d2018-04-19 19:49:44 -0700386 " -S SIZE[K|M|G] Break into sparse files no larger than SIZE.\n"
Elliott Hughesb76188f2018-04-03 13:30:18 -0700387 " --slot SLOT Use SLOT; 'all' for both slots, 'other' for\n"
388 " non-current slot (default: current active slot).\n"
389 " --set-active[=SLOT] Sets the active slot before rebooting.\n"
390 " --skip-secondary Don't flash secondary slots in flashall/update.\n"
391 " --skip-reboot Don't reboot device after flashing.\n"
392 " --disable-verity Sets disable-verity when flashing vbmeta.\n"
393 " --disable-verification Sets disable-verification when flashing vbmeta.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000394#if !defined(_WIN32)
Elliott Hughesb76188f2018-04-03 13:30:18 -0700395 " --wipe-and-use-fbe Enable file-based encryption, wiping userdata.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000396#endif
Elliott Hughesb76188f2018-04-03 13:30:18 -0700397 // TODO: remove --unbuffered?
398 " --unbuffered Don't buffer input or output.\n"
399 " --verbose, -v Verbose output.\n"
400 " --version Display version.\n"
401 " --help, -h Show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800402 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700403 // clang-format off
404 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000406
Tom Cherrydfd85df2018-09-20 14:45:05 -0700407static std::vector<char> LoadBootableImage(const std::string& kernel, const std::string& ramdisk,
408 const std::string& second_stage) {
409 std::vector<char> kernel_data;
410 if (!ReadFileToVector(kernel, &kernel_data)) {
411 die("cannot load '%s': %s", kernel.c_str(), strerror(errno));
412 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800413
Elliott Hughesfc797672015-04-07 20:12:50 -0700414 // Is this actually a boot image?
Tom Cherrydfd85df2018-09-20 14:45:05 -0700415 if (kernel_data.size() < sizeof(boot_img_hdr_v1)) {
Elliott Hughesaaa3b6b2018-01-18 16:08:24 -0800416 die("cannot load '%s': too short", kernel.c_str());
417 }
Tom Cherrydfd85df2018-09-20 14:45:05 -0700418 if (!memcmp(kernel_data.data(), BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughes577e8b42018-04-05 16:12:47 -0700419 if (!g_cmdline.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700420 bootimg_set_cmdline(reinterpret_cast<boot_img_hdr_v1*>(kernel_data.data()), g_cmdline);
Elliott Hughes577e8b42018-04-05 16:12:47 -0700421 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800422
Elliott Hughes4089d342017-10-27 14:21:12 -0700423 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800424
Tom Cherrydfd85df2018-09-20 14:45:05 -0700425 return kernel_data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426 }
427
Tom Cherrydfd85df2018-09-20 14:45:05 -0700428 std::vector<char> ramdisk_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700429 if (!ramdisk.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700430 if (!ReadFileToVector(ramdisk, &ramdisk_data)) {
431 die("cannot load '%s': %s", ramdisk.c_str(), strerror(errno));
432 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433 }
434
Tom Cherrydfd85df2018-09-20 14:45:05 -0700435 std::vector<char> second_stage_data;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700436 if (!second_stage.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700437 if (!ReadFileToVector(second_stage, &second_stage_data)) {
438 die("cannot load '%s': %s", second_stage.c_str(), strerror(errno));
439 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200440 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441 fprintf(stderr,"creating boot image...\n");
Elliott Hughesd6365a72017-05-08 18:04:49 -0700442
Tom Cherrydfd85df2018-09-20 14:45:05 -0700443 std::vector<char> out;
444 boot_img_hdr_v1* boot_image_data = mkbootimg(kernel_data, ramdisk_data, second_stage_data,
445 g_base_addr, g_boot_img_hdr, &out);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800446
Tom Cherrydfd85df2018-09-20 14:45:05 -0700447 if (!g_cmdline.empty()) bootimg_set_cmdline(boot_image_data, g_cmdline);
448 fprintf(stderr, "creating boot image - %zu bytes\n", out.size());
449
450 return out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451}
452
Tom Cherrydfd85df2018-09-20 14:45:05 -0700453static bool UnzipToMemory(ZipArchiveHandle zip, const std::string& entry_name,
454 std::vector<char>* out) {
455 ZipString zip_entry_name(entry_name.c_str());
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700456 ZipEntry zip_entry;
457 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Tom Cherrydfd85df2018-09-20 14:45:05 -0700458 fprintf(stderr, "archive does not contain '%s'\n", entry_name.c_str());
459 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 }
461
Tom Cherrydfd85df2018-09-20 14:45:05 -0700462 out->resize(zip_entry.uncompressed_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463
Tom Cherrydfd85df2018-09-20 14:45:05 -0700464 fprintf(stderr, "extracting %s (%zu MB) to RAM...\n", entry_name.c_str(),
465 out->size() / 1024 / 1024);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466
Tom Cherrydfd85df2018-09-20 14:45:05 -0700467 int error = ExtractToMemory(zip, &zip_entry, reinterpret_cast<uint8_t*>(out->data()),
468 out->size());
469 if (error != 0) die("failed to extract '%s': %s", entry_name.c_str(), ErrorCodeString(error));
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000470
Tom Cherrydfd85df2018-09-20 14:45:05 -0700471 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472}
473
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700474#if defined(_WIN32)
475
476// TODO: move this to somewhere it can be shared.
477
478#include <windows.h>
479
480// Windows' tmpfile(3) requires administrator rights because
481// it creates temporary files in the root directory.
482static FILE* win32_tmpfile() {
483 char temp_path[PATH_MAX];
484 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
485 if (nchars == 0 || nchars >= sizeof(temp_path)) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700486 die("GetTempPath failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700487 }
488
489 char filename[PATH_MAX];
490 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
Elliott Hughes4089d342017-10-27 14:21:12 -0700491 die("GetTempFileName failed, error %ld", GetLastError());
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700492 }
493
494 return fopen(filename, "w+bTD");
495}
496
497#define tmpfile win32_tmpfile
498
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000499static std::string make_temporary_directory() {
Elliott Hughes4089d342017-10-27 14:21:12 -0700500 die("make_temporary_directory not supported under Windows, sorry!");
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000501}
502
Elliott Hughes855cdf82018-04-02 14:24:03 -0700503static int make_temporary_fd(const char* /*what*/) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700504 // TODO: reimplement to avoid leaking a FILE*.
505 return fileno(tmpfile());
506}
507
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000508#else
509
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700510static std::string make_temporary_template() {
511 const char* tmpdir = getenv("TMPDIR");
512 if (tmpdir == nullptr) tmpdir = P_tmpdir;
513 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
514}
515
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000516static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700517 std::string result(make_temporary_template());
518 if (mkdtemp(&result[0]) == nullptr) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700519 die("unable to create temporary directory with template %s: %s",
520 result.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000521 }
522 return result;
523}
524
Elliott Hughes855cdf82018-04-02 14:24:03 -0700525static int make_temporary_fd(const char* what) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700526 std::string path_template(make_temporary_template());
527 int fd = mkstemp(&path_template[0]);
528 if (fd == -1) {
Elliott Hughesda1dbd62018-05-21 23:02:26 -0700529 die("failed to create temporary file for %s with template %s: %s\n",
530 path_template.c_str(), what, strerror(errno));
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700531 }
532 unlink(path_template.c_str());
533 return fd;
534}
535
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700536#endif
537
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000538static std::string create_fbemarker_tmpdir() {
539 std::string dir = make_temporary_directory();
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000540 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
541 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
542 if (fd == -1) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700543 die("unable to create FBE marker file %s locally: %s",
544 marker_file.c_str(), strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000545 }
546 close(fd);
547 return dir;
548}
549
550static void delete_fbemarker_tmpdir(const std::string& dir) {
551 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
552 if (unlink(marker_file.c_str()) == -1) {
553 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
554 marker_file.c_str(), errno, strerror(errno));
555 return;
556 }
557 if (rmdir(dir.c_str()) == -1) {
558 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
559 dir.c_str(), errno, strerror(errno));
560 return;
561 }
562}
563
Elliott Hughes45964a82017-05-03 22:43:23 -0700564static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughes855cdf82018-04-02 14:24:03 -0700565 unique_fd fd(make_temporary_fd(entry_name));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700566
Yusuke Sato07447542015-06-25 14:39:19 -0700567 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700568 ZipEntry zip_entry;
569 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
570 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
David Andersoncf444f32018-08-29 14:15:49 -0700571 errno = ENOENT;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000572 return -1;
573 }
574
Elliott Hughes23af1122017-11-10 08:42:17 -0800575 fprintf(stderr, "extracting %s (%" PRIu32 " MB) to disk...", entry_name,
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700576 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughes23af1122017-11-10 08:42:17 -0800577 double start = now();
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700578 int error = ExtractEntryToFile(zip, &zip_entry, fd);
579 if (error != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800580 die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700581 }
582
Elliott Hughes4089d342017-10-27 14:21:12 -0700583 if (lseek(fd, 0, SEEK_SET) != 0) {
Elliott Hughes23af1122017-11-10 08:42:17 -0800584 die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno));
Elliott Hughes4089d342017-10-27 14:21:12 -0700585 }
586
Elliott Hughes23af1122017-11-10 08:42:17 -0800587 fprintf(stderr, " took %.3fs\n", now() - start);
588
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700589 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700590}
591
Tom Cherry4aa60b32018-09-05 15:11:44 -0700592static void CheckRequirement(const std::string& cur_product, const std::string& var,
593 const std::string& product, bool invert,
594 const std::vector<std::string>& options) {
595 Status("Checking '" + var + "'");
Elliott Hughes5620d222018-03-28 08:20:00 -0700596
Tom Cherry4aa60b32018-09-05 15:11:44 -0700597 double start = now();
598
599 if (!product.empty()) {
600 if (product != cur_product) {
601 double split = now();
602 fprintf(stderr, "IGNORE, product is %s required only for %s [%7.3fs]\n",
603 cur_product.c_str(), product.c_str(), (split - start));
604 return;
605 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800606 }
Tom Cherry4aa60b32018-09-05 15:11:44 -0700607
608 std::string var_value;
609 if (!fb_getvar(var, &var_value)) {
610 fprintf(stderr, "FAILED\n\n");
611 fprintf(stderr, "Could not getvar for '%s' (%s)\n\n", var.c_str(), fb_get_error().c_str());
612 die("requirements not met!");
613 }
614
615 bool match = false;
616 for (const auto& option : options) {
617 if (option == var_value || (option.back() == '*' &&
618 !var_value.compare(0, option.length() - 1, option, 0,
619 option.length() - 1))) {
620 match = true;
621 break;
622 }
623 }
624
625 if (invert) {
626 match = !match;
627 }
628
629 if (match) {
630 double split = now();
631 fprintf(stderr, "OKAY [%7.3fs]\n", (split - start));
632 return;
633 }
634
635 fprintf(stderr, "FAILED\n\n");
636 fprintf(stderr, "Device %s is '%s'.\n", var.c_str(), var_value.c_str());
637 fprintf(stderr, "Update %s '%s'", invert ? "rejects" : "requires", options[0].c_str());
638 for (auto it = std::next(options.begin()); it != options.end(); ++it) {
639 fprintf(stderr, " or '%s'", it->c_str());
640 }
641 fprintf(stderr, ".\n\n");
642 die("requirements not met!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800643}
644
Tom Cherry4aa60b32018-09-05 15:11:44 -0700645bool ParseRequirementLine(const std::string& line, std::string* name, std::string* product,
646 bool* invert, std::vector<std::string>* options) {
Elliott Hughes5620d222018-03-28 08:20:00 -0700647 // "require product=alpha|beta|gamma"
648 // "require version-bootloader=1234"
649 // "require-for-product:gamma version-bootloader=istanbul|constantinople"
650 // "require partition-exists=vendor"
Tom Cherry4aa60b32018-09-05 15:11:44 -0700651 *product = "";
652 *invert = false;
Elliott Hughes5620d222018-03-28 08:20:00 -0700653
Tom Cherry4aa60b32018-09-05 15:11:44 -0700654 auto require_reject_regex = std::regex{"(require\\s+|reject\\s+)?\\s*(\\S+)\\s*=\\s*(.*)"};
655 auto require_product_regex =
656 std::regex{"require-for-product:\\s*(\\S+)\\s+(\\S+)\\s*=\\s*(.*)"};
657 std::smatch match_results;
658
659 if (std::regex_match(line, match_results, require_reject_regex)) {
660 *invert = Trim(match_results[1]) == "reject";
661 } else if (std::regex_match(line, match_results, require_product_regex)) {
662 *product = match_results[1];
663 } else {
664 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 }
666
Tom Cherry4aa60b32018-09-05 15:11:44 -0700667 *name = match_results[2];
Elliott Hughes253c18d2015-03-18 22:47:09 -0700668 // Work around an unfortunate name mismatch.
Tom Cherry4aa60b32018-09-05 15:11:44 -0700669 if (*name == "board") {
670 *name = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800671 }
672
Tom Cherry4aa60b32018-09-05 15:11:44 -0700673 auto raw_options = Split(match_results[3], "|");
674 for (const auto& option : raw_options) {
675 auto trimmed_option = Trim(option);
676 options->emplace_back(trimmed_option);
677 }
678
679 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680}
681
Tom Cherry4aa60b32018-09-05 15:11:44 -0700682// "require partition-exists=x" is a special case, added because of the trouble we had when
683// Pixel 2 shipped with new partitions and users used old versions of fastboot to flash them,
684// missing out new partitions. A device with new partitions can use "partition-exists" to
685// override the fields `optional_if_no_image` in the `images` array.
686static void HandlePartitionExists(const std::vector<std::string>& options) {
687 const std::string& partition_name = options[0];
688 std::string has_slot;
689 if (!fb_getvar("has-slot:" + partition_name, &has_slot) ||
690 (has_slot != "yes" && has_slot != "no")) {
691 die("device doesn't have required partition %s!", partition_name.c_str());
692 }
693 bool known_partition = false;
694 for (size_t i = 0; i < arraysize(images); ++i) {
695 if (images[i].nickname && images[i].nickname == partition_name) {
696 images[i].optional_if_no_image = false;
697 known_partition = true;
698 }
699 }
700 if (!known_partition) {
701 die("device requires partition %s which is not known to this version of fastboot",
702 partition_name.c_str());
703 }
704}
705
706static void CheckRequirements(const std::string& data) {
707 std::string cur_product;
708 if (!fb_getvar("product", &cur_product)) {
709 fprintf(stderr, "getvar:product FAILED (%s)\n", fb_get_error().c_str());
710 }
711
712 auto lines = Split(data, "\n");
713 for (const auto& line : lines) {
714 if (line.empty()) {
715 continue;
716 }
717
718 std::string name;
719 std::string product;
720 bool invert;
721 std::vector<std::string> options;
722
723 if (!ParseRequirementLine(line, &name, &product, &invert, &options)) {
724 fprintf(stderr, "android-info.txt syntax error: %s\n", line.c_str());
725 continue;
726 }
727 if (name == "partition-exists") {
728 HandlePartitionExists(options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800729 } else {
Tom Cherry4aa60b32018-09-05 15:11:44 -0700730 CheckRequirement(cur_product, name, product, invert, options);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 }
732 }
733}
734
Tom Cherry11f12092018-08-29 21:36:28 -0700735static void dump_info() {
736 fb_notice("--------------------------------------------");
737 fb_display("Bootloader Version...", "version-bootloader");
738 fb_display("Baseband Version.....", "version-baseband");
739 fb_display("Serial Number........", "serialno");
740 fb_notice("--------------------------------------------");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800741}
742
Tao Bao41cf35f2018-04-24 10:54:21 -0700743static struct sparse_file** load_sparse_files(int fd, int64_t max_size) {
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700744 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Elliott Hughes4089d342017-10-27 14:21:12 -0700745 if (!s) die("cannot sparse read file");
Colin Crossf8387882012-05-24 17:18:41 -0700746
Tao Bao41cf35f2018-04-24 10:54:21 -0700747 if (max_size <= 0 || max_size > std::numeric_limits<uint32_t>::max()) {
748 die("invalid max size %" PRId64, max_size);
749 }
750
Elliott Hughesfc797672015-04-07 20:12:50 -0700751 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Elliott Hughes4089d342017-10-27 14:21:12 -0700752 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700753
Elliott Hughes253c18d2015-03-18 22:47:09 -0700754 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Elliott Hughes4089d342017-10-27 14:21:12 -0700755 if (!out_s) die("Failed to allocate sparse file array");
Colin Crossf8387882012-05-24 17:18:41 -0700756
757 files = sparse_file_resparse(s, max_size, out_s, files);
Elliott Hughes4089d342017-10-27 14:21:12 -0700758 if (files < 0) die("Failed to resparse");
Colin Crossf8387882012-05-24 17:18:41 -0700759
760 return out_s;
761}
762
Aaron Wisnerdb511202018-06-26 15:38:35 -0500763static int64_t get_target_sparse_limit() {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700764 std::string max_download_size;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500765 if (!fb_getvar("max-download-size", &max_download_size) ||
Elliott Hughes855cdf82018-04-02 14:24:03 -0700766 max_download_size.empty()) {
767 verbose("target didn't report max-download-size");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700768 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700769 }
770
Elliott Hughes77c0e662015-11-02 15:51:12 -0800771 // Some bootloaders (angler, for example) send spurious whitespace too.
772 max_download_size = android::base::Trim(max_download_size);
773
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700774 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700775 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800776 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700777 return 0;
778 }
Elliott Hughes855cdf82018-04-02 14:24:03 -0700779 if (limit > 0) verbose("target reported max download size of %" PRId64 " bytes", limit);
Colin Crossf8387882012-05-24 17:18:41 -0700780 return limit;
781}
782
Aaron Wisnerdb511202018-06-26 15:38:35 -0500783static int64_t get_sparse_limit(int64_t size) {
Elliott Hughes542370d2018-04-19 19:49:44 -0700784 int64_t limit = sparse_limit;
785 if (limit == 0) {
786 // Unlimited, so see what the target device's limit is.
787 // TODO: shouldn't we apply this limit even if you've used -S?
Colin Crossf8387882012-05-24 17:18:41 -0700788 if (target_sparse_limit == -1) {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500789 target_sparse_limit = get_target_sparse_limit();
Colin Crossf8387882012-05-24 17:18:41 -0700790 }
791 if (target_sparse_limit > 0) {
792 limit = target_sparse_limit;
793 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700794 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700795 }
796 }
797
798 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500799 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700800 }
801
802 return 0;
803}
804
Aaron Wisnerdb511202018-06-26 15:38:35 -0500805static bool load_buf_fd(int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700806 int64_t sz = get_file_size(fd);
807 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700808 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700809 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700810
David Anderson32e376f2018-08-16 13:43:11 -0700811 if (sparse_file* s = sparse_file_import_auto(fd, false, false)) {
812 buf->image_size = sparse_file_len(s, false, false);
813 sparse_file_destroy(s);
814 } else {
815 buf->image_size = sz;
816 }
817
Elliott Hughesfc797672015-04-07 20:12:50 -0700818 lseek64(fd, 0, SEEK_SET);
Aaron Wisnerdb511202018-06-26 15:38:35 -0500819 int64_t limit = get_sparse_limit(sz);
Colin Crossf8387882012-05-24 17:18:41 -0700820 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700821 sparse_file** s = load_sparse_files(fd, limit);
822 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700823 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700824 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700825 buf->type = FB_BUFFER_SPARSE;
826 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700827 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500828 buf->type = FB_BUFFER_FD;
829 buf->data = nullptr;
830 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000831 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700832 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700833
Elliott Hughes53ec4952016-05-11 12:39:27 -0700834 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700835}
836
Aaron Wisnerdb511202018-06-26 15:38:35 -0500837static bool load_buf(const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500838 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
839
Elliott Hughes53ec4952016-05-11 12:39:27 -0700840 if (fd == -1) {
841 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700842 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500843
844 struct stat s;
845 if (fstat(fd, &s)) {
846 return false;
847 }
848 if (!S_ISREG(s.st_mode)) {
849 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
850 return false;
851 }
852
Aaron Wisnerdb511202018-06-26 15:38:35 -0500853 return load_buf_fd(fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700854}
855
David Zeuthenb6ea4352017-08-07 14:29:26 -0400856static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf) {
857 // Buffer needs to be at least the size of the VBMeta struct which
858 // is 256 bytes.
859 if (buf->sz < 256) {
860 return;
861 }
862
Elliott Hughes855cdf82018-04-02 14:24:03 -0700863 int fd = make_temporary_fd("vbmeta rewriting");
David Zeuthenb6ea4352017-08-07 14:29:26 -0400864
865 std::string data;
866 if (!android::base::ReadFdToString(buf->fd, &data)) {
867 die("Failed reading from vbmeta");
868 }
869
870 // There's a 32-bit big endian |flags| field at offset 120 where
871 // bit 0 corresponds to disable-verity and bit 1 corresponds to
872 // disable-verification.
873 //
874 // See external/avb/libavb/avb_vbmeta_image.h for the layout of
875 // the VBMeta struct.
876 if (g_disable_verity) {
877 data[123] |= 0x01;
878 }
879 if (g_disable_verification) {
880 data[123] |= 0x02;
881 }
882
883 if (!android::base::WriteStringToFd(data, fd)) {
884 die("Failed writing to modified vbmeta");
885 }
886 close(buf->fd);
887 buf->fd = fd;
888 lseek(fd, 0, SEEK_SET);
889}
890
Elliott Hughes5620d222018-03-28 08:20:00 -0700891static void flash_buf(const std::string& partition, struct fastboot_buffer *buf)
Rom Lemarchand622810c2013-06-28 09:54:59 -0700892{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700893 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700894
David Zeuthenb6ea4352017-08-07 14:29:26 -0400895 // Rewrite vbmeta if that's what we're flashing and modification has been requested.
896 if ((g_disable_verity || g_disable_verification) &&
Elliott Hughes5620d222018-03-28 08:20:00 -0700897 (partition == "vbmeta" || partition == "vbmeta_a" || partition == "vbmeta_b")) {
David Zeuthenb6ea4352017-08-07 14:29:26 -0400898 rewrite_vbmeta_buffer(buf);
899 }
900
Rom Lemarchand622810c2013-06-28 09:54:59 -0700901 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800902 case FB_BUFFER_SPARSE: {
903 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700904 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700905 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700906 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800907 sparse_files.emplace_back(*s, sz);
908 ++s;
909 }
910
911 for (size_t i = 0; i < sparse_files.size(); ++i) {
912 const auto& pair = sparse_files[i];
Tom Cherry11f12092018-08-29 21:36:28 -0700913 fb_flash_sparse(partition, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700914 }
915 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800916 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500917 case FB_BUFFER_FD:
Tom Cherry11f12092018-08-29 21:36:28 -0700918 fb_flash_fd(partition, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700919 break;
920 default:
921 die("unknown buffer type: %d", buf->type);
922 }
923}
924
Aaron Wisnerdb511202018-06-26 15:38:35 -0500925static std::string get_current_slot() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700926 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500927 if (!fb_getvar("current-slot", &current_slot)) return "";
Elliott Hughes42b18a52018-04-10 15:32:21 -0700928 return current_slot;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700929}
930
Aaron Wisnerdb511202018-06-26 15:38:35 -0500931static int get_slot_count() {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700932 std::string var;
Elliott Hughes42b18a52018-04-10 15:32:21 -0700933 int count = 0;
Aaron Wisnerdb511202018-06-26 15:38:35 -0500934 if (!fb_getvar("slot-count", &var) || !android::base::ParseInt(var, &count)) {
Elliott Hughes42b18a52018-04-10 15:32:21 -0700935 return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700937 return count;
938}
939
Aaron Wisnerdb511202018-06-26 15:38:35 -0500940static bool supports_AB() {
941 return get_slot_count() >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700942}
943
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700944// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700945static std::string get_other_slot(const std::string& current_slot, int count) {
946 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700947
Daniel Rosenberg80919472016-06-30 19:25:31 -0700948 char next = (current_slot[0] - 'a' + 1)%count + 'a';
949 return std::string(1, next);
950}
951
Aaron Wisnerdb511202018-06-26 15:38:35 -0500952static std::string get_other_slot(const std::string& current_slot) {
953 return get_other_slot(current_slot, get_slot_count());
Daniel Rosenberg80919472016-06-30 19:25:31 -0700954}
955
Aaron Wisnerdb511202018-06-26 15:38:35 -0500956static std::string get_other_slot(int count) {
957 return get_other_slot(get_current_slot(), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700958}
959
Aaron Wisnerdb511202018-06-26 15:38:35 -0500960static std::string get_other_slot() {
961 return get_other_slot(get_current_slot(), get_slot_count());
Alex Lightbb9b8a52016-06-29 09:26:44 -0700962}
963
Aaron Wisnerdb511202018-06-26 15:38:35 -0500964static std::string verify_slot(const std::string& slot_name, bool allow_all) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700965 std::string slot = slot_name;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700966 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800967 if (allow_all) {
968 return "all";
969 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500970 int count = get_slot_count();
Daniel Rosenberg80919472016-06-30 19:25:31 -0700971 if (count > 0) {
972 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800973 } else {
Elliott Hughes4089d342017-10-27 14:21:12 -0700974 die("No known slots");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800975 }
976 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700977 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800978
Aaron Wisnerdb511202018-06-26 15:38:35 -0500979 int count = get_slot_count();
Elliott Hughes4089d342017-10-27 14:21:12 -0700980 if (count == 0) die("Device does not support slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800981
Daniel Rosenberg80919472016-06-30 19:25:31 -0700982 if (slot == "other") {
Aaron Wisnerdb511202018-06-26 15:38:35 -0500983 std::string other = get_other_slot( count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700984 if (other == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -0700985 die("No known slots");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800986 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700987 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800988 }
989
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
991
992 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
993 for (int i=0; i<count; i++) {
994 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 exit(1);
998}
999
Aaron Wisnerdb511202018-06-26 15:38:35 -05001000static std::string verify_slot(const std::string& slot) {
1001 return verify_slot(slot, true);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001002}
1003
Aaron Wisnerdb511202018-06-26 15:38:35 -05001004static void do_for_partition(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001005 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001006 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001007 std::string current_slot;
1008
Aaron Wisnerdb511202018-06-26 15:38:35 -05001009 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001010 /* If has-slot is not supported, the answer is no. */
1011 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001012 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001013 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001014 if (slot == "") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001015 current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001016 if (current_slot == "") {
Elliott Hughes4089d342017-10-27 14:21:12 -07001017 die("Failed to identify current slot");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001018 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001019 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001020 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001021 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001022 }
1023 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001025 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001026 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001027 }
1028 func(part);
1029 }
1030}
1031
David Pursell0b156632015-10-30 11:22:01 -07001032/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1033 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1034 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1035 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001036 */
Aaron Wisnerdb511202018-06-26 15:38:35 -05001037static void do_for_partitions(const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001038 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001039 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001040
Daniel Rosenberg80919472016-06-30 19:25:31 -07001041 if (slot == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001042 if (!fb_getvar("has-slot:" + part, &has_slot)) {
Elliott Hughes4089d342017-10-27 14:21:12 -07001043 die("Could not check if partition %s has slot %s", part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001044 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001045 if (has_slot == "yes") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001046 for (int i=0; i < get_slot_count(); i++) {
1047 do_for_partition(part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001048 }
1049 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001050 do_for_partition(part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001051 }
1052 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001053 do_for_partition(part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001054 }
1055}
1056
Aaron Wisnerdb511202018-06-26 15:38:35 -05001057static void do_flash(const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001058 struct fastboot_buffer buf;
1059
Aaron Wisnerdb511202018-06-26 15:38:35 -05001060 if (!load_buf(fname, &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001061 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001062 }
1063 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001064}
1065
Daniel Rosenberg13454092016-06-27 19:43:11 -07001066// Sets slot_override as the active slot. If slot_override is blank,
1067// set current slot as active instead. This clears slot-unbootable.
Aaron Wisnerdb511202018-06-26 15:38:35 -05001068static void set_active(const std::string& slot_override) {
1069 if (!supports_AB()) return;
Elliott Hughes42b18a52018-04-10 15:32:21 -07001070
Daniel Rosenberg80919472016-06-30 19:25:31 -07001071 if (slot_override != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001072 fb_set_active(slot_override);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001073 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001074 std::string current_slot = get_current_slot();
Daniel Rosenberg80919472016-06-30 19:25:31 -07001075 if (current_slot != "") {
Elliott Hughesbd864202018-04-10 15:59:46 -07001076 fb_set_active(current_slot);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001077 }
1078 }
1079}
1080
David Anderson32e376f2018-08-16 13:43:11 -07001081static bool is_userspace_fastboot() {
1082 std::string value;
1083 return fb_getvar("is-userspace", &value) && value == "yes";
1084}
1085
Hridya Valsarajuf1f0a9c2018-08-02 10:46:21 -07001086static bool if_partition_exists(const std::string& partition, const std::string& slot) {
1087 std::string has_slot;
1088 std::string partition_name = partition;
1089
1090 if (fb_getvar("has-slot:" + partition, &has_slot) && has_slot == "yes") {
1091 if (slot == "") {
1092 std::string current_slot = get_current_slot();
1093 if (current_slot == "") {
1094 die("Failed to identify current slot");
1095 }
1096 partition_name += "_" + current_slot;
1097 } else {
1098 partition_name += "_" + slot;
1099 }
1100 }
1101 std::string partition_size;
1102 return fb_getvar("partition-size:" + partition_name, &partition_size);
1103}
1104
David Anderson32e376f2018-08-16 13:43:11 -07001105static bool is_logical(const std::string& partition) {
1106 std::string value;
1107 return fb_getvar("is-logical:" + partition, &value) && value == "yes";
1108}
1109
David Anderson1d887432018-08-27 16:47:32 -07001110static void reboot_to_userspace_fastboot() {
1111 if (!fb_reboot_to_userspace()) {
1112 die("Must reboot to userspace fastboot to flash logical partitions");
1113 }
1114
1115 // Give the current connection time to close.
1116 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
1117
1118 fb_reinit(open_device());
1119}
1120
David Andersoncf444f32018-08-29 14:15:49 -07001121class ImageSource {
1122 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001123 virtual bool ReadFile(const std::string& name, std::vector<char>* out) const = 0;
David Andersoncf444f32018-08-29 14:15:49 -07001124 virtual int OpenFile(const std::string& name) const = 0;
1125};
David Anderson32e376f2018-08-16 13:43:11 -07001126
David Andersoncf444f32018-08-29 14:15:49 -07001127class FlashAllTool {
1128 public:
1129 FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe);
David Anderson32e376f2018-08-16 13:43:11 -07001130
David Andersoncf444f32018-08-29 14:15:49 -07001131 void Flash();
David Anderson32e376f2018-08-16 13:43:11 -07001132
David Andersoncf444f32018-08-29 14:15:49 -07001133 private:
1134 void CheckRequirements();
1135 void DetermineSecondarySlot();
1136 void CollectImages();
1137 void FlashImages(const std::vector<std::pair<const Image*, std::string>>& images);
1138 void FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf);
1139 void UpdateSuperPartition();
David Anderson32e376f2018-08-16 13:43:11 -07001140
David Andersoncf444f32018-08-29 14:15:49 -07001141 const ImageSource& source_;
1142 std::string slot_override_;
1143 bool skip_secondary_;
1144 bool wipe_;
1145 std::string secondary_slot_;
1146 std::vector<std::pair<const Image*, std::string>> boot_images_;
1147 std::vector<std::pair<const Image*, std::string>> os_images_;
1148};
1149
1150FlashAllTool::FlashAllTool(const ImageSource& source, const std::string& slot_override, bool skip_secondary, bool wipe)
1151 : source_(source),
1152 slot_override_(slot_override),
1153 skip_secondary_(skip_secondary),
1154 wipe_(wipe)
1155{
David Anderson32e376f2018-08-16 13:43:11 -07001156}
1157
David Andersoncf444f32018-08-29 14:15:49 -07001158void FlashAllTool::Flash() {
1159 CheckRequirements();
1160 DetermineSecondarySlot();
1161 CollectImages();
David Anderson32e376f2018-08-16 13:43:11 -07001162
David Anderson95d40932018-08-28 12:14:33 -07001163 // First flash boot partitions. We allow this to happen either in userspace
1164 // or in bootloader fastboot.
David Andersoncf444f32018-08-29 14:15:49 -07001165 FlashImages(boot_images_);
David Anderson95d40932018-08-28 12:14:33 -07001166
1167 // Sync the super partition. This will reboot to userspace fastboot if needed.
David Andersoncf444f32018-08-29 14:15:49 -07001168 UpdateSuperPartition();
David Anderson95d40932018-08-28 12:14:33 -07001169
1170 // Resize any logical partition to 0, so each partition is reset to 0
1171 // extents, and will achieve more optimal allocation.
David Andersoncf444f32018-08-29 14:15:49 -07001172 for (const auto& [image, slot] : os_images_) {
David Anderson32e376f2018-08-16 13:43:11 -07001173 auto resize_partition = [](const std::string& partition) -> void {
1174 if (is_logical(partition)) {
Tom Cherry11f12092018-08-29 21:36:28 -07001175 fb_resize_partition(partition, "0");
David Anderson32e376f2018-08-16 13:43:11 -07001176 }
1177 };
David Anderson95d40932018-08-28 12:14:33 -07001178 do_for_partitions(image->part_name, slot, resize_partition, false);
David Anderson32e376f2018-08-16 13:43:11 -07001179 }
1180
David Anderson95d40932018-08-28 12:14:33 -07001181 // Flash OS images, resizing logical partitions as needed.
David Andersoncf444f32018-08-29 14:15:49 -07001182 FlashImages(os_images_);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001183
David Andersoncf444f32018-08-29 14:15:49 -07001184 if (slot_override_ == "all") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001185 set_active("a");
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001186 } else {
David Andersoncf444f32018-08-29 14:15:49 -07001187 set_active(slot_override_);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001188 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189}
1190
David Andersoncf444f32018-08-29 14:15:49 -07001191void FlashAllTool::CheckRequirements() {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001192 std::vector<char> contents;
1193 if (!source_.ReadFile("android-info.txt", &contents)) {
David Andersoncf444f32018-08-29 14:15:49 -07001194 die("could not read android-info.txt");
1195 }
Tom Cherry4aa60b32018-09-05 15:11:44 -07001196 ::CheckRequirements({contents.data(), contents.size()});
David Andersoncf444f32018-08-29 14:15:49 -07001197}
1198
1199void FlashAllTool::DetermineSecondarySlot() {
1200 if (skip_secondary_) {
1201 return;
1202 }
1203 if (slot_override_ != "") {
1204 secondary_slot_ = get_other_slot(slot_override_);
1205 } else {
1206 secondary_slot_ = get_other_slot();
1207 }
1208 if (secondary_slot_ == "") {
1209 if (supports_AB()) {
1210 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1211 }
1212 skip_secondary_ = true;
1213 }
1214}
1215
1216void FlashAllTool::CollectImages() {
1217 for (size_t i = 0; i < arraysize(images); ++i) {
1218 std::string slot = slot_override_;
1219 if (images[i].IsSecondary()) {
1220 if (skip_secondary_) {
1221 continue;
1222 }
1223 slot = secondary_slot_;
1224 }
1225 if (images[i].type == ImageType::BootCritical) {
1226 boot_images_.emplace_back(&images[i], slot);
1227 } else if (images[i].type == ImageType::Normal) {
1228 os_images_.emplace_back(&images[i], slot);
1229 }
1230 }
1231}
1232
1233void FlashAllTool::FlashImages(const std::vector<std::pair<const Image*, std::string>>& images) {
1234 for (const auto& [image, slot] : images) {
1235 fastboot_buffer buf;
1236 int fd = source_.OpenFile(image->img_name);
1237 if (fd < 0 || !load_buf_fd(fd, &buf)) {
1238 if (image->optional_if_no_image) {
1239 continue;
1240 }
1241 die("could not load '%s': %s", image->img_name, strerror(errno));
1242 }
1243 FlashImage(*image, slot, &buf);
1244 }
1245}
1246
1247void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastboot_buffer* buf) {
1248 auto flash = [&, this](const std::string& partition_name) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001249 std::vector<char> signature_data;
1250 if (source_.ReadFile(image.sig_name, &signature_data)) {
1251 fb_download("signature", signature_data);
Tom Cherry11f12092018-08-29 21:36:28 -07001252 fb_command("signature", "installing signature");
David Andersoncf444f32018-08-29 14:15:49 -07001253 }
1254
1255 if (is_logical(partition_name)) {
Tom Cherry11f12092018-08-29 21:36:28 -07001256 fb_resize_partition(partition_name, std::to_string(buf->image_size));
David Andersoncf444f32018-08-29 14:15:49 -07001257 }
1258 flash_buf(partition_name.c_str(), buf);
1259 };
1260 do_for_partitions(image.part_name, slot, flash, false);
1261}
1262
1263void FlashAllTool::UpdateSuperPartition() {
1264 if (!if_partition_exists("super", "")) {
1265 return;
1266 }
1267
1268 int fd = source_.OpenFile("super_empty.img");
1269 if (fd < 0) {
1270 return;
1271 }
1272 if (!is_userspace_fastboot()) {
1273 reboot_to_userspace_fastboot();
1274 }
Tom Cherry11f12092018-08-29 21:36:28 -07001275 fb_download_fd("super", fd, get_file_size(fd));
David Andersoncf444f32018-08-29 14:15:49 -07001276
1277 std::string command = "update-super:super";
1278 if (wipe_) {
1279 command += ":wipe";
1280 }
Tom Cherry11f12092018-08-29 21:36:28 -07001281 fb_command(command, "Updating super partition");
David Andersoncf444f32018-08-29 14:15:49 -07001282}
1283
1284class ZipImageSource final : public ImageSource {
1285 public:
1286 explicit ZipImageSource(ZipArchiveHandle zip) : zip_(zip) {}
Tom Cherrydfd85df2018-09-20 14:45:05 -07001287 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001288 int OpenFile(const std::string& name) const override;
1289
1290 private:
1291 ZipArchiveHandle zip_;
1292};
1293
Tom Cherrydfd85df2018-09-20 14:45:05 -07001294bool ZipImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
1295 return UnzipToMemory(zip_, name, out);
David Andersoncf444f32018-08-29 14:15:49 -07001296}
1297
1298int ZipImageSource::OpenFile(const std::string& name) const {
1299 return unzip_to_file(zip_, name.c_str());
1300}
1301
1302static void do_update(const char* filename, const std::string& slot_override, bool skip_secondary) {
Tom Cherry11f12092018-08-29 21:36:28 -07001303 dump_info();
David Andersoncf444f32018-08-29 14:15:49 -07001304
David Andersoncf444f32018-08-29 14:15:49 -07001305 ZipArchiveHandle zip;
1306 int error = OpenArchive(filename, &zip);
1307 if (error != 0) {
1308 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
1309 }
1310
1311 FlashAllTool tool(ZipImageSource(zip), slot_override, skip_secondary, false);
1312 tool.Flash();
1313
1314 CloseArchive(zip);
1315}
1316
1317class LocalImageSource final : public ImageSource {
1318 public:
Tom Cherrydfd85df2018-09-20 14:45:05 -07001319 bool ReadFile(const std::string& name, std::vector<char>* out) const override;
David Andersoncf444f32018-08-29 14:15:49 -07001320 int OpenFile(const std::string& name) const override;
1321};
1322
Tom Cherrydfd85df2018-09-20 14:45:05 -07001323bool LocalImageSource::ReadFile(const std::string& name, std::vector<char>* out) const {
David Andersoncf444f32018-08-29 14:15:49 -07001324 auto path = find_item_given_name(name);
1325 if (path.empty()) {
Tom Cherrydfd85df2018-09-20 14:45:05 -07001326 return false;
David Andersoncf444f32018-08-29 14:15:49 -07001327 }
Tom Cherrydfd85df2018-09-20 14:45:05 -07001328 return ReadFileToVector(path, out);
David Andersoncf444f32018-08-29 14:15:49 -07001329}
1330
1331int LocalImageSource::OpenFile(const std::string& name) const {
1332 auto path = find_item_given_name(name);
1333 return open(path.c_str(), O_RDONLY);
1334}
1335
1336static void do_flashall(const std::string& slot_override, bool skip_secondary, bool wipe) {
1337 std::string fname;
Tom Cherry11f12092018-08-29 21:36:28 -07001338 dump_info();
David Andersoncf444f32018-08-29 14:15:49 -07001339
David Andersoncf444f32018-08-29 14:15:49 -07001340 FlashAllTool tool(LocalImageSource(), slot_override, skip_secondary, wipe);
1341 tool.Flash();
1342}
1343
Elliott Hughesd6365a72017-05-08 18:04:49 -07001344static std::string next_arg(std::vector<std::string>* args) {
1345 if (args->empty()) syntax_error("expected argument");
1346 std::string result = args->front();
1347 args->erase(args->begin());
1348 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001349}
1350
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001351static void do_oem_command(const std::string& cmd, std::vector<std::string>* args) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001352 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001353
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001354 std::string command(cmd);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001355 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001356 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001357 }
Tom Cherry11f12092018-08-29 21:36:28 -07001358 fb_command(command, "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001359}
1360
Connor O'Brience16a8a2017-02-06 14:39:31 -08001361static std::string fb_fix_numeric_var(std::string var) {
1362 // Some bootloaders (angler, for example), send spurious leading whitespace.
1363 var = android::base::Trim(var);
1364 // Some bootloaders (hammerhead, for example) use implicit hex.
1365 // This code used to use strtol with base 16.
1366 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1367 return var;
1368}
1369
Aaron Wisnerdb511202018-06-26 15:38:35 -05001370static unsigned fb_get_flash_block_size(std::string name) {
Connor O'Brience16a8a2017-02-06 14:39:31 -08001371 std::string sizeString;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001372 if (!fb_getvar(name, &sizeString) || sizeString.empty()) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001373 // This device does not report flash block sizes, so return 0.
Connor O'Brience16a8a2017-02-06 14:39:31 -08001374 return 0;
1375 }
1376 sizeString = fb_fix_numeric_var(sizeString);
1377
1378 unsigned size;
1379 if (!android::base::ParseUint(sizeString, &size)) {
1380 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1381 return 0;
1382 }
Connor O'Brien6ef5c242017-11-01 17:37:32 -07001383 if ((size & (size - 1)) != 0) {
1384 fprintf(stderr, "Invalid %s %u: must be a power of 2.\n", name.c_str(), size);
Connor O'Brience16a8a2017-02-06 14:39:31 -08001385 return 0;
1386 }
1387 return size;
1388}
1389
Aaron Wisnerdb511202018-06-26 15:38:35 -05001390static void fb_perform_format(
Elliott Hughes5620d222018-03-28 08:20:00 -07001391 const std::string& partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001392 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001393 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001394 std::string partition_type, partition_size;
1395
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001396 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001397 const char* errMsg = nullptr;
1398 const struct fs_generator* gen = nullptr;
Jin Qian4a335822017-04-18 16:23:18 -07001399 TemporaryFile output;
1400 unique_fd fd;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001401
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001402 unsigned int limit = INT_MAX;
1403 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001404 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001405 }
1406 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001407 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001409
Aaron Wisnerdb511202018-06-26 15:38:35 -05001410 if (!fb_getvar("partition-type:" + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411 errMsg = "Can't determine partition type.\n";
1412 goto failed;
1413 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001414 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 if (partition_type != type_override) {
1416 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001417 partition.c_str(), partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001418 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001419 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001420 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421
Aaron Wisnerdb511202018-06-26 15:38:35 -05001422 if (!fb_getvar("partition-size:" + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001423 errMsg = "Unable to get partition size\n";
1424 goto failed;
1425 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001426 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 if (partition_size != size_override) {
1428 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughes5620d222018-03-28 08:20:00 -07001429 partition.c_str(), partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001430 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001431 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001432 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001433 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001434
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001435 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001436 if (!gen) {
1437 if (skip_if_not_supported) {
1438 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001439 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440 return;
1441 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001442 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1443 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001444 return;
1445 }
1446
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001447 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001448 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001449 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1450 return;
1451 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001452
Connor O'Brience16a8a2017-02-06 14:39:31 -08001453 unsigned eraseBlkSize, logicalBlkSize;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001454 eraseBlkSize = fb_get_flash_block_size("erase-block-size");
1455 logicalBlkSize = fb_get_flash_block_size("logical-block-size");
Connor O'Brience16a8a2017-02-06 14:39:31 -08001456
Jin Qian4a335822017-04-18 16:23:18 -07001457 if (fs_generator_generate(gen, output.path, size, initial_dir,
1458 eraseBlkSize, logicalBlkSize)) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001459 die("Cannot generate image for %s", partition.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001460 return;
1461 }
1462
Jin Qian4a335822017-04-18 16:23:18 -07001463 fd.reset(open(output.path, O_RDONLY));
1464 if (fd == -1) {
1465 fprintf(stderr, "Cannot open generated image: %s\n", strerror(errno));
1466 return;
1467 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001468 if (!load_buf_fd(fd.release(), &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001469 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001470 return;
1471 }
1472 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001473 return;
1474
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001475failed:
1476 if (skip_if_not_supported) {
1477 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001478 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001479 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001480 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001481}
1482
Aaron Wisnerdb511202018-06-26 15:38:35 -05001483int FastBootTool::Main(int argc, char* argv[]) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001484 bool wants_wipe = false;
1485 bool wants_reboot = false;
1486 bool wants_reboot_bootloader = false;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001487 bool wants_reboot_recovery = false;
1488 bool wants_reboot_fastboot = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001489 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001490 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001491 bool skip_secondary = false;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001492 bool set_fbe_marker = false;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001493 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001494 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001495 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001496
Elliott Hughes577e8b42018-04-05 16:12:47 -07001497 g_boot_img_hdr.kernel_addr = 0x00008000;
1498 g_boot_img_hdr.ramdisk_addr = 0x01000000;
1499 g_boot_img_hdr.second_addr = 0x00f00000;
1500 g_boot_img_hdr.tags_addr = 0x00000100;
1501 g_boot_img_hdr.page_size = 2048;
1502
JP Abgrall7b8970c2013-03-07 17:06:41 -08001503 const struct option longopts[] = {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001504 {"base", required_argument, 0, 0},
1505 {"cmdline", required_argument, 0, 0},
1506 {"disable-verification", no_argument, 0, 0},
1507 {"disable-verity", no_argument, 0, 0},
1508 {"header-version", required_argument, 0, 0},
Elliott Hughes379646b2015-06-02 13:50:00 -07001509 {"help", no_argument, 0, 'h'},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001510 {"kernel-offset", required_argument, 0, 0},
1511 {"os-patch-level", required_argument, 0, 0},
1512 {"os-version", required_argument, 0, 0},
1513 {"page-size", required_argument, 0, 0},
1514 {"ramdisk-offset", required_argument, 0, 0},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001515 {"set-active", optional_argument, 0, 'a'},
Mitchell Wills31dce302016-09-26 10:26:21 -07001516 {"skip-reboot", no_argument, 0, 0},
Elliott Hughes577e8b42018-04-05 16:12:47 -07001517 {"skip-secondary", no_argument, 0, 0},
1518 {"slot", required_argument, 0, 0},
1519 {"tags-offset", required_argument, 0, 0},
1520 {"unbuffered", no_argument, 0, 0},
Elliott Hughesf238d872018-03-29 14:46:29 -07001521 {"verbose", no_argument, 0, 'v'},
1522 {"version", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001523#if !defined(_WIN32)
1524 {"wipe-and-use-fbe", no_argument, 0, 0},
1525#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001526 {0, 0, 0, 0}
1527 };
Colin Cross8879f982012-05-22 17:53:34 -07001528
1529 serial = getenv("ANDROID_SERIAL");
1530
Elliott Hughes577e8b42018-04-05 16:12:47 -07001531 int c;
Elliott Hughesf3192bd2018-04-10 15:38:08 -07001532 while ((c = getopt_long(argc, argv, "a::hls:S:vw", longopts, &longindex)) != -1) {
Elliott Hughes577e8b42018-04-05 16:12:47 -07001533 if (c == 0) {
1534 std::string name{longopts[longindex].name};
1535 if (name == "base") {
1536 g_base_addr = strtoul(optarg, 0, 16);
1537 } else if (name == "cmdline") {
1538 g_cmdline = optarg;
1539 } else if (name == "disable-verification") {
1540 g_disable_verification = true;
1541 } else if (name == "disable-verity") {
1542 g_disable_verity = true;
1543 } else if (name == "header-version") {
1544 g_boot_img_hdr.header_version = strtoul(optarg, nullptr, 0);
1545 } else if (name == "kernel-offset") {
1546 g_boot_img_hdr.kernel_addr = strtoul(optarg, 0, 16);
1547 } else if (name == "os-patch-level") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001548 ParseOsPatchLevel(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001549 } else if (name == "os-version") {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001550 ParseOsVersion(&g_boot_img_hdr, optarg);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001551 } else if (name == "page-size") {
1552 g_boot_img_hdr.page_size = strtoul(optarg, nullptr, 0);
1553 if (g_boot_img_hdr.page_size == 0) die("invalid page size");
1554 } else if (name == "ramdisk-offset") {
1555 g_boot_img_hdr.ramdisk_addr = strtoul(optarg, 0, 16);
1556 } else if (name == "skip-reboot") {
1557 skip_reboot = true;
1558 } else if (name == "skip-secondary") {
1559 skip_secondary = true;
1560 } else if (name == "slot") {
1561 slot_override = optarg;
1562 } else if (name == "tags-offset") {
1563 g_boot_img_hdr.tags_addr = strtoul(optarg, 0, 16);
1564 } else if (name == "unbuffered") {
Elliott Hughesfc797672015-04-07 20:12:50 -07001565 setvbuf(stdout, nullptr, _IONBF, 0);
1566 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes577e8b42018-04-05 16:12:47 -07001567 } else if (name == "version") {
Dan Willemsenab971b52018-08-29 14:58:02 -07001568 fprintf(stdout, "fastboot version %s-%s\n", PLATFORM_TOOLS_VERSION, android::build::GetBuildNumber().c_str());
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001569 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001570 return 0;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001571#if !defined(_WIN32)
Elliott Hughes577e8b42018-04-05 16:12:47 -07001572 } else if (name == "wipe-and-use-fbe") {
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001573 wants_wipe = true;
1574 set_fbe_marker = true;
1575#endif
1576 } else {
Elliott Hughes855cdf82018-04-02 14:24:03 -07001577 die("unknown option %s", longopts[longindex].name);
Florian Bäuerle27ded482014-11-24 11:29:34 +01001578 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001579 } else {
1580 switch (c) {
1581 case 'a':
1582 wants_set_active = true;
1583 if (optarg) next_active = optarg;
1584 break;
1585 case 'h':
1586 return show_help();
Elliott Hughes577e8b42018-04-05 16:12:47 -07001587 case 'l':
1588 g_long_listing = true;
1589 break;
1590 case 's':
1591 serial = optarg;
1592 break;
1593 case 'S':
Elliott Hughes542370d2018-04-19 19:49:44 -07001594 if (!android::base::ParseByteCount(optarg, &sparse_limit)) {
1595 die("invalid sparse limit %s", optarg);
1596 }
Elliott Hughes577e8b42018-04-05 16:12:47 -07001597 break;
1598 case 'v':
1599 set_verbose();
1600 break;
1601 case 'w':
1602 wants_wipe = true;
1603 break;
1604 case '?':
1605 return 1;
1606 default:
1607 abort();
1608 }
Colin Cross8879f982012-05-22 17:53:34 -07001609 }
1610 }
1611
1612 argc -= optind;
1613 argv += optind;
1614
Elliott Hughesd6365a72017-05-08 18:04:49 -07001615 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001616
Colin Cross8fb6e062012-07-24 16:36:41 -07001617 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001619 return 0;
1620 }
1621
Colin Crossc7b75dc2012-08-29 18:17:06 -07001622 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001623 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001624 }
1625
David Pursell0b156632015-10-30 11:22:01 -07001626 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001627 if (transport == nullptr) {
1628 return 1;
1629 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001630 fastboot::FastBootDriver fb(transport);
1631 fb_init(fb);
David Pursell2ec418a2016-01-20 08:32:08 -08001632
Elliott Hughes5620d222018-03-28 08:20:00 -07001633 const double start = now();
1634
Aaron Wisnerdb511202018-06-26 15:38:35 -05001635 if (slot_override != "") slot_override = verify_slot(slot_override);
1636 if (next_active != "") next_active = verify_slot(next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001637
1638 if (wants_set_active) {
1639 if (next_active == "") {
1640 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001641 std::string current_slot;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001642 if (fb_getvar("current-slot", &current_slot)) {
1643 next_active = verify_slot(current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001644 } else {
1645 wants_set_active = false;
1646 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001647 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001648 next_active = verify_slot(slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001649 }
1650 }
1651 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001652
Elliott Hughesd6365a72017-05-08 18:04:49 -07001653 std::vector<std::string> args(argv, argv + argc);
1654 while (!args.empty()) {
1655 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001656
Elliott Hughesd6365a72017-05-08 18:04:49 -07001657 if (command == "getvar") {
1658 std::string variable = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001659 fb_display(variable, variable);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001660 } else if (command == "erase") {
1661 std::string partition = next_arg(&args);
1662 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001663 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001664 if (fb_getvar(std::string("partition-type:") + partition,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001665 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001666 fs_get_generator(partition_type) != nullptr) {
1667 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1668 partition_type.c_str());
1669 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001670
Tom Cherry11f12092018-08-29 21:36:28 -07001671 fb_erase(partition);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001672 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001673 do_for_partitions(partition, slot_override, erase, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001674 } else if (android::base::StartsWith(command, "format")) {
1675 // Parsing for: "format[:[type][:[size]]]"
1676 // Some valid things:
1677 // - select only the size, and leave default fs type:
1678 // format::0x4000000 userdata
1679 // - default fs type and size:
1680 // format userdata
1681 // format:: userdata
1682 std::vector<std::string> pieces = android::base::Split(command, ":");
1683 std::string type_override;
1684 if (pieces.size() > 1) type_override = pieces[1].c_str();
1685 std::string size_override;
1686 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001687
Elliott Hughesd6365a72017-05-08 18:04:49 -07001688 std::string partition = next_arg(&args);
1689
1690 auto format = [&](const std::string& partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001691 fb_perform_format(partition, 0, type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001692 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001693 do_for_partitions(partition.c_str(), slot_override, format, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001694 } else if (command == "signature") {
1695 std::string filename = next_arg(&args);
Tom Cherrydfd85df2018-09-20 14:45:05 -07001696 std::vector<char> data;
1697 if (!ReadFileToVector(filename, &data)) {
1698 die("could not load '%s': %s", filename.c_str(), strerror(errno));
1699 }
1700 if (data.size() != 256) die("signature must be 256 bytes (got %zu)", data.size());
1701 fb_download("signature", data);
Tom Cherry11f12092018-08-29 21:36:28 -07001702 fb_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001704 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001705
1706 if (args.size() == 1) {
1707 std::string what = next_arg(&args);
1708 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001709 wants_reboot = false;
1710 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001711 } else if (what == "recovery") {
1712 wants_reboot = false;
1713 wants_reboot_recovery = true;
1714 } else if (what == "fastboot") {
1715 wants_reboot = false;
1716 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001717 } else {
1718 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001719 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001720
Elliott Hughesca85df02015-02-25 10:02:00 -08001721 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001722 if (!args.empty()) syntax_error("junk after reboot command");
1723 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001724 wants_reboot_bootloader = true;
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001725 } else if (command == "reboot-recovery") {
1726 wants_reboot_recovery = true;
1727 } else if (command == "reboot-fastboot") {
1728 wants_reboot_fastboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001729 } else if (command == "continue") {
Tom Cherry11f12092018-08-29 21:36:28 -07001730 fb_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001731 } else if (command == "boot") {
1732 std::string kernel = next_arg(&args);
1733 std::string ramdisk;
1734 if (!args.empty()) ramdisk = next_arg(&args);
1735 std::string second_stage;
1736 if (!args.empty()) second_stage = next_arg(&args);
1737
Tom Cherrydfd85df2018-09-20 14:45:05 -07001738 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1739 fb_download("boot.img", data);
Tom Cherry11f12092018-08-29 21:36:28 -07001740 fb_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001741 } else if (command == "flash") {
1742 std::string pname = next_arg(&args);
1743
Elliott Hughes2810d002016-04-25 14:31:18 -07001744 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 if (!args.empty()) {
1746 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001747 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001748 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001749 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001750 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001751
1752 auto flash = [&](const std::string &partition) {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001753 do_flash(partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001754 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001755 do_for_partitions(pname.c_str(), slot_override, flash, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001756 } else if (command == "flash:raw") {
1757 std::string partition = next_arg(&args);
1758 std::string kernel = next_arg(&args);
1759 std::string ramdisk;
1760 if (!args.empty()) ramdisk = next_arg(&args);
1761 std::string second_stage;
1762 if (!args.empty()) second_stage = next_arg(&args);
1763
Tom Cherrydfd85df2018-09-20 14:45:05 -07001764 auto data = LoadBootableImage(kernel, ramdisk, second_stage);
1765 auto flashraw = [&data](const std::string& partition) {
1766 fb_flash(partition, data);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001767 };
Aaron Wisnerdb511202018-06-26 15:38:35 -05001768 do_for_partitions(partition, slot_override, flashraw, true);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001769 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001770 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001771 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
David Anderson32e376f2018-08-16 13:43:11 -07001772 do_flashall(slot_override, true, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001773 } else {
David Anderson32e376f2018-08-16 13:43:11 -07001774 do_flashall(slot_override, skip_secondary, wants_wipe);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001775 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001776 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001777 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001778 bool slot_all = (slot_override == "all");
1779 if (slot_all) {
1780 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1781 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001782 std::string filename = "update.zip";
1783 if (!args.empty()) {
1784 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001785 }
Aaron Wisnerdb511202018-06-26 15:38:35 -05001786 do_update(filename.c_str(), slot_override, skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001787 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001788 } else if (command == "set_active") {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001789 std::string slot = verify_slot(next_arg(&args), false);
Elliott Hughes5620d222018-03-28 08:20:00 -07001790 fb_set_active(slot);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001791 } else if (command == "stage") {
1792 std::string filename = next_arg(&args);
1793
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001794 struct fastboot_buffer buf;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001795 if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001796 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001797 }
Tom Cherry11f12092018-08-29 21:36:28 -07001798 fb_download_fd(filename, buf.fd, buf.sz);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001799 } else if (command == "get_staged") {
1800 std::string filename = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001801 fb_upload(filename);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001802 } else if (command == "oem") {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001803 do_oem_command("oem", &args);
Elliott Hughesd6365a72017-05-08 18:04:49 -07001804 } else if (command == "flashing") {
1805 if (args.empty()) {
1806 syntax_error("missing 'flashing' command");
1807 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1808 args[0] == "unlock_critical" ||
1809 args[0] == "lock_critical" ||
Elliott Hughes2e9b7792018-04-04 13:36:44 -07001810 args[0] == "get_unlock_ability")) {
Elliott Hughes1eec97a2017-05-15 16:53:53 -07001811 do_oem_command("flashing", &args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001812 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001813 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001814 }
David Anderson0d4277d2018-07-31 13:27:37 -07001815 } else if (command == "create-logical-partition") {
1816 std::string partition = next_arg(&args);
1817 std::string size = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001818 fb_create_partition(partition, size);
David Anderson0d4277d2018-07-31 13:27:37 -07001819 } else if (command == "delete-logical-partition") {
1820 std::string partition = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001821 fb_delete_partition(partition);
David Anderson0d4277d2018-07-31 13:27:37 -07001822 } else if (command == "resize-logical-partition") {
1823 std::string partition = next_arg(&args);
1824 std::string size = next_arg(&args);
Tom Cherry11f12092018-08-29 21:36:28 -07001825 fb_resize_partition(partition, size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001826 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001827 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001828 }
1829 }
1830
1831 if (wants_wipe) {
Paul Crowley4d170062018-04-24 17:06:30 -07001832 std::vector<std::string> partitions = { "userdata", "cache", "metadata" };
1833 for (const auto& partition : partitions) {
1834 std::string partition_type;
Aaron Wisnerdb511202018-06-26 15:38:35 -05001835 if (!fb_getvar(std::string{"partition-type:"} + partition, &partition_type)) continue;
Paul Crowley4d170062018-04-24 17:06:30 -07001836 if (partition_type.empty()) continue;
Tom Cherry11f12092018-08-29 21:36:28 -07001837 fb_erase(partition);
Paul Crowley4d170062018-04-24 17:06:30 -07001838 if (partition == "userdata" && set_fbe_marker) {
1839 fprintf(stderr, "setting FBE marker on initial userdata...\n");
1840 std::string initial_userdata_dir = create_fbemarker_tmpdir();
Aaron Wisnerdb511202018-06-26 15:38:35 -05001841 fb_perform_format(partition, 1, "", "", initial_userdata_dir);
Paul Crowley4d170062018-04-24 17:06:30 -07001842 delete_fbemarker_tmpdir(initial_userdata_dir);
1843 } else {
Aaron Wisnerdb511202018-06-26 15:38:35 -05001844 fb_perform_format(partition, 1, "", "", "");
Paul Crowley4d170062018-04-24 17:06:30 -07001845 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001846 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001847 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001848 if (wants_set_active) {
Elliott Hughes5620d222018-03-28 08:20:00 -07001849 fb_set_active(next_active);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001850 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001851 if (wants_reboot && !skip_reboot) {
Tom Cherry11f12092018-08-29 21:36:28 -07001852 fb_reboot();
1853 fb_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001854 } else if (wants_reboot_bootloader) {
Tom Cherry11f12092018-08-29 21:36:28 -07001855 fb_command("reboot-bootloader", "rebooting into bootloader");
1856 fb_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001857 } else if (wants_reboot_recovery) {
Tom Cherry11f12092018-08-29 21:36:28 -07001858 fb_command("reboot-recovery", "rebooting into recovery");
1859 fb_wait_for_disconnect();
Hridya Valsarajudea91b42018-07-17 11:14:01 -07001860 } else if (wants_reboot_fastboot) {
Tom Cherry11f12092018-08-29 21:36:28 -07001861 fb_command("reboot-fastboot", "rebooting into fastboot");
1862 fb_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001863 }
1864
Elliott Hughes5620d222018-03-28 08:20:00 -07001865 fprintf(stderr, "Finished. Total time: %.3fs\n", (now() - start));
David Anderson03de6452018-09-04 14:32:54 -07001866
1867 if (Transport* old_transport = fb.set_transport(nullptr)) {
1868 delete old_transport;
1869 }
Tom Cherry11f12092018-08-29 21:36:28 -07001870 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001871}
Elliott Hughes6ebec932018-04-10 14:22:13 -07001872
Aaron Wisnerdb511202018-06-26 15:38:35 -05001873void FastBootTool::ParseOsPatchLevel(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001874 unsigned year, month, day;
1875 if (sscanf(arg, "%u-%u-%u", &year, &month, &day) != 3) {
1876 syntax_error("OS patch level should be YYYY-MM-DD: %s", arg);
1877 }
1878 if (year < 2000 || year >= 2128) syntax_error("year out of range: %d", year);
1879 if (month < 1 || month > 12) syntax_error("month out of range: %d", month);
1880 hdr->SetOsPatchLevel(year, month);
1881}
1882
Aaron Wisnerdb511202018-06-26 15:38:35 -05001883void FastBootTool::ParseOsVersion(boot_img_hdr_v1* hdr, const char* arg) {
Elliott Hughes6ebec932018-04-10 14:22:13 -07001884 unsigned major = 0, minor = 0, patch = 0;
1885 std::vector<std::string> versions = android::base::Split(arg, ".");
1886 if (versions.size() < 1 || versions.size() > 3 ||
1887 (versions.size() >= 1 && !android::base::ParseUint(versions[0], &major)) ||
1888 (versions.size() >= 2 && !android::base::ParseUint(versions[1], &minor)) ||
1889 (versions.size() == 3 && !android::base::ParseUint(versions[2], &patch)) ||
1890 (major > 0x7f || minor > 0x7f || patch > 0x7f)) {
1891 syntax_error("bad OS version: %s", arg);
1892 }
1893 hdr->SetOsVersion(major, minor, patch);
1894}