blob: cb8e5c02666509101064b1c129a0653395b5eda8 [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>
Colin Crossf8387882012-05-24 17:18:41 -070058#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070059#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes253c18d2015-03-18 22:47:09 -070061#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080062#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070064#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080065#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080067#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070068#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Colin Crossf8387882012-05-24 17:18:41 -070070#ifndef O_BINARY
71#define O_BINARY 0
72#endif
73
Wink Savilleb98762f2011-04-04 17:54:59 -070074char cur_product[FB_RESPONSE_SZ + 1];
75
David Pursell2ec418a2016-01-20 08:32:08 -080076static const char* serial = nullptr;
77static const char* product = nullptr;
78static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070080static int long_listing = 0;
Chris Fries6a999712017-04-04 09:52:47 -050081// Don't resparse files in too-big chunks.
82// libsparse will support INT_MAX, but this results in large allocations, so
83// let's keep it at 1GB to avoid memory pressure on the host.
84static constexpr int64_t RESPARSE_LIMIT = 1 * 1024 * 1024 * 1024;
Colin Crossf8387882012-05-24 17:18:41 -070085static int64_t sparse_limit = -1;
86static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughesfc797672015-04-07 20:12:50 -070088static unsigned page_size = 2048;
89static unsigned base_addr = 0x10000000;
90static unsigned kernel_offset = 0x00008000;
91static unsigned ramdisk_offset = 0x01000000;
92static unsigned second_offset = 0x00f00000;
93static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080094
Paul Crowley8f7f56e2015-11-27 09:29:37 +000095static const std::string convert_fbe_marker_filename("convert_fbe");
96
Rom Lemarchand622810c2013-06-28 09:54:59 -070097enum fb_buffer_type {
98 FB_BUFFER,
99 FB_BUFFER_SPARSE,
100};
101
102struct fastboot_buffer {
103 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -0700104 void* data;
105 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700106};
107
108static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700109 char img_name[17];
110 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111 char part_name[9];
112 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700113 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700114} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700115 {"boot.img", "boot.sig", "boot", false, false},
116 {"boot_other.img", "boot.sig", "boot", true, true},
117 {"recovery.img", "recovery.sig", "recovery", true, false},
118 {"system.img", "system.sig", "system", false, false},
119 {"system_other.img", "system.sig", "system", true, true},
120 {"vendor.img", "vendor.sig", "vendor", true, false},
121 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700122 {"vbmeta.img", "vbmeta.sig", "vbmeta", true, false},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700123};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700124
Alex Lightbb9b8a52016-06-29 09:26:44 -0700125static std::string find_item_given_name(const char* img_name, const char* product) {
126 if(product) {
Elliott Hughes82ff3152016-08-31 15:07:18 -0700127 std::string path = android::base::GetExecutablePath();
Alex Lightbb9b8a52016-06-29 09:26:44 -0700128 path.erase(path.find_last_of('/'));
129 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
130 path.c_str(), product, img_name);
131 }
132
133 char *dir = getenv("ANDROID_PRODUCT_OUT");
134 if (dir == nullptr || dir[0] == '\0') {
135 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
136 }
137
138 return android::base::StringPrintf("%s/%s", dir, img_name);
139}
140
141std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700142 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700143
Elliott Hughes2810d002016-04-25 14:31:18 -0700144 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 fn = "boot.img";
146 } else if(!strcmp(item,"recovery")) {
147 fn = "recovery.img";
148 } else if(!strcmp(item,"system")) {
149 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700150 } else if(!strcmp(item,"vendor")) {
151 fn = "vendor.img";
Vineeta Srivastava20ef9862017-03-31 13:22:34 -0700152 } else if(!strcmp(item,"vbmeta")) {
153 fn = "vbmeta.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 } else if(!strcmp(item,"userdata")) {
155 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700156 } else if(!strcmp(item,"cache")) {
157 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 } else if(!strcmp(item,"info")) {
159 fn = "android-info.txt";
160 } else {
161 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700162 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 }
164
Alex Lightbb9b8a52016-06-29 09:26:44 -0700165 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166}
167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168static int64_t get_file_size(int fd) {
169 struct stat sb;
170 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700171}
172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800174 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700175 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176
Elliott Hughesfc797672015-04-07 20:12:50 -0700177 *sz = get_file_size(fd);
178 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700179 goto oops;
180 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800181
Elliott Hughesfc797672015-04-07 20:12:50 -0700182 data = (char*) malloc(*sz);
183 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Elliott Hughesfc797672015-04-07 20:12:50 -0700185 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 close(fd);
187
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188 return data;
189
190oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800191 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 close(fd);
193 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800194 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 return 0;
196}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197
Elliott Hughes2810d002016-04-25 14:31:18 -0700198static void* load_file(const std::string& path, int64_t* sz) {
199 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700200 if (fd == -1) return nullptr;
201 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700202}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800203
Elliott Hughesfc797672015-04-07 20:12:50 -0700204static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700205 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700206 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700207 return -1;
208 }
209
210 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
211 return -1;
212 }
213
Scott Anderson13081c62012-04-06 12:39:30 -0700214 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700216 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
217 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 return 0;
219}
220
Elliott Hughesfc797672015-04-07 20:12:50 -0700221static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800222 return match_fastboot_with_serial(info, serial);
223}
224
Elliott Hughesfc797672015-04-07 20:12:50 -0700225static int list_devices_callback(usb_ifc_info* info) {
226 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700228 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700230 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 if (!serial[0]) {
232 serial = "????????????";
233 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700234 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700235 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800236 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700237 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800238 printf("%-22s fastboot", serial.c_str());
239 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700240 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800241 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243
244 return -1;
245}
246
David Pursell2ec418a2016-01-20 08:32:08 -0800247// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
248// a specific device, otherwise the first USB device found will be used.
249//
250// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
251// Otherwise it blocks until the target is available.
252//
253// The returned Transport is a singleton, so multiple calls to this function will return the same
254// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700255static Transport* open_device() {
256 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800257 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258
David Pursell2ec418a2016-01-20 08:32:08 -0800259 if (transport != nullptr) {
260 return transport;
261 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800262
David Pursell4601c972016-02-05 15:35:09 -0800263 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800264 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800265 int port = 0;
266 if (serial != nullptr) {
267 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800268
David Pursell4601c972016-02-05 15:35:09 -0800269 if (android::base::StartsWith(serial, "tcp:")) {
270 protocol = Socket::Protocol::kTcp;
271 port = tcp::kDefaultPort;
272 net_address = serial + strlen("tcp:");
273 } else if (android::base::StartsWith(serial, "udp:")) {
274 protocol = Socket::Protocol::kUdp;
275 port = udp::kDefaultPort;
276 net_address = serial + strlen("udp:");
277 }
278
279 if (net_address != nullptr) {
280 std::string error;
281 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
282 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
283 error.c_str());
284 return nullptr;
285 }
David Pursell2ec418a2016-01-20 08:32:08 -0800286 }
287 }
288
289 while (true) {
290 if (!host.empty()) {
291 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800292 if (protocol == Socket::Protocol::kTcp) {
293 transport = tcp::Connect(host, port, &error).release();
294 } else if (protocol == Socket::Protocol::kUdp) {
295 transport = udp::Connect(host, port, &error).release();
296 }
297
David Pursell2ec418a2016-01-20 08:32:08 -0800298 if (transport == nullptr && announce) {
299 fprintf(stderr, "error: %s\n", error.c_str());
300 }
301 } else {
302 transport = usb_open(match_fastboot);
303 }
304
305 if (transport != nullptr) {
306 return transport;
307 }
308
David Pursell0b156632015-10-30 11:22:01 -0700309 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800310 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700311 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 }
Elliott Hughes290a2282016-11-14 17:08:47 -0800313 std::this_thread::sleep_for(std::chrono::milliseconds(1));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 }
315}
316
Elliott Hughesfc797672015-04-07 20:12:50 -0700317static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800318 // We don't actually open a USB device here,
319 // just getting our callback called so we can
320 // list all the connected devices.
321 usb_open(list_devices_callback);
322}
323
Elliott Hughesfc797672015-04-07 20:12:50 -0700324static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 fprintf(stderr,
326/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
327 "usage: fastboot [ <option> ] <command>\n"
328 "\n"
329 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700330 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700331 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700332 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg13454092016-06-27 19:43:11 -0700333 " if found -- recovery. If the device\n"
334 " supports slots, the slot that has\n"
335 " been flashed to is set as active.\n"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700336 " Secondary images may be flashed to\n"
337 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700338 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
339 " flashing lock Locks the device. Prevents flashing.\n"
340 " flashing unlock Unlocks the device. Allows flashing\n"
341 " any partition except\n"
342 " bootloader-related partitions.\n"
343 " flashing lock_critical Prevents flashing bootloader-related\n"
344 " partitions.\n"
345 " flashing unlock_critical Enables flashing bootloader-related\n"
346 " partitions.\n"
347 " flashing get_unlock_ability Queries bootloader to see if the\n"
348 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700349 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700350 " unlock nonce.\n"
351 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700352 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700353 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700354 " erase <partition> Erase a flash partition.\n"
355 " format[:[<fs type>][:[<size>]] <partition>\n"
356 " Format a flash partition. Can\n"
357 " override the fs type and/or size\n"
358 " the bootloader reports.\n"
359 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700360 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800361 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700362 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
363 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
364 " Create bootimage and flash it.\n"
365 " devices [-l] List all connected devices [with\n"
366 " device paths].\n"
367 " continue Continue with autoboot.\n"
Alexey Polyudove0bfb752017-01-03 19:39:13 -0800368 " reboot [bootloader|emergency] Reboot device [into bootloader or emergency mode].\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700369 " reboot-bootloader Reboot device into bootloader.\n"
370 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 "\n"
372 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " -w Erase userdata and cache (and format\n"
374 " if supported by partition type).\n"
375 " -u Do not erase partition before\n"
376 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800377 " -s <specific device> Specify a device. For USB, provide either\n"
378 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700379 " For ethernet, provide an address in the\n"
380 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800381 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700382 " -p <product> Specify product name.\n"
383 " -c <cmdline> Override kernel commandline.\n"
384 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800385 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700386 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800387 " --kernel-offset Specify a custom kernel offset.\n"
388 " (default: 0x00008000)\n"
389 " --ramdisk-offset Specify a custom ramdisk offset.\n"
390 " (default: 0x01000000)\n"
391 " --tags-offset Specify a custom tags offset.\n"
392 " (default: 0x00000100)\n"
393 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700394 " (default: 2048).\n"
395 " -S <size>[K|M|G] Automatically sparse files greater\n"
396 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700397 " --slot <slot> Specify slot name to be used if the\n"
398 " device supports slots. All operations\n"
399 " on partitions that support slots will\n"
400 " be done on the slot specified.\n"
401 " 'all' can be given to refer to all slots.\n"
402 " 'other' can be given to refer to a\n"
403 " non-current slot. If this flag is not\n"
404 " used, slotted partitions will default\n"
405 " to the current active slot.\n"
406 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800407 " provided, this will default to the value\n"
408 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800409 " supported, this does nothing. This will\n"
410 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700411 " --skip-secondary Will not flash secondary slots when\n"
412 " performing a flashall or update. This\n"
413 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700414 " --skip-reboot Will not reboot the device when\n"
415 " performing commands that normally\n"
416 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000417#if !defined(_WIN32)
418 " --wipe-and-use-fbe On devices which support it,\n"
419 " erase userdata and cache, and\n"
420 " enable file-based encryption\n"
421#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800422 " --unbuffered Do not buffer input or output.\n"
423 " --version Display version.\n"
424 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800425 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800426}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428static void* load_bootable_image(const char* kernel, const char* ramdisk,
429 const char* secondstage, int64_t* sz,
430 const char* cmdline) {
431 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432 fprintf(stderr, "no image specified\n");
433 return 0;
434 }
435
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 int64_t ksize;
437 void* kdata = load_file(kernel, &ksize);
438 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800439 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440 return 0;
441 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800442
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800444 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700445 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800446
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
449 return 0;
450 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800451
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800452 *sz = ksize;
453 return kdata;
454 }
455
Elliott Hughesfc797672015-04-07 20:12:50 -0700456 void* rdata = nullptr;
457 int64_t rsize = 0;
458 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800459 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700460 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800461 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 return 0;
463 }
464 }
465
Elliott Hughesfc797672015-04-07 20:12:50 -0700466 void* sdata = nullptr;
467 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200468 if (secondstage) {
469 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700470 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200471 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
472 return 0;
473 }
474 }
475
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800476 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700477 int64_t bsize = 0;
478 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800479 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200480 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800481 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700482 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800483 fprintf(stderr,"failed to create boot.img\n");
484 return 0;
485 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700486 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
487 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800489
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 return bdata;
491}
492
Elliott Hughesfc797672015-04-07 20:12:50 -0700493static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494{
Yusuke Sato07447542015-06-25 14:39:19 -0700495 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700496 ZipEntry zip_entry;
497 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700498 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000499 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 }
501
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700502 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700504 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700505 if (data == nullptr) {
506 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000507 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 }
509
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700510 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
511 if (error != 0) {
512 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800513 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000514 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000516
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800517 return data;
518}
519
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700520#if defined(_WIN32)
521
522// TODO: move this to somewhere it can be shared.
523
524#include <windows.h>
525
526// Windows' tmpfile(3) requires administrator rights because
527// it creates temporary files in the root directory.
528static FILE* win32_tmpfile() {
529 char temp_path[PATH_MAX];
530 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
531 if (nchars == 0 || nchars >= sizeof(temp_path)) {
532 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
533 return nullptr;
534 }
535
536 char filename[PATH_MAX];
537 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
538 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
539 return nullptr;
540 }
541
542 return fopen(filename, "w+bTD");
543}
544
545#define tmpfile win32_tmpfile
546
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000547static std::string make_temporary_directory() {
548 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
549 return "";
550}
551
552#else
553
554static std::string make_temporary_directory() {
555 const char *tmpdir = getenv("TMPDIR");
556 if (tmpdir == nullptr) {
557 tmpdir = P_tmpdir;
558 }
559 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
560 if (mkdtemp(&result[0]) == NULL) {
561 fprintf(stderr, "Unable to create temporary directory: %s\n",
562 strerror(errno));
563 return "";
564 }
565 return result;
566}
567
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700568#endif
569
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000570static std::string create_fbemarker_tmpdir() {
571 std::string dir = make_temporary_directory();
572 if (dir.empty()) {
573 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
574 return "";
575 }
576 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
577 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
578 if (fd == -1) {
579 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
580 marker_file.c_str(), errno, strerror(errno));
581 return "";
582 }
583 close(fd);
584 return dir;
585}
586
587static void delete_fbemarker_tmpdir(const std::string& dir) {
588 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
589 if (unlink(marker_file.c_str()) == -1) {
590 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
591 marker_file.c_str(), errno, strerror(errno));
592 return;
593 }
594 if (rmdir(dir.c_str()) == -1) {
595 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
596 dir.c_str(), errno, strerror(errno));
597 return;
598 }
599}
600
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700601static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
602 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700603 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700604 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
605 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700606 return -1;
607 }
608
Yusuke Sato07447542015-06-25 14:39:19 -0700609 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700610 ZipEntry zip_entry;
611 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
612 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700613 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000614 return -1;
615 }
616
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700617 int fd = fileno(fp);
618 int error = ExtractEntryToFile(zip, &zip_entry, fd);
619 if (error != 0) {
620 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700621 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700622 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700623 }
624
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000625 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700626 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700627 return fd;
628}
629
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800630static char *strip(char *s)
631{
632 int n;
633 while(*s && isspace(*s)) s++;
634 n = strlen(s);
635 while(n-- > 0) {
636 if(!isspace(s[n])) break;
637 s[n] = 0;
638 }
639 return s;
640}
641
642#define MAX_OPTIONS 32
643static int setup_requirement_line(char *name)
644{
645 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700646 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800647 unsigned n, count;
648 char *x;
649 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800650
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 if (!strncmp(name, "reject ", 7)) {
652 name += 7;
653 invert = 1;
654 } else if (!strncmp(name, "require ", 8)) {
655 name += 8;
656 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700657 } else if (!strncmp(name, "require-for-product:", 20)) {
658 // Get the product and point name past it
659 prod = name + 20;
660 name = strchr(name, ' ');
661 if (!name) return -1;
662 *name = 0;
663 name += 1;
664 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800665 }
666
667 x = strchr(name, '=');
668 if (x == 0) return 0;
669 *x = 0;
670 val[0] = x + 1;
671
672 for(count = 1; count < MAX_OPTIONS; count++) {
673 x = strchr(val[count - 1],'|');
674 if (x == 0) break;
675 *x = 0;
676 val[count] = x + 1;
677 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800678
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679 name = strip(name);
680 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800681
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800682 name = strip(name);
683 if (name == 0) return -1;
684
Elliott Hughes253c18d2015-03-18 22:47:09 -0700685 const char* var = name;
686 // Work around an unfortunate name mismatch.
687 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688
Elliott Hughes253c18d2015-03-18 22:47:09 -0700689 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800690 if (out == 0) return -1;
691
692 for(n = 0; n < count; n++) {
693 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700694 if (out[n] == 0) {
695 for(size_t i = 0; i < n; ++i) {
696 free((char*) out[i]);
697 }
698 free(out);
699 return -1;
700 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800701 }
702
Elliott Hughes253c18d2015-03-18 22:47:09 -0700703 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800704 return 0;
705}
706
Elliott Hughesfc797672015-04-07 20:12:50 -0700707static void setup_requirements(char* data, int64_t sz) {
708 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800709 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700710 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800711 *s++ = 0;
712 if (setup_requirement_line(data)) {
713 die("out of memory");
714 }
715 data = s;
716 } else {
717 s++;
718 }
719 }
720}
721
Elliott Hughesfc797672015-04-07 20:12:50 -0700722static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800723 fb_queue_notice("--------------------------------------------");
724 fb_queue_display("version-bootloader", "Bootloader Version...");
725 fb_queue_display("version-baseband", "Baseband Version.....");
726 fb_queue_display("serialno", "Serial Number........");
727 fb_queue_notice("--------------------------------------------");
728}
729
Rom Lemarchand622810c2013-06-28 09:54:59 -0700730static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700731{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700732 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700733 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700734 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700735 }
736
Elliott Hughesfc797672015-04-07 20:12:50 -0700737 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700738 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700739 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700740 }
741
Elliott Hughes253c18d2015-03-18 22:47:09 -0700742 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700743 if (!out_s) {
744 die("Failed to allocate sparse file array\n");
745 }
746
747 files = sparse_file_resparse(s, max_size, out_s, files);
748 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700749 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700750 }
751
752 return out_s;
753}
754
David Pursell0b156632015-10-30 11:22:01 -0700755static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700756 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700757 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
758 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800759 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700760 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762
Elliott Hughes77c0e662015-11-02 15:51:12 -0800763 // Some bootloaders (angler, for example) send spurious whitespace too.
764 max_download_size = android::base::Trim(max_download_size);
765
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700766 uint64_t limit;
Elliott Hughesda46b392016-10-11 17:09:00 -0700767 if (!android::base::ParseUint(max_download_size, &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800768 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700769 return 0;
770 }
771 if (limit > 0) {
772 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
773 }
Colin Crossf8387882012-05-24 17:18:41 -0700774 return limit;
775}
776
David Pursell0b156632015-10-30 11:22:01 -0700777static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700778 int64_t limit;
779
780 if (sparse_limit == 0) {
781 return 0;
782 } else if (sparse_limit > 0) {
783 limit = sparse_limit;
784 } else {
785 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700786 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700787 }
788 if (target_sparse_limit > 0) {
789 limit = target_sparse_limit;
790 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700791 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700792 }
793 }
794
795 if (size > limit) {
Chris Fries6a999712017-04-04 09:52:47 -0500796 return std::min(limit, RESPARSE_LIMIT);
Colin Crossf8387882012-05-24 17:18:41 -0700797 }
798
799 return 0;
800}
801
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700802// Until we get lazy inode table init working in make_ext4fs, we need to
803// erase partitions of type ext4 before flashing a filesystem so no stale
804// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700805static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800806 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700807 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800808 return false;
809 }
810 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700811}
812
Elliott Hughes53ec4952016-05-11 12:39:27 -0700813static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700814 int64_t sz = get_file_size(fd);
815 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700816 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700818
Elliott Hughesfc797672015-04-07 20:12:50 -0700819 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700820 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700821 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700822 sparse_file** s = load_sparse_files(fd, limit);
823 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700824 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700825 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700826 buf->type = FB_BUFFER_SPARSE;
827 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700828 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700829 void* data = load_fd(fd, &sz);
830 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700831 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700832 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000833 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700834 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700835
Elliott Hughes53ec4952016-05-11 12:39:27 -0700836 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837}
838
Elliott Hughes53ec4952016-05-11 12:39:27 -0700839static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
840 int fd = open(fname, O_RDONLY | O_BINARY);
841 if (fd == -1) {
842 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700843 }
David Pursell0b156632015-10-30 11:22:01 -0700844 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700845}
846
847static void flash_buf(const char *pname, struct fastboot_buffer *buf)
848{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700849 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700850
851 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800852 case FB_BUFFER_SPARSE: {
853 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700854 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700856 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800857 sparse_files.emplace_back(*s, sz);
858 ++s;
859 }
860
861 for (size_t i = 0; i < sparse_files.size(); ++i) {
862 const auto& pair = sparse_files[i];
863 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700864 }
865 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800866 }
867
Rom Lemarchand622810c2013-06-28 09:54:59 -0700868 case FB_BUFFER:
869 fb_queue_flash(pname, buf->data, buf->sz);
870 break;
871 default:
872 die("unknown buffer type: %d", buf->type);
873 }
874}
875
Daniel Rosenberg80919472016-06-30 19:25:31 -0700876static std::string get_current_slot(Transport* transport)
877{
878 std::string current_slot;
879 if (fb_getvar(transport, "current-slot", &current_slot)) {
880 if (current_slot == "_a") return "a"; // Legacy support
881 if (current_slot == "_b") return "b"; // Legacy support
882 return current_slot;
883 }
884 return "";
885}
886
887// Legacy support
888static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700889 std::vector<std::string> suffixes;
890 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700891 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700892 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700893 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700894 suffixes = android::base::Split(suffix_list, ",");
895 // Unfortunately some devices will return an error message in the
896 // guise of a valid value. If we only see only one suffix, it's probably
897 // not real.
898 if (suffixes.size() == 1) {
899 suffixes.clear();
900 }
901 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700902}
903
Daniel Rosenberg80919472016-06-30 19:25:31 -0700904// Legacy support
905static bool supports_AB_obsolete(Transport* transport) {
906 return !get_suffixes_obsolete(transport).empty();
907}
908
909static int get_slot_count(Transport* transport) {
910 std::string var;
911 int count;
912 if (!fb_getvar(transport, "slot-count", &var)) {
913 if (supports_AB_obsolete(transport)) return 2; // Legacy support
914 }
Elliott Hughesda46b392016-10-11 17:09:00 -0700915 if (!android::base::ParseInt(var, &count)) return 0;
Daniel Rosenberg80919472016-06-30 19:25:31 -0700916 return count;
917}
918
Alex Lightbb9b8a52016-06-29 09:26:44 -0700919static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700920 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700921}
922
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700923// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700924static std::string get_other_slot(const std::string& current_slot, int count) {
925 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700926
Daniel Rosenberg80919472016-06-30 19:25:31 -0700927 char next = (current_slot[0] - 'a' + 1)%count + 'a';
928 return std::string(1, next);
929}
930
931static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
932 return get_other_slot(current_slot, get_slot_count(transport));
933}
934
935static std::string get_other_slot(Transport* transport, int count) {
936 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700937}
938
Alex Lightbb9b8a52016-06-29 09:26:44 -0700939static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700940 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700941}
942
Daniel Rosenberg80919472016-06-30 19:25:31 -0700943static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
944 std::string slot = slot_name;
945 if (slot == "_a") slot = "a"; // Legacy support
946 if (slot == "_b") slot = "b"; // Legacy support
947 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800948 if (allow_all) {
949 return "all";
950 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700951 int count = get_slot_count(transport);
952 if (count > 0) {
953 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800954 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800955 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800956 }
957 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700958 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800959
Daniel Rosenberg80919472016-06-30 19:25:31 -0700960 int count = get_slot_count(transport);
961 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800962
Daniel Rosenberg80919472016-06-30 19:25:31 -0700963 if (slot == "other") {
964 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700965 if (other == "") {
966 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800967 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700968 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800969 }
970
Daniel Rosenberg80919472016-06-30 19:25:31 -0700971 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
972
973 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
974 for (int i=0; i<count; i++) {
975 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700976 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700977
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 exit(1);
979}
980
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800982 return verify_slot(transport, slot, true);
983}
984
Daniel Rosenberg80919472016-06-30 19:25:31 -0700985static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700986 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800987 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700988 std::string current_slot;
989
Daniel Rosenberg80919472016-06-30 19:25:31 -0700990 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800991 /* If has-slot is not supported, the answer is no. */
992 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700993 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800994 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700995 if (slot == "") {
996 current_slot = get_current_slot(transport);
997 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700998 die("Failed to identify current slot.\n");
999 }
Daniel Rosenberg80919472016-06-30 19:25:31 -07001000 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001001 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001002 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001003 }
1004 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001005 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -07001006 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -07001007 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001008 }
1009 func(part);
1010 }
1011}
1012
David Pursell0b156632015-10-30 11:22:01 -07001013/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1014 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1015 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1016 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001017 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001019 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001020 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001021
Daniel Rosenberg80919472016-06-30 19:25:31 -07001022 if (slot == "all") {
1023 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1024 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001025 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001026 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001027 for (int i=0; i < get_slot_count(transport); i++) {
1028 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001029 }
1030 } else {
David Pursell0b156632015-10-30 11:22:01 -07001031 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001032 }
1033 } else {
David Pursell0b156632015-10-30 11:22:01 -07001034 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001035 }
1036}
1037
David Pursell0b156632015-10-30 11:22:01 -07001038static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001039 struct fastboot_buffer buf;
1040
Elliott Hughes53ec4952016-05-11 12:39:27 -07001041 if (!load_buf(transport, fname, &buf)) {
1042 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001043 }
1044 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001045}
1046
Elliott Hughesfc797672015-04-07 20:12:50 -07001047static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1048 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001049 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001050 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001051 fb_queue_download("signature", data, sz);
1052 fb_queue_command("signature", "installing signature");
1053}
1054
Daniel Rosenberg13454092016-06-27 19:43:11 -07001055// Sets slot_override as the active slot. If slot_override is blank,
1056// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001057static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001058 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001059 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001060 if (supports_AB_obsolete(transport)) {
1061 separator = "_"; // Legacy support
1062 } else {
1063 return;
1064 }
1065 }
1066 if (slot_override != "") {
1067 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001068 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001069 std::string current_slot = get_current_slot(transport);
1070 if (current_slot != "") {
1071 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001072 }
1073 }
1074}
1075
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001076static 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 -08001077 queue_info_dump();
1078
Wink Savilleb98762f2011-04-04 17:54:59 -07001079 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1080
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001081 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001082 int error = OpenArchive(filename, &zip);
1083 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001084 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001085 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001086 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087
Elliott Hughesfc797672015-04-07 20:12:50 -07001088 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001089 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001090 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001091 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001092 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001093 }
1094
Elliott Hughes253c18d2015-03-18 22:47:09 -07001095 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001096
Alex Lightbb9b8a52016-06-29 09:26:44 -07001097 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001098 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001099 if (slot_override != "") {
1100 secondary = get_other_slot(transport, slot_override);
1101 } else {
1102 secondary = get_other_slot(transport);
1103 }
1104 if (secondary == "") {
1105 if (supports_AB(transport)) {
1106 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1107 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001108 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001109 }
1110 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001111 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001112 const char* slot = slot_override.c_str();
1113 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001114 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001115 slot = secondary.c_str();
1116 } else {
1117 continue;
1118 }
1119 }
1120
Elliott Hughes253c18d2015-03-18 22:47:09 -07001121 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001122 if (fd == -1) {
1123 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001124 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001125 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001126 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001127 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001128 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001129 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001130 if (!load_buf_fd(transport, fd, &buf)) {
1131 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1132 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001133
1134 auto update = [&](const std::string &partition) {
1135 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001136 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001137 fb_queue_erase(partition.c_str());
1138 }
1139 flash_buf(partition.c_str(), &buf);
1140 /* not closing the fd here since the sparse code keeps the fd around
1141 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1142 * program exits.
1143 */
1144 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001145 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001146 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001147
1148 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001149 if (slot_override == "all") {
1150 set_active(transport, "a");
1151 } else {
1152 set_active(transport, slot_override);
1153 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001154}
1155
Alex Lightbb9b8a52016-06-29 09:26:44 -07001156static void do_send_signature(const std::string& fn) {
1157 std::size_t extension_loc = fn.find(".img");
1158 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001159
Alex Lightbb9b8a52016-06-29 09:26:44 -07001160 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001161
1162 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001163 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001164 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001165
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001166 fb_queue_download("signature", data, sz);
1167 fb_queue_command("signature", "installing signature");
1168}
1169
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001170static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001171 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001172 queue_info_dump();
1173
Wink Savilleb98762f2011-04-04 17:54:59 -07001174 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1175
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001176 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001177 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001178
Elliott Hughesfc797672015-04-07 20:12:50 -07001179 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001180 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001181 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001182
1183 setup_requirements(reinterpret_cast<char*>(data), sz);
1184
Alex Lightbb9b8a52016-06-29 09:26:44 -07001185 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001186 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001187 if (slot_override != "") {
1188 secondary = get_other_slot(transport, slot_override);
1189 } else {
1190 secondary = get_other_slot(transport);
1191 }
1192 if (secondary == "") {
1193 if (supports_AB(transport)) {
1194 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1195 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001196 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001197 }
1198 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001199
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001200 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001201 const char* slot = NULL;
1202 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001203 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001204 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001205 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001206 }
1207 if (!slot) continue;
1208 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001209 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001210 if (!load_buf(transport, fname.c_str(), &buf)) {
1211 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001212 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001213 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001214
1215 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001216 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001217 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001218 fb_queue_erase(partition.c_str());
1219 }
1220 flash_buf(partition.c_str(), &buf);
1221 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001222 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001223 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001224
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001225 if (slot_override == "all") {
1226 set_active(transport, "a");
1227 } else {
1228 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001229 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001230}
1231
1232#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001233#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001234
Elliott Hughes45be42e2015-09-02 20:15:48 -07001235static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001236{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001237 if (argc <= 2) return 0;
1238 skip(2);
1239
1240 /*
1241 * Process unlock_bootloader, we have to load the message file
1242 * and send that to the remote device.
1243 */
1244 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001245
1246 int64_t sz;
1247 void* data = load_file(*argv, &sz);
1248 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001249 fb_queue_download("unlock_message", data, sz);
1250 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1251 skip(1);
1252 return 0;
1253}
1254
Elliott Hughes4a667302017-03-15 09:37:28 -07001255static int do_oem_command(int argc, char** argv) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001256 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001257
Elliott Hughes4a667302017-03-15 09:37:28 -07001258 std::string command;
1259 while (argc > 0) {
1260 command += *argv;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001261 skip(1);
Elliott Hughes4a667302017-03-15 09:37:28 -07001262 if (argc != 0) command += " ";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001263 }
1264
Elliott Hughes4a667302017-03-15 09:37:28 -07001265 fb_queue_command(command.c_str(), "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001266 return 0;
1267}
1268
Colin Crossf8387882012-05-24 17:18:41 -07001269static int64_t parse_num(const char *arg)
1270{
1271 char *endptr;
1272 unsigned long long num;
1273
1274 num = strtoull(arg, &endptr, 0);
1275 if (endptr == arg) {
1276 return -1;
1277 }
1278
1279 if (*endptr == 'k' || *endptr == 'K') {
1280 if (num >= (-1ULL) / 1024) {
1281 return -1;
1282 }
1283 num *= 1024LL;
1284 endptr++;
1285 } else if (*endptr == 'm' || *endptr == 'M') {
1286 if (num >= (-1ULL) / (1024 * 1024)) {
1287 return -1;
1288 }
1289 num *= 1024LL * 1024LL;
1290 endptr++;
1291 } else if (*endptr == 'g' || *endptr == 'G') {
1292 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1293 return -1;
1294 }
1295 num *= 1024LL * 1024LL * 1024LL;
1296 endptr++;
1297 }
1298
1299 if (*endptr != '\0') {
1300 return -1;
1301 }
1302
1303 if (num > INT64_MAX) {
1304 return -1;
1305 }
1306
1307 return num;
1308}
1309
Connor O'Brience16a8a2017-02-06 14:39:31 -08001310static std::string fb_fix_numeric_var(std::string var) {
1311 // Some bootloaders (angler, for example), send spurious leading whitespace.
1312 var = android::base::Trim(var);
1313 // Some bootloaders (hammerhead, for example) use implicit hex.
1314 // This code used to use strtol with base 16.
1315 if (!android::base::StartsWith(var, "0x")) var = "0x" + var;
1316 return var;
1317}
1318
1319static unsigned fb_get_flash_block_size(Transport* transport, std::string name) {
1320 std::string sizeString;
1321 if (!fb_getvar(transport, name.c_str(), &sizeString)) {
1322 /* This device does not report flash block sizes, so return 0 */
1323 return 0;
1324 }
1325 sizeString = fb_fix_numeric_var(sizeString);
1326
1327 unsigned size;
1328 if (!android::base::ParseUint(sizeString, &size)) {
1329 fprintf(stderr, "Couldn't parse %s '%s'.\n", name.c_str(), sizeString.c_str());
1330 return 0;
1331 }
1332 if (size < 4096 || (size & (size - 1)) != 0) {
1333 fprintf(stderr, "Invalid %s %u: must be a power of 2 and at least 4096.\n",
1334 name.c_str(), size);
1335 return 0;
1336 }
1337 return size;
1338}
1339
David Pursell0b156632015-10-30 11:22:01 -07001340static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001341 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001342 const char* type_override, const char* size_override,
1343 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001344 std::string partition_type, partition_size;
1345
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001346 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001347 const char* errMsg = nullptr;
1348 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349 int fd;
1350
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001351 unsigned int limit = INT_MAX;
1352 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001354 }
1355 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001356 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001357 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001358
David Pursell0b156632015-10-30 11:22:01 -07001359 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001360 errMsg = "Can't determine partition type.\n";
1361 goto failed;
1362 }
JP Abgrall7e859742014-05-06 15:14:15 -07001363 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 if (partition_type != type_override) {
1365 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1366 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001367 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001368 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001369 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001370
David Pursell0b156632015-10-30 11:22:01 -07001371 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001372 errMsg = "Unable to get partition size\n";
1373 goto failed;
1374 }
JP Abgrall7e859742014-05-06 15:14:15 -07001375 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001376 if (partition_size != size_override) {
1377 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1378 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001379 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001380 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001381 }
Connor O'Brience16a8a2017-02-06 14:39:31 -08001382 partition_size = fb_fix_numeric_var(partition_size);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001383
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001384 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385 if (!gen) {
1386 if (skip_if_not_supported) {
1387 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001388 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 return;
1390 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001391 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1392 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001393 return;
1394 }
1395
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001396 int64_t size;
Elliott Hughesda46b392016-10-11 17:09:00 -07001397 if (!android::base::ParseInt(partition_size, &size)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001398 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1399 return;
1400 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001401
1402 fd = fileno(tmpfile());
Connor O'Brience16a8a2017-02-06 14:39:31 -08001403
1404 unsigned eraseBlkSize, logicalBlkSize;
1405 eraseBlkSize = fb_get_flash_block_size(transport, "erase-block-size");
1406 logicalBlkSize = fb_get_flash_block_size(transport, "logical-block-size");
1407
1408 if (fs_generator_generate(gen, fd, size, initial_dir, eraseBlkSize, logicalBlkSize)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001409 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001410 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001411 return;
1412 }
1413
Elliott Hughes53ec4952016-05-11 12:39:27 -07001414 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001415 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001416 close(fd);
1417 return;
1418 }
1419 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001420 return;
1421
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001422failed:
1423 if (skip_if_not_supported) {
1424 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001425 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001426 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001427 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001428}
1429
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001430int main(int argc, char **argv)
1431{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001432 bool wants_wipe = false;
1433 bool wants_reboot = false;
1434 bool wants_reboot_bootloader = false;
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001435 bool wants_reboot_emergency = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001436 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001437 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001438 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001439 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001440 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001441 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001442 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001443 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001444 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001445 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001446
JP Abgrall7b8970c2013-03-07 17:06:41 -08001447 const struct option longopts[] = {
1448 {"base", required_argument, 0, 'b'},
1449 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001450 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001451 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001452 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001453 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001454 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001455 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001456 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001457 {"help", no_argument, 0, 'h'},
1458 {"unbuffered", no_argument, 0, 0},
1459 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001460 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001461 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001462 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001463 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001464 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001465#if !defined(_WIN32)
1466 {"wipe-and-use-fbe", no_argument, 0, 0},
1467#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001468 {0, 0, 0, 0}
1469 };
Colin Cross8879f982012-05-22 17:53:34 -07001470
1471 serial = getenv("ANDROID_SERIAL");
1472
1473 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001474 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 -07001475 if (c < 0) {
1476 break;
1477 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001478 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001479 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001480 case 'a':
1481 wants_set_active = true;
1482 if (optarg)
1483 next_active = optarg;
1484 break;
Colin Cross8879f982012-05-22 17:53:34 -07001485 case 'b':
1486 base_addr = strtoul(optarg, 0, 16);
1487 break;
Colin Cross8879f982012-05-22 17:53:34 -07001488 case 'c':
1489 cmdline = optarg;
1490 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001491 case 'h':
1492 usage();
1493 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001494 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001495 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001496 unsigned long val;
1497
1498 val = strtoul(optarg, &endptr, 0);
1499 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1500 die("invalid vendor id '%s'", optarg);
1501 vendor_id = (unsigned short)val;
1502 break;
1503 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001504 case 'k':
1505 kernel_offset = strtoul(optarg, 0, 16);
1506 break;
1507 case 'l':
1508 long_listing = 1;
1509 break;
1510 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001511 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001512 if (!page_size) die("invalid page size");
1513 break;
1514 case 'p':
1515 product = optarg;
1516 break;
1517 case 'r':
1518 ramdisk_offset = strtoul(optarg, 0, 16);
1519 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001520 case 't':
1521 tags_offset = strtoul(optarg, 0, 16);
1522 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001523 case 's':
1524 serial = optarg;
1525 break;
1526 case 'S':
1527 sparse_limit = parse_num(optarg);
1528 if (sparse_limit < 0) {
1529 die("invalid sparse limit");
1530 }
1531 break;
1532 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001533 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001534 break;
1535 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001536 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001537 break;
Colin Cross8879f982012-05-22 17:53:34 -07001538 case '?':
1539 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001540 case 0:
1541 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001542 setvbuf(stdout, nullptr, _IONBF, 0);
1543 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001544 } else if (strcmp("version", longopts[longindex].name) == 0) {
1545 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
Elliott Hughes1fd46df2017-03-30 15:08:28 -07001546 fprintf(stdout, "Installed as %s\n", android::base::GetExecutablePath().c_str());
Elliott Hughes379646b2015-06-02 13:50:00 -07001547 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001548 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1549 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001550 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1551 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001552 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1553 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001554#if !defined(_WIN32)
1555 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1556 wants_wipe = true;
1557 set_fbe_marker = true;
1558#endif
1559 } else {
1560 fprintf(stderr, "Internal error in options processing for %s\n",
1561 longopts[longindex].name);
1562 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001563 }
1564 break;
Colin Cross8879f982012-05-22 17:53:34 -07001565 default:
1566 abort();
1567 }
1568 }
1569
1570 argc -= optind;
1571 argv += optind;
1572
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001573 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001574 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001575 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576 }
1577
Colin Cross8fb6e062012-07-24 16:36:41 -07001578 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001579 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001580 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001581 return 0;
1582 }
1583
Colin Crossc7b75dc2012-08-29 18:17:06 -07001584 if (argc > 0 && !strcmp(*argv, "help")) {
1585 usage();
1586 return 0;
1587 }
1588
David Pursell0b156632015-10-30 11:22:01 -07001589 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001590 if (transport == nullptr) {
1591 return 1;
1592 }
1593
Daniel Rosenberg80919472016-06-30 19:25:31 -07001594 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1595 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1596 }
1597 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1598 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001599
1600 if (wants_set_active) {
1601 if (next_active == "") {
1602 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001603 std::string current_slot;
1604 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001605 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001606 } else {
1607 wants_set_active = false;
1608 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001609 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001610 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001611 }
1612 }
1613 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001614
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001615 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001616 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001617 require(2);
1618 fb_queue_display(argv[1], argv[1]);
1619 skip(2);
1620 } else if(!strcmp(*argv, "erase")) {
1621 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001622
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001623 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001624 std::string partition_type;
1625 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001626 fs_get_generator(partition_type) != nullptr) {
1627 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1628 partition_type.c_str());
1629 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001630
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001631 fb_queue_erase(partition.c_str());
1632 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001633 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001634 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001635 } else if(!strncmp(*argv, "format", strlen("format"))) {
1636 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001637 char *type_override = nullptr;
1638 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001639 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001640 /*
1641 * Parsing for: "format[:[type][:[size]]]"
1642 * Some valid things:
1643 * - select ontly the size, and leave default fs type:
1644 * format::0x4000000 userdata
1645 * - default fs type and size:
1646 * format userdata
1647 * format:: userdata
1648 */
1649 overrides = strchr(*argv, ':');
1650 if (overrides) {
1651 overrides++;
1652 size_override = strchr(overrides, ':');
1653 if (size_override) {
1654 size_override[0] = '\0';
1655 size_override++;
1656 }
1657 type_override = overrides;
1658 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001659 if (type_override && !type_override[0]) type_override = nullptr;
1660 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001661
1662 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001663 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001664 fb_queue_erase(partition.c_str());
1665 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001666 fb_perform_format(transport, partition.c_str(), 0,
1667 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001668 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001669 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001670 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001671 } else if(!strcmp(*argv, "signature")) {
1672 require(2);
1673 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001674 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001675 if (sz != 256) die("signature must be 256 bytes");
1676 fb_queue_download("signature", data, sz);
1677 fb_queue_command("signature", "installing signature");
1678 skip(2);
1679 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001680 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001681 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001682 if (argc > 0) {
1683 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001684 wants_reboot = false;
1685 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001686 skip(1);
Pavlin Radoslavov6adbd4e2017-01-25 19:07:46 -08001687 } else if (!strcmp(*argv, "emergency")) {
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001688 wants_reboot = false;
1689 wants_reboot_emergency = true;
1690 skip(1);
1691 }
Elliott Hughesca85df02015-02-25 10:02:00 -08001692 }
1693 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001694 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001695 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001696 skip(1);
1697 } else if (!strcmp(*argv, "continue")) {
1698 fb_queue_command("continue", "resuming boot");
1699 skip(1);
1700 } else if(!strcmp(*argv, "boot")) {
1701 char *kname = 0;
1702 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001703 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001704 skip(1);
1705 if (argc > 0) {
1706 kname = argv[0];
1707 skip(1);
1708 }
1709 if (argc > 0) {
1710 rname = argv[0];
1711 skip(1);
1712 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001713 if (argc > 0) {
1714 sname = argv[0];
1715 skip(1);
1716 }
1717 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001718 if (data == 0) return 1;
1719 fb_queue_download("boot.img", data, sz);
1720 fb_queue_command("boot", "booting");
1721 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001722 char* pname = argv[1];
1723 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001724 require(2);
1725 if (argc > 2) {
1726 fname = argv[2];
1727 skip(3);
1728 } else {
1729 fname = find_item(pname, product);
1730 skip(2);
1731 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001732 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001733
1734 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001735 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001736 fb_queue_erase(partition.c_str());
1737 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001738 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001739 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001740 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001741 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001742 char *kname = argv[2];
1743 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001744 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001745 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001746 skip(3);
1747 if (argc > 0) {
1748 rname = argv[0];
1749 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001750 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001751 if (argc > 0) {
1752 sname = argv[0];
1753 skip(1);
1754 }
1755 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001756 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001757 auto flashraw = [&](const std::string &partition) {
1758 fb_queue_flash(partition.c_str(), data, sz);
1759 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001760 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001761 } else if(!strcmp(*argv, "flashall")) {
1762 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001763 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001764 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1765 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001766 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001767 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001768 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001769 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001770 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001771 bool slot_all = (slot_override == "all");
1772 if (slot_all) {
1773 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1774 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001775 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001776 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001777 skip(2);
1778 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001779 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001780 skip(1);
1781 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001782 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001783 } else if(!strcmp(*argv, "set_active")) {
1784 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001785 std::string slot = verify_slot(transport, std::string(argv[1]), false);
David Pursell04396f62016-12-15 16:16:10 -08001786 // Legacy support: verify_slot() removes leading underscores, we need to put them back
1787 // in for old bootloaders. Legacy bootloaders do not have the slot-count variable but
1788 // do have slot-suffixes.
1789 std::string var;
1790 if (!fb_getvar(transport, "slot-count", &var) &&
1791 fb_getvar(transport, "slot-suffixes", &var)) {
1792 slot = "_" + slot;
1793 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001794 fb_set_active(slot.c_str());
1795 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001796 } else if(!strcmp(*argv, "oem")) {
1797 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001798 } else if(!strcmp(*argv, "flashing")) {
1799 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1800 !strcmp(*(argv+1), "lock") ||
1801 !strcmp(*(argv+1), "unlock_critical") ||
1802 !strcmp(*(argv+1), "lock_critical") ||
1803 !strcmp(*(argv+1), "get_unlock_ability") ||
1804 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1805 !strcmp(*(argv+1), "lock_bootloader"))) {
1806 argc = do_oem_command(argc, argv);
1807 } else
1808 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1809 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001810 } else {
1811 usage();
1812 return 1;
1813 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001814 } else {
1815 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001816 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001817 }
1818 }
1819
1820 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001821 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001822 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001823 if (set_fbe_marker) {
1824 fprintf(stderr, "setting FBE marker...\n");
1825 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1826 if (initial_userdata_dir.empty()) {
1827 return 1;
1828 }
1829 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1830 delete_fbemarker_tmpdir(initial_userdata_dir);
1831 } else {
1832 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1833 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001834
1835 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001836 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001837 fprintf(stderr, "wiping cache...\n");
1838 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001839 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001840 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001841 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001842 if (wants_set_active) {
1843 fb_set_active(next_active.c_str());
1844 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001845 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001846 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001847 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001848 } else if (wants_reboot_bootloader) {
1849 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001850 fb_queue_wait_for_disconnect();
Alexey Polyudove0bfb752017-01-03 19:39:13 -08001851 } else if (wants_reboot_emergency) {
1852 fb_queue_command("reboot-emergency", "rebooting into emergency download (EDL) mode");
1853 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001854 }
1855
David Pursell0b156632015-10-30 11:22:01 -07001856 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001857}