blob: e1f289ae1adfbb86c006bed40018a4eb09b0ae43 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070029#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <errno.h>
31#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070032#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070033#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070035#include <stdint.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070041#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070042#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080043
Elliott Hughes290a2282016-11-14 17:08:47 -080044#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070045#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050056#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070057#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070058#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Elliott Hughes253c18d2015-03-18 22:47:09 -070060#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080061#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070063#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080064#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070065#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080066#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070067#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Chris Fries0ea946c2017-04-12 10:25:57 -050069using android::base::unique_fd;
70
Colin Crossf8387882012-05-24 17:18:41 -070071#ifndef O_BINARY
72#define O_BINARY 0
73#endif
74
Wink Savilleb98762f2011-04-04 17:54:59 -070075char cur_product[FB_RESPONSE_SZ + 1];
76
David Pursell2ec418a2016-01-20 08:32:08 -080077static const char* serial = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -080078static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070080static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070085static int64_t sparse_limit = -1;
86static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughesfc797672015-04-07 20:12:50 -070088static unsigned page_size = 2048;
89static unsigned base_addr = 0x10000000;
90static unsigned kernel_offset = 0x00008000;
91static unsigned ramdisk_offset = 0x01000000;
92static unsigned second_offset = 0x00f00000;
93static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080094
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -050098 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -070099 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500106 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700107};
108
109static struct {
Elliott Hughes45964a82017-05-03 22:43:23 -0700110 const char* nickname;
111 const char* img_name;
112 const char* sig_name;
113 const char* part_name;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700114 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700116} images[] = {
Elliott Hughes45964a82017-05-03 22:43:23 -0700117 // clang-format off
118 { "boot", "boot.img", "boot.sig", "boot", false, false },
119 { nullptr, "boot_other.img", "boot.sig", "boot", true, true },
120 { "dtbo", "dtbo.img", "dtbo.sig", "dtbo", true, false },
Dmitry Shmidtde8c08c2017-05-15 10:24:04 -0700121 { "dts", "dt.img", "dt.sig", "dts", true, false },
Elliott Hughes45964a82017-05-03 22:43:23 -0700122 { "recovery", "recovery.img", "recovery.sig", "recovery", true, false },
123 { "system", "system.img", "system.sig", "system", false, false },
124 { nullptr, "system_other.img", "system.sig", "system", true, true },
125 { "vbmeta", "vbmeta.img", "vbmeta.sig", "vbmeta", true, false },
126 { "vendor", "vendor.img", "vendor.sig", "vendor", true, false },
127 { nullptr, "vendor_other.img", "vendor.sig", "vendor", true, true },
128 // clang-format on
Rom Lemarchand622810c2013-06-28 09:54:59 -0700129};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700130
Elliott Hughes45964a82017-05-03 22:43:23 -0700131static std::string find_item_given_name(const char* img_name) {
132 char* dir = getenv("ANDROID_PRODUCT_OUT");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700133 if (dir == nullptr || dir[0] == '\0') {
Elliott Hughes45964a82017-05-03 22:43:23 -0700134 die("ANDROID_PRODUCT_OUT not set");
Alex Lightbb9b8a52016-06-29 09:26:44 -0700135 }
Alex Lightbb9b8a52016-06-29 09:26:44 -0700136 return android::base::StringPrintf("%s/%s", dir, img_name);
137}
138
Elliott Hughes29d5d7d2017-05-11 15:05:13 -0700139static std::string find_item(const std::string& item) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700140 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughesd6365a72017-05-08 18:04:49 -0700141 if (images[i].nickname && item == images[i].nickname) {
Elliott Hughes45964a82017-05-03 22:43:23 -0700142 return find_item_given_name(images[i].img_name);
143 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 }
145
Elliott Hughesd6365a72017-05-08 18:04:49 -0700146 if (item == "userdata") return find_item_given_name("userdata.img");
147 if (item == "cache") return find_item_given_name("cache.img");
Elliott Hughes45964a82017-05-03 22:43:23 -0700148
Elliott Hughesd6365a72017-05-08 18:04:49 -0700149 fprintf(stderr, "unknown partition '%s'\n", item.c_str());
Elliott Hughes45964a82017-05-03 22:43:23 -0700150 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151}
152
Elliott Hughesfc797672015-04-07 20:12:50 -0700153static int64_t get_file_size(int fd) {
154 struct stat sb;
155 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700156}
157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800159 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700160 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
Elliott Hughesfc797672015-04-07 20:12:50 -0700162 *sz = get_file_size(fd);
163 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700164 goto oops;
165 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166
Elliott Hughesfc797672015-04-07 20:12:50 -0700167 data = (char*) malloc(*sz);
168 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169
Elliott Hughesfc797672015-04-07 20:12:50 -0700170 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800171 close(fd);
172
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 return data;
174
175oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800176 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 close(fd);
178 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800179 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180 return 0;
181}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182
Elliott Hughes2810d002016-04-25 14:31:18 -0700183static void* load_file(const std::string& path, int64_t* sz) {
184 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700185 if (fd == -1) return nullptr;
186 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700190 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700191 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700192 return -1;
193 }
194
195 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
196 return -1;
197 }
198
Scott Anderson13081c62012-04-06 12:39:30 -0700199 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700201 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
202 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203 return 0;
204}
205
Elliott Hughesfc797672015-04-07 20:12:50 -0700206static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800207 return match_fastboot_with_serial(info, serial);
208}
209
Elliott Hughesfc797672015-04-07 20:12:50 -0700210static int list_devices_callback(usb_ifc_info* info) {
211 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800212 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700213 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800214 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700215 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 if (!serial[0]) {
217 serial = "????????????";
218 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700219 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700220 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800221 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700222 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800223 printf("%-22s fastboot", serial.c_str());
224 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700225 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800226 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800227 }
228
229 return -1;
230}
231
David Pursell2ec418a2016-01-20 08:32:08 -0800232// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
233// a specific device, otherwise the first USB device found will be used.
234//
235// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
236// Otherwise it blocks until the target is available.
237//
238// The returned Transport is a singleton, so multiple calls to this function will return the same
239// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700240static Transport* open_device() {
241 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800242 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243
David Pursell2ec418a2016-01-20 08:32:08 -0800244 if (transport != nullptr) {
245 return transport;
246 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800247
David Pursell4601c972016-02-05 15:35:09 -0800248 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800249 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800250 int port = 0;
251 if (serial != nullptr) {
252 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800253
David Pursell4601c972016-02-05 15:35:09 -0800254 if (android::base::StartsWith(serial, "tcp:")) {
255 protocol = Socket::Protocol::kTcp;
256 port = tcp::kDefaultPort;
257 net_address = serial + strlen("tcp:");
258 } else if (android::base::StartsWith(serial, "udp:")) {
259 protocol = Socket::Protocol::kUdp;
260 port = udp::kDefaultPort;
261 net_address = serial + strlen("udp:");
262 }
263
264 if (net_address != nullptr) {
265 std::string error;
266 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
267 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
268 error.c_str());
269 return nullptr;
270 }
David Pursell2ec418a2016-01-20 08:32:08 -0800271 }
272 }
273
274 while (true) {
275 if (!host.empty()) {
276 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800277 if (protocol == Socket::Protocol::kTcp) {
278 transport = tcp::Connect(host, port, &error).release();
279 } else if (protocol == Socket::Protocol::kUdp) {
280 transport = udp::Connect(host, port, &error).release();
281 }
282
David Pursell2ec418a2016-01-20 08:32:08 -0800283 if (transport == nullptr && announce) {
284 fprintf(stderr, "error: %s\n", error.c_str());
285 }
286 } else {
287 transport = usb_open(match_fastboot);
288 }
289
290 if (transport != nullptr) {
291 return transport;
292 }
293
David Pursell0b156632015-10-30 11:22:01 -0700294 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800295 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700296 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800298 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 }
300}
301
Elliott Hughesfc797672015-04-07 20:12:50 -0700302static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 // We don't actually open a USB device here,
304 // just getting our callback called so we can
305 // list all the connected devices.
306 usb_open(list_devices_callback);
307}
308
Elliott Hughesd6365a72017-05-08 18:04:49 -0700309static void syntax_error(const char* fmt, ...) {
310 fprintf(stderr, "fastboot: usage: ");
311
312 va_list ap;
313 va_start(ap, fmt);
314 vfprintf(stderr, fmt, ap);
315 va_end(ap);
316
317 fprintf(stderr, "\n");
318 exit(1);
319}
320
321static int show_help() {
322 // clang-format off
323 fprintf(stdout,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800324/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
325 "usage: fastboot [ <option> ] <command>\n"
326 "\n"
327 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700328 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700329 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700330 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700331 " if found -- recovery. If the device\n"
332 " supports slots, the slot that has\n"
333 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700334 " Secondary images may be flashed to\n"
335 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700336 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
337 " flashing lock Locks the device. Prevents flashing.\n"
338 " flashing unlock Unlocks the device. Allows flashing\n"
339 " any partition except\n"
340 " bootloader-related partitions.\n"
341 " flashing lock_critical Prevents flashing bootloader-related\n"
342 " partitions.\n"
343 " flashing unlock_critical Enables flashing bootloader-related\n"
344 " partitions.\n"
345 " flashing get_unlock_ability Queries bootloader to see if the\n"
346 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700347 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700348 " unlock nonce.\n"
349 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700350 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700351 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700352 " erase <partition> Erase a flash partition.\n"
353 " format[:[<fs type>][:[<size>]] <partition>\n"
354 " Format a flash partition. Can\n"
355 " override the fs type and/or size\n"
356 " the bootloader reports.\n"
357 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700358 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800359 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700360 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
Elliott Hughesd6365a72017-05-08 18:04:49 -0700361 " flash:raw <bootable-partition> <kernel> [ <ramdisk> [ <second> ] ]\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700362 " Create bootimage and flash it.\n"
363 " devices [-l] List all connected devices [with\n"
364 " device paths].\n"
365 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800366 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700367 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800368 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
369 " stage <infile> Sends contents of <infile> to stage for\n"
370 " the next command. Supported only on\n"
371 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800372 " get_staged <outfile> Receives data to <outfile> staged by the\n"
373 " last command. Supported only on Android\n"
374 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700375 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 "\n"
377 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700378 " -w Erase userdata and cache (and format\n"
379 " if supported by partition type).\n"
380 " -u Do not erase partition before\n"
381 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800382 " -s <specific device> Specify a device. For USB, provide either\n"
383 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700384 " For ethernet, provide an address in the\n"
385 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800386 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700387 " -c <cmdline> Override kernel commandline.\n"
388 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800389 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700390 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800391 " --kernel-offset Specify a custom kernel offset.\n"
392 " (default: 0x00008000)\n"
393 " --ramdisk-offset Specify a custom ramdisk offset.\n"
394 " (default: 0x01000000)\n"
395 " --tags-offset Specify a custom tags offset.\n"
396 " (default: 0x00000100)\n"
397 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700398 " (default: 2048).\n"
399 " -S <size>[K|M|G] Automatically sparse files greater\n"
400 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700401 " --slot <slot> Specify slot name to be used if the\n"
402 " device supports slots. All operations\n"
403 " on partitions that support slots will\n"
404 " be done on the slot specified.\n"
405 " 'all' can be given to refer to all slots.\n"
406 " 'other' can be given to refer to a\n"
407 " non-current slot. If this flag is not\n"
408 " used, slotted partitions will default\n"
409 " to the current active slot.\n"
410 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800411 " provided, this will default to the value\n"
412 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800413 " supported, this does nothing. This will\n"
414 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700415 " --skip-secondary Will not flash secondary slots when\n"
416 " performing a flashall or update. This\n"
417 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700418 " --skip-reboot Will not reboot the device when\n"
419 " performing commands that normally\n"
420 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000421#if !defined(_WIN32)
422 " --wipe-and-use-fbe On devices which support it,\n"
423 " erase userdata and cache, and\n"
424 " enable file-based encryption\n"
425#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800426 " --unbuffered Do not buffer input or output.\n"
427 " --version Display version.\n"
428 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800429 );
Elliott Hughesd6365a72017-05-08 18:04:49 -0700430 // clang-format off
431 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000433
Elliott Hughesd6365a72017-05-08 18:04:49 -0700434static void* load_bootable_image(const std::string& kernel, const std::string& ramdisk,
435 const std::string& second_stage, int64_t* sz,
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 const char* cmdline) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700437 int64_t ksize;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700438 void* kdata = load_file(kernel.c_str(), &ksize);
439 if (kdata == nullptr) die("cannot load '%s': %s\n", kernel.c_str(), strerror(errno));
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800440
Elliott Hughesfc797672015-04-07 20:12:50 -0700441 // Is this actually a boot image?
Elliott Hughesd6365a72017-05-08 18:04:49 -0700442 if (!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800444
Elliott Hughesd6365a72017-05-08 18:04:49 -0700445 if (!ramdisk.empty()) die("cannot boot a boot.img *and* ramdisk\n");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800446
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447 *sz = ksize;
448 return kdata;
449 }
450
Elliott Hughesfc797672015-04-07 20:12:50 -0700451 void* rdata = nullptr;
452 int64_t rsize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700453 if (!ramdisk.empty()) {
454 rdata = load_file(ramdisk.c_str(), &rsize);
455 if (rdata == nullptr) die("cannot load '%s': %s\n", ramdisk.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800456 }
457
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 void* sdata = nullptr;
459 int64_t ssize = 0;
Elliott Hughesd6365a72017-05-08 18:04:49 -0700460 if (!second_stage.empty()) {
461 sdata = load_file(second_stage.c_str(), &ssize);
462 if (sdata == nullptr) die("cannot load '%s': %s\n", second_stage.c_str(), strerror(errno));
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200463 }
464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 int64_t bsize = 0;
467 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800468 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200469 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800470 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesd6365a72017-05-08 18:04:49 -0700471 if (bdata == nullptr) die("failed to create boot.img\n");
472
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
474 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 return bdata;
478}
479
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700480static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz) {
Yusuke Sato07447542015-06-25 14:39:19 -0700481 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700482 ZipEntry zip_entry;
483 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700484 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000485 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 }
487
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700488 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700490 fprintf(stderr, "extracting %s (%" PRId64 " MB)...\n", entry_name, *sz / 1024 / 1024);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700491 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700492 if (data == nullptr) {
493 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000494 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
496
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700497 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
498 if (error != 0) {
499 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000501 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 return data;
505}
506
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700507#if defined(_WIN32)
508
509// TODO: move this to somewhere it can be shared.
510
511#include <windows.h>
512
513// Windows' tmpfile(3) requires administrator rights because
514// it creates temporary files in the root directory.
515static FILE* win32_tmpfile() {
516 char temp_path[PATH_MAX];
517 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
518 if (nchars == 0 || nchars >= sizeof(temp_path)) {
519 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
520 return nullptr;
521 }
522
523 char filename[PATH_MAX];
524 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
525 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
526 return nullptr;
527 }
528
529 return fopen(filename, "w+bTD");
530}
531
532#define tmpfile win32_tmpfile
533
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000534static std::string make_temporary_directory() {
535 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
536 return "";
537}
538
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700539static int make_temporary_fd() {
540 // TODO: reimplement to avoid leaking a FILE*.
541 return fileno(tmpfile());
542}
543
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000544#else
545
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700546static std::string make_temporary_template() {
547 const char* tmpdir = getenv("TMPDIR");
548 if (tmpdir == nullptr) tmpdir = P_tmpdir;
549 return std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
550}
551
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000552static std::string make_temporary_directory() {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700553 std::string result(make_temporary_template());
554 if (mkdtemp(&result[0]) == nullptr) {
555 fprintf(stderr, "Unable to create temporary directory: %s\n", strerror(errno));
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000556 return "";
557 }
558 return result;
559}
560
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700561static int make_temporary_fd() {
562 std::string path_template(make_temporary_template());
563 int fd = mkstemp(&path_template[0]);
564 if (fd == -1) {
565 fprintf(stderr, "Unable to create temporary file: %s\n", strerror(errno));
566 return -1;
567 }
568 unlink(path_template.c_str());
569 return fd;
570}
571
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700572#endif
573
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000574static std::string create_fbemarker_tmpdir() {
575 std::string dir = make_temporary_directory();
576 if (dir.empty()) {
577 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
578 return "";
579 }
580 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
581 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
582 if (fd == -1) {
583 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
584 marker_file.c_str(), errno, strerror(errno));
585 return "";
586 }
587 close(fd);
588 return dir;
589}
590
591static void delete_fbemarker_tmpdir(const std::string& dir) {
592 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
593 if (unlink(marker_file.c_str()) == -1) {
594 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
595 marker_file.c_str(), errno, strerror(errno));
596 return;
597 }
598 if (rmdir(dir.c_str()) == -1) {
599 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
600 dir.c_str(), errno, strerror(errno));
601 return;
602 }
603}
604
Elliott Hughes45964a82017-05-03 22:43:23 -0700605static int unzip_to_file(ZipArchiveHandle zip, const char* entry_name) {
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700606 unique_fd fd(make_temporary_fd());
607 if (fd == -1) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700608 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
609 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700610 return -1;
611 }
612
Yusuke Sato07447542015-06-25 14:39:19 -0700613 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700614 ZipEntry zip_entry;
615 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
616 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000617 return -1;
618 }
619
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700620 fprintf(stderr, "extracting %s (%" PRIu32 " MB)...\n", entry_name,
621 zip_entry.uncompressed_length / 1024 / 1024);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700622 int error = ExtractEntryToFile(zip, &zip_entry, fd);
623 if (error != 0) {
624 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
625 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700626 }
627
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000628 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700629 // TODO: We're leaking 'fp' here.
Elliott Hughesbbfc2812017-04-25 18:33:09 -0700630 return fd.release();
Rom Lemarchand622810c2013-06-28 09:54:59 -0700631}
632
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800633static char *strip(char *s)
634{
635 int n;
636 while(*s && isspace(*s)) s++;
637 n = strlen(s);
638 while(n-- > 0) {
639 if(!isspace(s[n])) break;
640 s[n] = 0;
641 }
642 return s;
643}
644
645#define MAX_OPTIONS 32
646static int setup_requirement_line(char *name)
647{
648 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700649 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800650 unsigned n, count;
651 char *x;
652 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800653
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800654 if (!strncmp(name, "reject ", 7)) {
655 name += 7;
656 invert = 1;
657 } else if (!strncmp(name, "require ", 8)) {
658 name += 8;
659 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700660 } else if (!strncmp(name, "require-for-product:", 20)) {
661 // Get the product and point name past it
662 prod = name + 20;
663 name = strchr(name, ' ');
664 if (!name) return -1;
665 *name = 0;
666 name += 1;
667 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800668 }
669
670 x = strchr(name, '=');
671 if (x == 0) return 0;
672 *x = 0;
673 val[0] = x + 1;
674
675 for(count = 1; count < MAX_OPTIONS; count++) {
676 x = strchr(val[count - 1],'|');
677 if (x == 0) break;
678 *x = 0;
679 val[count] = x + 1;
680 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800681
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 name = strip(name);
683 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800684
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 name = strip(name);
686 if (name == 0) return -1;
687
Elliott Hughes253c18d2015-03-18 22:47:09 -0700688 const char* var = name;
689 // Work around an unfortunate name mismatch.
690 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800691
Elliott Hughes253c18d2015-03-18 22:47:09 -0700692 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 if (out == 0) return -1;
694
695 for(n = 0; n < count; n++) {
696 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700697 if (out[n] == 0) {
698 for(size_t i = 0; i < n; ++i) {
699 free((char*) out[i]);
700 }
701 free(out);
702 return -1;
703 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704 }
705
Elliott Hughes253c18d2015-03-18 22:47:09 -0700706 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 return 0;
708}
709
Elliott Hughesfc797672015-04-07 20:12:50 -0700710static void setup_requirements(char* data, int64_t sz) {
711 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700713 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 *s++ = 0;
715 if (setup_requirement_line(data)) {
716 die("out of memory");
717 }
718 data = s;
719 } else {
720 s++;
721 }
722 }
723}
724
Elliott Hughesfc797672015-04-07 20:12:50 -0700725static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800726 fb_queue_notice("--------------------------------------------");
727 fb_queue_display("version-bootloader", "Bootloader Version...");
728 fb_queue_display("version-baseband", "Baseband Version.....");
729 fb_queue_display("serialno", "Serial Number........");
730 fb_queue_notice("--------------------------------------------");
731}
732
Rom Lemarchand622810c2013-06-28 09:54:59 -0700733static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700734{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700735 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700736 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700737 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700738 }
739
Elliott Hughesfc797672015-04-07 20:12:50 -0700740 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700741 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700742 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700743 }
744
Elliott Hughes253c18d2015-03-18 22:47:09 -0700745 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700746 if (!out_s) {
747 die("Failed to allocate sparse file array\n");
748 }
749
750 files = sparse_file_resparse(s, max_size, out_s, files);
751 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700752 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700753 }
754
755 return out_s;
756}
757
David Pursell0b156632015-10-30 11:22:01 -0700758static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700759 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700760 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
761 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800762 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700763 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700764 }
765
Elliott Hughes77c0e662015-11-02 15:51:12 -0800766 // Some bootloaders (angler, for example) send spurious whitespace too.
767 max_download_size = android::base::Trim(max_download_size);
768
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700769 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700770 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800771 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700772 return 0;
773 }
774 if (limit > 0) {
775 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
776 }
Colin Crossf8387882012-05-24 17:18:41 -0700777 return limit;
778}
779
David Pursell0b156632015-10-30 11:22:01 -0700780static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700781 int64_t limit;
782
783 if (sparse_limit == 0) {
784 return 0;
785 } else if (sparse_limit > 0) {
786 limit = sparse_limit;
787 } else {
788 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700789 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700790 }
791 if (target_sparse_limit > 0) {
792 limit = target_sparse_limit;
793 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700794 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700795 }
796 }
797
798 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500799 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700800 }
801
802 return 0;
803}
804
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700805// Until we get lazy inode table init working in make_ext4fs, we need to
806// erase partitions of type ext4 before flashing a filesystem so no stale
807// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700808static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800809 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700810 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800811 return false;
812 }
813 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700814}
815
Elliott Hughes53ec4952016-05-11 12:39:27 -0700816static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700817 int64_t sz = get_file_size(fd);
818 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700819 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700820 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700821
Elliott Hughesfc797672015-04-07 20:12:50 -0700822 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700823 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700824 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700825 sparse_file** s = load_sparse_files(fd, limit);
826 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700827 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700828 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700829 buf->type = FB_BUFFER_SPARSE;
830 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700831 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500832 buf->type = FB_BUFFER_FD;
833 buf->data = nullptr;
834 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000835 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700836 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837
Elliott Hughes53ec4952016-05-11 12:39:27 -0700838 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839}
840
Elliott Hughes53ec4952016-05-11 12:39:27 -0700841static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500842 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
843
Elliott Hughes53ec4952016-05-11 12:39:27 -0700844 if (fd == -1) {
845 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500847
848 struct stat s;
849 if (fstat(fd, &s)) {
850 return false;
851 }
852 if (!S_ISREG(s.st_mode)) {
853 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
854 return false;
855 }
856
857 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700858}
859
860static void flash_buf(const char *pname, struct fastboot_buffer *buf)
861{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700862 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700863
864 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800865 case FB_BUFFER_SPARSE: {
866 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700867 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700869 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800870 sparse_files.emplace_back(*s, sz);
871 ++s;
872 }
873
874 for (size_t i = 0; i < sparse_files.size(); ++i) {
875 const auto& pair = sparse_files[i];
876 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700877 }
878 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800879 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500880 case FB_BUFFER_FD:
881 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700882 break;
883 default:
884 die("unknown buffer type: %d", buf->type);
885 }
886}
887
Daniel Rosenberg80919472016-06-30 19:25:31 -0700888static std::string get_current_slot(Transport* transport)
889{
890 std::string current_slot;
891 if (fb_getvar(transport, "current-slot", &current_slot)) {
892 if (current_slot == "_a") return "a"; // Legacy support
893 if (current_slot == "_b") return "b"; // Legacy support
894 return current_slot;
895 }
896 return "";
897}
898
899// Legacy support
900static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700901 std::vector<std::string> suffixes;
902 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700903 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700904 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700905 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700906 suffixes = android::base::Split(suffix_list, ",");
907 // Unfortunately some devices will return an error message in the
908 // guise of a valid value. If we only see only one suffix, it's probably
909 // not real.
910 if (suffixes.size() == 1) {
911 suffixes.clear();
912 }
913 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700914}
915
Daniel Rosenberg80919472016-06-30 19:25:31 -0700916// Legacy support
917static bool supports_AB_obsolete(Transport* transport) {
918 return !get_suffixes_obsolete(transport).empty();
919}
920
921static int get_slot_count(Transport* transport) {
922 std::string var;
923 int count;
924 if (!fb_getvar(transport, "slot-count", &var)) {
925 if (supports_AB_obsolete(transport)) return 2; // Legacy support
926 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700927 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700928 return count;
929}
930
Alex Lightbb9b8a52016-06-29 09:26:44 -0700931static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700932 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700933}
934
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700935// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936static std::string get_other_slot(const std::string& current_slot, int count) {
937 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700938
Daniel Rosenberg80919472016-06-30 19:25:31 -0700939 char next = (current_slot[0] - 'a' + 1)%count + 'a';
940 return std::string(1, next);
941}
942
943static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
944 return get_other_slot(current_slot, get_slot_count(transport));
945}
946
947static std::string get_other_slot(Transport* transport, int count) {
948 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949}
950
Alex Lightbb9b8a52016-06-29 09:26:44 -0700951static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700952 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700953}
954
Daniel Rosenberg80919472016-06-30 19:25:31 -0700955static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
956 std::string slot = slot_name;
957 if (slot == "_a") slot = "a"; // Legacy support
958 if (slot == "_b") slot = "b"; // Legacy support
959 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800960 if (allow_all) {
961 return "all";
962 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 int count = get_slot_count(transport);
964 if (count > 0) {
965 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800966 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800967 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800968 }
969 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700970 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800971
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972 int count = get_slot_count(transport);
973 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800974
Daniel Rosenberg80919472016-06-30 19:25:31 -0700975 if (slot == "other") {
976 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700977 if (other == "") {
978 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800979 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700980 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800981 }
982
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
984
985 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
986 for (int i=0; i<count; i++) {
987 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700988 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700989
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700990 exit(1);
991}
992
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800994 return verify_slot(transport, slot, true);
995}
996
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700998 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800999 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000 std::string current_slot;
1001
Daniel Rosenberg80919472016-06-30 19:25:31 -07001002 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001003 /* If has-slot is not supported, the answer is no. */
1004 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001005 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001006 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 if (slot == "") {
1008 current_slot = get_current_slot(transport);
1009 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001010 die("Failed to identify current slot.\n");
1011 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001012 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001014 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001015 }
1016 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001017 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001018 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001019 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001020 }
1021 func(part);
1022 }
1023}
1024
David Pursell0b156632015-10-30 11:22:01 -07001025/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1026 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1027 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1028 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001029 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001030static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001031 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001032 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001033
Daniel Rosenberg80919472016-06-30 19:25:31 -07001034 if (slot == "all") {
1035 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1036 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001038 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001039 for (int i=0; i < get_slot_count(transport); i++) {
1040 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001041 }
1042 } else {
David Pursell0b156632015-10-30 11:22:01 -07001043 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001044 }
1045 } else {
David Pursell0b156632015-10-30 11:22:01 -07001046 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001047 }
1048}
1049
David Pursell0b156632015-10-30 11:22:01 -07001050static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001051 struct fastboot_buffer buf;
1052
Elliott Hughes53ec4952016-05-11 12:39:27 -07001053 if (!load_buf(transport, fname, &buf)) {
1054 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001055 }
1056 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001057}
1058
Elliott Hughes45964a82017-05-03 22:43:23 -07001059static void do_update_signature(ZipArchiveHandle zip, const char* filename) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001060 int64_t sz;
Elliott Hughes45964a82017-05-03 22:43:23 -07001061 void* data = unzip_file(zip, filename, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001062 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001063 fb_queue_download("signature", data, sz);
1064 fb_queue_command("signature", "installing signature");
1065}
1066
Daniel Rosenberg13454092016-06-27 19:43:11 -07001067// Sets slot_override as the active slot. If slot_override is blank,
1068// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001069static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001070 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001071 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001072 if (supports_AB_obsolete(transport)) {
1073 separator = "_"; // Legacy support
1074 } else {
1075 return;
1076 }
1077 }
1078 if (slot_override != "") {
1079 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001080 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001081 std::string current_slot = get_current_slot(transport);
1082 if (current_slot != "") {
1083 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001084 }
1085 }
1086}
1087
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001088static 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 -08001089 queue_info_dump();
1090
Wink Savilleb98762f2011-04-04 17:54:59 -07001091 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1092
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001093 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001094 int error = OpenArchive(filename, &zip);
1095 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001096 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001097 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001098 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001099
Elliott Hughesfc797672015-04-07 20:12:50 -07001100 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001101 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001102 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001103 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001104 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001105 }
1106
Elliott Hughes253c18d2015-03-18 22:47:09 -07001107 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001108
Alex Lightbb9b8a52016-06-29 09:26:44 -07001109 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001110 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001111 if (slot_override != "") {
1112 secondary = get_other_slot(transport, slot_override);
1113 } else {
1114 secondary = get_other_slot(transport);
1115 }
1116 if (secondary == "") {
1117 if (supports_AB(transport)) {
1118 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1119 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001120 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001121 }
1122 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001123 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001124 const char* slot = slot_override.c_str();
1125 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001126 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001127 slot = secondary.c_str();
1128 } else {
1129 continue;
1130 }
1131 }
1132
Elliott Hughes253c18d2015-03-18 22:47:09 -07001133 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001134 if (fd == -1) {
1135 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001136 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001137 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001138 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001139 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001140 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001141 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001142 if (!load_buf_fd(transport, fd, &buf)) {
1143 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1144 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001145
1146 auto update = [&](const std::string &partition) {
1147 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001148 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001149 fb_queue_erase(partition.c_str());
1150 }
1151 flash_buf(partition.c_str(), &buf);
1152 /* not closing the fd here since the sparse code keeps the fd around
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001153 * but hasn't mmaped data yet. The temporary file will get cleaned up when the
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001154 * program exits.
1155 */
1156 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001157 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001158 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001159
1160 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001161 if (slot_override == "all") {
1162 set_active(transport, "a");
1163 } else {
1164 set_active(transport, slot_override);
1165 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001166}
1167
Alex Lightbb9b8a52016-06-29 09:26:44 -07001168static void do_send_signature(const std::string& fn) {
1169 std::size_t extension_loc = fn.find(".img");
1170 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001171
Alex Lightbb9b8a52016-06-29 09:26:44 -07001172 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001173
1174 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001175 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001176 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001177
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178 fb_queue_download("signature", data, sz);
1179 fb_queue_command("signature", "installing signature");
1180}
1181
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001182static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001183 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 queue_info_dump();
1185
Wink Savilleb98762f2011-04-04 17:54:59 -07001186 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1187
Elliott Hughes45964a82017-05-03 22:43:23 -07001188 fname = find_item_given_name("android-info.txt");
Elliott Hughes2810d002016-04-25 14:31:18 -07001189 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001190
Elliott Hughesfc797672015-04-07 20:12:50 -07001191 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001192 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001193 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001194
1195 setup_requirements(reinterpret_cast<char*>(data), sz);
1196
Alex Lightbb9b8a52016-06-29 09:26:44 -07001197 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001198 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001199 if (slot_override != "") {
1200 secondary = get_other_slot(transport, slot_override);
1201 } else {
1202 secondary = get_other_slot(transport);
1203 }
1204 if (secondary == "") {
1205 if (supports_AB(transport)) {
1206 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1207 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001208 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001209 }
1210 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001211
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001212 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001213 const char* slot = NULL;
1214 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001215 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001216 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001217 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001218 }
1219 if (!slot) continue;
Elliott Hughes45964a82017-05-03 22:43:23 -07001220 fname = find_item_given_name(images[i].img_name);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001221 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001222 if (!load_buf(transport, fname.c_str(), &buf)) {
1223 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001224 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001225 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001226
1227 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001228 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001229 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001230 fb_queue_erase(partition.c_str());
1231 }
1232 flash_buf(partition.c_str(), &buf);
1233 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001234 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001235 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001236
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001237 if (slot_override == "all") {
1238 set_active(transport, "a");
1239 } else {
1240 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001241 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001242}
1243
Elliott Hughesd6365a72017-05-08 18:04:49 -07001244static std::string next_arg(std::vector<std::string>* args) {
1245 if (args->empty()) syntax_error("expected argument");
1246 std::string result = args->front();
1247 args->erase(args->begin());
1248 return result;
Patrick Tjin51e8b032015-09-01 08:15:23 -07001249}
1250
Elliott Hughesd6365a72017-05-08 18:04:49 -07001251static void do_bypass_unlock_command(std::vector<std::string>* args) {
1252 if (args->empty()) syntax_error("missing unlock_bootloader request");
1253
1254 std::string filename = next_arg(args);
1255
1256 int64_t sz;
1257 void* data = load_file(filename.c_str(), &sz);
1258 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
1259 fb_queue_download("unlock_message", data, sz);
1260 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1261}
1262
1263static void do_oem_command(std::vector<std::string>* args) {
1264 if (args->empty()) syntax_error("empty oem command");
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001265
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001266 std::string command("oem");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001267 while (!args->empty()) {
Elliott Hughes29d5d7d2017-05-11 15:05:13 -07001268 command += " " + next_arg(args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001269 }
Elliott Hughes4a667302017-03-15 09:37:28 -07001270 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001271}
1272
Colin Crossf8387882012-05-24 17:18:41 -07001273static int64_t parse_num(const char *arg)
1274{
1275 char *endptr;
1276 unsigned long long num;
1277
1278 num = strtoull(arg, &endptr, 0);
1279 if (endptr == arg) {
1280 return -1;
1281 }
1282
1283 if (*endptr == 'k' || *endptr == 'K') {
1284 if (num >= (-1ULL) / 1024) {
1285 return -1;
1286 }
1287 num *= 1024LL;
1288 endptr++;
1289 } else if (*endptr == 'm' || *endptr == 'M') {
1290 if (num >= (-1ULL) / (1024 * 1024)) {
1291 return -1;
1292 }
1293 num *= 1024LL * 1024LL;
1294 endptr++;
1295 } else if (*endptr == 'g' || *endptr == 'G') {
1296 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1297 return -1;
1298 }
1299 num *= 1024LL * 1024LL * 1024LL;
1300 endptr++;
1301 }
1302
1303 if (*endptr != '\0') {
1304 return -1;
1305 }
1306
1307 if (num > INT64_MAX) {
1308 return -1;
1309 }
1310
1311 return num;
1312}
1313
Connor O'Brience16a8a2017-02-06 14:39:31 -08001314static std::string fb_fix_numeric_var(std::string var) {
1315 // Some bootloaders (angler, for example), send spurious leading whitespace.
1316 var = android::base::Trim(var);
1317 // Some bootloaders (hammerhead, for example) use implicit hex.
1318 // This code used to use strtol with base 16.
1319 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1320 return var;
1321}
1322
1323static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1324 std::string sizeString;
1325 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1326 /* This device does not report flash block sizes, so return 0 */
1327 return 0;
1328 }
1329 sizeString = fb_fix_numeric_var(sizeString);
1330
1331 unsigned size;
1332 if (!android::base::ParseUint(sizeString, &size)) {
1333 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1334 return 0;
1335 }
1336 if (size < 4096 || (size & (size - 1)) != 0) {
1337 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1338 name.c_str(), size);
1339 return 0;
1340 }
1341 return size;
1342}
1343
David Pursell0b156632015-10-30 11:22:01 -07001344static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001345 const char* partition, int skip_if_not_supported,
Elliott Hughesd6365a72017-05-08 18:04:49 -07001346 const std::string& type_override, const std::string& size_override,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001347 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001348 std::string partition_type, partition_size;
1349
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001350 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001351 const char* errMsg = nullptr;
1352 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 int fd;
1354
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001355 unsigned int limit = INT_MAX;
1356 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001357 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001358 }
1359 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001360 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001361 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001362
David Pursell0b156632015-10-30 11:22:01 -07001363 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001364 errMsg = "Can't determine partition type.\n";
1365 goto failed;
1366 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001367 if (!type_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001368 if (partition_type != type_override) {
1369 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
Elliott Hughesd6365a72017-05-08 18:04:49 -07001370 partition, partition_type.c_str(), type_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001371 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001372 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001373 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001374
David Pursell0b156632015-10-30 11:22:01 -07001375 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001376 errMsg = "Unable to get partition size\n";
1377 goto failed;
1378 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001379 if (!size_override.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 if (partition_size != size_override) {
1381 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
Elliott Hughesd6365a72017-05-08 18:04:49 -07001382 partition, partition_size.c_str(), size_override.c_str());
JP Abgrall7e859742014-05-06 15:14:15 -07001383 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001384 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001385 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001386 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001387
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001388 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 if (!gen) {
1390 if (skip_if_not_supported) {
1391 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001392 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001393 return;
1394 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001395 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1396 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001397 return;
1398 }
1399
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001400 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001401 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001402 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1403 return;
1404 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001405
Elliott Hughesbbfc2812017-04-25 18:33:09 -07001406 fd = make_temporary_fd();
1407 if (fd == -1) return;
Connor O'Brience16a8a2017-02-06 14:39:31 -08001408
1409 unsigned eraseBlkSize, logicalBlkSize;
1410 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1411 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1412
1413 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001414 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001415 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001416 return;
1417 }
1418
Elliott Hughes53ec4952016-05-11 12:39:27 -07001419 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001420 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421 close(fd);
1422 return;
1423 }
1424 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001425 return;
1426
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001427failed:
1428 if (skip_if_not_supported) {
1429 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001430 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001431 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001432 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001433}
1434
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001435int main(int argc, char **argv)
1436{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001437 bool wants_wipe = false;
1438 bool wants_reboot = false;
1439 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001440 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001441 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001442 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001443 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001444 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001445 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001446 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001447 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001448 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001449 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001450 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001451
JP Abgrall7b8970c2013-03-07 17:06:41 -08001452 const struct option longopts[] = {
1453 {"base", required_argument, 0, 'b'},
1454 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001455 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001456 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001457 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001458 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001459 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001460 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001461 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001462 {"help", no_argument, 0, 'h'},
1463 {"unbuffered", no_argument, 0, 0},
1464 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001465 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001466 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001467 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001468 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001469 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001470#if !defined(_WIN32)
1471 {"wipe-and-use-fbe", no_argument, 0, 0},
1472#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001473 {0, 0, 0, 0}
1474 };
Colin Cross8879f982012-05-22 17:53:34 -07001475
1476 serial = getenv("ANDROID_SERIAL");
1477
1478 while (1) {
Elliott Hughes45964a82017-05-03 22:43:23 -07001479 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 -07001480 if (c < 0) {
1481 break;
1482 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001483 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001484 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001485 case 'a':
1486 wants_set_active = true;
1487 if (optarg)
1488 next_active = optarg;
1489 break;
Colin Cross8879f982012-05-22 17:53:34 -07001490 case 'b':
1491 base_addr = strtoul(optarg, 0, 16);
1492 break;
Colin Cross8879f982012-05-22 17:53:34 -07001493 case 'c':
1494 cmdline = optarg;
1495 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001496 case 'h':
Elliott Hughesd6365a72017-05-08 18:04:49 -07001497 return show_help();
Colin Cross8879f982012-05-22 17:53:34 -07001498 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001499 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001500 unsigned long val;
1501
1502 val = strtoul(optarg, &endptr, 0);
1503 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1504 die("invalid vendor id '%s'", optarg);
1505 vendor_id = (unsigned short)val;
1506 break;
1507 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001508 case 'k':
1509 kernel_offset = strtoul(optarg, 0, 16);
1510 break;
1511 case 'l':
1512 long_listing = 1;
1513 break;
1514 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001515 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001516 if (!page_size) die("invalid page size");
1517 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001518 case 'r':
1519 ramdisk_offset = strtoul(optarg, 0, 16);
1520 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001521 case 't':
1522 tags_offset = strtoul(optarg, 0, 16);
1523 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001524 case 's':
1525 serial = optarg;
1526 break;
1527 case 'S':
1528 sparse_limit = parse_num(optarg);
1529 if (sparse_limit < 0) {
1530 die("invalid sparse limit");
1531 }
1532 break;
1533 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001534 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001535 break;
1536 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001537 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001538 break;
Colin Cross8879f982012-05-22 17:53:34 -07001539 case '?':
1540 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001541 case 0:
1542 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001543 setvbuf(stdout, nullptr, _IONBF, 0);
1544 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001545 } else if (strcmp("version", longopts[longindex].name) == 0) {
Elliott Hughes4d4f64f2017-05-08 11:30:34 -07001546 fprintf(stdout, "fastboot version %s\n", FASTBOOT_VERSION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001547 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001548 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001549 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1550 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001551 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1552 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001553 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1554 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001555#if !defined(_WIN32)
1556 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1557 wants_wipe = true;
1558 set_fbe_marker = true;
1559#endif
1560 } else {
1561 fprintf(stderr, "Internal error in options processing for %s\n",
1562 longopts[longindex].name);
1563 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001564 }
1565 break;
Colin Cross8879f982012-05-22 17:53:34 -07001566 default:
1567 abort();
1568 }
1569 }
1570
1571 argc -= optind;
1572 argv += optind;
1573
Elliott Hughesd6365a72017-05-08 18:04:49 -07001574 if (argc == 0 && !wants_wipe && !wants_set_active) syntax_error("no command");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001575
Colin Cross8fb6e062012-07-24 16:36:41 -07001576 if (argc > 0 && !strcmp(*argv, "devices")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001577 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001578 return 0;
1579 }
1580
Colin Crossc7b75dc2012-08-29 18:17:06 -07001581 if (argc > 0 && !strcmp(*argv, "help")) {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001582 return show_help();
Colin Crossc7b75dc2012-08-29 18:17:06 -07001583 }
1584
David Pursell0b156632015-10-30 11:22:01 -07001585 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001586 if (transport == nullptr) {
1587 return 1;
1588 }
1589
Daniel Rosenberg80919472016-06-30 19:25:31 -07001590 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1591 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1592 }
1593 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1594 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001595
1596 if (wants_set_active) {
1597 if (next_active == "") {
1598 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001599 std::string current_slot;
1600 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001601 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001602 } else {
1603 wants_set_active = false;
1604 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001605 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001606 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001607 }
1608 }
1609 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001610
Elliott Hughesd6365a72017-05-08 18:04:49 -07001611 std::vector<std::string> args(argv, argv + argc);
1612 while (!args.empty()) {
1613 std::string command = next_arg(&args);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001614
Elliott Hughesd6365a72017-05-08 18:04:49 -07001615 if (command == "getvar") {
1616 std::string variable = next_arg(&args);
1617 fb_queue_display(variable.c_str(), variable.c_str());
1618 } else if (command == "erase") {
1619 std::string partition = next_arg(&args);
1620 auto erase = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001621 std::string partition_type;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001622 if (fb_getvar(transport, std::string("partition-type:") + partition,
1623 &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001624 fs_get_generator(partition_type) != nullptr) {
1625 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1626 partition_type.c_str());
1627 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001628
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001629 fb_queue_erase(partition.c_str());
1630 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001631 do_for_partitions(transport, partition, slot_override, erase, true);
1632 } else if (android::base::StartsWith(command, "format")) {
1633 // Parsing for: "format[:[type][:[size]]]"
1634 // Some valid things:
1635 // - select only the size, and leave default fs type:
1636 // format::0x4000000 userdata
1637 // - default fs type and size:
1638 // format userdata
1639 // format:: userdata
1640 std::vector<std::string> pieces = android::base::Split(command, ":");
1641 std::string type_override;
1642 if (pieces.size() > 1) type_override = pieces[1].c_str();
1643 std::string size_override;
1644 if (pieces.size() > 2) size_override = pieces[2].c_str();
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001645
Elliott Hughesd6365a72017-05-08 18:04:49 -07001646 std::string partition = next_arg(&args);
1647
1648 auto format = [&](const std::string& partition) {
David Pursell0b156632015-10-30 11:22:01 -07001649 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001650 fb_queue_erase(partition.c_str());
1651 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001652 fb_perform_format(transport, partition.c_str(), 0, type_override, size_override,
1653 "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001654 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001655 do_for_partitions(transport, partition.c_str(), slot_override, format, true);
1656 } else if (command == "signature") {
1657 std::string filename = next_arg(&args);
1658 data = load_file(filename.c_str(), &sz);
1659 if (data == nullptr) die("could not load '%s': %s", filename.c_str(), strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001660 if (sz != 256) die("signature must be 256 bytes");
1661 fb_queue_download("signature", data, sz);
1662 fb_queue_command("signature", "installing signature");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001663 } else if (command == "reboot") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001664 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001665
1666 if (args.size() == 1) {
1667 std::string what = next_arg(&args);
1668 if (what == "bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001669 wants_reboot = false;
1670 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001671 } else if (what == "emergency") {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001672 wants_reboot = false;
1673 wants_reboot_emergency = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001674 } else {
1675 syntax_error("unknown reboot target %s", what.c_str());
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001676 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001677
Elliott Hughesca85df02015-02-25 10:02:00 -08001678 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001679 if (!args.empty()) syntax_error("junk after reboot command");
1680 } else if (command == "reboot-bootloader") {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001681 wants_reboot_bootloader = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001682 } else if (command == "continue") {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001683 fb_queue_command("continue", "resuming boot");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001684 } else if (command == "boot") {
1685 std::string kernel = next_arg(&args);
1686 std::string ramdisk;
1687 if (!args.empty()) ramdisk = next_arg(&args);
1688 std::string second_stage;
1689 if (!args.empty()) second_stage = next_arg(&args);
1690
1691 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001692 fb_queue_download("boot.img", data, sz);
1693 fb_queue_command("boot", "booting");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001694 } else if (command == "flash") {
1695 std::string pname = next_arg(&args);
1696
Elliott Hughes2810d002016-04-25 14:31:18 -07001697 std::string fname;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001698 if (!args.empty()) {
1699 fname = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001700 } else {
Elliott Hughes45964a82017-05-03 22:43:23 -07001701 fname = find_item(pname);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001702 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001703 if (fname.empty()) die("cannot determine image filename for '%s'", pname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001704
1705 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001706 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001707 fb_queue_erase(partition.c_str());
1708 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001709 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001710 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001711 do_for_partitions(transport, pname.c_str(), slot_override, flash, true);
1712 } else if (command == "flash:raw") {
1713 std::string partition = next_arg(&args);
1714 std::string kernel = next_arg(&args);
1715 std::string ramdisk;
1716 if (!args.empty()) ramdisk = next_arg(&args);
1717 std::string second_stage;
1718 if (!args.empty()) second_stage = next_arg(&args);
1719
1720 data = load_bootable_image(kernel, ramdisk, second_stage, &sz, cmdline);
1721 auto flashraw = [&](const std::string& partition) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001722 fb_queue_flash(partition.c_str(), data, sz);
1723 };
Elliott Hughesd6365a72017-05-08 18:04:49 -07001724 do_for_partitions(transport, partition, slot_override, flashraw, true);
1725 } else if (command == "flashall") {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001726 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001727 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1728 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001729 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001730 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001731 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001732 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001733 } else if (command == "update") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001734 bool slot_all = (slot_override == "all");
1735 if (slot_all) {
1736 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1737 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001738 std::string filename = "update.zip";
1739 if (!args.empty()) {
1740 filename = next_arg(&args);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001741 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001742 do_update(transport, filename.c_str(), slot_override, erase_first,
1743 skip_secondary || slot_all);
Mitchell Wills31dce302016-09-26 10:26:21 -07001744 wants_reboot = true;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001745 } else if (command == "set_active") {
1746 std::string slot = verify_slot(transport, next_arg(&args), false);
1747
David Pursell04396f62016-12-15 16:16:10 -08001748 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1749 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1750 // do have slot-suffixes.
1751 std::string var;
1752 if (!fb_getvar(transport, "slot-count", &var) &&
1753 fb_getvar(transport, "slot-suffixes", &var)) {
1754 slot = "_" + slot;
1755 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001756 fb_set_active(slot.c_str());
Elliott Hughesd6365a72017-05-08 18:04:49 -07001757 } else if (command == "stage") {
1758 std::string filename = next_arg(&args);
1759
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001760 struct fastboot_buffer buf;
Elliott Hughesd6365a72017-05-08 18:04:49 -07001761 if (!load_buf(transport, filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1762 die("cannot load '%s'", filename.c_str());
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001763 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001764 fb_queue_download_fd(filename.c_str(), buf.fd, buf.sz);
1765 } else if (command == "get_staged") {
1766 std::string filename = next_arg(&args);
1767 fb_queue_upload(filename.c_str());
1768 } else if (command == "oem") {
1769 do_oem_command(&args);
1770 } else if (command == "flashing") {
1771 if (args.empty()) {
1772 syntax_error("missing 'flashing' command");
1773 } else if (args.size() == 1 && (args[0] == "unlock" || args[0] == "lock" ||
1774 args[0] == "unlock_critical" ||
1775 args[0] == "lock_critical" ||
1776 args[0] == "get_unlock_ability" ||
1777 args[0] == "get_unlock_bootloader_nonce" ||
1778 args[0] == "lock_bootloader")) {
1779 do_oem_command(&args);
1780 } else if (args.size() == 2 && args[0] == "unlock_bootloader") {
1781 do_bypass_unlock_command(&args);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001782 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001783 syntax_error("unknown 'flashing' command %s", args[0].c_str());
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001784 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001785 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001786 syntax_error("unknown command %s", command.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001787 }
1788 }
1789
1790 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001791 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001792 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001793 if (set_fbe_marker) {
1794 fprintf(stderr, "setting FBE marker...\n");
1795 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1796 if (initial_userdata_dir.empty()) {
1797 return 1;
1798 }
Elliott Hughesd6365a72017-05-08 18:04:49 -07001799 fb_perform_format(transport, "userdata", 1, "", "", initial_userdata_dir);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001800 delete_fbemarker_tmpdir(initial_userdata_dir);
1801 } else {
Elliott Hughesd6365a72017-05-08 18:04:49 -07001802 fb_perform_format(transport, "userdata", 1, "", "", "");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001803 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001804
1805 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001806 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001807 fprintf(stderr, "wiping cache...\n");
1808 fb_queue_erase("cache");
Elliott Hughesd6365a72017-05-08 18:04:49 -07001809 fb_perform_format(transport, "cache", 1, "", "", "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001810 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001811 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001812 if (wants_set_active) {
1813 fb_set_active(next_active.c_str());
1814 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001815 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001816 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001817 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001818 } else if (wants_reboot_bootloader) {
1819 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001820 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001821 } else if (wants_reboot_emergency) {
1822 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1823 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001824 }
1825
David Pursell0b156632015-10-30 11:22:01 -07001826 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001827}