blob: 0d59901b6fcbb3086fed59ab517e1308fa86ea05 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Tsu Chiang Chuangee520552011-02-25 18:38:53 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Colin Crossf8387882012-05-24 17:18:41 -070029#define _LARGEFILE64_SOURCE
30
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070031#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032#include <errno.h>
33#include <fcntl.h>
Colin Cross8879f982012-05-22 17:53:34 -070034#include <getopt.h>
Ying Wangcf86e2f2014-05-15 20:06:40 -070035#include <inttypes.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070036#include <limits.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070037#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sys/stat.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#include <sys/time.h>
Colin Crossf8387882012-05-24 17:18:41 -070043#include <sys/types.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070044#include <unistd.h>
David Pursell2ec418a2016-01-20 08:32:08 -080045
Elliott Hughes290a2282016-11-14 17:08:47 -080046#include <chrono>
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070047#include <functional>
Elliott Hughes290a2282016-11-14 17:08:47 -080048#include <thread>
Josh Gao9da9ac52016-01-19 14:50:18 -080049#include <utility>
50#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070051
Elliott Hughes82ff3152016-08-31 15:07:18 -070052#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070053#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080055#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070056#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080057#include <android-base/strings.h>
Chris Fries0ea946c2017-04-12 10:25:57 -050058#include <android-base/unique_fd.h>
Colin Crossf8387882012-05-24 17:18:41 -070059#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070060#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Elliott Hughes253c18d2015-03-18 22:47:09 -070062#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080063#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070065#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080066#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070067#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080068#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070069#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Chris Fries0ea946c2017-04-12 10:25:57 -050071using android::base::unique_fd;
72
Colin Crossf8387882012-05-24 17:18:41 -070073#ifndef O_BINARY
74#define O_BINARY 0
75#endif
76
Wink Savilleb98762f2011-04-04 17:54:59 -070077char cur_product[FB_RESPONSE_SZ + 1];
78
David Pursell2ec418a2016-01-20 08:32:08 -080079static const char* serial = nullptr;
80static const char* product = nullptr;
81static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070083static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050084// Don't resparse files in too-big chunks.
85// libsparse will support INT_MAX, but this results in large allocations, so
86// let's keep it at 1GB to avoid memory pressure on the host.
87static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070088static int64_t sparse_limit = -1;
89static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090
Elliott Hughesfc797672015-04-07 20:12:50 -070091static unsigned page_size = 2048;
92static unsigned base_addr = 0x10000000;
93static unsigned kernel_offset = 0x00008000;
94static unsigned ramdisk_offset = 0x01000000;
95static unsigned second_offset = 0x00f00000;
96static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080097
Paul Crowley8f7f56e2015-11-27 09:29:37 +000098static const std::string convert_fbe_marker_filename("convert_fbe");
99
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100enum fb_buffer_type {
Chris Fries0ea946c2017-04-12 10:25:57 -0500101 FB_BUFFER_FD,
Rom Lemarchand622810c2013-06-28 09:54:59 -0700102 FB_BUFFER_SPARSE,
103};
104
105struct fastboot_buffer {
106 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700107 void* data;
108 int64_t sz;
Chris Fries0ea946c2017-04-12 10:25:57 -0500109 int fd;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700110};
111
112static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700113 char img_name[17];
114 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115 char part_name[9];
116 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700117 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700118} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700119 {"boot.img", "boot.sig", "boot", false, false},
120 {"boot_other.img", "boot.sig", "boot", true, true},
121 {"recovery.img", "recovery.sig", "recovery", true, false},
122 {"system.img", "system.sig", "system", false, false},
123 {"system_other.img", "system.sig", "system", true, true},
124 {"vendor.img", "vendor.sig", "vendor", true, false},
125 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700126 {"vbmeta.img", "vbmeta.sig", "vbmeta", true, false},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700127};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700128
Alex Lightbb9b8a52016-06-29 09:26:44 -0700129static std::string find_item_given_name(const char* img_name, const char* product) {
130 if(product) {
Elliott Hughes82ff3152016-08-31 15:07:18 -0700131 std::string path = android::base::GetExecutablePath();
Alex Lightbb9b8a52016-06-29 09:26:44 -0700132 path.erase(path.find_last_of('/'));
133 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
134 path.c_str(), product, img_name);
135 }
136
137 char *dir = getenv("ANDROID_PRODUCT_OUT");
138 if (dir == nullptr || dir[0] == '\0') {
139 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
140 }
141
142 return android::base::StringPrintf("%s/%s", dir, img_name);
143}
144
145std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700146 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700147
Elliott Hughes2810d002016-04-25 14:31:18 -0700148 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 fn = "boot.img";
150 } else if(!strcmp(item,"recovery")) {
151 fn = "recovery.img";
152 } else if(!strcmp(item,"system")) {
153 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700154 } else if(!strcmp(item,"vendor")) {
155 fn = "vendor.img";
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700156 } else if(!strcmp(item,"vbmeta")) {
157 fn = "vbmeta.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 } else if(!strcmp(item,"userdata")) {
159 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700160 } else if(!strcmp(item,"cache")) {
161 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 } else if(!strcmp(item,"info")) {
163 fn = "android-info.txt";
164 } else {
165 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700166 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
168
Alex Lightbb9b8a52016-06-29 09:26:44 -0700169 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170}
171
Elliott Hughesfc797672015-04-07 20:12:50 -0700172static int64_t get_file_size(int fd) {
173 struct stat sb;
174 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700175}
176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800178 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700179 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
Elliott Hughesfc797672015-04-07 20:12:50 -0700181 *sz = get_file_size(fd);
182 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183 goto oops;
184 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185
Elliott Hughesfc797672015-04-07 20:12:50 -0700186 data = (char*) malloc(*sz);
187 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughesfc797672015-04-07 20:12:50 -0700189 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 close(fd);
191
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 return data;
193
194oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800195 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 close(fd);
197 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800198 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 return 0;
200}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201
Elliott Hughes2810d002016-04-25 14:31:18 -0700202static void* load_file(const std::string& path, int64_t* sz) {
203 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700204 if (fd == -1) return nullptr;
205 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700206}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
Elliott Hughesfc797672015-04-07 20:12:50 -0700208static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700209 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700210 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700211 return -1;
212 }
213
214 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
215 return -1;
216 }
217
Scott Anderson13081c62012-04-06 12:39:30 -0700218 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700220 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
221 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 return 0;
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800226 return match_fastboot_with_serial(info, serial);
227}
228
Elliott Hughesfc797672015-04-07 20:12:50 -0700229static int list_devices_callback(usb_ifc_info* info) {
230 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800231 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700232 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800233 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700234 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235 if (!serial[0]) {
236 serial = "????????????";
237 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700238 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700239 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800240 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700241 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800242 printf("%-22s fastboot", serial.c_str());
243 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700244 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800245 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 }
247
248 return -1;
249}
250
David Pursell2ec418a2016-01-20 08:32:08 -0800251// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
252// a specific device, otherwise the first USB device found will be used.
253//
254// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
255// Otherwise it blocks until the target is available.
256//
257// The returned Transport is a singleton, so multiple calls to this function will return the same
258// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700259static Transport* open_device() {
260 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800261 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262
David Pursell2ec418a2016-01-20 08:32:08 -0800263 if (transport != nullptr) {
264 return transport;
265 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800266
David Pursell4601c972016-02-05 15:35:09 -0800267 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800268 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800269 int port = 0;
270 if (serial != nullptr) {
271 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800272
David Pursell4601c972016-02-05 15:35:09 -0800273 if (android::base::StartsWith(serial, "tcp:")) {
274 protocol = Socket::Protocol::kTcp;
275 port = tcp::kDefaultPort;
276 net_address = serial + strlen("tcp:");
277 } else if (android::base::StartsWith(serial, "udp:")) {
278 protocol = Socket::Protocol::kUdp;
279 port = udp::kDefaultPort;
280 net_address = serial + strlen("udp:");
281 }
282
283 if (net_address != nullptr) {
284 std::string error;
285 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
286 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
287 error.c_str());
288 return nullptr;
289 }
David Pursell2ec418a2016-01-20 08:32:08 -0800290 }
291 }
292
293 while (true) {
294 if (!host.empty()) {
295 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800296 if (protocol == Socket::Protocol::kTcp) {
297 transport = tcp::Connect(host, port, &error).release();
298 } else if (protocol == Socket::Protocol::kUdp) {
299 transport = udp::Connect(host, port, &error).release();
300 }
301
David Pursell2ec418a2016-01-20 08:32:08 -0800302 if (transport == nullptr && announce) {
303 fprintf(stderr, "error: %s\n", error.c_str());
304 }
305 } else {
306 transport = usb_open(match_fastboot);
307 }
308
309 if (transport != nullptr) {
310 return transport;
311 }
312
David Pursell0b156632015-10-30 11:22:01 -0700313 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800314 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700315 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800317 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 }
319}
320
Elliott Hughesfc797672015-04-07 20:12:50 -0700321static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322 // We don't actually open a USB device here,
323 // just getting our callback called so we can
324 // list all the connected devices.
325 usb_open(list_devices_callback);
326}
327
Elliott Hughesfc797672015-04-07 20:12:50 -0700328static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800329 fprintf(stderr,
330/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
331 "usage: fastboot [ <option> ] <command>\n"
332 "\n"
333 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700334 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700335 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700336 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700337 " if found -- recovery. If the device\n"
338 " supports slots, the slot that has\n"
339 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700340 " Secondary images may be flashed to\n"
341 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700342 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
343 " flashing lock Locks the device. Prevents flashing.\n"
344 " flashing unlock Unlocks the device. Allows flashing\n"
345 " any partition except\n"
346 " bootloader-related partitions.\n"
347 " flashing lock_critical Prevents flashing bootloader-related\n"
348 " partitions.\n"
349 " flashing unlock_critical Enables flashing bootloader-related\n"
350 " partitions.\n"
351 " flashing get_unlock_ability Queries bootloader to see if the\n"
352 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700353 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700354 " unlock nonce.\n"
355 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700356 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700357 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700358 " erase <partition> Erase a flash partition.\n"
359 " format[:[<fs type>][:[<size>]] <partition>\n"
360 " Format a flash partition. Can\n"
361 " override the fs type and/or size\n"
362 " the bootloader reports.\n"
363 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700364 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800365 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700366 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
367 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
368 " Create bootimage and flash it.\n"
369 " devices [-l] List all connected devices [with\n"
370 " device paths].\n"
371 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800372 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " reboot-bootloader Reboot device into bootloader.\n"
Jocelyn Bohr98cc2832017-01-26 19:20:53 -0800374 " oem <parameter1> ... <parameterN> Executes oem specific command.\n"
375 " stage <infile> Sends contents of <infile> to stage for\n"
376 " the next command. Supported only on\n"
377 " Android Things devices.\n"
Jocelyn Bohr91fefad2017-01-27 14:17:56 -0800378 " get_staged <outfile> Receives data to <outfile> staged by the\n"
379 " last command. Supported only on Android\n"
380 " Things devices.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700381 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 "\n"
383 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700384 " -w Erase userdata and cache (and format\n"
385 " if supported by partition type).\n"
386 " -u Do not erase partition before\n"
387 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800388 " -s <specific device> Specify a device. For USB, provide either\n"
389 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700390 " For ethernet, provide an address in the\n"
391 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800392 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700393 " -p <product> Specify product name.\n"
394 " -c <cmdline> Override kernel commandline.\n"
395 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800396 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700397 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800398 " --kernel-offset Specify a custom kernel offset.\n"
399 " (default: 0x00008000)\n"
400 " --ramdisk-offset Specify a custom ramdisk offset.\n"
401 " (default: 0x01000000)\n"
402 " --tags-offset Specify a custom tags offset.\n"
403 " (default: 0x00000100)\n"
404 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700405 " (default: 2048).\n"
406 " -S <size>[K|M|G] Automatically sparse files greater\n"
407 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700408 " --slot <slot> Specify slot name to be used if the\n"
409 " device supports slots. All operations\n"
410 " on partitions that support slots will\n"
411 " be done on the slot specified.\n"
412 " 'all' can be given to refer to all slots.\n"
413 " 'other' can be given to refer to a\n"
414 " non-current slot. If this flag is not\n"
415 " used, slotted partitions will default\n"
416 " to the current active slot.\n"
417 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800418 " provided, this will default to the value\n"
419 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800420 " supported, this does nothing. This will\n"
421 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700422 " --skip-secondary Will not flash secondary slots when\n"
423 " performing a flashall or update. This\n"
424 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700425 " --skip-reboot Will not reboot the device when\n"
426 " performing commands that normally\n"
427 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000428#if !defined(_WIN32)
429 " --wipe-and-use-fbe On devices which support it,\n"
430 " erase userdata and cache, and\n"
431 " enable file-based encryption\n"
432#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800433 " --unbuffered Do not buffer input or output.\n"
434 " --version Display version.\n"
435 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000438
Elliott Hughesfc797672015-04-07 20:12:50 -0700439static void* load_bootable_image(const char* kernel, const char* ramdisk,
440 const char* secondstage, int64_t* sz,
441 const char* cmdline) {
442 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 fprintf(stderr, "no image specified\n");
444 return 0;
445 }
446
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 int64_t ksize;
448 void* kdata = load_file(kernel, &ksize);
449 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800450 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800451 return 0;
452 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800453
Elliott Hughesfc797672015-04-07 20:12:50 -0700454 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800457
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
460 return 0;
461 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 *sz = ksize;
464 return kdata;
465 }
466
Elliott Hughesfc797672015-04-07 20:12:50 -0700467 void* rdata = nullptr;
468 int64_t rsize = 0;
469 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700471 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800472 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800473 return 0;
474 }
475 }
476
Elliott Hughesfc797672015-04-07 20:12:50 -0700477 void* sdata = nullptr;
478 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200479 if (secondstage) {
480 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700481 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200482 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
483 return 0;
484 }
485 }
486
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700488 int64_t bsize = 0;
489 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800490 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200491 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800492 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700493 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494 fprintf(stderr,"failed to create boot.img\n");
495 return 0;
496 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700497 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
498 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800500
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800501 return bdata;
502}
503
Elliott Hughesfc797672015-04-07 20:12:50 -0700504static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800505{
Yusuke Sato07447542015-06-25 14:39:19 -0700506 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700507 ZipEntry zip_entry;
508 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700509 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000510 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 }
512
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700513 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800514
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700515 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700516 if (data == nullptr) {
517 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000518 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
520
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700521 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
522 if (error != 0) {
523 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800524 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000525 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000527
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800528 return data;
529}
530
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700531#if defined(_WIN32)
532
533// TODO: move this to somewhere it can be shared.
534
535#include <windows.h>
536
537// Windows' tmpfile(3) requires administrator rights because
538// it creates temporary files in the root directory.
539static FILE* win32_tmpfile() {
540 char temp_path[PATH_MAX];
541 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
542 if (nchars == 0 || nchars >= sizeof(temp_path)) {
543 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
544 return nullptr;
545 }
546
547 char filename[PATH_MAX];
548 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
549 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
550 return nullptr;
551 }
552
553 return fopen(filename, "w+bTD");
554}
555
556#define tmpfile win32_tmpfile
557
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000558static std::string make_temporary_directory() {
559 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
560 return "";
561}
562
563#else
564
565static std::string make_temporary_directory() {
566 const char *tmpdir = getenv("TMPDIR");
567 if (tmpdir == nullptr) {
568 tmpdir = P_tmpdir;
569 }
570 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
571 if (mkdtemp(&result[0]) == NULL) {
572 fprintf(stderr, "Unable to create temporary directory: %s\n",
573 strerror(errno));
574 return "";
575 }
576 return result;
577}
578
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700579#endif
580
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000581static std::string create_fbemarker_tmpdir() {
582 std::string dir = make_temporary_directory();
583 if (dir.empty()) {
584 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
585 return "";
586 }
587 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
588 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
589 if (fd == -1) {
590 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
591 marker_file.c_str(), errno, strerror(errno));
592 return "";
593 }
594 close(fd);
595 return dir;
596}
597
598static void delete_fbemarker_tmpdir(const std::string& dir) {
599 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
600 if (unlink(marker_file.c_str()) == -1) {
601 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
602 marker_file.c_str(), errno, strerror(errno));
603 return;
604 }
605 if (rmdir(dir.c_str()) == -1) {
606 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
607 dir.c_str(), errno, strerror(errno));
608 return;
609 }
610}
611
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700612static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
613 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700614 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700615 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
616 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700617 return -1;
618 }
619
Yusuke Sato07447542015-06-25 14:39:19 -0700620 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700621 ZipEntry zip_entry;
622 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
623 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700624 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000625 return -1;
626 }
627
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700628 int fd = fileno(fp);
629 int error = ExtractEntryToFile(zip, &zip_entry, fd);
630 if (error != 0) {
631 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700632 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700633 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700634 }
635
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000636 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700637 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700638 return fd;
639}
640
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800641static char *strip(char *s)
642{
643 int n;
644 while(*s && isspace(*s)) s++;
645 n = strlen(s);
646 while(n-- > 0) {
647 if(!isspace(s[n])) break;
648 s[n] = 0;
649 }
650 return s;
651}
652
653#define MAX_OPTIONS 32
654static int setup_requirement_line(char *name)
655{
656 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700657 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800658 unsigned n, count;
659 char *x;
660 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800661
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800662 if (!strncmp(name, "reject ", 7)) {
663 name += 7;
664 invert = 1;
665 } else if (!strncmp(name, "require ", 8)) {
666 name += 8;
667 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700668 } else if (!strncmp(name, "require-for-product:", 20)) {
669 // Get the product and point name past it
670 prod = name + 20;
671 name = strchr(name, ' ');
672 if (!name) return -1;
673 *name = 0;
674 name += 1;
675 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800676 }
677
678 x = strchr(name, '=');
679 if (x == 0) return 0;
680 *x = 0;
681 val[0] = x + 1;
682
683 for(count = 1; count < MAX_OPTIONS; count++) {
684 x = strchr(val[count - 1],'|');
685 if (x == 0) break;
686 *x = 0;
687 val[count] = x + 1;
688 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800689
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 name = strip(name);
691 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800692
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 name = strip(name);
694 if (name == 0) return -1;
695
Elliott Hughes253c18d2015-03-18 22:47:09 -0700696 const char* var = name;
697 // Work around an unfortunate name mismatch.
698 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800699
Elliott Hughes253c18d2015-03-18 22:47:09 -0700700 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 if (out == 0) return -1;
702
703 for(n = 0; n < count; n++) {
704 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700705 if (out[n] == 0) {
706 for(size_t i = 0; i < n; ++i) {
707 free((char*) out[i]);
708 }
709 free(out);
710 return -1;
711 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800712 }
713
Elliott Hughes253c18d2015-03-18 22:47:09 -0700714 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800715 return 0;
716}
717
Elliott Hughesfc797672015-04-07 20:12:50 -0700718static void setup_requirements(char* data, int64_t sz) {
719 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800720 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700721 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800722 *s++ = 0;
723 if (setup_requirement_line(data)) {
724 die("out of memory");
725 }
726 data = s;
727 } else {
728 s++;
729 }
730 }
731}
732
Elliott Hughesfc797672015-04-07 20:12:50 -0700733static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800734 fb_queue_notice("--------------------------------------------");
735 fb_queue_display("version-bootloader", "Bootloader Version...");
736 fb_queue_display("version-baseband", "Baseband Version.....");
737 fb_queue_display("serialno", "Serial Number........");
738 fb_queue_notice("--------------------------------------------");
739}
740
Rom Lemarchand622810c2013-06-28 09:54:59 -0700741static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700742{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700743 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700744 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700745 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700746 }
747
Elliott Hughesfc797672015-04-07 20:12:50 -0700748 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700749 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700750 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700751 }
752
Elliott Hughes253c18d2015-03-18 22:47:09 -0700753 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700754 if (!out_s) {
755 die("Failed to allocate sparse file array\n");
756 }
757
758 files = sparse_file_resparse(s, max_size, out_s, files);
759 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700760 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762
763 return out_s;
764}
765
David Pursell0b156632015-10-30 11:22:01 -0700766static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700767 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700768 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
769 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800770 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700771 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700772 }
773
Elliott Hughes77c0e662015-11-02 15:51:12 -0800774 // Some bootloaders (angler, for example) send spurious whitespace too.
775 max_download_size = android::base::Trim(max_download_size);
776
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700777 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700778 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800779 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700780 return 0;
781 }
782 if (limit > 0) {
783 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
784 }
Colin Crossf8387882012-05-24 17:18:41 -0700785 return limit;
786}
787
David Pursell0b156632015-10-30 11:22:01 -0700788static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700789 int64_t limit;
790
791 if (sparse_limit == 0) {
792 return 0;
793 } else if (sparse_limit > 0) {
794 limit = sparse_limit;
795 } else {
796 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700797 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700798 }
799 if (target_sparse_limit > 0) {
800 limit = target_sparse_limit;
801 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700802 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700803 }
804 }
805
806 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500807 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700808 }
809
810 return 0;
811}
812
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700813// Until we get lazy inode table init working in make_ext4fs, we need to
814// erase partitions of type ext4 before flashing a filesystem so no stale
815// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700816static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800817 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700818 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800819 return false;
820 }
821 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700822}
823
Elliott Hughes53ec4952016-05-11 12:39:27 -0700824static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700825 int64_t sz = get_file_size(fd);
826 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700827 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700828 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700829
Elliott Hughesfc797672015-04-07 20:12:50 -0700830 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700831 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700832 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700833 sparse_file** s = load_sparse_files(fd, limit);
834 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700835 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700836 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837 buf->type = FB_BUFFER_SPARSE;
838 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700839 } else {
Chris Fries0ea946c2017-04-12 10:25:57 -0500840 buf->type = FB_BUFFER_FD;
841 buf->data = nullptr;
842 buf->fd = fd;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000843 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700844 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700845
Elliott Hughes53ec4952016-05-11 12:39:27 -0700846 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700847}
848
Elliott Hughes53ec4952016-05-11 12:39:27 -0700849static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
Chris Fries0ea946c2017-04-12 10:25:57 -0500850 unique_fd fd(TEMP_FAILURE_RETRY(open(fname, O_RDONLY | O_BINARY)));
851
Elliott Hughes53ec4952016-05-11 12:39:27 -0700852 if (fd == -1) {
853 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700854 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500855
856 struct stat s;
857 if (fstat(fd, &s)) {
858 return false;
859 }
860 if (!S_ISREG(s.st_mode)) {
861 errno = S_ISDIR(s.st_mode) ? EISDIR : EINVAL;
862 return false;
863 }
864
865 return load_buf_fd(transport, fd.release(), buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700866}
867
868static void flash_buf(const char *pname, struct fastboot_buffer *buf)
869{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700870 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700871
872 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800873 case FB_BUFFER_SPARSE: {
874 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700875 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700876 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700877 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800878 sparse_files.emplace_back(*s, sz);
879 ++s;
880 }
881
882 for (size_t i = 0; i < sparse_files.size(); ++i) {
883 const auto& pair = sparse_files[i];
884 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700885 }
886 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800887 }
Chris Fries0ea946c2017-04-12 10:25:57 -0500888 case FB_BUFFER_FD:
889 fb_queue_flash_fd(pname, buf->fd, buf->sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700890 break;
891 default:
892 die("unknown buffer type: %d", buf->type);
893 }
894}
895
Daniel Rosenberg80919472016-06-30 19:25:31 -0700896static std::string get_current_slot(Transport* transport)
897{
898 std::string current_slot;
899 if (fb_getvar(transport, "current-slot", &current_slot)) {
900 if (current_slot == "_a") return "a"; // Legacy support
901 if (current_slot == "_b") return "b"; // Legacy support
902 return current_slot;
903 }
904 return "";
905}
906
907// Legacy support
908static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700909 std::vector<std::string> suffixes;
910 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700911 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700912 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700913 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700914 suffixes = android::base::Split(suffix_list, ",");
915 // Unfortunately some devices will return an error message in the
916 // guise of a valid value. If we only see only one suffix, it's probably
917 // not real.
918 if (suffixes.size() == 1) {
919 suffixes.clear();
920 }
921 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700922}
923
Daniel Rosenberg80919472016-06-30 19:25:31 -0700924// Legacy support
925static bool supports_AB_obsolete(Transport* transport) {
926 return !get_suffixes_obsolete(transport).empty();
927}
928
929static int get_slot_count(Transport* transport) {
930 std::string var;
931 int count;
932 if (!fb_getvar(transport, "slot-count", &var)) {
933 if (supports_AB_obsolete(transport)) return 2; // Legacy support
934 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700935 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700936 return count;
937}
938
Alex Lightbb9b8a52016-06-29 09:26:44 -0700939static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700940 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700941}
942
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700943// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700944static std::string get_other_slot(const std::string& current_slot, int count) {
945 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700946
Daniel Rosenberg80919472016-06-30 19:25:31 -0700947 char next = (current_slot[0] - 'a' + 1)%count + 'a';
948 return std::string(1, next);
949}
950
951static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
952 return get_other_slot(current_slot, get_slot_count(transport));
953}
954
955static std::string get_other_slot(Transport* transport, int count) {
956 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700957}
958
Alex Lightbb9b8a52016-06-29 09:26:44 -0700959static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700960 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700961}
962
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
964 std::string slot = slot_name;
965 if (slot == "_a") slot = "a"; // Legacy support
966 if (slot == "_b") slot = "b"; // Legacy support
967 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800968 if (allow_all) {
969 return "all";
970 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700971 int count = get_slot_count(transport);
972 if (count > 0) {
973 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800974 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800975 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800976 }
977 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800979
Daniel Rosenberg80919472016-06-30 19:25:31 -0700980 int count = get_slot_count(transport);
981 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800982
Daniel Rosenberg80919472016-06-30 19:25:31 -0700983 if (slot == "other") {
984 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700985 if (other == "") {
986 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800987 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700988 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800989 }
990
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
992
993 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
994 for (int i=0; i<count; i++) {
995 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700996 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700997
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998 exit(1);
999}
1000
Daniel Rosenberg80919472016-06-30 19:25:31 -07001001static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001002 return verify_slot(transport, slot, true);
1003}
1004
Daniel Rosenberg80919472016-06-30 19:25:31 -07001005static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001006 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001007 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001008 std::string current_slot;
1009
Daniel Rosenberg80919472016-06-30 19:25:31 -07001010 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001011 /* If has-slot is not supported, the answer is no. */
1012 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001013 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001014 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001015 if (slot == "") {
1016 current_slot = get_current_slot(transport);
1017 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001018 die("Failed to identify current slot.\n");
1019 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001020 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001022 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001023 }
1024 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001025 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001026 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001027 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001028 }
1029 func(part);
1030 }
1031}
1032
David Pursell0b156632015-10-30 11:22:01 -07001033/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1034 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1035 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1036 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001037 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001038static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001039 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001040 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001041
Daniel Rosenberg80919472016-06-30 19:25:31 -07001042 if (slot == "all") {
1043 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1044 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001045 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001046 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001047 for (int i=0; i < get_slot_count(transport); i++) {
1048 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001049 }
1050 } else {
David Pursell0b156632015-10-30 11:22:01 -07001051 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001052 }
1053 } else {
David Pursell0b156632015-10-30 11:22:01 -07001054 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001055 }
1056}
1057
David Pursell0b156632015-10-30 11:22:01 -07001058static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001059 struct fastboot_buffer buf;
1060
Elliott Hughes53ec4952016-05-11 12:39:27 -07001061 if (!load_buf(transport, fname, &buf)) {
1062 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001063 }
1064 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001065}
1066
Elliott Hughesfc797672015-04-07 20:12:50 -07001067static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1068 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001069 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001070 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001071 fb_queue_download("signature", data, sz);
1072 fb_queue_command("signature", "installing signature");
1073}
1074
Daniel Rosenberg13454092016-06-27 19:43:11 -07001075// Sets slot_override as the active slot. If slot_override is blank,
1076// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001077static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001078 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001079 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001080 if (supports_AB_obsolete(transport)) {
1081 separator = "_"; // Legacy support
1082 } else {
1083 return;
1084 }
1085 }
1086 if (slot_override != "") {
1087 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001088 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001089 std::string current_slot = get_current_slot(transport);
1090 if (current_slot != "") {
1091 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001092 }
1093 }
1094}
1095
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001096static 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 -08001097 queue_info_dump();
1098
Wink Savilleb98762f2011-04-04 17:54:59 -07001099 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1100
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001101 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001102 int error = OpenArchive(filename, &zip);
1103 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001104 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001105 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001106 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001107
Elliott Hughesfc797672015-04-07 20:12:50 -07001108 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001109 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001110 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001111 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001112 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001113 }
1114
Elliott Hughes253c18d2015-03-18 22:47:09 -07001115 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001116
Alex Lightbb9b8a52016-06-29 09:26:44 -07001117 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001118 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001119 if (slot_override != "") {
1120 secondary = get_other_slot(transport, slot_override);
1121 } else {
1122 secondary = get_other_slot(transport);
1123 }
1124 if (secondary == "") {
1125 if (supports_AB(transport)) {
1126 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1127 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001128 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001129 }
1130 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001131 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001132 const char* slot = slot_override.c_str();
1133 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001134 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001135 slot = secondary.c_str();
1136 } else {
1137 continue;
1138 }
1139 }
1140
Elliott Hughes253c18d2015-03-18 22:47:09 -07001141 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001142 if (fd == -1) {
1143 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001144 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001145 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001146 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001147 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001148 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001149 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001150 if (!load_buf_fd(transport, fd, &buf)) {
1151 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1152 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001153
1154 auto update = [&](const std::string &partition) {
1155 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001156 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001157 fb_queue_erase(partition.c_str());
1158 }
1159 flash_buf(partition.c_str(), &buf);
1160 /* not closing the fd here since the sparse code keeps the fd around
1161 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1162 * program exits.
1163 */
1164 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001165 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001166 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001167
1168 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001169 if (slot_override == "all") {
1170 set_active(transport, "a");
1171 } else {
1172 set_active(transport, slot_override);
1173 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001174}
1175
Alex Lightbb9b8a52016-06-29 09:26:44 -07001176static void do_send_signature(const std::string& fn) {
1177 std::size_t extension_loc = fn.find(".img");
1178 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001179
Alex Lightbb9b8a52016-06-29 09:26:44 -07001180 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001181
1182 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001183 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001184 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001185
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001186 fb_queue_download("signature", data, sz);
1187 fb_queue_command("signature", "installing signature");
1188}
1189
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001190static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001191 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001192 queue_info_dump();
1193
Wink Savilleb98762f2011-04-04 17:54:59 -07001194 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1195
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001196 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001197 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001198
Elliott Hughesfc797672015-04-07 20:12:50 -07001199 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001200 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001201 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001202
1203 setup_requirements(reinterpret_cast<char*>(data), sz);
1204
Alex Lightbb9b8a52016-06-29 09:26:44 -07001205 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001206 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001207 if (slot_override != "") {
1208 secondary = get_other_slot(transport, slot_override);
1209 } else {
1210 secondary = get_other_slot(transport);
1211 }
1212 if (secondary == "") {
1213 if (supports_AB(transport)) {
1214 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1215 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001216 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001217 }
1218 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001219
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001220 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001221 const char* slot = NULL;
1222 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001223 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001224 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001225 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001226 }
1227 if (!slot) continue;
1228 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001229 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001230 if (!load_buf(transport, fname.c_str(), &buf)) {
1231 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001232 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001233 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001234
1235 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001236 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001237 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001238 fb_queue_erase(partition.c_str());
1239 }
1240 flash_buf(partition.c_str(), &buf);
1241 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001242 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001243 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001244
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001245 if (slot_override == "all") {
1246 set_active(transport, "a");
1247 } else {
1248 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001249 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001250}
1251
1252#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001253#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001254
Elliott Hughes45be42e2015-09-02 20:15:48 -07001255static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001256{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001257 if (argc <= 2) return 0;
1258 skip(2);
1259
1260 /*
1261 * Process unlock_bootloader, we have to load the message file
1262 * and send that to the remote device.
1263 */
1264 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001265
1266 int64_t sz;
1267 void* data = load_file(*argv, &sz);
1268 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001269 fb_queue_download("unlock_message", data, sz);
1270 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1271 skip(1);
1272 return 0;
1273}
1274
Elliott Hughes4a667302017-03-15 09:37:28 -07001275static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001276 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001277
Elliott Hughes4a667302017-03-15 09:37:28 -07001278 std::string command;
1279 while (argc > 0) {
1280 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001281 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001282 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001283 }
1284
Elliott Hughes4a667302017-03-15 09:37:28 -07001285 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001286 return 0;
1287}
1288
Colin Crossf8387882012-05-24 17:18:41 -07001289static int64_t parse_num(const char *arg)
1290{
1291 char *endptr;
1292 unsigned long long num;
1293
1294 num = strtoull(arg, &endptr, 0);
1295 if (endptr == arg) {
1296 return -1;
1297 }
1298
1299 if (*endptr == 'k' || *endptr == 'K') {
1300 if (num >= (-1ULL) / 1024) {
1301 return -1;
1302 }
1303 num *= 1024LL;
1304 endptr++;
1305 } else if (*endptr == 'm' || *endptr == 'M') {
1306 if (num >= (-1ULL) / (1024 * 1024)) {
1307 return -1;
1308 }
1309 num *= 1024LL * 1024LL;
1310 endptr++;
1311 } else if (*endptr == 'g' || *endptr == 'G') {
1312 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1313 return -1;
1314 }
1315 num *= 1024LL * 1024LL * 1024LL;
1316 endptr++;
1317 }
1318
1319 if (*endptr != '\0') {
1320 return -1;
1321 }
1322
1323 if (num > INT64_MAX) {
1324 return -1;
1325 }
1326
1327 return num;
1328}
1329
Connor O'Brience16a8a2017-02-06 14:39:31 -08001330static std::string fb_fix_numeric_var(std::string var) {
1331 // Some bootloaders (angler, for example), send spurious leading whitespace.
1332 var = android::base::Trim(var);
1333 // Some bootloaders (hammerhead, for example) use implicit hex.
1334 // This code used to use strtol with base 16.
1335 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1336 return var;
1337}
1338
1339static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1340 std::string sizeString;
1341 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1342 /* This device does not report flash block sizes, so return 0 */
1343 return 0;
1344 }
1345 sizeString = fb_fix_numeric_var(sizeString);
1346
1347 unsigned size;
1348 if (!android::base::ParseUint(sizeString, &size)) {
1349 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1350 return 0;
1351 }
1352 if (size < 4096 || (size & (size - 1)) != 0) {
1353 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1354 name.c_str(), size);
1355 return 0;
1356 }
1357 return size;
1358}
1359
David Pursell0b156632015-10-30 11:22:01 -07001360static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001361 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001362 const char* type_override, const char* size_override,
1363 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 std::string partition_type, partition_size;
1365
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001366 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001367 const char* errMsg = nullptr;
1368 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001369 int fd;
1370
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001371 unsigned int limit = INT_MAX;
1372 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001373 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001374 }
1375 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001376 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001377 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001378
David Pursell0b156632015-10-30 11:22:01 -07001379 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001380 errMsg = "Can't determine partition type.\n";
1381 goto failed;
1382 }
JP Abgrall7e859742014-05-06 15:14:15 -07001383 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001384 if (partition_type != type_override) {
1385 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1386 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001387 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001388 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001389 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001390
David Pursell0b156632015-10-30 11:22:01 -07001391 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001392 errMsg = "Unable to get partition size\n";
1393 goto failed;
1394 }
JP Abgrall7e859742014-05-06 15:14:15 -07001395 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 if (partition_size != size_override) {
1397 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1398 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001399 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001400 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001401 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001402 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001403
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001404 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001405 if (!gen) {
1406 if (skip_if_not_supported) {
1407 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001408 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001409 return;
1410 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001411 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1412 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001413 return;
1414 }
1415
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001416 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001417 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001418 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1419 return;
1420 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001421
1422 fd = fileno(tmpfile());
Connor O'Brience16a8a2017-02-06 14:39:31 -08001423
1424 unsigned eraseBlkSize, logicalBlkSize;
1425 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1426 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1427
1428 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001429 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001430 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001431 return;
1432 }
1433
Elliott Hughes53ec4952016-05-11 12:39:27 -07001434 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001435 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001436 close(fd);
1437 return;
1438 }
1439 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001440 return;
1441
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001442failed:
1443 if (skip_if_not_supported) {
1444 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001445 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001446 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001447 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001448}
1449
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001450int main(int argc, char **argv)
1451{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001452 bool wants_wipe = false;
1453 bool wants_reboot = false;
1454 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001455 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001456 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001457 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001458 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001459 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001460 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001461 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001462 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001463 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001464 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001465 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001466
JP Abgrall7b8970c2013-03-07 17:06:41 -08001467 const struct option longopts[] = {
1468 {"base", required_argument, 0, 'b'},
1469 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001470 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001471 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001472 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001473 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001474 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001475 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001476 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001477 {"help", no_argument, 0, 'h'},
1478 {"unbuffered", no_argument, 0, 0},
1479 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001480 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001481 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001482 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001483 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001484 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001485#if !defined(_WIN32)
1486 {"wipe-and-use-fbe", no_argument, 0, 0},
1487#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001488 {0, 0, 0, 0}
1489 };
Colin Cross8879f982012-05-22 17:53:34 -07001490
1491 serial = getenv("ANDROID_SERIAL");
1492
1493 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001494 int c = getopt_long(argc, argv, "wub:k:n:r:t:s:S:lp:c:i:m:ha::", longopts, &longindex);
Colin Cross8879f982012-05-22 17:53:34 -07001495 if (c < 0) {
1496 break;
1497 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001498 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001499 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001500 case 'a':
1501 wants_set_active = true;
1502 if (optarg)
1503 next_active = optarg;
1504 break;
Colin Cross8879f982012-05-22 17:53:34 -07001505 case 'b':
1506 base_addr = strtoul(optarg, 0, 16);
1507 break;
Colin Cross8879f982012-05-22 17:53:34 -07001508 case 'c':
1509 cmdline = optarg;
1510 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001511 case 'h':
1512 usage();
1513 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001514 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001515 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001516 unsigned long val;
1517
1518 val = strtoul(optarg, &endptr, 0);
1519 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1520 die("invalid vendor id '%s'", optarg);
1521 vendor_id = (unsigned short)val;
1522 break;
1523 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001524 case 'k':
1525 kernel_offset = strtoul(optarg, 0, 16);
1526 break;
1527 case 'l':
1528 long_listing = 1;
1529 break;
1530 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001531 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001532 if (!page_size) die("invalid page size");
1533 break;
1534 case 'p':
1535 product = optarg;
1536 break;
1537 case 'r':
1538 ramdisk_offset = strtoul(optarg, 0, 16);
1539 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001540 case 't':
1541 tags_offset = strtoul(optarg, 0, 16);
1542 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001543 case 's':
1544 serial = optarg;
1545 break;
1546 case 'S':
1547 sparse_limit = parse_num(optarg);
1548 if (sparse_limit < 0) {
1549 die("invalid sparse limit");
1550 }
1551 break;
1552 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001553 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001554 break;
1555 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001556 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001557 break;
Colin Cross8879f982012-05-22 17:53:34 -07001558 case '?':
1559 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001560 case 0:
1561 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001562 setvbuf(stdout, nullptr, _IONBF, 0);
1563 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001564 } else if (strcmp("version", longopts[longindex].name) == 0) {
1565 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001566 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001567 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001568 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1569 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001570 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1571 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001572 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1573 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001574#if !defined(_WIN32)
1575 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1576 wants_wipe = true;
1577 set_fbe_marker = true;
1578#endif
1579 } else {
1580 fprintf(stderr, "Internal error in options processing for %s\n",
1581 longopts[longindex].name);
1582 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001583 }
1584 break;
Colin Cross8879f982012-05-22 17:53:34 -07001585 default:
1586 abort();
1587 }
1588 }
1589
1590 argc -= optind;
1591 argv += optind;
1592
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001593 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001594 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001595 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001596 }
1597
Colin Cross8fb6e062012-07-24 16:36:41 -07001598 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001599 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001600 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001601 return 0;
1602 }
1603
Colin Crossc7b75dc2012-08-29 18:17:06 -07001604 if (argc > 0 && !strcmp(*argv, "help")) {
1605 usage();
1606 return 0;
1607 }
1608
David Pursell0b156632015-10-30 11:22:01 -07001609 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001610 if (transport == nullptr) {
1611 return 1;
1612 }
1613
Daniel Rosenberg80919472016-06-30 19:25:31 -07001614 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1615 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1616 }
1617 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1618 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001619
1620 if (wants_set_active) {
1621 if (next_active == "") {
1622 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001623 std::string current_slot;
1624 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001625 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001626 } else {
1627 wants_set_active = false;
1628 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001629 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001630 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001631 }
1632 }
1633 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001634
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001635 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001636 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001637 require(2);
1638 fb_queue_display(argv[1], argv[1]);
1639 skip(2);
1640 } else if(!strcmp(*argv, "erase")) {
1641 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001642
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001643 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001644 std::string partition_type;
1645 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001646 fs_get_generator(partition_type) != nullptr) {
1647 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1648 partition_type.c_str());
1649 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001650
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001651 fb_queue_erase(partition.c_str());
1652 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001653 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001655 } else if(!strncmp(*argv, "format", strlen("format"))) {
1656 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001657 char *type_override = nullptr;
1658 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001659 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001660 /*
1661 * Parsing for: "format[:[type][:[size]]]"
1662 * Some valid things:
1663 * - select ontly the size, and leave default fs type:
1664 * format::0x4000000 userdata
1665 * - default fs type and size:
1666 * format userdata
1667 * format:: userdata
1668 */
1669 overrides = strchr(*argv, ':');
1670 if (overrides) {
1671 overrides++;
1672 size_override = strchr(overrides, ':');
1673 if (size_override) {
1674 size_override[0] = '\0';
1675 size_override++;
1676 }
1677 type_override = overrides;
1678 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001679 if (type_override && !type_override[0]) type_override = nullptr;
1680 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001681
1682 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001683 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001684 fb_queue_erase(partition.c_str());
1685 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001686 fb_perform_format(transport, partition.c_str(), 0,
1687 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001688 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001689 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001690 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001691 } else if(!strcmp(*argv, "signature")) {
1692 require(2);
1693 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001694 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001695 if (sz != 256) die("signature must be 256 bytes");
1696 fb_queue_download("signature", data, sz);
1697 fb_queue_command("signature", "installing signature");
1698 skip(2);
1699 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001700 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001701 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001702 if (argc > 0) {
1703 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001704 wants_reboot = false;
1705 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001706 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001707 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001708 wants_reboot = false;
1709 wants_reboot_emergency = true;
1710 skip(1);
1711 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001712 }
1713 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001714 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001715 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001716 skip(1);
1717 } else if (!strcmp(*argv, "continue")) {
1718 fb_queue_command("continue", "resuming boot");
1719 skip(1);
1720 } else if(!strcmp(*argv, "boot")) {
1721 char *kname = 0;
1722 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001723 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001724 skip(1);
1725 if (argc > 0) {
1726 kname = argv[0];
1727 skip(1);
1728 }
1729 if (argc > 0) {
1730 rname = argv[0];
1731 skip(1);
1732 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001733 if (argc > 0) {
1734 sname = argv[0];
1735 skip(1);
1736 }
1737 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001738 if (data == 0) return 1;
1739 fb_queue_download("boot.img", data, sz);
1740 fb_queue_command("boot", "booting");
1741 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001742 char* pname = argv[1];
1743 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001744 require(2);
1745 if (argc > 2) {
1746 fname = argv[2];
1747 skip(3);
1748 } else {
1749 fname = find_item(pname, product);
1750 skip(2);
1751 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001752 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001753
1754 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001755 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001756 fb_queue_erase(partition.c_str());
1757 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001758 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001759 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001760 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001761 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001762 char *kname = argv[2];
1763 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001764 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001765 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001766 skip(3);
1767 if (argc > 0) {
1768 rname = argv[0];
1769 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001770 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001771 if (argc > 0) {
1772 sname = argv[0];
1773 skip(1);
1774 }
1775 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001776 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001777 auto flashraw = [&](const std::string &partition) {
1778 fb_queue_flash(partition.c_str(), data, sz);
1779 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001780 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001781 } else if(!strcmp(*argv, "flashall")) {
1782 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001783 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001784 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1785 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001786 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001787 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001788 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001789 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001790 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001791 bool slot_all = (slot_override == "all");
1792 if (slot_all) {
1793 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1794 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001795 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001796 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001797 skip(2);
1798 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001799 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001800 skip(1);
1801 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001802 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001803 } else if(!strcmp(*argv, "set_active")) {
1804 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001805 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001806 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1807 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1808 // do have slot-suffixes.
1809 std::string var;
1810 if (!fb_getvar(transport, "slot-count", &var) &&
1811 fb_getvar(transport, "slot-suffixes", &var)) {
1812 slot = "_" + slot;
1813 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001814 fb_set_active(slot.c_str());
1815 skip(2);
Jocelyn Bohr98cc2832017-01-26 19:20:53 -08001816 } else if(!strcmp(*argv, "stage")) {
1817 require(2);
1818 std::string infile(argv[1]);
1819 skip(2);
1820 struct fastboot_buffer buf;
1821 if (!load_buf(transport, infile.c_str(), &buf) || buf.type != FB_BUFFER_FD) {
1822 die("cannot load '%s'", infile.c_str());
1823 }
1824 fb_queue_download_fd(infile.c_str(), buf.fd, buf.sz);
Jocelyn Bohr91fefad2017-01-27 14:17:56 -08001825 } else if(!strcmp(*argv, "get_staged")) {
1826 require(2);
1827 char *outfile = argv[1];
1828 skip(2);
1829 fb_queue_upload(outfile);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001830 } else if(!strcmp(*argv, "oem")) {
1831 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001832 } else if(!strcmp(*argv, "flashing")) {
1833 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1834 !strcmp(*(argv+1), "lock") ||
1835 !strcmp(*(argv+1), "unlock_critical") ||
1836 !strcmp(*(argv+1), "lock_critical") ||
1837 !strcmp(*(argv+1), "get_unlock_ability") ||
1838 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1839 !strcmp(*(argv+1), "lock_bootloader"))) {
1840 argc = do_oem_command(argc, argv);
1841 } else
1842 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1843 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001844 } else {
1845 usage();
1846 return 1;
1847 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001848 } else {
1849 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001850 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001851 }
1852 }
1853
1854 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001855 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001856 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001857 if (set_fbe_marker) {
1858 fprintf(stderr, "setting FBE marker...\n");
1859 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1860 if (initial_userdata_dir.empty()) {
1861 return 1;
1862 }
1863 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1864 delete_fbemarker_tmpdir(initial_userdata_dir);
1865 } else {
1866 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1867 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001868
1869 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001870 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001871 fprintf(stderr, "wiping cache...\n");
1872 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001873 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001874 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001875 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001876 if (wants_set_active) {
1877 fb_set_active(next_active.c_str());
1878 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001879 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001880 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001881 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001882 } else if (wants_reboot_bootloader) {
1883 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001884 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001885 } else if (wants_reboot_emergency) {
1886 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1887 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001888 }
1889
David Pursell0b156632015-10-30 11:22:01 -07001890 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001891}