blob: 9efe98533c5a40ba713817a629640e95c0a6e4f0 [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
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080048#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080049#include <utility>
50#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070051
Elliott Hughes82ff3152016-08-31 15:07:18 -070052#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070053#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080055#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070056#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080057#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050058#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070059#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070060#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Elliott Hughes253c18d2015-03-18 22:47:09 -070062#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080063#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070065#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080066#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070067#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080068#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070069#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Chris Fries0ea946c2017-04-12 10:25:57 -050071using android::base::unique_fd;
72
Colin Crossf8387882012-05-24 17:18:41 -070073#ifndef O_BINARY
74#define O_BINARY 0
75#endif
76
Wink Savilleb98762f2011-04-04 17:54:59 -070077char cur_product[FB_RESPONSE_SZ + 1];
78
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* serial = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -080080static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070082static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050083// Don't resparse files in too-big chunks.
84// libsparse will support INT_MAX, but this results in large allocations, so
85// let's keep it at 1GB to avoid memory pressure on the host.
86static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070087static int64_t sparse_limit = -1;
88static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089
Elliott Hughesfc797672015-04-07 20:12:50 -070090static unsigned page_size = 2048;
91static unsigned base_addr = 0x10000000;
92static unsigned kernel_offset = 0x00008000;
93static unsigned ramdisk_offset = 0x01000000;
94static unsigned second_offset = 0x00f00000;
95static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080096
Paul Crowley8f7f56e2015-11-27 09:29:37 +000097static const std::string convert_fbe_marker_filename("convert_fbe");
98
Rom Lemarchand622810c2013-06-28 09:54:59 -070099enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500100 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700101 FB_BUFFER_SPARSE,
102};
103
104struct fastboot_buffer {
105 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700106 void* data;
107 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500108 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700109};
110
111static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700112 const char* nickname;
113 const char* img_name;
114 const char* sig_name;
115 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700116 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700117 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700118} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700119 // clang-format off
120 { "boot", "boot.img", "boot.sig", "boot", false, false },
121 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
122 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
123 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
124 { "system", "system.img", "system.sig", "system", false, false },
125 { nullptr, "system_other.img", "system.sig", "system", true, true },
126 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
127 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
128 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
129 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700130};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700131
Elliott Hughes45964a82017-05-03 22:43:23 -0700132static std::string find_item_given_name(const char* img_name) {
133 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700134 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700135 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700136 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700137 return android::base::StringPrintf("%s/%s", dir, img_name);
138}
139
Elliott Hughes45964a82017-05-03 22:43:23 -0700140std::string find_item(const char* item) {
141 for (size_t i = 0; i < arraysize(images); ++i) {
142 if (images[i].nickname && !strcmp(images[i].nickname, item)) {
143 return find_item_given_name(images[i].img_name);
144 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 }
146
Elliott Hughes45964a82017-05-03 22:43:23 -0700147 if (!strcmp(item, "userdata")) return find_item_given_name("userdata.img");
148 if (!strcmp(item, "cache")) return find_item_given_name("cache.img");
149
150 fprintf(stderr, "unknown partition '%s'\n", item);
151 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152}
153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154static int64_t get_file_size(int fd) {
155 struct stat sb;
156 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700157}
158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800160 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700161 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 *sz = get_file_size(fd);
164 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700165 goto oops;
166 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 data = (char*) malloc(*sz);
169 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170
Elliott Hughesfc797672015-04-07 20:12:50 -0700171 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 close(fd);
173
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 return data;
175
176oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800177 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 close(fd);
179 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800180 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181 return 0;
182}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183
Elliott Hughes2810d002016-04-25 14:31:18 -0700184static void* load_file(const std::string& path, int64_t* sz) {
185 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700186 if (fd == -1) return nullptr;
187 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700188}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189
Elliott Hughesfc797672015-04-07 20:12:50 -0700190static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700191 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700192 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700193 return -1;
194 }
195
196 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
197 return -1;
198 }
199
Scott Anderson13081c62012-04-06 12:39:30 -0700200 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700202 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
203 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 return 0;
205}
206
Elliott Hughesfc797672015-04-07 20:12:50 -0700207static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800208 return match_fastboot_with_serial(info, serial);
209}
210
Elliott Hughesfc797672015-04-07 20:12:50 -0700211static int list_devices_callback(usb_ifc_info* info) {
212 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800213 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700214 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800215 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 if (!serial[0]) {
218 serial = "????????????";
219 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700220 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700221 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700223 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800224 printf("%-22s fastboot", serial.c_str());
225 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700226 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800228 }
229
230 return -1;
231}
232
David Pursell2ec418a2016-01-20 08:32:08 -0800233// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
234// a specific device, otherwise the first USB device found will be used.
235//
236// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
237// Otherwise it blocks until the target is available.
238//
239// The returned Transport is a singleton, so multiple calls to this function will return the same
240// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700241static Transport* open_device() {
242 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800243 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244
David Pursell2ec418a2016-01-20 08:32:08 -0800245 if (transport != nullptr) {
246 return transport;
247 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800248
David Pursell4601c972016-02-05 15:35:09 -0800249 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800250 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800251 int port = 0;
252 if (serial != nullptr) {
253 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800254
David Pursell4601c972016-02-05 15:35:09 -0800255 if (android::base::StartsWith(serial, "tcp:")) {
256 protocol = Socket::Protocol::kTcp;
257 port = tcp::kDefaultPort;
258 net_address = serial + strlen("tcp:");
259 } else if (android::base::StartsWith(serial, "udp:")) {
260 protocol = Socket::Protocol::kUdp;
261 port = udp::kDefaultPort;
262 net_address = serial + strlen("udp:");
263 }
264
265 if (net_address != nullptr) {
266 std::string error;
267 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
268 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
269 error.c_str());
270 return nullptr;
271 }
David Pursell2ec418a2016-01-20 08:32:08 -0800272 }
273 }
274
275 while (true) {
276 if (!host.empty()) {
277 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800278 if (protocol == Socket::Protocol::kTcp) {
279 transport = tcp::Connect(host, port, &error).release();
280 } else if (protocol == Socket::Protocol::kUdp) {
281 transport = udp::Connect(host, port, &error).release();
282 }
283
David Pursell2ec418a2016-01-20 08:32:08 -0800284 if (transport == nullptr && announce) {
285 fprintf(stderr, "error: %s\n", error.c_str());
286 }
287 } else {
288 transport = usb_open(match_fastboot);
289 }
290
291 if (transport != nullptr) {
292 return transport;
293 }
294
David Pursell0b156632015-10-30 11:22:01 -0700295 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800296 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700297 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800299 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300 }
301}
302
Elliott Hughesfc797672015-04-07 20:12:50 -0700303static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 // We don't actually open a USB device here,
305 // just getting our callback called so we can
306 // list all the connected devices.
307 usb_open(list_devices_callback);
308}
309
Elliott Hughesfc797672015-04-07 20:12:50 -0700310static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311 fprintf(stderr,
312/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
313 "usage: fastboot [ <option> ] <command>\n"
314 "\n"
315 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700316 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700317 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700318 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700319 " if found -- recovery. If the device\n"
320 " supports slots, the slot that has\n"
321 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700322 " Secondary images may be flashed to\n"
323 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700324 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
325 " flashing lock Locks the device. Prevents flashing.\n"
326 " flashing unlock Unlocks the device. Allows flashing\n"
327 " any partition except\n"
328 " bootloader-related partitions.\n"
329 " flashing lock_critical Prevents flashing bootloader-related\n"
330 " partitions.\n"
331 " flashing unlock_critical Enables flashing bootloader-related\n"
332 " partitions.\n"
333 " flashing get_unlock_ability Queries bootloader to see if the\n"
334 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700335 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700336 " unlock nonce.\n"
337 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700338 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700339 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700340 " erase <partition> Erase a flash partition.\n"
341 " format[:[<fs type>][:[<size>]] <partition>\n"
342 " Format a flash partition. Can\n"
343 " override the fs type and/or size\n"
344 " the bootloader reports.\n"
345 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700346 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800347 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700348 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
349 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
350 " Create bootimage and flash it.\n"
351 " devices [-l] List all connected devices [with\n"
352 " device paths].\n"
353 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800354 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700355 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800356 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
357 " stage <infile> Sends contents of <infile> to stage for\n"
358 " the next command. Supported only on\n"
359 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800360 " get_staged <outfile> Receives data to <outfile> staged by the\n"
361 " last command. Supported only on Android\n"
362 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700363 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 "\n"
365 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " -w Erase userdata and cache (and format\n"
367 " if supported by partition type).\n"
368 " -u Do not erase partition before\n"
369 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800370 " -s <specific device> Specify a device. For USB, provide either\n"
371 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700372 " For ethernet, provide an address in the\n"
373 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800374 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700375 " -c <cmdline> Override kernel commandline.\n"
376 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800377 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700378 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800379 " --kernel-offset Specify a custom kernel offset.\n"
380 " (default: 0x00008000)\n"
381 " --ramdisk-offset Specify a custom ramdisk offset.\n"
382 " (default: 0x01000000)\n"
383 " --tags-offset Specify a custom tags offset.\n"
384 " (default: 0x00000100)\n"
385 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700386 " (default: 2048).\n"
387 " -S <size>[K|M|G] Automatically sparse files greater\n"
388 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700389 " --slot <slot> Specify slot name to be used if the\n"
390 " device supports slots. All operations\n"
391 " on partitions that support slots will\n"
392 " be done on the slot specified.\n"
393 " 'all' can be given to refer to all slots.\n"
394 " 'other' can be given to refer to a\n"
395 " non-current slot. If this flag is not\n"
396 " used, slotted partitions will default\n"
397 " to the current active slot.\n"
398 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800399 " provided, this will default to the value\n"
400 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800401 " supported, this does nothing. This will\n"
402 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700403 " --skip-secondary Will not flash secondary slots when\n"
404 " performing a flashall or update. This\n"
405 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700406 " --skip-reboot Will not reboot the device when\n"
407 " performing commands that normally\n"
408 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000409#if !defined(_WIN32)
410 " --wipe-and-use-fbe On devices which support it,\n"
411 " erase userdata and cache, and\n"
412 " enable file-based encryption\n"
413#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800414 " --unbuffered Do not buffer input or output.\n"
415 " --version Display version.\n"
416 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000419
Elliott Hughesfc797672015-04-07 20:12:50 -0700420static void* load_bootable_image(const char* kernel, const char* ramdisk,
421 const char* secondstage, int64_t* sz,
422 const char* cmdline) {
423 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 fprintf(stderr, "no image specified\n");
425 return 0;
426 }
427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 int64_t ksize;
429 void* kdata = load_file(kernel, &ksize);
430 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800431 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 return 0;
433 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800434
Elliott Hughesfc797672015-04-07 20:12:50 -0700435 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700437 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800438
Elliott Hughesfc797672015-04-07 20:12:50 -0700439 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
441 return 0;
442 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800443
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 *sz = ksize;
445 return kdata;
446 }
447
Elliott Hughesfc797672015-04-07 20:12:50 -0700448 void* rdata = nullptr;
449 int64_t rsize = 0;
450 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700452 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800453 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 return 0;
455 }
456 }
457
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 void* sdata = nullptr;
459 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200460 if (secondstage) {
461 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700462 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200463 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
464 return 0;
465 }
466 }
467
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800468 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700469 int64_t bsize = 0;
470 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800471 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200472 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800473 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700474 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 fprintf(stderr,"failed to create boot.img\n");
476 return 0;
477 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700478 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
479 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800481
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800482 return bdata;
483}
484
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700485static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700486 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700487 ZipEntry zip_entry;
488 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700489 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000490 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 }
492
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700493 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700495 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700496 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700497 if (data == nullptr) {
498 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000499 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 }
501
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700502 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
503 if (error != 0) {
504 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000506 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800507 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000508
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800509 return data;
510}
511
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700512#if defined(_WIN32)
513
514// TODO: move this to somewhere it can be shared.
515
516#include <windows.h>
517
518// Windows' tmpfile(3) requires administrator rights because
519// it creates temporary files in the root directory.
520static FILE* win32_tmpfile() {
521 char temp_path[PATH_MAX];
522 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
523 if (nchars == 0 || nchars >= sizeof(temp_path)) {
524 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
525 return nullptr;
526 }
527
528 char filename[PATH_MAX];
529 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
530 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
531 return nullptr;
532 }
533
534 return fopen(filename, "w+bTD");
535}
536
537#define tmpfile win32_tmpfile
538
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000539static std::string make_temporary_directory() {
540 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
541 return "";
542}
543
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700544static int make_temporary_fd() {
545 // TODO: reimplement to avoid leaking a FILE*.
546 return fileno(tmpfile());
547}
548
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000549#else
550
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700551static std::string make_temporary_template() {
552 const char* tmpdir = getenv("TMPDIR");
553 if (tmpdir == nullptr) tmpdir = P_tmpdir;
554 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
555}
556
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000557static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700558 std::string result(make_temporary_template());
559 if (mkdtemp(&result[0]) == nullptr) {
560 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000561 return "";
562 }
563 return result;
564}
565
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700566static int make_temporary_fd() {
567 std::string path_template(make_temporary_template());
568 int fd = mkstemp(&path_template[0]);
569 if (fd == -1) {
570 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
571 return -1;
572 }
573 unlink(path_template.c_str());
574 return fd;
575}
576
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700577#endif
578
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000579static std::string create_fbemarker_tmpdir() {
580 std::string dir = make_temporary_directory();
581 if (dir.empty()) {
582 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
583 return "";
584 }
585 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
586 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
587 if (fd == -1) {
588 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
589 marker_file.c_str(), errno, strerror(errno));
590 return "";
591 }
592 close(fd);
593 return dir;
594}
595
596static void delete_fbemarker_tmpdir(const std::string& dir) {
597 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
598 if (unlink(marker_file.c_str()) == -1) {
599 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
600 marker_file.c_str(), errno, strerror(errno));
601 return;
602 }
603 if (rmdir(dir.c_str()) == -1) {
604 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
605 dir.c_str(), errno, strerror(errno));
606 return;
607 }
608}
609
Elliott Hughes45964a82017-05-03 22:43:23 -0700610static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700611 unique_fd fd(make_temporary_fd());
612 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700613 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
614 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700615 return -1;
616 }
617
Yusuke Sato07447542015-06-25 14:39:19 -0700618 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700619 ZipEntry zip_entry;
620 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
621 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000622 return -1;
623 }
624
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700625 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
626 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700627 int error = ExtractEntryToFile(zip, &zip_entry, fd);
628 if (error != 0) {
629 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
630 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700631 }
632
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000633 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700634 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700635 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700636}
637
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638static char *strip(char *s)
639{
640 int n;
641 while(*s && isspace(*s)) s++;
642 n = strlen(s);
643 while(n-- > 0) {
644 if(!isspace(s[n])) break;
645 s[n] = 0;
646 }
647 return s;
648}
649
650#define MAX_OPTIONS 32
651static int setup_requirement_line(char *name)
652{
653 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700654 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800655 unsigned n, count;
656 char *x;
657 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800658
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 if (!strncmp(name, "reject ", 7)) {
660 name += 7;
661 invert = 1;
662 } else if (!strncmp(name, "require ", 8)) {
663 name += 8;
664 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700665 } else if (!strncmp(name, "require-for-product:", 20)) {
666 // Get the product and point name past it
667 prod = name + 20;
668 name = strchr(name, ' ');
669 if (!name) return -1;
670 *name = 0;
671 name += 1;
672 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 }
674
675 x = strchr(name, '=');
676 if (x == 0) return 0;
677 *x = 0;
678 val[0] = x + 1;
679
680 for(count = 1; count < MAX_OPTIONS; count++) {
681 x = strchr(val[count - 1],'|');
682 if (x == 0) break;
683 *x = 0;
684 val[count] = x + 1;
685 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800686
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800687 name = strip(name);
688 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800689
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 name = strip(name);
691 if (name == 0) return -1;
692
Elliott Hughes253c18d2015-03-18 22:47:09 -0700693 const char* var = name;
694 // Work around an unfortunate name mismatch.
695 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800696
Elliott Hughes253c18d2015-03-18 22:47:09 -0700697 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800698 if (out == 0) return -1;
699
700 for(n = 0; n < count; n++) {
701 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700702 if (out[n] == 0) {
703 for(size_t i = 0; i < n; ++i) {
704 free((char*) out[i]);
705 }
706 free(out);
707 return -1;
708 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 }
710
Elliott Hughes253c18d2015-03-18 22:47:09 -0700711 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 return 0;
713}
714
Elliott Hughesfc797672015-04-07 20:12:50 -0700715static void setup_requirements(char* data, int64_t sz) {
716 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800717 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700718 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800719 *s++ = 0;
720 if (setup_requirement_line(data)) {
721 die("out of memory");
722 }
723 data = s;
724 } else {
725 s++;
726 }
727 }
728}
729
Elliott Hughesfc797672015-04-07 20:12:50 -0700730static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800731 fb_queue_notice("--------------------------------------------");
732 fb_queue_display("version-bootloader", "Bootloader Version...");
733 fb_queue_display("version-baseband", "Baseband Version.....");
734 fb_queue_display("serialno", "Serial Number........");
735 fb_queue_notice("--------------------------------------------");
736}
737
Rom Lemarchand622810c2013-06-28 09:54:59 -0700738static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700739{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700740 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700741 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700743 }
744
Elliott Hughesfc797672015-04-07 20:12:50 -0700745 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700746 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700747 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700748 }
749
Elliott Hughes253c18d2015-03-18 22:47:09 -0700750 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700751 if (!out_s) {
752 die("Failed to allocate sparse file array\n");
753 }
754
755 files = sparse_file_resparse(s, max_size, out_s, files);
756 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700757 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700758 }
759
760 return out_s;
761}
762
David Pursell0b156632015-10-30 11:22:01 -0700763static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700764 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700765 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
766 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800767 fprintf(stderr, "target didn't report max-download-size\n");
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 }
779 if (limit > 0) {
780 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
781 }
Colin Crossf8387882012-05-24 17:18:41 -0700782 return limit;
783}
784
David Pursell0b156632015-10-30 11:22:01 -0700785static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700786 int64_t limit;
787
788 if (sparse_limit == 0) {
789 return 0;
790 } else if (sparse_limit > 0) {
791 limit = sparse_limit;
792 } else {
793 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700794 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700795 }
796 if (target_sparse_limit > 0) {
797 limit = target_sparse_limit;
798 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700799 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700800 }
801 }
802
803 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500804 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700805 }
806
807 return 0;
808}
809
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700810// Until we get lazy inode table init working in make_ext4fs, we need to
811// erase partitions of type ext4 before flashing a filesystem so no stale
812// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700813static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800814 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700815 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800816 return false;
817 }
818 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700819}
820
Elliott Hughes53ec4952016-05-11 12:39:27 -0700821static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700822 int64_t sz = get_file_size(fd);
823 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700824 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700825 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700826
Elliott Hughesfc797672015-04-07 20:12:50 -0700827 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700828 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700829 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700830 sparse_file** s = load_sparse_files(fd, limit);
831 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700832 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700833 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700834 buf->type = FB_BUFFER_SPARSE;
835 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700836 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500837 buf->type = FB_BUFFER_FD;
838 buf->data = nullptr;
839 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000840 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700841 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700842
Elliott Hughes53ec4952016-05-11 12:39:27 -0700843 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700844}
845
Elliott Hughes53ec4952016-05-11 12:39:27 -0700846static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500847 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
848
Elliott Hughes53ec4952016-05-11 12:39:27 -0700849 if (fd == -1) {
850 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700851 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500852
853 struct stat s;
854 if (fstat(fd, &s)) {
855 return false;
856 }
857 if (!S_ISREG(s.st_mode)) {
858 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
859 return false;
860 }
861
862 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863}
864
865static void flash_buf(const char *pname, struct fastboot_buffer *buf)
866{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700867 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868
869 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800870 case FB_BUFFER_SPARSE: {
871 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700872 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700873 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700874 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800875 sparse_files.emplace_back(*s, sz);
876 ++s;
877 }
878
879 for (size_t i = 0; i < sparse_files.size(); ++i) {
880 const auto& pair = sparse_files[i];
881 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882 }
883 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800884 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500885 case FB_BUFFER_FD:
886 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700887 break;
888 default:
889 die("unknown buffer type: %d", buf->type);
890 }
891}
892
Daniel Rosenberg80919472016-06-30 19:25:31 -0700893static std::string get_current_slot(Transport* transport)
894{
895 std::string current_slot;
896 if (fb_getvar(transport, "current-slot", &current_slot)) {
897 if (current_slot == "_a") return "a"; // Legacy support
898 if (current_slot == "_b") return "b"; // Legacy support
899 return current_slot;
900 }
901 return "";
902}
903
904// Legacy support
905static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700906 std::vector<std::string> suffixes;
907 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700908 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700909 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700910 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700911 suffixes = android::base::Split(suffix_list, ",");
912 // Unfortunately some devices will return an error message in the
913 // guise of a valid value. If we only see only one suffix, it's probably
914 // not real.
915 if (suffixes.size() == 1) {
916 suffixes.clear();
917 }
918 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700919}
920
Daniel Rosenberg80919472016-06-30 19:25:31 -0700921// Legacy support
922static bool supports_AB_obsolete(Transport* transport) {
923 return !get_suffixes_obsolete(transport).empty();
924}
925
926static int get_slot_count(Transport* transport) {
927 std::string var;
928 int count;
929 if (!fb_getvar(transport, "slot-count", &var)) {
930 if (supports_AB_obsolete(transport)) return 2; // Legacy support
931 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700932 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700933 return count;
934}
935
Alex Lightbb9b8a52016-06-29 09:26:44 -0700936static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700937 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700938}
939
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700940// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700941static std::string get_other_slot(const std::string& current_slot, int count) {
942 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700943
Daniel Rosenberg80919472016-06-30 19:25:31 -0700944 char next = (current_slot[0] - 'a' + 1)%count + 'a';
945 return std::string(1, next);
946}
947
948static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
949 return get_other_slot(current_slot, get_slot_count(transport));
950}
951
952static std::string get_other_slot(Transport* transport, int count) {
953 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700954}
955
Alex Lightbb9b8a52016-06-29 09:26:44 -0700956static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700957 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700958}
959
Daniel Rosenberg80919472016-06-30 19:25:31 -0700960static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
961 std::string slot = slot_name;
962 if (slot == "_a") slot = "a"; // Legacy support
963 if (slot == "_b") slot = "b"; // Legacy support
964 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800965 if (allow_all) {
966 return "all";
967 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968 int count = get_slot_count(transport);
969 if (count > 0) {
970 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800971 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800972 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800973 }
974 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700975 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800976
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977 int count = get_slot_count(transport);
978 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800979
Daniel Rosenberg80919472016-06-30 19:25:31 -0700980 if (slot == "other") {
981 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700982 if (other == "") {
983 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800984 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700985 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800986 }
987
Daniel Rosenberg80919472016-06-30 19:25:31 -0700988 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
989
990 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
991 for (int i=0; i<count; i++) {
992 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700994
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700995 exit(1);
996}
997
Daniel Rosenberg80919472016-06-30 19:25:31 -0700998static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800999 return verify_slot(transport, slot, true);
1000}
1001
Daniel Rosenberg80919472016-06-30 19:25:31 -07001002static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001003 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001004 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005 std::string current_slot;
1006
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001008 /* If has-slot is not supported, the answer is no. */
1009 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001010 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001011 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 if (slot == "") {
1013 current_slot = get_current_slot(transport);
1014 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 die("Failed to identify current slot.\n");
1016 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001017 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001018 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001019 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001020 }
1021 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001022 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001023 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001024 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 }
1026 func(part);
1027 }
1028}
1029
David Pursell0b156632015-10-30 11:22:01 -07001030/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1031 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1032 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1033 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001034 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001035static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001036 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001037 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001038
Daniel Rosenberg80919472016-06-30 19:25:31 -07001039 if (slot == "all") {
1040 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1041 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001042 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001043 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001044 for (int i=0; i < get_slot_count(transport); i++) {
1045 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001046 }
1047 } else {
David Pursell0b156632015-10-30 11:22:01 -07001048 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001049 }
1050 } else {
David Pursell0b156632015-10-30 11:22:01 -07001051 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001052 }
1053}
1054
David Pursell0b156632015-10-30 11:22:01 -07001055static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001056 struct fastboot_buffer buf;
1057
Elliott Hughes53ec4952016-05-11 12:39:27 -07001058 if (!load_buf(transport, fname, &buf)) {
1059 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001060 }
1061 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001062}
1063
Elliott Hughes45964a82017-05-03 22:43:23 -07001064static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001065 int64_t sz;
Elliott Hughes45964a82017-05-03 22:43:23 -07001066 void* data = unzip_file(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001067 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068 fb_queue_download("signature", data, sz);
1069 fb_queue_command("signature", "installing signature");
1070}
1071
Daniel Rosenberg13454092016-06-27 19:43:11 -07001072// Sets slot_override as the active slot. If slot_override is blank,
1073// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001074static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001075 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001076 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001077 if (supports_AB_obsolete(transport)) {
1078 separator = "_"; // Legacy support
1079 } else {
1080 return;
1081 }
1082 }
1083 if (slot_override != "") {
1084 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001085 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001086 std::string current_slot = get_current_slot(transport);
1087 if (current_slot != "") {
1088 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001089 }
1090 }
1091}
1092
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001093static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001094 queue_info_dump();
1095
Wink Savilleb98762f2011-04-04 17:54:59 -07001096 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1097
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001098 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001099 int error = OpenArchive(filename, &zip);
1100 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001101 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001102 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001103 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001104
Elliott Hughesfc797672015-04-07 20:12:50 -07001105 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001106 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001107 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001108 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001109 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001110 }
1111
Elliott Hughes253c18d2015-03-18 22:47:09 -07001112 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113
Alex Lightbb9b8a52016-06-29 09:26:44 -07001114 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001115 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001116 if (slot_override != "") {
1117 secondary = get_other_slot(transport, slot_override);
1118 } else {
1119 secondary = get_other_slot(transport);
1120 }
1121 if (secondary == "") {
1122 if (supports_AB(transport)) {
1123 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1124 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001125 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001126 }
1127 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001128 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 const char* slot = slot_override.c_str();
1130 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001131 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001132 slot = secondary.c_str();
1133 } else {
1134 continue;
1135 }
1136 }
1137
Elliott Hughes253c18d2015-03-18 22:47:09 -07001138 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001139 if (fd == -1) {
1140 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001141 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001142 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001143 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001144 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001145 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001146 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001147 if (!load_buf_fd(transport, fd, &buf)) {
1148 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1149 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001150
1151 auto update = [&](const std::string &partition) {
1152 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001153 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001154 fb_queue_erase(partition.c_str());
1155 }
1156 flash_buf(partition.c_str(), &buf);
1157 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001158 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001159 * program exits.
1160 */
1161 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001162 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001164
1165 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001166 if (slot_override == "all") {
1167 set_active(transport, "a");
1168 } else {
1169 set_active(transport, slot_override);
1170 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001171}
1172
Alex Lightbb9b8a52016-06-29 09:26:44 -07001173static void do_send_signature(const std::string& fn) {
1174 std::size_t extension_loc = fn.find(".img");
1175 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001176
Alex Lightbb9b8a52016-06-29 09:26:44 -07001177 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001178
1179 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001180 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001181 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001182
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001183 fb_queue_download("signature", data, sz);
1184 fb_queue_command("signature", "installing signature");
1185}
1186
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001187static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001188 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001189 queue_info_dump();
1190
Wink Savilleb98762f2011-04-04 17:54:59 -07001191 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1192
Elliott Hughes45964a82017-05-03 22:43:23 -07001193 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001194 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001195
Elliott Hughesfc797672015-04-07 20:12:50 -07001196 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001197 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001198 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001199
1200 setup_requirements(reinterpret_cast<char*>(data), sz);
1201
Alex Lightbb9b8a52016-06-29 09:26:44 -07001202 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001203 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001204 if (slot_override != "") {
1205 secondary = get_other_slot(transport, slot_override);
1206 } else {
1207 secondary = get_other_slot(transport);
1208 }
1209 if (secondary == "") {
1210 if (supports_AB(transport)) {
1211 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1212 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001213 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001214 }
1215 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001216
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001217 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001218 const char* slot = NULL;
1219 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001220 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001221 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001222 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001223 }
1224 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001225 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001226 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001227 if (!load_buf(transport, fname.c_str(), &buf)) {
1228 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001229 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001230 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001231
1232 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001233 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001234 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001235 fb_queue_erase(partition.c_str());
1236 }
1237 flash_buf(partition.c_str(), &buf);
1238 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001239 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001240 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001241
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001242 if (slot_override == "all") {
1243 set_active(transport, "a");
1244 } else {
1245 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001246 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247}
1248
1249#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001250#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001251
Elliott Hughes45be42e2015-09-02 20:15:48 -07001252static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001253{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001254 if (argc <= 2) return 0;
1255 skip(2);
1256
1257 /*
1258 * Process unlock_bootloader, we have to load the message file
1259 * and send that to the remote device.
1260 */
1261 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001262
1263 int64_t sz;
1264 void* data = load_file(*argv, &sz);
1265 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001266 fb_queue_download("unlock_message", data, sz);
1267 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1268 skip(1);
1269 return 0;
1270}
1271
Elliott Hughes4a667302017-03-15 09:37:28 -07001272static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001273 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001274
Elliott Hughes4a667302017-03-15 09:37:28 -07001275 std::string command;
1276 while (argc > 0) {
1277 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001278 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001279 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001280 }
1281
Elliott Hughes4a667302017-03-15 09:37:28 -07001282 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 return 0;
1284}
1285
Colin Crossf8387882012-05-24 17:18:41 -07001286static int64_t parse_num(const char *arg)
1287{
1288 char *endptr;
1289 unsigned long long num;
1290
1291 num = strtoull(arg, &endptr, 0);
1292 if (endptr == arg) {
1293 return -1;
1294 }
1295
1296 if (*endptr == 'k' || *endptr == 'K') {
1297 if (num >= (-1ULL) / 1024) {
1298 return -1;
1299 }
1300 num *= 1024LL;
1301 endptr++;
1302 } else if (*endptr == 'm' || *endptr == 'M') {
1303 if (num >= (-1ULL) / (1024 * 1024)) {
1304 return -1;
1305 }
1306 num *= 1024LL * 1024LL;
1307 endptr++;
1308 } else if (*endptr == 'g' || *endptr == 'G') {
1309 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1310 return -1;
1311 }
1312 num *= 1024LL * 1024LL * 1024LL;
1313 endptr++;
1314 }
1315
1316 if (*endptr != '\0') {
1317 return -1;
1318 }
1319
1320 if (num > INT64_MAX) {
1321 return -1;
1322 }
1323
1324 return num;
1325}
1326
Connor O'Brience16a8a2017-02-06 14:39:31 -08001327static std::string fb_fix_numeric_var(std::string var) {
1328 // Some bootloaders (angler, for example), send spurious leading whitespace.
1329 var = android::base::Trim(var);
1330 // Some bootloaders (hammerhead, for example) use implicit hex.
1331 // This code used to use strtol with base 16.
1332 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1333 return var;
1334}
1335
1336static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1337 std::string sizeString;
1338 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1339 /* This device does not report flash block sizes, so return 0 */
1340 return 0;
1341 }
1342 sizeString = fb_fix_numeric_var(sizeString);
1343
1344 unsigned size;
1345 if (!android::base::ParseUint(sizeString, &size)) {
1346 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1347 return 0;
1348 }
1349 if (size < 4096 || (size & (size - 1)) != 0) {
1350 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1351 name.c_str(), size);
1352 return 0;
1353 }
1354 return size;
1355}
1356
David Pursell0b156632015-10-30 11:22:01 -07001357static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001358 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001359 const char* type_override, const char* size_override,
1360 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001361 std::string partition_type, partition_size;
1362
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001363 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 const char* errMsg = nullptr;
1365 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001366 int fd;
1367
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001368 unsigned int limit = INT_MAX;
1369 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001371 }
1372 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001373 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001374 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001375
David Pursell0b156632015-10-30 11:22:01 -07001376 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001377 errMsg = "Can't determine partition type.\n";
1378 goto failed;
1379 }
JP Abgrall7e859742014-05-06 15:14:15 -07001380 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001381 if (partition_type != type_override) {
1382 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1383 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001384 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001385 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001386 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001387
David Pursell0b156632015-10-30 11:22:01 -07001388 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 errMsg = "Unable to get partition size\n";
1390 goto failed;
1391 }
JP Abgrall7e859742014-05-06 15:14:15 -07001392 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001393 if (partition_size != size_override) {
1394 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1395 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001396 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001397 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001398 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001399 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001400
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001401 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001402 if (!gen) {
1403 if (skip_if_not_supported) {
1404 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001405 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001406 return;
1407 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1409 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001410 return;
1411 }
1412
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001413 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001414 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1416 return;
1417 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001418
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001419 fd = make_temporary_fd();
1420 if (fd == -1) return;
Connor O'Brience16a8a2017-02-06 14:39:31 -08001421
1422 unsigned eraseBlkSize, logicalBlkSize;
1423 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1424 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1425
1426 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001427 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001428 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001429 return;
1430 }
1431
Elliott Hughes53ec4952016-05-11 12:39:27 -07001432 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001433 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001434 close(fd);
1435 return;
1436 }
1437 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001438 return;
1439
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440failed:
1441 if (skip_if_not_supported) {
1442 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001443 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001444 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001445 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446}
1447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001448int main(int argc, char **argv)
1449{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001450 bool wants_wipe = false;
1451 bool wants_reboot = false;
1452 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001453 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001454 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001455 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001456 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001457 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001458 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001459 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001460 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001461 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001462 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001463 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001464
JP Abgrall7b8970c2013-03-07 17:06:41 -08001465 const struct option longopts[] = {
1466 {"base", required_argument, 0, 'b'},
1467 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001468 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001469 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001470 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001471 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001472 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001473 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001474 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001475 {"help", no_argument, 0, 'h'},
1476 {"unbuffered", no_argument, 0, 0},
1477 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001478 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001479 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001480 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001481 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001482 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001483#if !defined(_WIN32)
1484 {"wipe-and-use-fbe", no_argument, 0, 0},
1485#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001486 {0, 0, 0, 0}
1487 };
Colin Cross8879f982012-05-22 17:53:34 -07001488
1489 serial = getenv("ANDROID_SERIAL");
1490
1491 while (1) {
Elliott Hughes45964a82017-05-03 22:43:23 -07001492 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lc:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001493 if (c < 0) {
1494 break;
1495 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001496 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001497 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001498 case 'a':
1499 wants_set_active = true;
1500 if (optarg)
1501 next_active = optarg;
1502 break;
Colin Cross8879f982012-05-22 17:53:34 -07001503 case 'b':
1504 base_addr = strtoul(optarg, 0, 16);
1505 break;
Colin Cross8879f982012-05-22 17:53:34 -07001506 case 'c':
1507 cmdline = optarg;
1508 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001509 case 'h':
1510 usage();
1511 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001512 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001513 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001514 unsigned long val;
1515
1516 val = strtoul(optarg, &endptr, 0);
1517 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1518 die("invalid vendor id '%s'", optarg);
1519 vendor_id = (unsigned short)val;
1520 break;
1521 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001522 case 'k':
1523 kernel_offset = strtoul(optarg, 0, 16);
1524 break;
1525 case 'l':
1526 long_listing = 1;
1527 break;
1528 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001529 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001530 if (!page_size) die("invalid page size");
1531 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001532 case 'r':
1533 ramdisk_offset = strtoul(optarg, 0, 16);
1534 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001535 case 't':
1536 tags_offset = strtoul(optarg, 0, 16);
1537 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001538 case 's':
1539 serial = optarg;
1540 break;
1541 case 'S':
1542 sparse_limit = parse_num(optarg);
1543 if (sparse_limit < 0) {
1544 die("invalid sparse limit");
1545 }
1546 break;
1547 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001548 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001549 break;
1550 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001551 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001552 break;
Colin Cross8879f982012-05-22 17:53:34 -07001553 case '?':
1554 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001555 case 0:
1556 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001557 setvbuf(stdout, nullptr, _IONBF, 0);
1558 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001559 } else if (strcmp("version", longopts[longindex].name) == 0) {
1560 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001561 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001562 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001563 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1564 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001565 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1566 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001567 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1568 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001569#if !defined(_WIN32)
1570 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1571 wants_wipe = true;
1572 set_fbe_marker = true;
1573#endif
1574 } else {
1575 fprintf(stderr, "Internal error in options processing for %s\n",
1576 longopts[longindex].name);
1577 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001578 }
1579 break;
Colin Cross8879f982012-05-22 17:53:34 -07001580 default:
1581 abort();
1582 }
1583 }
1584
1585 argc -= optind;
1586 argv += optind;
1587
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001588 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001589 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001590 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001591 }
1592
Colin Cross8fb6e062012-07-24 16:36:41 -07001593 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001594 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001595 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001596 return 0;
1597 }
1598
Colin Crossc7b75dc2012-08-29 18:17:06 -07001599 if (argc > 0 && !strcmp(*argv, "help")) {
1600 usage();
1601 return 0;
1602 }
1603
David Pursell0b156632015-10-30 11:22:01 -07001604 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001605 if (transport == nullptr) {
1606 return 1;
1607 }
1608
Daniel Rosenberg80919472016-06-30 19:25:31 -07001609 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1610 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1611 }
1612 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1613 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001614
1615 if (wants_set_active) {
1616 if (next_active == "") {
1617 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001618 std::string current_slot;
1619 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001620 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001621 } else {
1622 wants_set_active = false;
1623 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001624 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001625 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001626 }
1627 }
1628 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001629
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001630 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001631 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001632 require(2);
1633 fb_queue_display(argv[1], argv[1]);
1634 skip(2);
1635 } else if(!strcmp(*argv, "erase")) {
1636 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001637
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001638 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001639 std::string partition_type;
1640 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001641 fs_get_generator(partition_type) != nullptr) {
1642 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1643 partition_type.c_str());
1644 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001645
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001646 fb_queue_erase(partition.c_str());
1647 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001648 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001649 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001650 } else if(!strncmp(*argv, "format", strlen("format"))) {
1651 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001652 char *type_override = nullptr;
1653 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001654 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001655 /*
1656 * Parsing for: "format[:[type][:[size]]]"
1657 * Some valid things:
1658 * - select ontly the size, and leave default fs type:
1659 * format::0x4000000 userdata
1660 * - default fs type and size:
1661 * format userdata
1662 * format:: userdata
1663 */
1664 overrides = strchr(*argv, ':');
1665 if (overrides) {
1666 overrides++;
1667 size_override = strchr(overrides, ':');
1668 if (size_override) {
1669 size_override[0] = '\0';
1670 size_override++;
1671 }
1672 type_override = overrides;
1673 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001674 if (type_override && !type_override[0]) type_override = nullptr;
1675 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001676
1677 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001678 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001679 fb_queue_erase(partition.c_str());
1680 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001681 fb_perform_format(transport, partition.c_str(), 0,
1682 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001683 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001684 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001685 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001686 } else if(!strcmp(*argv, "signature")) {
1687 require(2);
1688 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001689 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001690 if (sz != 256) die("signature must be 256 bytes");
1691 fb_queue_download("signature", data, sz);
1692 fb_queue_command("signature", "installing signature");
1693 skip(2);
1694 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001695 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001696 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001697 if (argc > 0) {
1698 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001699 wants_reboot = false;
1700 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001701 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001702 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001703 wants_reboot = false;
1704 wants_reboot_emergency = true;
1705 skip(1);
1706 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001707 }
1708 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001709 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001710 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001711 skip(1);
1712 } else if (!strcmp(*argv, "continue")) {
1713 fb_queue_command("continue", "resuming boot");
1714 skip(1);
1715 } else if(!strcmp(*argv, "boot")) {
1716 char *kname = 0;
1717 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001718 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001719 skip(1);
1720 if (argc > 0) {
1721 kname = argv[0];
1722 skip(1);
1723 }
1724 if (argc > 0) {
1725 rname = argv[0];
1726 skip(1);
1727 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001728 if (argc > 0) {
1729 sname = argv[0];
1730 skip(1);
1731 }
1732 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001733 if (data == 0) return 1;
1734 fb_queue_download("boot.img", data, sz);
1735 fb_queue_command("boot", "booting");
1736 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001737 char* pname = argv[1];
1738 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001739 require(2);
1740 if (argc > 2) {
1741 fname = argv[2];
1742 skip(3);
1743 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001744 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001745 skip(2);
1746 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001747 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001748
1749 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001750 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001751 fb_queue_erase(partition.c_str());
1752 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001753 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001754 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001755 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001756 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001757 char *kname = argv[2];
1758 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001759 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001760 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001761 skip(3);
1762 if (argc > 0) {
1763 rname = argv[0];
1764 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001765 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001766 if (argc > 0) {
1767 sname = argv[0];
1768 skip(1);
1769 }
1770 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001771 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001772 auto flashraw = [&](const std::string &partition) {
1773 fb_queue_flash(partition.c_str(), data, sz);
1774 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001775 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001776 } else if(!strcmp(*argv, "flashall")) {
1777 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001778 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001779 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1780 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001781 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001782 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001783 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001784 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001785 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001786 bool slot_all = (slot_override == "all");
1787 if (slot_all) {
1788 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1789 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001790 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001791 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001792 skip(2);
1793 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001794 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001795 skip(1);
1796 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001797 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001798 } else if(!strcmp(*argv, "set_active")) {
1799 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001800 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001801 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1802 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1803 // do have slot-suffixes.
1804 std::string var;
1805 if (!fb_getvar(transport, "slot-count", &var) &&
1806 fb_getvar(transport, "slot-suffixes", &var)) {
1807 slot = "_" + slot;
1808 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001809 fb_set_active(slot.c_str());
1810 skip(2);
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001811 } else if(!strcmp(*argv, "stage")) {
1812 require(2);
1813 std::string infile(argv[1]);
1814 skip(2);
1815 struct fastboot_buffer buf;
1816 if (!load_buf(transport, infile.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1817 die("cannot load '%s'", infile.c_str());
1818 }
1819 fb_queue_download_fd(infile.c_str(), buf.fd, buf.sz);
Jocelyn Bohr91fefad2017-01-27 14:17:56 -08001820 } else if(!strcmp(*argv, "get_staged")) {
1821 require(2);
1822 char *outfile = argv[1];
1823 skip(2);
1824 fb_queue_upload(outfile);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001825 } else if(!strcmp(*argv, "oem")) {
1826 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001827 } else if(!strcmp(*argv, "flashing")) {
1828 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1829 !strcmp(*(argv+1), "lock") ||
1830 !strcmp(*(argv+1), "unlock_critical") ||
1831 !strcmp(*(argv+1), "lock_critical") ||
1832 !strcmp(*(argv+1), "get_unlock_ability") ||
1833 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1834 !strcmp(*(argv+1), "lock_bootloader"))) {
1835 argc = do_oem_command(argc, argv);
1836 } else
1837 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1838 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001839 } else {
1840 usage();
1841 return 1;
1842 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001843 } else {
1844 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001845 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001846 }
1847 }
1848
1849 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001850 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001851 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001852 if (set_fbe_marker) {
1853 fprintf(stderr, "setting FBE marker...\n");
1854 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1855 if (initial_userdata_dir.empty()) {
1856 return 1;
1857 }
1858 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1859 delete_fbemarker_tmpdir(initial_userdata_dir);
1860 } else {
1861 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1862 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001863
1864 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001865 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001866 fprintf(stderr, "wiping cache...\n");
1867 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001868 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001869 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001870 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001871 if (wants_set_active) {
1872 fb_set_active(next_active.c_str());
1873 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001874 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001875 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001876 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001877 } else if (wants_reboot_bootloader) {
1878 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001879 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001880 } else if (wants_reboot_emergency) {
1881 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1882 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001883 }
1884
David Pursell0b156632015-10-30 11:22:01 -07001885 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001886}