blob: d35a87486534bcaddf85cb21229752f922482d55 [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 Rosenberg6c985092016-06-28 01:36:21 -0700102 char img_name[17];
103 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700104 char part_name[9];
105 bool is_optional;
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700106 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700107} images[] = {
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700108 {"boot.img", "boot.sig", "boot", false, false},
109 {"boot_other.img", "boot.sig", "boot", true, true},
110 {"recovery.img", "recovery.sig", "recovery", true, false},
111 {"system.img", "system.sig", "system", false, false},
112 {"system_other.img", "system.sig", "system", true, true},
113 {"vendor.img", "vendor.sig", "vendor", true, false},
114 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700115};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700116
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700117static std::string find_item_given_name(const char* img_name, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118 char *dir;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700120 if(product) {
Alex Lightdba3e992016-06-29 11:58:22 -0700121 std::string path = get_my_path();
122 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
123 path.c_str(), product, img_name);
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700124 }
125
126 dir = getenv("ANDROID_PRODUCT_OUT");
127 if((dir == 0) || (dir[0] == 0)) {
128 die("neither -p product specified nor ANDROID_PRODUCT_OUT set");
129 }
130
131 return android::base::StringPrintf("%s/%s", dir, img_name);
132}
133
134std::string find_item(const char* item, const char* product) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 const char *fn;
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 if(!strcmp(item,"boot")) {
138 fn = "boot.img";
139 } else if(!strcmp(item,"recovery")) {
140 fn = "recovery.img";
141 } else if(!strcmp(item,"system")) {
142 fn = "system.img";
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700143 } else if(!strcmp(item,"vendor")) {
144 fn = "vendor.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145 } else if(!strcmp(item,"userdata")) {
146 fn = "userdata.img";
Jean-Baptiste Querud7608a42011-09-30 14:39:25 -0700147 } else if(!strcmp(item,"cache")) {
148 fn = "cache.img";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 } else if(!strcmp(item,"info")) {
150 fn = "android-info.txt";
151 } else {
152 fprintf(stderr,"unknown partition '%s'\n", item);
Elliott Hughes2810d002016-04-25 14:31:18 -0700153 return "";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 }
155
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700156 return find_item_given_name(fn, product);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157}
158
Elliott Hughesfc797672015-04-07 20:12:50 -0700159static int64_t get_file_size(int fd) {
160 struct stat sb;
161 return fstat(fd, &sb) == -1 ? -1 : sb.st_size;
Colin Crossf8387882012-05-24 17:18:41 -0700162}
163
Elliott Hughesfc797672015-04-07 20:12:50 -0700164static void* load_fd(int fd, int64_t* sz) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800165 int errno_tmp;
Elliott Hughesfc797672015-04-07 20:12:50 -0700166 char* data = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167
Elliott Hughesfc797672015-04-07 20:12:50 -0700168 *sz = get_file_size(fd);
169 if (*sz < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700170 goto oops;
171 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172
Elliott Hughesfc797672015-04-07 20:12:50 -0700173 data = (char*) malloc(*sz);
174 if (data == nullptr) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175
Elliott Hughesfc797672015-04-07 20:12:50 -0700176 if(read(fd, data, *sz) != *sz) goto oops;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 close(fd);
178
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 return data;
180
181oops:
Matt Gumbel64ba2582011-12-08 11:59:38 -0800182 errno_tmp = errno;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800183 close(fd);
184 if(data != 0) free(data);
Matt Gumbel64ba2582011-12-08 11:59:38 -0800185 errno = errno_tmp;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 return 0;
187}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
Elliott Hughes2810d002016-04-25 14:31:18 -0700189static void* load_file(const std::string& path, int64_t* sz) {
190 int fd = open(path.c_str(), O_RDONLY | O_BINARY);
Elliott Hughesfc797672015-04-07 20:12:50 -0700191 if (fd == -1) return nullptr;
192 return load_fd(fd, sz);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700193}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Elliott Hughesfc797672015-04-07 20:12:50 -0700195static int match_fastboot_with_serial(usb_ifc_info* info, const char* local_serial) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700196 // Require a matching vendor id if the user specified one with -i.
Elliott Hughesb46964f2015-08-19 17:49:45 -0700197 if (vendor_id != 0 && info->dev_vendor != vendor_id) {
Elliott Hughese1746fd2015-08-10 15:30:54 -0700198 return -1;
199 }
200
201 if (info->ifc_class != 0xff || info->ifc_subclass != 0x42 || info->ifc_protocol != 0x03) {
202 return -1;
203 }
204
Scott Anderson13081c62012-04-06 12:39:30 -0700205 // require matching serial number or device path if requested
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206 // at the command line with the -s option.
JP Abgralla032ded2012-06-06 11:53:33 -0700207 if (local_serial && (strcmp(local_serial, info->serial_number) != 0 &&
208 strcmp(local_serial, info->device_path) != 0)) return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800209 return 0;
210}
211
Elliott Hughesfc797672015-04-07 20:12:50 -0700212static int match_fastboot(usb_ifc_info* info) {
JP Abgrall7b8970c2013-03-07 17:06:41 -0800213 return match_fastboot_with_serial(info, serial);
214}
215
Elliott Hughesfc797672015-04-07 20:12:50 -0700216static int list_devices_callback(usb_ifc_info* info) {
217 if (match_fastboot_with_serial(info, nullptr) == 0) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800218 std::string serial = info->serial_number;
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700219 if (!info->writable) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800220 serial = UsbNoPermissionsShortHelpText();
Elliott Hughesb4add9b2009-10-06 18:07:49 -0700221 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 if (!serial[0]) {
223 serial = "????????????";
224 }
Scott Anderson866b1bd2012-06-04 20:29:11 -0700225 // output compatible with "adb devices"
Scott Anderson13081c62012-04-06 12:39:30 -0700226 if (!long_listing) {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800227 printf("%s\tfastboot", serial.c_str());
Scott Anderson13081c62012-04-06 12:39:30 -0700228 } else {
Elliott Hughes1b708d32015-12-11 19:07:01 -0800229 printf("%-22s fastboot", serial.c_str());
230 if (strlen(info->device_path) > 0) printf(" %s", info->device_path);
Scott Anderson13081c62012-04-06 12:39:30 -0700231 }
Elliott Hughes1b708d32015-12-11 19:07:01 -0800232 putchar('\n');
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 }
234
235 return -1;
236}
237
David Pursell2ec418a2016-01-20 08:32:08 -0800238// Opens a new Transport connected to a device. If |serial| is non-null it will be used to identify
239// a specific device, otherwise the first USB device found will be used.
240//
241// If |serial| is non-null but invalid, this prints an error message to stderr and returns nullptr.
242// Otherwise it blocks until the target is available.
243//
244// The returned Transport is a singleton, so multiple calls to this function will return the same
245// object, and the caller should not attempt to delete the returned Transport.
David Pursell0b156632015-10-30 11:22:01 -0700246static Transport* open_device() {
247 static Transport* transport = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800248 bool announce = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
David Pursell2ec418a2016-01-20 08:32:08 -0800250 if (transport != nullptr) {
251 return transport;
252 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800253
David Pursell4601c972016-02-05 15:35:09 -0800254 Socket::Protocol protocol = Socket::Protocol::kTcp;
David Pursell2ec418a2016-01-20 08:32:08 -0800255 std::string host;
David Pursell4601c972016-02-05 15:35:09 -0800256 int port = 0;
257 if (serial != nullptr) {
258 const char* net_address = nullptr;
David Pursell2ec418a2016-01-20 08:32:08 -0800259
David Pursell4601c972016-02-05 15:35:09 -0800260 if (android::base::StartsWith(serial, "tcp:")) {
261 protocol = Socket::Protocol::kTcp;
262 port = tcp::kDefaultPort;
263 net_address = serial + strlen("tcp:");
264 } else if (android::base::StartsWith(serial, "udp:")) {
265 protocol = Socket::Protocol::kUdp;
266 port = udp::kDefaultPort;
267 net_address = serial + strlen("udp:");
268 }
269
270 if (net_address != nullptr) {
271 std::string error;
272 if (!android::base::ParseNetAddress(net_address, &host, &port, nullptr, &error)) {
273 fprintf(stderr, "error: Invalid network address '%s': %s\n", net_address,
274 error.c_str());
275 return nullptr;
276 }
David Pursell2ec418a2016-01-20 08:32:08 -0800277 }
278 }
279
280 while (true) {
281 if (!host.empty()) {
282 std::string error;
David Pursell4601c972016-02-05 15:35:09 -0800283 if (protocol == Socket::Protocol::kTcp) {
284 transport = tcp::Connect(host, port, &error).release();
285 } else if (protocol == Socket::Protocol::kUdp) {
286 transport = udp::Connect(host, port, &error).release();
287 }
288
David Pursell2ec418a2016-01-20 08:32:08 -0800289 if (transport == nullptr && announce) {
290 fprintf(stderr, "error: %s\n", error.c_str());
291 }
292 } else {
293 transport = usb_open(match_fastboot);
294 }
295
296 if (transport != nullptr) {
297 return transport;
298 }
299
David Pursell0b156632015-10-30 11:22:01 -0700300 if (announce) {
David Pursell2ec418a2016-01-20 08:32:08 -0800301 announce = false;
Elliott Hughesb46964f2015-08-19 17:49:45 -0700302 fprintf(stderr, "< waiting for %s >\n", serial ? serial : "any device");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 }
Mark Salyzyn5957c1f2014-04-30 14:05:28 -0700304 usleep(1000);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 }
306}
307
Elliott Hughesfc797672015-04-07 20:12:50 -0700308static void list_devices() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309 // We don't actually open a USB device here,
310 // just getting our callback called so we can
311 // list all the connected devices.
312 usb_open(list_devices_callback);
313}
314
Elliott Hughesfc797672015-04-07 20:12:50 -0700315static void usage() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800316 fprintf(stderr,
317/* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */
318 "usage: fastboot [ <option> ] <command>\n"
319 "\n"
320 "commands:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700321 " update <filename> Reflash device from update.zip.\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700322 " Sets the flashed slot as active.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700323 " flashall Flash boot, system, vendor, and --\n"
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -0700324 " if found -- recovery. If the device\n"
325 " supports slots, the slot that has\n"
326 " been flashed to is set as active.\n"
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700327 " Secondary images may be flashed to\n"
328 " an inactive slot.\n"
329 " flash-primary Same as flashall, but do not flash\n"
330 " secondary images.\n"
331 " flash-secondary Only flashes the secondary images.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700332 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
333 " flashing lock Locks the device. Prevents flashing.\n"
334 " flashing unlock Unlocks the device. Allows flashing\n"
335 " any partition except\n"
336 " bootloader-related partitions.\n"
337 " flashing lock_critical Prevents flashing bootloader-related\n"
338 " partitions.\n"
339 " flashing unlock_critical Enables flashing bootloader-related\n"
340 " partitions.\n"
341 " flashing get_unlock_ability Queries bootloader to see if the\n"
342 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700343 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700344 " unlock nonce.\n"
345 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700346 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700347 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700348 " erase <partition> Erase a flash partition.\n"
349 " format[:[<fs type>][:[<size>]] <partition>\n"
350 " Format a flash partition. Can\n"
351 " override the fs type and/or size\n"
352 " the bootloader reports.\n"
353 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800354 " set_active <suffix> Sets the active slot. If slots are\n"
355 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700356 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
357 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
358 " Create bootimage and flash it.\n"
359 " devices [-l] List all connected devices [with\n"
360 " device paths].\n"
361 " continue Continue with autoboot.\n"
362 " reboot [bootloader] Reboot device [into bootloader].\n"
363 " reboot-bootloader Reboot device into bootloader.\n"
364 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 "\n"
366 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700367 " -w Erase userdata and cache (and format\n"
368 " if supported by partition type).\n"
369 " -u Do not erase partition before\n"
370 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800371 " -s <specific device> Specify a device. For USB, provide either\n"
372 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700373 " For ethernet, provide an address in the\n"
374 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800375 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700376 " -p <product> Specify product name.\n"
377 " -c <cmdline> Override kernel commandline.\n"
378 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800379 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700380 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800381 " --kernel-offset Specify a custom kernel offset.\n"
382 " (default: 0x00008000)\n"
383 " --ramdisk-offset Specify a custom ramdisk offset.\n"
384 " (default: 0x01000000)\n"
385 " --tags-offset Specify a custom tags offset.\n"
386 " (default: 0x00000100)\n"
387 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700388 " (default: 2048).\n"
389 " -S <size>[K|M|G] Automatically sparse files greater\n"
390 " than 'size'. 0 to disable.\n"
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700391 " --slot <suffix> Specify slot suffix to be used if the\n"
392 " device supports slots. This will be\n"
393 " added to all partition names that use\n"
394 " slots. 'all' can be given to refer\n"
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800395 " to all slots. 'other' can be given to\n"
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700396 " refer to a non-current slot. If this\n"
397 " flag is not used, slotted partitions\n"
398 " will default to the current active slot.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800399 " -a, --set-active[=<suffix>] Sets the active slot. If no suffix is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800400 " provided, this will default to the value\n"
401 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800402 " supported, this does nothing. This will\n"
403 " run after all non-reboot commands.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000404#if !defined(_WIN32)
405 " --wipe-and-use-fbe On devices which support it,\n"
406 " erase userdata and cache, and\n"
407 " enable file-based encryption\n"
408#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800409 " --unbuffered Do not buffer input or output.\n"
410 " --version Display version.\n"
411 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800413}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000414
Elliott Hughesfc797672015-04-07 20:12:50 -0700415static void* load_bootable_image(const char* kernel, const char* ramdisk,
416 const char* secondstage, int64_t* sz,
417 const char* cmdline) {
418 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 fprintf(stderr, "no image specified\n");
420 return 0;
421 }
422
Elliott Hughesfc797672015-04-07 20:12:50 -0700423 int64_t ksize;
424 void* kdata = load_file(kernel, &ksize);
425 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800426 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427 return 0;
428 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800429
Elliott Hughesfc797672015-04-07 20:12:50 -0700430 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700432 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800433
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
436 return 0;
437 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800438
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 *sz = ksize;
440 return kdata;
441 }
442
Elliott Hughesfc797672015-04-07 20:12:50 -0700443 void* rdata = nullptr;
444 int64_t rsize = 0;
445 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800448 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 return 0;
450 }
451 }
452
Elliott Hughesfc797672015-04-07 20:12:50 -0700453 void* sdata = nullptr;
454 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200455 if (secondstage) {
456 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700457 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200458 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
459 return 0;
460 }
461 }
462
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700464 int64_t bsize = 0;
465 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800466 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200467 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800468 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700469 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470 fprintf(stderr,"failed to create boot.img\n");
471 return 0;
472 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
474 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800475 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800476
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 return bdata;
478}
479
Elliott Hughesfc797672015-04-07 20:12:50 -0700480static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481{
Yusuke Sato07447542015-06-25 14:39:19 -0700482 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700483 ZipEntry zip_entry;
484 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700485 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000486 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800487 }
488
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700489 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700491 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700492 if (data == nullptr) {
493 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000494 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 }
496
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700497 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
498 if (error != 0) {
499 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000501 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800502 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000503
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 return data;
505}
506
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700507#if defined(_WIN32)
508
509// TODO: move this to somewhere it can be shared.
510
511#include <windows.h>
512
513// Windows' tmpfile(3) requires administrator rights because
514// it creates temporary files in the root directory.
515static FILE* win32_tmpfile() {
516 char temp_path[PATH_MAX];
517 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
518 if (nchars == 0 || nchars >= sizeof(temp_path)) {
519 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
520 return nullptr;
521 }
522
523 char filename[PATH_MAX];
524 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
525 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
526 return nullptr;
527 }
528
529 return fopen(filename, "w+bTD");
530}
531
532#define tmpfile win32_tmpfile
533
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000534static std::string make_temporary_directory() {
535 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
536 return "";
537}
538
539#else
540
541static std::string make_temporary_directory() {
542 const char *tmpdir = getenv("TMPDIR");
543 if (tmpdir == nullptr) {
544 tmpdir = P_tmpdir;
545 }
546 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
547 if (mkdtemp(&result[0]) == NULL) {
548 fprintf(stderr, "Unable to create temporary directory: %s\n",
549 strerror(errno));
550 return "";
551 }
552 return result;
553}
554
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700555#endif
556
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000557static std::string create_fbemarker_tmpdir() {
558 std::string dir = make_temporary_directory();
559 if (dir.empty()) {
560 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
561 return "";
562 }
563 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
564 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
565 if (fd == -1) {
566 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
567 marker_file.c_str(), errno, strerror(errno));
568 return "";
569 }
570 close(fd);
571 return dir;
572}
573
574static void delete_fbemarker_tmpdir(const std::string& dir) {
575 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
576 if (unlink(marker_file.c_str()) == -1) {
577 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
578 marker_file.c_str(), errno, strerror(errno));
579 return;
580 }
581 if (rmdir(dir.c_str()) == -1) {
582 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
583 dir.c_str(), errno, strerror(errno));
584 return;
585 }
586}
587
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700588static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
589 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700590 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700591 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
592 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700593 return -1;
594 }
595
Yusuke Sato07447542015-06-25 14:39:19 -0700596 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700597 ZipEntry zip_entry;
598 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
599 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000600 return -1;
601 }
602
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700603 int fd = fileno(fp);
604 int error = ExtractEntryToFile(zip, &zip_entry, fd);
605 if (error != 0) {
606 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
607 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700608 }
609
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000610 lseek(fd, 0, SEEK_SET);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700611 return fd;
612}
613
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800614static char *strip(char *s)
615{
616 int n;
617 while(*s && isspace(*s)) s++;
618 n = strlen(s);
619 while(n-- > 0) {
620 if(!isspace(s[n])) break;
621 s[n] = 0;
622 }
623 return s;
624}
625
626#define MAX_OPTIONS 32
627static int setup_requirement_line(char *name)
628{
629 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700630 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800631 unsigned n, count;
632 char *x;
633 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800634
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800635 if (!strncmp(name, "reject ", 7)) {
636 name += 7;
637 invert = 1;
638 } else if (!strncmp(name, "require ", 8)) {
639 name += 8;
640 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700641 } else if (!strncmp(name, "require-for-product:", 20)) {
642 // Get the product and point name past it
643 prod = name + 20;
644 name = strchr(name, ' ');
645 if (!name) return -1;
646 *name = 0;
647 name += 1;
648 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800649 }
650
651 x = strchr(name, '=');
652 if (x == 0) return 0;
653 *x = 0;
654 val[0] = x + 1;
655
656 for(count = 1; count < MAX_OPTIONS; count++) {
657 x = strchr(val[count - 1],'|');
658 if (x == 0) break;
659 *x = 0;
660 val[count] = x + 1;
661 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800662
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800663 name = strip(name);
664 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800665
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800666 name = strip(name);
667 if (name == 0) return -1;
668
Elliott Hughes253c18d2015-03-18 22:47:09 -0700669 const char* var = name;
670 // Work around an unfortunate name mismatch.
671 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800672
Elliott Hughes253c18d2015-03-18 22:47:09 -0700673 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800674 if (out == 0) return -1;
675
676 for(n = 0; n < count; n++) {
677 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700678 if (out[n] == 0) {
679 for(size_t i = 0; i < n; ++i) {
680 free((char*) out[i]);
681 }
682 free(out);
683 return -1;
684 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800685 }
686
Elliott Hughes253c18d2015-03-18 22:47:09 -0700687 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800688 return 0;
689}
690
Elliott Hughesfc797672015-04-07 20:12:50 -0700691static void setup_requirements(char* data, int64_t sz) {
692 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800693 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700694 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 *s++ = 0;
696 if (setup_requirement_line(data)) {
697 die("out of memory");
698 }
699 data = s;
700 } else {
701 s++;
702 }
703 }
704}
705
Elliott Hughesfc797672015-04-07 20:12:50 -0700706static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800707 fb_queue_notice("--------------------------------------------");
708 fb_queue_display("version-bootloader", "Bootloader Version...");
709 fb_queue_display("version-baseband", "Baseband Version.....");
710 fb_queue_display("serialno", "Serial Number........");
711 fb_queue_notice("--------------------------------------------");
712}
713
Rom Lemarchand622810c2013-06-28 09:54:59 -0700714static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700715{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700716 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700717 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700718 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700719 }
720
Elliott Hughesfc797672015-04-07 20:12:50 -0700721 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700722 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700723 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700724 }
725
Elliott Hughes253c18d2015-03-18 22:47:09 -0700726 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700727 if (!out_s) {
728 die("Failed to allocate sparse file array\n");
729 }
730
731 files = sparse_file_resparse(s, max_size, out_s, files);
732 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700733 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700734 }
735
736 return out_s;
737}
738
David Pursell0b156632015-10-30 11:22:01 -0700739static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700740 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700741 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
742 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800743 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700744 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700745 }
746
Elliott Hughes77c0e662015-11-02 15:51:12 -0800747 // Some bootloaders (angler, for example) send spurious whitespace too.
748 max_download_size = android::base::Trim(max_download_size);
749
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700750 uint64_t limit;
751 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800752 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700753 return 0;
754 }
755 if (limit > 0) {
756 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
757 }
Colin Crossf8387882012-05-24 17:18:41 -0700758 return limit;
759}
760
David Pursell0b156632015-10-30 11:22:01 -0700761static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700762 int64_t limit;
763
764 if (sparse_limit == 0) {
765 return 0;
766 } else if (sparse_limit > 0) {
767 limit = sparse_limit;
768 } else {
769 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700770 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700771 }
772 if (target_sparse_limit > 0) {
773 limit = target_sparse_limit;
774 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700775 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700776 }
777 }
778
779 if (size > limit) {
780 return limit;
781 }
782
783 return 0;
784}
785
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700786// Until we get lazy inode table init working in make_ext4fs, we need to
787// erase partitions of type ext4 before flashing a filesystem so no stale
788// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700789static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800790 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700791 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800792 return false;
793 }
794 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700795}
796
Elliott Hughes53ec4952016-05-11 12:39:27 -0700797static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700798 int64_t sz = get_file_size(fd);
799 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700800 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700801 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700802
Elliott Hughesfc797672015-04-07 20:12:50 -0700803 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700804 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700805 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700806 sparse_file** s = load_sparse_files(fd, limit);
807 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700808 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700809 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700810 buf->type = FB_BUFFER_SPARSE;
811 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700812 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700813 void* data = load_fd(fd, &sz);
814 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700815 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700816 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000817 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700818 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700819
Elliott Hughes53ec4952016-05-11 12:39:27 -0700820 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700821}
822
Elliott Hughes53ec4952016-05-11 12:39:27 -0700823static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
824 int fd = open(fname, O_RDONLY | O_BINARY);
825 if (fd == -1) {
826 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700827 }
David Pursell0b156632015-10-30 11:22:01 -0700828 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700829}
830
831static void flash_buf(const char *pname, struct fastboot_buffer *buf)
832{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700833 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700834
835 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800836 case FB_BUFFER_SPARSE: {
837 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700838 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700839 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700840 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800841 sparse_files.emplace_back(*s, sz);
842 ++s;
843 }
844
845 for (size_t i = 0; i < sparse_files.size(); ++i) {
846 const auto& pair = sparse_files[i];
847 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700848 }
849 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800850 }
851
Rom Lemarchand622810c2013-06-28 09:54:59 -0700852 case FB_BUFFER:
853 fb_queue_flash(pname, buf->data, buf->sz);
854 break;
855 default:
856 die("unknown buffer type: %d", buf->type);
857 }
858}
859
David Pursell0b156632015-10-30 11:22:01 -0700860static std::vector<std::string> get_suffixes(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700861 std::vector<std::string> suffixes;
862 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700863 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700864 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700865 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700866 suffixes = android::base::Split(suffix_list, ",");
867 // Unfortunately some devices will return an error message in the
868 // guise of a valid value. If we only see only one suffix, it's probably
869 // not real.
870 if (suffixes.size() == 1) {
871 suffixes.clear();
872 }
873 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700874}
875
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700876static bool supports_AB(Transport* transport) {
877 return !get_suffixes(transport).empty();
878}
879
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700880// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700881static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
Alex Light6c60ea82016-06-14 14:29:09 -0700882 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700883
Alex Light6c60ea82016-06-14 14:29:09 -0700884 if (!suffixes.empty()) {
885 for (size_t i = 0; i < suffixes.size(); i++) {
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700886 if (current_slot == suffixes[i]) {
887 return suffixes[(i+1)%suffixes.size()];
888 }
Alex Light6c60ea82016-06-14 14:29:09 -0700889 }
Alex Light6c60ea82016-06-14 14:29:09 -0700890 }
Alex Light6c60ea82016-06-14 14:29:09 -0700891 return "";
892}
893
Daniel Rosenberg6c985092016-06-28 01:36:21 -0700894static std::string get_other_slot(Transport* transport) {
895 std::string current_slot;
896 fb_getvar(transport, "current-slot", &current_slot);
897 return get_other_slot(transport, current_slot);
898}
899
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800900static std::string verify_slot(Transport* transport, const char *slot, bool allow_all) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700901 if (strcmp(slot, "all") == 0) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800902 if (allow_all) {
903 return "all";
904 } else {
905 std::vector<std::string> suffixes = get_suffixes(transport);
906 if (!suffixes.empty()) {
907 return suffixes[0];
908 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800909 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800910 }
911 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700912 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800913
David Pursell0b156632015-10-30 11:22:01 -0700914 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800915
916 if (strcmp(slot, "other") == 0) {
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700917 std::string current_slot;
918 if (!fb_getvar(transport, "current-slot", &current_slot)) {
919 die("Failed to identify current slot.");
920 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700921 std::string other = get_other_slot(transport, current_slot);
922 if (other == "") {
923 die("No known slots.");
Daniel Rosenberg8c32da32016-06-27 16:46:50 -0700924 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700925 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800926 }
927
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700928 for (const std::string &suffix : suffixes) {
929 if (suffix == slot)
930 return slot;
931 }
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700932 if (suffixes.empty()) {
933 fprintf(stderr, "Device does not support slots.\n");
934 } else {
935 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot);
936 for (const std::string &suffix : suffixes) {
937 fprintf(stderr, "%s\n", suffix.c_str());
938 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700939 }
940 exit(1);
941}
942
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800943static std::string verify_slot(Transport* transport, const char *slot) {
944 return verify_slot(transport, slot, true);
945}
946
David Pursell0b156632015-10-30 11:22:01 -0700947static void do_for_partition(Transport* transport, const char *part, const char *slot,
948 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800949 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700950 std::string current_slot;
951
David Pursell0b156632015-10-30 11:22:01 -0700952 if (!fb_getvar(transport, std::string("has-slot:")+part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800953 /* If has-slot is not supported, the answer is no. */
954 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700955 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800956 if (has_slot == "yes") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700957 if (!slot || slot[0] == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700958 if (!fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700959 die("Failed to identify current slot.\n");
960 }
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800961 func(std::string(part) + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700962 } else {
Daniel Rosenberg996ecd82015-11-13 12:51:23 -0800963 func(std::string(part) + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700964 }
965 } else {
966 if (force_slot && slot && slot[0]) {
David Pursell0b156632015-10-30 11:22:01 -0700967 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
968 part, slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 }
970 func(part);
971 }
972}
973
David Pursell0b156632015-10-30 11:22:01 -0700974/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
975 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
976 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
977 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700978 */
David Pursell0b156632015-10-30 11:22:01 -0700979static void do_for_partitions(Transport* transport, const char *part, const char *slot,
980 std::function<void(const std::string&)> func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800981 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700982
983 if (slot && strcmp(slot, "all") == 0) {
David Pursell0b156632015-10-30 11:22:01 -0700984 if (!fb_getvar(transport, std::string("has-slot:") + part, &has_slot)) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700985 die("Could not check if partition %s has slot.", part);
986 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800987 if (has_slot == "yes") {
David Pursell0b156632015-10-30 11:22:01 -0700988 std::vector<std::string> suffixes = get_suffixes(transport);
Daniel Rosenberg190d9682016-06-27 23:03:48 -0700989 if (suffixes.empty()) {
990 die("Error reading suffixes.\n");
991 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700992 for (std::string &suffix : suffixes) {
David Pursell0b156632015-10-30 11:22:01 -0700993 do_for_partition(transport, part, suffix.c_str(), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994 }
995 } else {
David Pursell0b156632015-10-30 11:22:01 -0700996 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700997 }
998 } else {
David Pursell0b156632015-10-30 11:22:01 -0700999 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001000 }
1001}
1002
David Pursell0b156632015-10-30 11:22:01 -07001003static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001004 struct fastboot_buffer buf;
1005
Elliott Hughes53ec4952016-05-11 12:39:27 -07001006 if (!load_buf(transport, fname, &buf)) {
1007 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001008 }
1009 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001010}
1011
Elliott Hughesfc797672015-04-07 20:12:50 -07001012static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1013 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001014 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001015 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001016 fb_queue_download("signature", data, sz);
1017 fb_queue_command("signature", "installing signature");
1018}
1019
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001020// Sets slot_override as the active slot. If slot_override is blank,
1021// set current slot as active instead. This clears slot-unbootable.
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001022static void set_active(Transport* transport, const std::string& slot_override) {
Alex Lightdba3e992016-06-29 11:58:22 -07001023 if (!supports_AB(transport)) {
1024 return;
1025 } else if (slot_override != "") {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001026 fb_set_active(slot_override.c_str());
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001027 } else {
1028 std::string current_slot;
1029 if (fb_getvar(transport, "current-slot", &current_slot)) {
1030 current_slot = verify_slot(transport, current_slot.c_str(), false);
1031 fb_set_active(current_slot.c_str());
1032 }
1033 }
1034}
1035
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001036static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001037 queue_info_dump();
1038
Wink Savilleb98762f2011-04-04 17:54:59 -07001039 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1040
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001041 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001042 int error = OpenArchive(filename, &zip);
1043 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001044 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001045 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001046 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001047
Elliott Hughesfc797672015-04-07 20:12:50 -07001048 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001049 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001050 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001051 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001052 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001053 }
1054
Elliott Hughes253c18d2015-03-18 22:47:09 -07001055 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001056
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001057 std::string secondary;
1058 bool update_secondary = slot_override != "all";
1059 if (update_secondary) {
1060 if (slot_override != "") {
1061 secondary = get_other_slot(transport, slot_override);
1062 } else {
1063 secondary = get_other_slot(transport);
1064 }
1065 if (secondary == "") {
1066 if (supports_AB(transport)) {
1067 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1068 }
1069 update_secondary = false;
1070 }
1071 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001072 for (size_t i = 0; i < arraysize(images); ++i) {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001073 const char* slot = slot_override.c_str();
1074 if (images[i].is_secondary) {
1075 if (update_secondary) {
1076 slot = secondary.c_str();
1077 } else {
1078 continue;
1079 }
1080 }
1081
Elliott Hughes253c18d2015-03-18 22:47:09 -07001082 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001083 if (fd == -1) {
1084 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001085 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001086 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001087 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001088 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001089 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001090 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001091 if (!load_buf_fd(transport, fd, &buf)) {
1092 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1093 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001094
1095 auto update = [&](const std::string &partition) {
1096 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001097 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001098 fb_queue_erase(partition.c_str());
1099 }
1100 flash_buf(partition.c_str(), &buf);
1101 /* not closing the fd here since the sparse code keeps the fd around
1102 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1103 * program exits.
1104 */
1105 };
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001106 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001107 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001108
1109 CloseArchive(zip);
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001110 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001111}
1112
Elliott Hughes2810d002016-04-25 14:31:18 -07001113static void do_send_signature(const char* filename) {
1114 if (android::base::EndsWith(filename, ".img") == false) {
1115 return;
1116 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001117
Elliott Hughes2810d002016-04-25 14:31:18 -07001118 std::string sig_path = filename;
1119 sig_path.erase(sig_path.size() - 4);
1120 sig_path += ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001121
1122 int64_t sz;
Elliott Hughes2810d002016-04-25 14:31:18 -07001123 void* data = load_file(sig_path, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001124 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001125
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001126 fb_queue_download("signature", data, sz);
1127 fb_queue_command("signature", "installing signature");
1128}
1129
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001130static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool flash_primary, bool flash_secondary) {
1131 std::string fname;
1132 if (flash_primary) {
1133 queue_info_dump();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001134
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001135 fb_queue_query_save("product", cur_product, sizeof(cur_product));
Wink Savilleb98762f2011-04-04 17:54:59 -07001136
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001137 fname = find_item("info", product);
Alex Lightdba3e992016-06-29 11:58:22 -07001138 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001139
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001140 int64_t sz;
1141 void* data = load_file(fname.c_str(), &sz);
1142 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001143
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001144 setup_requirements(reinterpret_cast<char*>(data), sz);
1145 }
1146 std::string secondary;
1147 if (flash_secondary) {
1148 if (slot_override != "") {
1149 secondary = get_other_slot(transport, slot_override);
1150 } else {
1151 secondary = get_other_slot(transport);
1152 }
1153 if (secondary == "") {
1154 if (supports_AB(transport)) {
1155 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1156 }
1157 flash_secondary = false;
1158 }
1159 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001160
Elliott Hughesa5dac1a2016-06-28 21:59:33 -07001161 for (size_t i = 0; i < arraysize(images); i++) {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001162 const char* slot = NULL;
1163 if (images[i].is_secondary) {
1164 if (flash_secondary) slot = secondary.c_str();
1165 } else {
1166 if (flash_primary) slot = slot_override.c_str();
1167 }
1168 if (!slot) continue;
1169 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001170 fastboot_buffer buf;
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001171 if (load_buf(transport, fname.c_str(), &buf)) {
Elliott Hughes53ec4952016-05-11 12:39:27 -07001172 if (images[i].is_optional) continue;
Rom Lemarchand622810c2013-06-28 09:54:59 -07001173 die("could not load %s\n", images[i].img_name);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001174 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001175
1176 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001177 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001178 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001179 fb_queue_erase(partition.c_str());
1180 }
1181 flash_buf(partition.c_str(), &buf);
1182 };
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001183 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001184 }
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001185
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001186 if (flash_primary) set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001187}
1188
1189#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001190#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001191
Elliott Hughes45be42e2015-09-02 20:15:48 -07001192static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001193{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001194 if (argc <= 2) return 0;
1195 skip(2);
1196
1197 /*
1198 * Process unlock_bootloader, we have to load the message file
1199 * and send that to the remote device.
1200 */
1201 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001202
1203 int64_t sz;
1204 void* data = load_file(*argv, &sz);
1205 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001206 fb_queue_download("unlock_message", data, sz);
1207 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1208 skip(1);
1209 return 0;
1210}
1211
Elliott Hughes45be42e2015-09-02 20:15:48 -07001212static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001213{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 char command[256];
1215 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001216
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001217 command[0] = 0;
1218 while(1) {
1219 strcat(command,*argv);
1220 skip(1);
1221 if(argc == 0) break;
1222 strcat(command," ");
1223 }
1224
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001225 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001226 return 0;
1227}
1228
Colin Crossf8387882012-05-24 17:18:41 -07001229static int64_t parse_num(const char *arg)
1230{
1231 char *endptr;
1232 unsigned long long num;
1233
1234 num = strtoull(arg, &endptr, 0);
1235 if (endptr == arg) {
1236 return -1;
1237 }
1238
1239 if (*endptr == 'k' || *endptr == 'K') {
1240 if (num >= (-1ULL) / 1024) {
1241 return -1;
1242 }
1243 num *= 1024LL;
1244 endptr++;
1245 } else if (*endptr == 'm' || *endptr == 'M') {
1246 if (num >= (-1ULL) / (1024 * 1024)) {
1247 return -1;
1248 }
1249 num *= 1024LL * 1024LL;
1250 endptr++;
1251 } else if (*endptr == 'g' || *endptr == 'G') {
1252 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1253 return -1;
1254 }
1255 num *= 1024LL * 1024LL * 1024LL;
1256 endptr++;
1257 }
1258
1259 if (*endptr != '\0') {
1260 return -1;
1261 }
1262
1263 if (num > INT64_MAX) {
1264 return -1;
1265 }
1266
1267 return num;
1268}
1269
David Pursell0b156632015-10-30 11:22:01 -07001270static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001271 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001272 const char* type_override, const char* size_override,
1273 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001274 std::string partition_type, partition_size;
1275
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001276 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001277 const char* errMsg = nullptr;
1278 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001279 int fd;
1280
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001281 unsigned int limit = INT_MAX;
1282 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001283 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001284 }
1285 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001286 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001287 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001288
David Pursell0b156632015-10-30 11:22:01 -07001289 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001290 errMsg = "Can't determine partition type.\n";
1291 goto failed;
1292 }
JP Abgrall7e859742014-05-06 15:14:15 -07001293 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001294 if (partition_type != type_override) {
1295 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1296 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001297 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001298 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001299 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001300
David Pursell0b156632015-10-30 11:22:01 -07001301 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001302 errMsg = "Unable to get partition size\n";
1303 goto failed;
1304 }
JP Abgrall7e859742014-05-06 15:14:15 -07001305 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001306 if (partition_size != size_override) {
1307 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1308 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001309 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001310 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001311 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001312 // Some bootloaders (angler, for example), send spurious leading whitespace.
1313 partition_size = android::base::Trim(partition_size);
1314 // Some bootloaders (hammerhead, for example) use implicit hex.
1315 // This code used to use strtol with base 16.
1316 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001317
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001318 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001319 if (!gen) {
1320 if (skip_if_not_supported) {
1321 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001322 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001323 return;
1324 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001325 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1326 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001327 return;
1328 }
1329
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001330 int64_t size;
1331 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1332 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1333 return;
1334 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001335
1336 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001337 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001338 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001339 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001340 return;
1341 }
1342
Elliott Hughes53ec4952016-05-11 12:39:27 -07001343 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001344 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001345 close(fd);
1346 return;
1347 }
1348 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001349 return;
1350
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001351failed:
1352 if (skip_if_not_supported) {
1353 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001354 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001355 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001356 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001357}
1358
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001359int main(int argc, char **argv)
1360{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001361 bool wants_wipe = false;
1362 bool wants_reboot = false;
1363 bool wants_reboot_bootloader = false;
1364 bool wants_set_active = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001365 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001366 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001367 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001368 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001369 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001370 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001371 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001372
JP Abgrall7b8970c2013-03-07 17:06:41 -08001373 const struct option longopts[] = {
1374 {"base", required_argument, 0, 'b'},
1375 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001376 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001377 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001378 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001379 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001380 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001381 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001382 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001383 {"help", no_argument, 0, 'h'},
1384 {"unbuffered", no_argument, 0, 0},
1385 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001386 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001387 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001388 {"set-active", optional_argument, 0, 'a'},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001389#if !defined(_WIN32)
1390 {"wipe-and-use-fbe", no_argument, 0, 0},
1391#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001392 {0, 0, 0, 0}
1393 };
Colin Cross8879f982012-05-22 17:53:34 -07001394
1395 serial = getenv("ANDROID_SERIAL");
1396
1397 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001398 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 -07001399 if (c < 0) {
1400 break;
1401 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001402 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001403 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001404 case 'a':
1405 wants_set_active = true;
1406 if (optarg)
1407 next_active = optarg;
1408 break;
Colin Cross8879f982012-05-22 17:53:34 -07001409 case 'b':
1410 base_addr = strtoul(optarg, 0, 16);
1411 break;
Colin Cross8879f982012-05-22 17:53:34 -07001412 case 'c':
1413 cmdline = optarg;
1414 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001415 case 'h':
1416 usage();
1417 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001418 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001419 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001420 unsigned long val;
1421
1422 val = strtoul(optarg, &endptr, 0);
1423 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1424 die("invalid vendor id '%s'", optarg);
1425 vendor_id = (unsigned short)val;
1426 break;
1427 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001428 case 'k':
1429 kernel_offset = strtoul(optarg, 0, 16);
1430 break;
1431 case 'l':
1432 long_listing = 1;
1433 break;
1434 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001435 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001436 if (!page_size) die("invalid page size");
1437 break;
1438 case 'p':
1439 product = optarg;
1440 break;
1441 case 'r':
1442 ramdisk_offset = strtoul(optarg, 0, 16);
1443 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001444 case 't':
1445 tags_offset = strtoul(optarg, 0, 16);
1446 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001447 case 's':
1448 serial = optarg;
1449 break;
1450 case 'S':
1451 sparse_limit = parse_num(optarg);
1452 if (sparse_limit < 0) {
1453 die("invalid sparse limit");
1454 }
1455 break;
1456 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001457 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001458 break;
1459 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001460 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001461 break;
Colin Cross8879f982012-05-22 17:53:34 -07001462 case '?':
1463 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001464 case 0:
1465 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001466 setvbuf(stdout, nullptr, _IONBF, 0);
1467 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001468 } else if (strcmp("version", longopts[longindex].name) == 0) {
1469 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1470 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001471 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1472 slot_override = std::string(optarg);
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001473#if !defined(_WIN32)
1474 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1475 wants_wipe = true;
1476 set_fbe_marker = true;
1477#endif
1478 } else {
1479 fprintf(stderr, "Internal error in options processing for %s\n",
1480 longopts[longindex].name);
1481 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001482 }
1483 break;
Colin Cross8879f982012-05-22 17:53:34 -07001484 default:
1485 abort();
1486 }
1487 }
1488
1489 argc -= optind;
1490 argv += optind;
1491
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001492 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001493 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001494 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001495 }
1496
Colin Cross8fb6e062012-07-24 16:36:41 -07001497 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001498 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001499 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001500 return 0;
1501 }
1502
Colin Crossc7b75dc2012-08-29 18:17:06 -07001503 if (argc > 0 && !strcmp(*argv, "help")) {
1504 usage();
1505 return 0;
1506 }
1507
David Pursell0b156632015-10-30 11:22:01 -07001508 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001509 if (transport == nullptr) {
1510 return 1;
1511 }
1512
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001513 if (slot_override != "") slot_override = verify_slot(transport, slot_override.c_str());
1514 if (next_active != "") next_active = verify_slot(transport, next_active.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001515
1516 if (wants_set_active) {
1517 if (next_active == "") {
1518 if (slot_override == "") {
Daniel Rosenberg9c9a6c62016-06-27 19:43:11 -07001519 std::string current_slot;
1520 if (fb_getvar(transport, "current-slot", &current_slot)) {
1521 next_active = verify_slot(transport, current_slot.c_str(), false);
1522 } else {
1523 wants_set_active = false;
1524 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001525 } else {
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001526 next_active = verify_slot(transport, slot_override.c_str(), false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001527 }
1528 }
1529 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001530
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001531 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001532 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001533 require(2);
1534 fb_queue_display(argv[1], argv[1]);
1535 skip(2);
1536 } else if(!strcmp(*argv, "erase")) {
1537 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001538
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001539 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001540 std::string partition_type;
1541 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001542 fs_get_generator(partition_type) != nullptr) {
1543 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1544 partition_type.c_str());
1545 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001546
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001547 fb_queue_erase(partition.c_str());
1548 };
David Pursell0b156632015-10-30 11:22:01 -07001549 do_for_partitions(transport, argv[1], slot_override.c_str(), erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001550 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001551 } else if(!strncmp(*argv, "format", strlen("format"))) {
1552 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001553 char *type_override = nullptr;
1554 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001555 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001556 /*
1557 * Parsing for: "format[:[type][:[size]]]"
1558 * Some valid things:
1559 * - select ontly the size, and leave default fs type:
1560 * format::0x4000000 userdata
1561 * - default fs type and size:
1562 * format userdata
1563 * format:: userdata
1564 */
1565 overrides = strchr(*argv, ':');
1566 if (overrides) {
1567 overrides++;
1568 size_override = strchr(overrides, ':');
1569 if (size_override) {
1570 size_override[0] = '\0';
1571 size_override++;
1572 }
1573 type_override = overrides;
1574 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001575 if (type_override && !type_override[0]) type_override = nullptr;
1576 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001577
1578 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001579 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001580 fb_queue_erase(partition.c_str());
1581 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001582 fb_perform_format(transport, partition.c_str(), 0,
1583 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001584 };
David Pursell0b156632015-10-30 11:22:01 -07001585 do_for_partitions(transport, argv[1], slot_override.c_str(), format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001586 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001587 } else if(!strcmp(*argv, "signature")) {
1588 require(2);
1589 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001590 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001591 if (sz != 256) die("signature must be 256 bytes");
1592 fb_queue_download("signature", data, sz);
1593 fb_queue_command("signature", "installing signature");
1594 skip(2);
1595 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001596 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001597 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001598 if (argc > 0) {
1599 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001600 wants_reboot = false;
1601 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001602 skip(1);
1603 }
1604 }
1605 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001606 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001607 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001608 skip(1);
1609 } else if (!strcmp(*argv, "continue")) {
1610 fb_queue_command("continue", "resuming boot");
1611 skip(1);
1612 } else if(!strcmp(*argv, "boot")) {
1613 char *kname = 0;
1614 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001615 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001616 skip(1);
1617 if (argc > 0) {
1618 kname = argv[0];
1619 skip(1);
1620 }
1621 if (argc > 0) {
1622 rname = argv[0];
1623 skip(1);
1624 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001625 if (argc > 0) {
1626 sname = argv[0];
1627 skip(1);
1628 }
1629 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001630 if (data == 0) return 1;
1631 fb_queue_download("boot.img", data, sz);
1632 fb_queue_command("boot", "booting");
1633 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001634 char* pname = argv[1];
1635 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001636 require(2);
1637 if (argc > 2) {
1638 fname = argv[2];
1639 skip(3);
1640 } else {
1641 fname = find_item(pname, product);
1642 skip(2);
1643 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001644 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001645
1646 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001647 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001648 fb_queue_erase(partition.c_str());
1649 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001650 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001651 };
David Pursell0b156632015-10-30 11:22:01 -07001652 do_for_partitions(transport, pname, slot_override.c_str(), flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001653 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001654 char *kname = argv[2];
1655 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001656 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001657 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001658 skip(3);
1659 if (argc > 0) {
1660 rname = argv[0];
1661 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001662 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001663 if (argc > 0) {
1664 sname = argv[0];
1665 skip(1);
1666 }
1667 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001668 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001669 auto flashraw = [&](const std::string &partition) {
1670 fb_queue_flash(partition.c_str(), data, sz);
1671 };
David Pursell0b156632015-10-30 11:22:01 -07001672 do_for_partitions(transport, argv[1], slot_override.c_str(), flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001673 } else if(!strcmp(*argv, "flashall")) {
1674 skip(1);
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001675 if (slot_override == "all") {
1676 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.");
1677 do_flashall(transport, slot_override, erase_first, true, false);
1678 } else {
1679 do_flashall(transport, slot_override, erase_first, true, true);
1680 }
1681 wants_reboot = true;
1682 } else if(!strcmp(*argv, "flash-primary")) {
1683 skip(1);
1684 do_flashall(transport, slot_override, erase_first, true, false);
1685 wants_reboot = true;
1686 } else if(!strcmp(*argv, "flash-secondary")) {
1687 skip(1);
1688 do_flashall(transport, slot_override, erase_first, false, true);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001689 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001690 } else if(!strcmp(*argv, "update")) {
1691 if (argc > 1) {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001692 do_update(transport, argv[1], slot_override, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001693 skip(2);
1694 } else {
Daniel Rosenberg6c985092016-06-28 01:36:21 -07001695 do_update(transport, "update.zip", slot_override, erase_first);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001696 skip(1);
1697 }
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001698 wants_reboot = 1;
1699 } else if(!strcmp(*argv, "set_active")) {
1700 require(2);
1701 std::string slot = verify_slot(transport, argv[1], false);
1702 fb_set_active(slot.c_str());
1703 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001704 } else if(!strcmp(*argv, "oem")) {
1705 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001706 } else if(!strcmp(*argv, "flashing")) {
1707 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1708 !strcmp(*(argv+1), "lock") ||
1709 !strcmp(*(argv+1), "unlock_critical") ||
1710 !strcmp(*(argv+1), "lock_critical") ||
1711 !strcmp(*(argv+1), "get_unlock_ability") ||
1712 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1713 !strcmp(*(argv+1), "lock_bootloader"))) {
1714 argc = do_oem_command(argc, argv);
1715 } else
1716 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1717 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001718 } else {
1719 usage();
1720 return 1;
1721 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001722 } else {
1723 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001724 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001725 }
1726 }
1727
1728 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001729 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001730 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001731 if (set_fbe_marker) {
1732 fprintf(stderr, "setting FBE marker...\n");
1733 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1734 if (initial_userdata_dir.empty()) {
1735 return 1;
1736 }
1737 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1738 delete_fbemarker_tmpdir(initial_userdata_dir);
1739 } else {
1740 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1741 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001742
1743 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001744 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001745 fprintf(stderr, "wiping cache...\n");
1746 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001747 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001748 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001749 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001750 if (wants_set_active) {
1751 fb_set_active(next_active.c_str());
1752 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001753 if (wants_reboot) {
1754 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001755 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001756 } else if (wants_reboot_bootloader) {
1757 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001758 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001759 }
1760
David Pursell0b156632015-10-30 11:22:01 -07001761 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001762}