blob: 3ae7408cb38ee71fe8dda5dd99a554c9d7fa1d9f [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
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -070046#include <functional>
Josh Gao9da9ac52016-01-19 14:50:18 -080047#include <utility>
48#include <vector>
Colin Crossf8387882012-05-24 17:18:41 -070049
Elliott Hughes749ae2d2016-06-28 14:48:45 -070050#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080052#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070053#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080054#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070055#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070056#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Elliott Hughes253c18d2015-03-18 22:47:09 -070058#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080059#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070061#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080062#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070063#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080064#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070065#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Colin Crossf8387882012-05-24 17:18:41 -070067#ifndef O_BINARY
68#define O_BINARY 0
69#endif
70
Wink Savilleb98762f2011-04-04 17:54:59 -070071char cur_product[FB_RESPONSE_SZ + 1];
72
David Pursell2ec418a2016-01-20 08:32:08 -080073static const char* serial = nullptr;
74static const char* product = nullptr;
75static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070077static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070078static int64_t sparse_limit = -1;
79static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
Elliott Hughesfc797672015-04-07 20:12:50 -070081static unsigned page_size = 2048;
82static unsigned base_addr = 0x10000000;
83static unsigned kernel_offset = 0x00008000;
84static unsigned ramdisk_offset = 0x01000000;
85static unsigned second_offset = 0x00f00000;
86static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080087
Paul Crowley8f7f56e2015-11-27 09:29:37 +000088static const std::string convert_fbe_marker_filename("convert_fbe");
89
Rom Lemarchand622810c2013-06-28 09:54:59 -070090enum fb_buffer_type {
91 FB_BUFFER,
92 FB_BUFFER_SPARSE,
93};
94
95struct fastboot_buffer {
96 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070097 void* data;
98 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -070099};
100
101static struct {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700102 char img_name[13];
103 char sig_name[13];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 char part_name[9];
105 bool is_optional;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700106} images[] = {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700107 {"boot.img", "boot.sig", "boot", false},
108 {"recovery.img", "recovery.sig", "recovery", true},
109 {"system.img", "system.sig", "system", false},
110 {"vendor.img", "vendor.sig", "vendor", true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700111};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700112
Elliott Hughes2810d002016-04-25 14:31:18 -0700113static std::string find_item(const char* item, const char* product) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700114 const char *fn;
Elliott Hughes2810d002016-04-25 14:31:18 -0700115 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 fn = "boot.img";
117 } else if(!strcmp(item,"recovery")) {
118 fn = "recovery.img";
119 } else if(!strcmp(item,"system")) {
120 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700121 } else if(!strcmp(item,"vendor")) {
122 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 } else if(!strcmp(item,"userdata")) {
124 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700125 } else if(!strcmp(item,"cache")) {
126 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 } else if(!strcmp(item,"info")) {
128 fn = "android-info.txt";
129 } else {
130 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700131 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133
Elliott Hughes2810d002016-04-25 14:31:18 -0700134 if (product) {
135 std::string path = get_my_path();
136 path.erase(path.find_last_of('/'));
137 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
138 path.c_str(), product, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800140
Elliott Hughes2810d002016-04-25 14:31:18 -0700141 char* dir = getenv("ANDROID_PRODUCT_OUT");
142 if (dir == nullptr || dir[0] == '\0') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800145
Elliott Hughes2810d002016-04-25 14:31:18 -0700146 return android::base::StringPrintf("%s/%s", dir, fn);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147}
148
Elliott Hughesfc797672015-04-07 20:12:50 -0700149static int64_t get_file_size(int fd) {
150 struct stat sb;
151 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700152}
153
Elliott Hughesfc797672015-04-07 20:12:50 -0700154static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800155 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700156 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157
Elliott Hughesfc797672015-04-07 20:12:50 -0700158 *sz = get_file_size(fd);
159 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700160 goto oops;
161 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162
Elliott Hughesfc797672015-04-07 20:12:50 -0700163 data = (char*) malloc(*sz);
164 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 close(fd);
168
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 return data;
170
171oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800172 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 close(fd);
174 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800175 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 return 0;
177}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
Elliott Hughes2810d002016-04-25 14:31:18 -0700179static void* load_file(const std::string& path, int64_t* sz) {
180 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700181 if (fd == -1) return nullptr;
182 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700183}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800184
Elliott Hughesfc797672015-04-07 20:12:50 -0700185static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700186 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700187 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700188 return -1;
189 }
190
191 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
192 return -1;
193 }
194
Scott Anderson13081c62012-04-06 12:39:30 -0700195 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700197 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
198 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 return 0;
200}
201
Elliott Hughesfc797672015-04-07 20:12:50 -0700202static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800203 return match_fastboot_with_serial(info, serial);
204}
205
Elliott Hughesfc797672015-04-07 20:12:50 -0700206static int list_devices_callback(usb_ifc_info* info) {
207 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800208 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700209 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800210 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700211 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 if (!serial[0]) {
213 serial = "????????????";
214 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700215 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700216 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800217 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700218 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800219 printf("%-22s fastboot", serial.c_str());
220 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700221 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800222 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 }
224
225 return -1;
226}
227
David Pursell2ec418a2016-01-20 08:32:08 -0800228// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
229// a specific device, otherwise the first USB device found will be used.
230//
231// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
232// Otherwise it blocks until the target is available.
233//
234// The returned Transport is a singleton, so multiple calls to this function will return the same
235// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700236static Transport* open_device() {
237 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800238 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239
David Pursell2ec418a2016-01-20 08:32:08 -0800240 if (transport != nullptr) {
241 return transport;
242 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800243
David Pursell4601c972016-02-05 15:35:09 -0800244 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800245 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800246 int port = 0;
247 if (serial != nullptr) {
248 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800249
David Pursell4601c972016-02-05 15:35:09 -0800250 if (android::base::StartsWith(serial, "tcp:")) {
251 protocol = Socket::Protocol::kTcp;
252 port = tcp::kDefaultPort;
253 net_address = serial + strlen("tcp:");
254 } else if (android::base::StartsWith(serial, "udp:")) {
255 protocol = Socket::Protocol::kUdp;
256 port = udp::kDefaultPort;
257 net_address = serial + strlen("udp:");
258 }
259
260 if (net_address != nullptr) {
261 std::string error;
262 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
263 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
264 error.c_str());
265 return nullptr;
266 }
David Pursell2ec418a2016-01-20 08:32:08 -0800267 }
268 }
269
270 while (true) {
271 if (!host.empty()) {
272 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800273 if (protocol == Socket::Protocol::kTcp) {
274 transport = tcp::Connect(host, port, &error).release();
275 } else if (protocol == Socket::Protocol::kUdp) {
276 transport = udp::Connect(host, port, &error).release();
277 }
278
David Pursell2ec418a2016-01-20 08:32:08 -0800279 if (transport == nullptr && announce) {
280 fprintf(stderr, "error: %s\n", error.c_str());
281 }
282 } else {
283 transport = usb_open(match_fastboot);
284 }
285
286 if (transport != nullptr) {
287 return transport;
288 }
289
David Pursell0b156632015-10-30 11:22:01 -0700290 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800291 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700292 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700294 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295 }
296}
297
Elliott Hughesfc797672015-04-07 20:12:50 -0700298static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 // We don't actually open a USB device here,
300 // just getting our callback called so we can
301 // list all the connected devices.
302 usb_open(list_devices_callback);
303}
304
Elliott Hughesfc797672015-04-07 20:12:50 -0700305static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306 fprintf(stderr,
307/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
308 "usage: fastboot [ <option> ] <command>\n"
309 "\n"
310 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700311 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700312 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700313 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700314 " if found -- recovery. If the device\n"
315 " supports slots, the slot that has\n"
316 " been flashed to is set as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700317 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
318 " flashing lock Locks the device. Prevents flashing.\n"
319 " flashing unlock Unlocks the device. Allows flashing\n"
320 " any partition except\n"
321 " bootloader-related partitions.\n"
322 " flashing lock_critical Prevents flashing bootloader-related\n"
323 " partitions.\n"
324 " flashing unlock_critical Enables flashing bootloader-related\n"
325 " partitions.\n"
326 " flashing get_unlock_ability Queries bootloader to see if the\n"
327 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700328 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700329 " unlock nonce.\n"
330 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700331 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700332 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700333 " erase <partition> Erase a flash partition.\n"
334 " format[:[<fs type>][:[<size>]] <partition>\n"
335 " Format a flash partition. Can\n"
336 " override the fs type and/or size\n"
337 " the bootloader reports.\n"
338 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800339 " set_active <suffix> Sets the active slot. If slots are\n"
340 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700341 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
342 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
343 " Create bootimage and flash it.\n"
344 " devices [-l] List all connected devices [with\n"
345 " device paths].\n"
346 " continue Continue with autoboot.\n"
347 " reboot [bootloader] Reboot device [into bootloader].\n"
348 " reboot-bootloader Reboot device into bootloader.\n"
349 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350 "\n"
351 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700352 " -w Erase userdata and cache (and format\n"
353 " if supported by partition type).\n"
354 " -u Do not erase partition before\n"
355 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800356 " -s <specific device> Specify a device. For USB, provide either\n"
357 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700358 " For ethernet, provide an address in the\n"
359 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800360 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700361 " -p <product> Specify product name.\n"
362 " -c <cmdline> Override kernel commandline.\n"
363 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800364 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700365 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800366 " --kernel-offset Specify a custom kernel offset.\n"
367 " (default: 0x00008000)\n"
368 " --ramdisk-offset Specify a custom ramdisk offset.\n"
369 " (default: 0x01000000)\n"
370 " --tags-offset Specify a custom tags offset.\n"
371 " (default: 0x00000100)\n"
372 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " (default: 2048).\n"
374 " -S <size>[K|M|G] Automatically sparse files greater\n"
375 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700376 " --slot <suffix> Specify slot suffix to be used if the\n"
377 " device supports slots. This will be\n"
378 " added to all partition names that use\n"
379 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800380 " to all slots. 'other' can be given to\n"
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700381 " refer to a non-current slot. If this\n"
382 " flag is not used, slotted partitions\n"
383 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800384 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800385 " provided, this will default to the value\n"
386 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800387 " supported, this does nothing. This will\n"
388 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000389#if !defined(_WIN32)
390 " --wipe-and-use-fbe On devices which support it,\n"
391 " erase userdata and cache, and\n"
392 " enable file-based encryption\n"
393#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800394 " --unbuffered Do not buffer input or output.\n"
395 " --version Display version.\n"
396 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800397 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000399
Elliott Hughesfc797672015-04-07 20:12:50 -0700400static void* load_bootable_image(const char* kernel, const char* ramdisk,
401 const char* secondstage, int64_t* sz,
402 const char* cmdline) {
403 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404 fprintf(stderr, "no image specified\n");
405 return 0;
406 }
407
Elliott Hughesfc797672015-04-07 20:12:50 -0700408 int64_t ksize;
409 void* kdata = load_file(kernel, &ksize);
410 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800411 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 return 0;
413 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800414
Elliott Hughesfc797672015-04-07 20:12:50 -0700415 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700417 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800418
Elliott Hughesfc797672015-04-07 20:12:50 -0700419 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
421 return 0;
422 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800423
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424 *sz = ksize;
425 return kdata;
426 }
427
Elliott Hughesfc797672015-04-07 20:12:50 -0700428 void* rdata = nullptr;
429 int64_t rsize = 0;
430 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700432 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800433 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 return 0;
435 }
436 }
437
Elliott Hughesfc797672015-04-07 20:12:50 -0700438 void* sdata = nullptr;
439 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200440 if (secondstage) {
441 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700442 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200443 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
444 return 0;
445 }
446 }
447
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700449 int64_t bsize = 0;
450 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800451 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200452 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800453 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700454 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 fprintf(stderr,"failed to create boot.img\n");
456 return 0;
457 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700458 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
459 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800460 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462 return bdata;
463}
464
Elliott Hughesfc797672015-04-07 20:12:50 -0700465static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800466{
Yusuke Sato07447542015-06-25 14:39:19 -0700467 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700468 ZipEntry zip_entry;
469 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700470 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000471 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472 }
473
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700474 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700476 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700477 if (data == nullptr) {
478 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000479 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800480 }
481
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700482 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
483 if (error != 0) {
484 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000486 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000488
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 return data;
490}
491
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700492#if defined(_WIN32)
493
494// TODO: move this to somewhere it can be shared.
495
496#include <windows.h>
497
498// Windows' tmpfile(3) requires administrator rights because
499// it creates temporary files in the root directory.
500static FILE* win32_tmpfile() {
501 char temp_path[PATH_MAX];
502 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
503 if (nchars == 0 || nchars >= sizeof(temp_path)) {
504 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
505 return nullptr;
506 }
507
508 char filename[PATH_MAX];
509 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
510 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
511 return nullptr;
512 }
513
514 return fopen(filename, "w+bTD");
515}
516
517#define tmpfile win32_tmpfile
518
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000519static std::string make_temporary_directory() {
520 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
521 return "";
522}
523
524#else
525
526static std::string make_temporary_directory() {
527 const char *tmpdir = getenv("TMPDIR");
528 if (tmpdir == nullptr) {
529 tmpdir = P_tmpdir;
530 }
531 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
532 if (mkdtemp(&result[0]) == NULL) {
533 fprintf(stderr, "Unable to create temporary directory: %s\n",
534 strerror(errno));
535 return "";
536 }
537 return result;
538}
539
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700540#endif
541
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000542static std::string create_fbemarker_tmpdir() {
543 std::string dir = make_temporary_directory();
544 if (dir.empty()) {
545 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
546 return "";
547 }
548 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
549 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
550 if (fd == -1) {
551 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
552 marker_file.c_str(), errno, strerror(errno));
553 return "";
554 }
555 close(fd);
556 return dir;
557}
558
559static void delete_fbemarker_tmpdir(const std::string& dir) {
560 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
561 if (unlink(marker_file.c_str()) == -1) {
562 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
563 marker_file.c_str(), errno, strerror(errno));
564 return;
565 }
566 if (rmdir(dir.c_str()) == -1) {
567 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
568 dir.c_str(), errno, strerror(errno));
569 return;
570 }
571}
572
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700573static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
574 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700575 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700576 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
577 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700578 return -1;
579 }
580
Yusuke Sato07447542015-06-25 14:39:19 -0700581 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700582 ZipEntry zip_entry;
583 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
584 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000585 return -1;
586 }
587
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700588 int fd = fileno(fp);
589 int error = ExtractEntryToFile(zip, &zip_entry, fd);
590 if (error != 0) {
591 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
592 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700593 }
594
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000595 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700596 return fd;
597}
598
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800599static char *strip(char *s)
600{
601 int n;
602 while(*s && isspace(*s)) s++;
603 n = strlen(s);
604 while(n-- > 0) {
605 if(!isspace(s[n])) break;
606 s[n] = 0;
607 }
608 return s;
609}
610
611#define MAX_OPTIONS 32
612static int setup_requirement_line(char *name)
613{
614 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700615 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800616 unsigned n, count;
617 char *x;
618 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800619
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800620 if (!strncmp(name, "reject ", 7)) {
621 name += 7;
622 invert = 1;
623 } else if (!strncmp(name, "require ", 8)) {
624 name += 8;
625 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700626 } else if (!strncmp(name, "require-for-product:", 20)) {
627 // Get the product and point name past it
628 prod = name + 20;
629 name = strchr(name, ' ');
630 if (!name) return -1;
631 *name = 0;
632 name += 1;
633 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800634 }
635
636 x = strchr(name, '=');
637 if (x == 0) return 0;
638 *x = 0;
639 val[0] = x + 1;
640
641 for(count = 1; count < MAX_OPTIONS; count++) {
642 x = strchr(val[count - 1],'|');
643 if (x == 0) break;
644 *x = 0;
645 val[count] = x + 1;
646 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800647
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800648 name = strip(name);
649 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800650
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800651 name = strip(name);
652 if (name == 0) return -1;
653
Elliott Hughes253c18d2015-03-18 22:47:09 -0700654 const char* var = name;
655 // Work around an unfortunate name mismatch.
656 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800657
Elliott Hughes253c18d2015-03-18 22:47:09 -0700658 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800659 if (out == 0) return -1;
660
661 for(n = 0; n < count; n++) {
662 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700663 if (out[n] == 0) {
664 for(size_t i = 0; i < n; ++i) {
665 free((char*) out[i]);
666 }
667 free(out);
668 return -1;
669 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 }
671
Elliott Hughes253c18d2015-03-18 22:47:09 -0700672 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 return 0;
674}
675
Elliott Hughesfc797672015-04-07 20:12:50 -0700676static void setup_requirements(char* data, int64_t sz) {
677 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800678 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700679 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800680 *s++ = 0;
681 if (setup_requirement_line(data)) {
682 die("out of memory");
683 }
684 data = s;
685 } else {
686 s++;
687 }
688 }
689}
690
Elliott Hughesfc797672015-04-07 20:12:50 -0700691static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 fb_queue_notice("--------------------------------------------");
693 fb_queue_display("version-bootloader", "Bootloader Version...");
694 fb_queue_display("version-baseband", "Baseband Version.....");
695 fb_queue_display("serialno", "Serial Number........");
696 fb_queue_notice("--------------------------------------------");
697}
698
Rom Lemarchand622810c2013-06-28 09:54:59 -0700699static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700700{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700701 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700702 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700703 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700704 }
705
Elliott Hughesfc797672015-04-07 20:12:50 -0700706 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700707 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700708 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700709 }
710
Elliott Hughes253c18d2015-03-18 22:47:09 -0700711 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700712 if (!out_s) {
713 die("Failed to allocate sparse file array\n");
714 }
715
716 files = sparse_file_resparse(s, max_size, out_s, files);
717 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700718 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700719 }
720
721 return out_s;
722}
723
David Pursell0b156632015-10-30 11:22:01 -0700724static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700725 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700726 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
727 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800728 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700729 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700730 }
731
Elliott Hughes77c0e662015-11-02 15:51:12 -0800732 // Some bootloaders (angler, for example) send spurious whitespace too.
733 max_download_size = android::base::Trim(max_download_size);
734
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700735 uint64_t limit;
736 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800737 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700738 return 0;
739 }
740 if (limit > 0) {
741 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
742 }
Colin Crossf8387882012-05-24 17:18:41 -0700743 return limit;
744}
745
David Pursell0b156632015-10-30 11:22:01 -0700746static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700747 int64_t limit;
748
749 if (sparse_limit == 0) {
750 return 0;
751 } else if (sparse_limit > 0) {
752 limit = sparse_limit;
753 } else {
754 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700755 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700756 }
757 if (target_sparse_limit > 0) {
758 limit = target_sparse_limit;
759 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700760 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700761 }
762 }
763
764 if (size > limit) {
765 return limit;
766 }
767
768 return 0;
769}
770
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700771// Until we get lazy inode table init working in make_ext4fs, we need to
772// erase partitions of type ext4 before flashing a filesystem so no stale
773// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700774static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800775 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700776 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800777 return false;
778 }
779 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700780}
781
Elliott Hughes53ec4952016-05-11 12:39:27 -0700782static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700783 int64_t sz = get_file_size(fd);
784 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700785 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700786 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700787
Elliott Hughesfc797672015-04-07 20:12:50 -0700788 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700789 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700790 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700791 sparse_file** s = load_sparse_files(fd, limit);
792 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700793 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700794 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700795 buf->type = FB_BUFFER_SPARSE;
796 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700797 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700798 void* data = load_fd(fd, &sz);
799 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700800 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700801 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000802 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700803 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700804
Elliott Hughes53ec4952016-05-11 12:39:27 -0700805 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700806}
807
Elliott Hughes53ec4952016-05-11 12:39:27 -0700808static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
809 int fd = open(fname, O_RDONLY | O_BINARY);
810 if (fd == -1) {
811 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700812 }
David Pursell0b156632015-10-30 11:22:01 -0700813 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700814}
815
816static void flash_buf(const char *pname, struct fastboot_buffer *buf)
817{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700818 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819
820 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800821 case FB_BUFFER_SPARSE: {
822 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700823 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700824 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700825 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800826 sparse_files.emplace_back(*s, sz);
827 ++s;
828 }
829
830 for (size_t i = 0; i < sparse_files.size(); ++i) {
831 const auto& pair = sparse_files[i];
832 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700833 }
834 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800835 }
836
Rom Lemarchand622810c2013-06-28 09:54:59 -0700837 case FB_BUFFER:
838 fb_queue_flash(pname, buf->data, buf->sz);
839 break;
840 default:
841 die("unknown buffer type: %d", buf->type);
842 }
843}
844
David Pursell0b156632015-10-30 11:22:01 -0700845static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700846 std::vector<std::string> suffixes;
847 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700848 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700849 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700850 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700851 suffixes = android::base::Split(suffix_list, ",");
852 // Unfortunately some devices will return an error message in the
853 // guise of a valid value. If we only see only one suffix, it's probably
854 // not real.
855 if (suffixes.size() == 1) {
856 suffixes.clear();
857 }
858 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700859}
860
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700861// Given a current slot, this returns what the 'other' slot is.
862static std::string get_other_slot(Transport* transport, std::string& current_slot) {
Alex Light6c60ea82016-06-14 14:29:09 -0700863 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700864
Alex Light6c60ea82016-06-14 14:29:09 -0700865 if (!suffixes.empty()) {
866 for (size_t i = 0; i < suffixes.size(); i++) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700867 if (current_slot == suffixes[i]) {
868 return suffixes[(i+1)%suffixes.size()];
869 }
Alex Light6c60ea82016-06-14 14:29:09 -0700870 }
Alex Light6c60ea82016-06-14 14:29:09 -0700871 }
Alex Light6c60ea82016-06-14 14:29:09 -0700872 return "";
873}
874
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800875static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700876 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800877 if (allow_all) {
878 return "all";
879 } else {
880 std::vector<std::string> suffixes = get_suffixes(transport);
881 if (!suffixes.empty()) {
882 return suffixes[0];
883 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800884 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800885 }
886 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700887 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800888
David Pursell0b156632015-10-30 11:22:01 -0700889 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800890
891 if (strcmp(slot, "other") == 0) {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700892 std::string current_slot;
893 if (!fb_getvar(transport, "current-slot", &current_slot)) {
894 die("Failed to identify current slot.");
895 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700896 std::string other = get_other_slot(transport, current_slot);
897 if (other == "") {
898 die("No known slots.");
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700899 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700900 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800901 }
902
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700903 for (const std::string &suffix : suffixes) {
904 if (suffix == slot)
905 return slot;
906 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700907 if (suffixes.empty()) {
908 fprintf(stderr, "Device does not support slots.\n");
909 } else {
910 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
911 for (const std::string &suffix : suffixes) {
912 fprintf(stderr, "%s\n", suffix.c_str());
913 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700914 }
915 exit(1);
916}
917
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800918static std::string verify_slot(Transport* transport, const char *slot) {
919 return verify_slot(transport, slot, true);
920}
921
David Pursell0b156632015-10-30 11:22:01 -0700922static void do_for_partition(Transport* transport, const char *part, const char *slot,
923 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800924 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700925 std::string current_slot;
926
David Pursell0b156632015-10-30 11:22:01 -0700927 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800928 /* If has-slot is not supported, the answer is no. */
929 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700930 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800931 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700932 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700933 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700934 die("Failed to identify current slot.\n");
935 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800936 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700937 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800938 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700939 }
940 } else {
941 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700942 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
943 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700944 }
945 func(part);
946 }
947}
948
David Pursell0b156632015-10-30 11:22:01 -0700949/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
950 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
951 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
952 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700953 */
David Pursell0b156632015-10-30 11:22:01 -0700954static void do_for_partitions(Transport* transport, const char *part, const char *slot,
955 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800956 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700957
958 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700959 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700960 die("Could not check if partition %s has slot.", part);
961 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800962 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700963 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700964 if (suffixes.empty()) {
965 die("Error reading suffixes.\n");
966 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700968 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 }
970 } else {
David Pursell0b156632015-10-30 11:22:01 -0700971 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700972 }
973 } else {
David Pursell0b156632015-10-30 11:22:01 -0700974 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700975 }
976}
977
David Pursell0b156632015-10-30 11:22:01 -0700978static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700979 struct fastboot_buffer buf;
980
Elliott Hughes53ec4952016-05-11 12:39:27 -0700981 if (!load_buf(transport, fname, &buf)) {
982 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700983 }
984 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -0700985}
986
Elliott Hughesfc797672015-04-07 20:12:50 -0700987static void do_update_signature(ZipArchiveHandle zip, char* fn) {
988 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700989 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -0700990 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800991 fb_queue_download("signature", data, sz);
992 fb_queue_command("signature", "installing signature");
993}
994
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700995// Sets slot_override as the active slot. If slot_override is blank,
996// set current slot as active instead. This clears slot-unbootable.
997static void set_active(Transport* transport, const char* slot_override) {
998 if (slot_override && slot_override[0]) {
999 fb_set_active(slot_override);
1000 } else {
1001 std::string current_slot;
1002 if (fb_getvar(transport, "current-slot", &current_slot)) {
1003 current_slot = verify_slot(transport, current_slot.c_str(), false);
1004 fb_set_active(current_slot.c_str());
1005 }
1006 }
1007}
1008
David Pursell0b156632015-10-30 11:22:01 -07001009static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001010 queue_info_dump();
1011
Wink Savilleb98762f2011-04-04 17:54:59 -07001012 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1013
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001014 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001015 int error = OpenArchive(filename, &zip);
1016 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001017 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001018 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001019 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001020
Elliott Hughesfc797672015-04-07 20:12:50 -07001021 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001022 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001023 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001024 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001025 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001026 }
1027
Elliott Hughes253c18d2015-03-18 22:47:09 -07001028 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001029
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001030 for (size_t i = 0; i < arraysize(images); ++i) {
Elliott Hughes253c18d2015-03-18 22:47:09 -07001031 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001032 if (fd == -1) {
1033 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001034 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001035 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001036 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001037 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001038 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001039 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001040 if (!load_buf_fd(transport, fd, &buf)) {
1041 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1042 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001043
1044 auto update = [&](const std::string &partition) {
1045 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001046 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001047 fb_queue_erase(partition.c_str());
1048 }
1049 flash_buf(partition.c_str(), &buf);
1050 /* not closing the fd here since the sparse code keeps the fd around
1051 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1052 * program exits.
1053 */
1054 };
David Pursell0b156632015-10-30 11:22:01 -07001055 do_for_partitions(transport, images[i].part_name, slot_override, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001057
1058 CloseArchive(zip);
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001059 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001060}
1061
Elliott Hughes2810d002016-04-25 14:31:18 -07001062static void do_send_signature(const char* filename) {
1063 if (android::base::EndsWith(filename, ".img") == false) {
1064 return;
1065 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001066
Elliott Hughes2810d002016-04-25 14:31:18 -07001067 std::string sig_path = filename;
1068 sig_path.erase(sig_path.size() - 4);
1069 sig_path += ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001070
1071 int64_t sz;
Elliott Hughes2810d002016-04-25 14:31:18 -07001072 void* data = load_file(sig_path, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001073 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001074
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001075 fb_queue_download("signature", data, sz);
1076 fb_queue_command("signature", "installing signature");
1077}
1078
David Pursell0b156632015-10-30 11:22:01 -07001079static void do_flashall(Transport* transport, const char* slot_override, int erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001080 queue_info_dump();
1081
Wink Savilleb98762f2011-04-04 17:54:59 -07001082 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1083
Elliott Hughes2810d002016-04-25 14:31:18 -07001084 std::string fname = find_item("info", product);
1085 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001086
Elliott Hughesfc797672015-04-07 20:12:50 -07001087 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001088 void* data = load_file(fname, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001089 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001090
1091 setup_requirements(reinterpret_cast<char*>(data), sz);
1092
Elliott Hughesa5dac1a2016-06-28 21:59:33 -07001093 for (size_t i = 0; i < arraysize(images); i++) {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -07001094 fname = find_item(images[i].part_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001095 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001096 if (!load_buf(transport, fname.c_str(), &buf)) {
1097 if (images[i].is_optional) continue;
1098 die("could not load '%s': %s", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001099 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001100
1101 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001102 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001103 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001104 fb_queue_erase(partition.c_str());
1105 }
1106 flash_buf(partition.c_str(), &buf);
1107 };
Daniel Rosenberg8c32da32016-06-27 16:46:50 -07001108 do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001109 }
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001110
1111 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001112}
1113
1114#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001115#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001116
Elliott Hughes45be42e2015-09-02 20:15:48 -07001117static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001118{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001119 if (argc <= 2) return 0;
1120 skip(2);
1121
1122 /*
1123 * Process unlock_bootloader, we have to load the message file
1124 * and send that to the remote device.
1125 */
1126 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001127
1128 int64_t sz;
1129 void* data = load_file(*argv, &sz);
1130 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001131 fb_queue_download("unlock_message", data, sz);
1132 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1133 skip(1);
1134 return 0;
1135}
1136
Elliott Hughes45be42e2015-09-02 20:15:48 -07001137static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001138{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001139 char command[256];
1140 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001142 command[0] = 0;
1143 while(1) {
1144 strcat(command,*argv);
1145 skip(1);
1146 if(argc == 0) break;
1147 strcat(command," ");
1148 }
1149
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001150 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001151 return 0;
1152}
1153
Colin Crossf8387882012-05-24 17:18:41 -07001154static int64_t parse_num(const char *arg)
1155{
1156 char *endptr;
1157 unsigned long long num;
1158
1159 num = strtoull(arg, &endptr, 0);
1160 if (endptr == arg) {
1161 return -1;
1162 }
1163
1164 if (*endptr == 'k' || *endptr == 'K') {
1165 if (num >= (-1ULL) / 1024) {
1166 return -1;
1167 }
1168 num *= 1024LL;
1169 endptr++;
1170 } else if (*endptr == 'm' || *endptr == 'M') {
1171 if (num >= (-1ULL) / (1024 * 1024)) {
1172 return -1;
1173 }
1174 num *= 1024LL * 1024LL;
1175 endptr++;
1176 } else if (*endptr == 'g' || *endptr == 'G') {
1177 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1178 return -1;
1179 }
1180 num *= 1024LL * 1024LL * 1024LL;
1181 endptr++;
1182 }
1183
1184 if (*endptr != '\0') {
1185 return -1;
1186 }
1187
1188 if (num > INT64_MAX) {
1189 return -1;
1190 }
1191
1192 return num;
1193}
1194
David Pursell0b156632015-10-30 11:22:01 -07001195static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001196 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001197 const char* type_override, const char* size_override,
1198 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001199 std::string partition_type, partition_size;
1200
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001201 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001202 const char* errMsg = nullptr;
1203 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001204 int fd;
1205
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001206 unsigned int limit = INT_MAX;
1207 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001208 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001209 }
1210 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001211 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001212 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001213
David Pursell0b156632015-10-30 11:22:01 -07001214 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001215 errMsg = "Can't determine partition type.\n";
1216 goto failed;
1217 }
JP Abgrall7e859742014-05-06 15:14:15 -07001218 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001219 if (partition_type != type_override) {
1220 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1221 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001222 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001223 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001224 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001225
David Pursell0b156632015-10-30 11:22:01 -07001226 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001227 errMsg = "Unable to get partition size\n";
1228 goto failed;
1229 }
JP Abgrall7e859742014-05-06 15:14:15 -07001230 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001231 if (partition_size != size_override) {
1232 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1233 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001234 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001235 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001236 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001237 // Some bootloaders (angler, for example), send spurious leading whitespace.
1238 partition_size = android::base::Trim(partition_size);
1239 // Some bootloaders (hammerhead, for example) use implicit hex.
1240 // This code used to use strtol with base 16.
1241 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001242
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001243 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001244 if (!gen) {
1245 if (skip_if_not_supported) {
1246 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001247 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001248 return;
1249 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001250 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1251 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001252 return;
1253 }
1254
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001255 int64_t size;
1256 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1257 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1258 return;
1259 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001260
1261 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001262 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001263 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001264 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001265 return;
1266 }
1267
Elliott Hughes53ec4952016-05-11 12:39:27 -07001268 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001269 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001270 close(fd);
1271 return;
1272 }
1273 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001274 return;
1275
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001276failed:
1277 if (skip_if_not_supported) {
1278 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001279 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001280 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001281 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001282}
1283
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001284int main(int argc, char **argv)
1285{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001286 bool wants_wipe = false;
1287 bool wants_reboot = false;
1288 bool wants_reboot_bootloader = false;
1289 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001290 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001291 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001292 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001293 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001294 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001295 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001296 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001297
JP Abgrall7b8970c2013-03-07 17:06:41 -08001298 const struct option longopts[] = {
1299 {"base", required_argument, 0, 'b'},
1300 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001301 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001302 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001303 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001304 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001305 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001306 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001307 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001308 {"help", no_argument, 0, 'h'},
1309 {"unbuffered", no_argument, 0, 0},
1310 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001311 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001312 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001313 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001314#if !defined(_WIN32)
1315 {"wipe-and-use-fbe", no_argument, 0, 0},
1316#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001317 {0, 0, 0, 0}
1318 };
Colin Cross8879f982012-05-22 17:53:34 -07001319
1320 serial = getenv("ANDROID_SERIAL");
1321
1322 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001323 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 -07001324 if (c < 0) {
1325 break;
1326 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001327 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001328 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001329 case 'a':
1330 wants_set_active = true;
1331 if (optarg)
1332 next_active = optarg;
1333 break;
Colin Cross8879f982012-05-22 17:53:34 -07001334 case 'b':
1335 base_addr = strtoul(optarg, 0, 16);
1336 break;
Colin Cross8879f982012-05-22 17:53:34 -07001337 case 'c':
1338 cmdline = optarg;
1339 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001340 case 'h':
1341 usage();
1342 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001343 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001344 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001345 unsigned long val;
1346
1347 val = strtoul(optarg, &endptr, 0);
1348 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1349 die("invalid vendor id '%s'", optarg);
1350 vendor_id = (unsigned short)val;
1351 break;
1352 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001353 case 'k':
1354 kernel_offset = strtoul(optarg, 0, 16);
1355 break;
1356 case 'l':
1357 long_listing = 1;
1358 break;
1359 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001360 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001361 if (!page_size) die("invalid page size");
1362 break;
1363 case 'p':
1364 product = optarg;
1365 break;
1366 case 'r':
1367 ramdisk_offset = strtoul(optarg, 0, 16);
1368 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001369 case 't':
1370 tags_offset = strtoul(optarg, 0, 16);
1371 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001372 case 's':
1373 serial = optarg;
1374 break;
1375 case 'S':
1376 sparse_limit = parse_num(optarg);
1377 if (sparse_limit < 0) {
1378 die("invalid sparse limit");
1379 }
1380 break;
1381 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001382 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001383 break;
1384 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001385 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001386 break;
Colin Cross8879f982012-05-22 17:53:34 -07001387 case '?':
1388 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001389 case 0:
1390 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001391 setvbuf(stdout, nullptr, _IONBF, 0);
1392 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001393 } else if (strcmp("version", longopts[longindex].name) == 0) {
1394 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1395 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001396 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1397 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001398#if !defined(_WIN32)
1399 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1400 wants_wipe = true;
1401 set_fbe_marker = true;
1402#endif
1403 } else {
1404 fprintf(stderr, "Internal error in options processing for %s\n",
1405 longopts[longindex].name);
1406 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001407 }
1408 break;
Colin Cross8879f982012-05-22 17:53:34 -07001409 default:
1410 abort();
1411 }
1412 }
1413
1414 argc -= optind;
1415 argv += optind;
1416
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001417 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001418 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001419 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001420 }
1421
Colin Cross8fb6e062012-07-24 16:36:41 -07001422 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001423 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001424 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001425 return 0;
1426 }
1427
Colin Crossc7b75dc2012-08-29 18:17:06 -07001428 if (argc > 0 && !strcmp(*argv, "help")) {
1429 usage();
1430 return 0;
1431 }
1432
David Pursell0b156632015-10-30 11:22:01 -07001433 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001434 if (transport == nullptr) {
1435 return 1;
1436 }
1437
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001438 if (slot_override != "")
David Pursell0b156632015-10-30 11:22:01 -07001439 slot_override = verify_slot(transport, slot_override.c_str());
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001440 if (next_active != "")
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001441 next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001442
1443 if (wants_set_active) {
1444 if (next_active == "") {
1445 if (slot_override == "") {
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001446 std::string current_slot;
1447 if (fb_getvar(transport, "current-slot", &current_slot)) {
1448 next_active = verify_slot(transport, current_slot.c_str(), false);
1449 } else {
1450 wants_set_active = false;
1451 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001452 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001453 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001454 }
1455 }
1456 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001457
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001458 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001459 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001460 require(2);
1461 fb_queue_display(argv[1], argv[1]);
1462 skip(2);
1463 } else if(!strcmp(*argv, "erase")) {
1464 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001465
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001466 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001467 std::string partition_type;
1468 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001469 fs_get_generator(partition_type) != nullptr) {
1470 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1471 partition_type.c_str());
1472 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001473
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001474 fb_queue_erase(partition.c_str());
1475 };
David Pursell0b156632015-10-30 11:22:01 -07001476 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001477 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001478 } else if(!strncmp(*argv, "format", strlen("format"))) {
1479 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001480 char *type_override = nullptr;
1481 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001482 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001483 /*
1484 * Parsing for: "format[:[type][:[size]]]"
1485 * Some valid things:
1486 * - select ontly the size, and leave default fs type:
1487 * format::0x4000000 userdata
1488 * - default fs type and size:
1489 * format userdata
1490 * format:: userdata
1491 */
1492 overrides = strchr(*argv, ':');
1493 if (overrides) {
1494 overrides++;
1495 size_override = strchr(overrides, ':');
1496 if (size_override) {
1497 size_override[0] = '\0';
1498 size_override++;
1499 }
1500 type_override = overrides;
1501 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001502 if (type_override && !type_override[0]) type_override = nullptr;
1503 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001504
1505 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001506 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001507 fb_queue_erase(partition.c_str());
1508 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001509 fb_perform_format(transport, partition.c_str(), 0,
1510 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001511 };
David Pursell0b156632015-10-30 11:22:01 -07001512 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001513 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001514 } else if(!strcmp(*argv, "signature")) {
1515 require(2);
1516 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001517 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001518 if (sz != 256) die("signature must be 256 bytes");
1519 fb_queue_download("signature", data, sz);
1520 fb_queue_command("signature", "installing signature");
1521 skip(2);
1522 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001523 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001524 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001525 if (argc > 0) {
1526 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001527 wants_reboot = false;
1528 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001529 skip(1);
1530 }
1531 }
1532 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001533 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001534 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001535 skip(1);
1536 } else if (!strcmp(*argv, "continue")) {
1537 fb_queue_command("continue", "resuming boot");
1538 skip(1);
1539 } else if(!strcmp(*argv, "boot")) {
1540 char *kname = 0;
1541 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001542 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001543 skip(1);
1544 if (argc > 0) {
1545 kname = argv[0];
1546 skip(1);
1547 }
1548 if (argc > 0) {
1549 rname = argv[0];
1550 skip(1);
1551 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001552 if (argc > 0) {
1553 sname = argv[0];
1554 skip(1);
1555 }
1556 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001557 if (data == 0) return 1;
1558 fb_queue_download("boot.img", data, sz);
1559 fb_queue_command("boot", "booting");
1560 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001561 char* pname = argv[1];
1562 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001563 require(2);
1564 if (argc > 2) {
1565 fname = argv[2];
1566 skip(3);
1567 } else {
1568 fname = find_item(pname, product);
1569 skip(2);
1570 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001571 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001572
1573 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001574 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001575 fb_queue_erase(partition.c_str());
1576 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001577 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001578 };
David Pursell0b156632015-10-30 11:22:01 -07001579 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001580 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001581 char *kname = argv[2];
1582 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001583 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001584 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001585 skip(3);
1586 if (argc > 0) {
1587 rname = argv[0];
1588 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001589 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001590 if (argc > 0) {
1591 sname = argv[0];
1592 skip(1);
1593 }
1594 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001595 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001596 auto flashraw = [&](const std::string &partition) {
1597 fb_queue_flash(partition.c_str(), data, sz);
1598 };
David Pursell0b156632015-10-30 11:22:01 -07001599 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001600 } else if(!strcmp(*argv, "flashall")) {
1601 skip(1);
David Pursell0b156632015-10-30 11:22:01 -07001602 do_flashall(transport, slot_override.c_str(), erase_first);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001603 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001604 } else if(!strcmp(*argv, "update")) {
1605 if (argc > 1) {
David Pursell0b156632015-10-30 11:22:01 -07001606 do_update(transport, argv[1], slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001607 skip(2);
1608 } else {
David Pursell0b156632015-10-30 11:22:01 -07001609 do_update(transport, "update.zip", slot_override.c_str(), erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001610 skip(1);
1611 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001612 wants_reboot = 1;
1613 } else if(!strcmp(*argv, "set_active")) {
1614 require(2);
1615 std::string slot = verify_slot(transport, argv[1], false);
1616 fb_set_active(slot.c_str());
1617 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001618 } else if(!strcmp(*argv, "oem")) {
1619 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001620 } else if(!strcmp(*argv, "flashing")) {
1621 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1622 !strcmp(*(argv+1), "lock") ||
1623 !strcmp(*(argv+1), "unlock_critical") ||
1624 !strcmp(*(argv+1), "lock_critical") ||
1625 !strcmp(*(argv+1), "get_unlock_ability") ||
1626 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1627 !strcmp(*(argv+1), "lock_bootloader"))) {
1628 argc = do_oem_command(argc, argv);
1629 } else
1630 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1631 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001632 } else {
1633 usage();
1634 return 1;
1635 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001636 } else {
1637 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001638 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001639 }
1640 }
1641
1642 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001643 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001644 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001645 if (set_fbe_marker) {
1646 fprintf(stderr, "setting FBE marker...\n");
1647 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1648 if (initial_userdata_dir.empty()) {
1649 return 1;
1650 }
1651 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1652 delete_fbemarker_tmpdir(initial_userdata_dir);
1653 } else {
1654 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1655 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001656
1657 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001658 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001659 fprintf(stderr, "wiping cache...\n");
1660 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001661 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001662 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001663 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001664 if (wants_set_active) {
1665 fb_set_active(next_active.c_str());
1666 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001667 if (wants_reboot) {
1668 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001669 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001670 } else if (wants_reboot_bootloader) {
1671 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001672 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001673 }
1674
David Pursell0b156632015-10-30 11:22:01 -07001675 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001676}