blob: d6b631f12900ffc9f2a0b81f7f0567207ea64805 [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 Hughes82ff3152016-08-31 15:07:18 -070050#include <android-base/file.h>
Elliott Hughes749ae2d2016-06-28 14:48:45 -070051#include <android-base/macros.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/parseint.h>
David Pursell2ec418a2016-01-20 08:32:08 -080053#include <android-base/parsenetaddress.h>
Elliott Hughes2810d002016-04-25 14:31:18 -070054#include <android-base/stringprintf.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080055#include <android-base/strings.h>
Colin Crossf8387882012-05-24 17:18:41 -070056#include <sparse/sparse.h>
Elliott Hughesd30ad8a2015-03-18 23:12:44 -070057#include <ziparchive/zip_archive.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Elliott Hughes253c18d2015-03-18 22:47:09 -070059#include "bootimg_utils.h"
Elliott Hughes1b708d32015-12-11 19:07:01 -080060#include "diagnose_usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070062#include "fs.h"
David Pursell2ec418a2016-01-20 08:32:08 -080063#include "tcp.h"
David Pursell0b156632015-10-30 11:22:01 -070064#include "transport.h"
David Pursell4601c972016-02-05 15:35:09 -080065#include "udp.h"
David Pursell0b156632015-10-30 11:22:01 -070066#include "usb.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Colin Crossf8387882012-05-24 17:18:41 -070068#ifndef O_BINARY
69#define O_BINARY 0
70#endif
71
Wink Savilleb98762f2011-04-04 17:54:59 -070072char cur_product[FB_RESPONSE_SZ + 1];
73
David Pursell2ec418a2016-01-20 08:32:08 -080074static const char* serial = nullptr;
75static const char* product = nullptr;
76static const char* cmdline = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077static unsigned short vendor_id = 0;
Scott Anderson13081c62012-04-06 12:39:30 -070078static int long_listing = 0;
Colin Crossf8387882012-05-24 17:18:41 -070079static int64_t sparse_limit = -1;
80static int64_t target_sparse_limit = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
Elliott Hughesfc797672015-04-07 20:12:50 -070082static unsigned page_size = 2048;
83static unsigned base_addr = 0x10000000;
84static unsigned kernel_offset = 0x00008000;
85static unsigned ramdisk_offset = 0x01000000;
86static unsigned second_offset = 0x00f00000;
87static unsigned tags_offset = 0x00000100;
JP Abgrall7b8970c2013-03-07 17:06:41 -080088
Paul Crowley8f7f56e2015-11-27 09:29:37 +000089static const std::string convert_fbe_marker_filename("convert_fbe");
90
Rom Lemarchand622810c2013-06-28 09:54:59 -070091enum fb_buffer_type {
92 FB_BUFFER,
93 FB_BUFFER_SPARSE,
94};
95
96struct fastboot_buffer {
97 enum fb_buffer_type type;
Elliott Hughesfc797672015-04-07 20:12:50 -070098 void* data;
99 int64_t sz;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700100};
101
102static struct {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700103 char img_name[17];
104 char sig_name[17];
Rom Lemarchand622810c2013-06-28 09:54:59 -0700105 char part_name[9];
106 bool is_optional;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700107 bool is_secondary;
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700108} images[] = {
Alex Lightbb9b8a52016-06-29 09:26:44 -0700109 {"boot.img", "boot.sig", "boot", false, false},
110 {"boot_other.img", "boot.sig", "boot", true, true},
111 {"recovery.img", "recovery.sig", "recovery", true, false},
112 {"system.img", "system.sig", "system", false, false},
113 {"system_other.img", "system.sig", "system", true, true},
114 {"vendor.img", "vendor.sig", "vendor", true, false},
115 {"vendor_other.img", "vendor.sig", "vendor", true, true},
Rom Lemarchand622810c2013-06-28 09:54:59 -0700116};
Brian Swetland2a63bb72009-04-28 16:05:07 -0700117
Alex Lightbb9b8a52016-06-29 09:26:44 -0700118static std::string find_item_given_name(const char* img_name, const char* product) {
119 if(product) {
Elliott Hughes82ff3152016-08-31 15:07:18 -0700120 std::string path = android::base::GetExecutablePath();
Alex Lightbb9b8a52016-06-29 09:26:44 -0700121 path.erase(path.find_last_of('/'));
122 return android::base::StringPrintf("%s/../../../target/product/%s/%s",
123 path.c_str(), product, img_name);
124 }
125
126 char *dir = getenv("ANDROID_PRODUCT_OUT");
127 if (dir == nullptr || 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) {
Elliott Hughes253c18d2015-03-18 22:47:09 -0700135 const char *fn;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700136
Elliott Hughes2810d002016-04-25 14:31:18 -0700137 if (!strcmp(item,"boot")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 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
Alex Lightbb9b8a52016-06-29 09:26:44 -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 Rosenberg13454092016-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 Rosenberg13454092016-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"
Alex Lightbb9b8a52016-06-29 09:26:44 -0700327 " Secondary images may be flashed to\n"
328 " an inactive slot.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700329 " flash <partition> [ <filename> ] Write a file to a flash partition.\n"
330 " flashing lock Locks the device. Prevents flashing.\n"
331 " flashing unlock Unlocks the device. Allows flashing\n"
332 " any partition except\n"
333 " bootloader-related partitions.\n"
334 " flashing lock_critical Prevents flashing bootloader-related\n"
335 " partitions.\n"
336 " flashing unlock_critical Enables flashing bootloader-related\n"
337 " partitions.\n"
338 " flashing get_unlock_ability Queries bootloader to see if the\n"
339 " device is unlocked.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700340 " flashing get_unlock_bootloader_nonce Queries the bootloader to get the\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700341 " unlock nonce.\n"
342 " flashing unlock_bootloader <request> Issue unlock bootloader using request.\n"
Patrick Tjin51e8b032015-09-01 08:15:23 -0700343 " flashing lock_bootloader Locks the bootloader to prevent\n"
Elliott Hughes45be42e2015-09-02 20:15:48 -0700344 " bootloader version rollback.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700345 " erase <partition> Erase a flash partition.\n"
346 " format[:[<fs type>][:[<size>]] <partition>\n"
347 " Format a flash partition. Can\n"
348 " override the fs type and/or size\n"
349 " the bootloader reports.\n"
350 " getvar <variable> Display a bootloader variable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700351 " set_active <slot> Sets the active slot. If slots are\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800352 " not supported, this does nothing.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700353 " boot <kernel> [ <ramdisk> [ <second> ] ] Download and boot kernel.\n"
354 " flash:raw boot <kernel> [ <ramdisk> [ <second> ] ]\n"
355 " Create bootimage and flash it.\n"
356 " devices [-l] List all connected devices [with\n"
357 " device paths].\n"
358 " continue Continue with autoboot.\n"
359 " reboot [bootloader] Reboot device [into bootloader].\n"
360 " reboot-bootloader Reboot device into bootloader.\n"
361 " help Show this help message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800362 "\n"
363 "options:\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700364 " -w Erase userdata and cache (and format\n"
365 " if supported by partition type).\n"
366 " -u Do not erase partition before\n"
367 " formatting.\n"
David Pursell2ec418a2016-01-20 08:32:08 -0800368 " -s <specific device> Specify a device. For USB, provide either\n"
369 " a serial number or path to device port.\n"
Alex Deymof62d0cd2016-03-24 19:51:05 -0700370 " For ethernet, provide an address in the\n"
371 " form <protocol>:<hostname>[:port] where\n"
David Pursell4601c972016-02-05 15:35:09 -0800372 " <protocol> is either tcp or udp.\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700373 " -p <product> Specify product name.\n"
374 " -c <cmdline> Override kernel commandline.\n"
375 " -i <vendor id> Specify a custom USB vendor id.\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800376 " -b, --base <base_addr> Specify a custom kernel base\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700377 " address (default: 0x10000000).\n"
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800378 " --kernel-offset Specify a custom kernel offset.\n"
379 " (default: 0x00008000)\n"
380 " --ramdisk-offset Specify a custom ramdisk offset.\n"
381 " (default: 0x01000000)\n"
382 " --tags-offset Specify a custom tags offset.\n"
383 " (default: 0x00000100)\n"
384 " -n, --page-size <page size> Specify the nand page size\n"
Elliott Hughes08df5332015-06-10 11:58:14 -0700385 " (default: 2048).\n"
386 " -S <size>[K|M|G] Automatically sparse files greater\n"
387 " than 'size'. 0 to disable.\n"
Daniel Rosenberg80919472016-06-30 19:25:31 -0700388 " --slot <slot> Specify slot name to be used if the\n"
389 " device supports slots. All operations\n"
390 " on partitions that support slots will\n"
391 " be done on the slot specified.\n"
392 " 'all' can be given to refer to all slots.\n"
393 " 'other' can be given to refer to a\n"
394 " non-current slot. If this flag is not\n"
395 " used, slotted partitions will default\n"
396 " to the current active slot.\n"
397 " -a, --set-active[=<slot>] Sets the active slot. If no slot is\n"
Daniel Rosenberg0d088562015-11-11 16:15:30 -0800398 " provided, this will default to the value\n"
399 " given by --slot. If slots are not\n"
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800400 " supported, this does nothing. This will\n"
401 " run after all non-reboot commands.\n"
Daniel Rosenberg92b44762016-07-13 20:03:25 -0700402 " --skip-secondary Will not flash secondary slots when\n"
403 " performing a flashall or update. This\n"
404 " will preserve data on other slots.\n"
Mitchell Wills31dce302016-09-26 10:26:21 -0700405 " --skip-reboot Will not reboot the device when\n"
406 " performing commands that normally\n"
407 " trigger a reboot.\n"
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000408#if !defined(_WIN32)
409 " --wipe-and-use-fbe On devices which support it,\n"
410 " erase userdata and cache, and\n"
411 " enable file-based encryption\n"
412#endif
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -0800413 " --unbuffered Do not buffer input or output.\n"
414 " --version Display version.\n"
415 " -h, --help show this message.\n"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 );
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417}
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000418
Elliott Hughesfc797672015-04-07 20:12:50 -0700419static void* load_bootable_image(const char* kernel, const char* ramdisk,
420 const char* secondstage, int64_t* sz,
421 const char* cmdline) {
422 if (kernel == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800423 fprintf(stderr, "no image specified\n");
424 return 0;
425 }
426
Elliott Hughesfc797672015-04-07 20:12:50 -0700427 int64_t ksize;
428 void* kdata = load_file(kernel, &ksize);
429 if (kdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800430 fprintf(stderr, "cannot load '%s': %s\n", kernel, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 return 0;
432 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800433
Elliott Hughesfc797672015-04-07 20:12:50 -0700434 // Is this actually a boot image?
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 if(!memcmp(kdata, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700436 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) kdata, cmdline);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800437
Elliott Hughesfc797672015-04-07 20:12:50 -0700438 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 fprintf(stderr, "cannot boot a boot.img *and* ramdisk\n");
440 return 0;
441 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443 *sz = ksize;
444 return kdata;
445 }
446
Elliott Hughesfc797672015-04-07 20:12:50 -0700447 void* rdata = nullptr;
448 int64_t rsize = 0;
449 if (ramdisk) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 rdata = load_file(ramdisk, &rsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700451 if (rdata == nullptr) {
Matt Gumbel64ba2582011-12-08 11:59:38 -0800452 fprintf(stderr,"cannot load '%s': %s\n", ramdisk, strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 return 0;
454 }
455 }
456
Elliott Hughesfc797672015-04-07 20:12:50 -0700457 void* sdata = nullptr;
458 int64_t ssize = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200459 if (secondstage) {
460 sdata = load_file(secondstage, &ssize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700461 if (sdata == nullptr) {
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200462 fprintf(stderr,"cannot load '%s': %s\n", secondstage, strerror(errno));
463 return 0;
464 }
465 }
466
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467 fprintf(stderr,"creating boot image...\n");
Elliott Hughesfc797672015-04-07 20:12:50 -0700468 int64_t bsize = 0;
469 void* bdata = mkbootimg(kdata, ksize, kernel_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800470 rdata, rsize, ramdisk_offset,
Jeremy Compostellaa42adff2014-07-17 17:17:54 +0200471 sdata, ssize, second_offset,
JP Abgrall7b8970c2013-03-07 17:06:41 -0800472 page_size, base_addr, tags_offset, &bsize);
Elliott Hughesfc797672015-04-07 20:12:50 -0700473 if (bdata == nullptr) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474 fprintf(stderr,"failed to create boot.img\n");
475 return 0;
476 }
Elliott Hughesfc797672015-04-07 20:12:50 -0700477 if (cmdline) bootimg_set_cmdline((boot_img_hdr*) bdata, cmdline);
478 fprintf(stderr, "creating boot image - %" PRId64 " bytes\n", bsize);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800479 *sz = bsize;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800480
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481 return bdata;
482}
483
Elliott Hughesfc797672015-04-07 20:12:50 -0700484static void* unzip_file(ZipArchiveHandle zip, const char* entry_name, int64_t* sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485{
Yusuke Sato07447542015-06-25 14:39:19 -0700486 ZipString zip_entry_name(entry_name);
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700487 ZipEntry zip_entry;
488 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700489 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000490 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491 }
492
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700493 *sz = zip_entry.uncompressed_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494
Elliott Hughesd30ad8a2015-03-18 23:12:44 -0700495 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(zip_entry.uncompressed_length));
Elliott Hughesfc797672015-04-07 20:12:50 -0700496 if (data == nullptr) {
497 fprintf(stderr, "failed to allocate %" PRId64 " bytes for '%s'\n", *sz, entry_name);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000498 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 }
500
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700501 int error = ExtractToMemory(zip, &zip_entry, data, zip_entry.uncompressed_length);
502 if (error != 0) {
503 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 free(data);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000505 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800506 }
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000507
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800508 return data;
509}
510
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700511#if defined(_WIN32)
512
513// TODO: move this to somewhere it can be shared.
514
515#include <windows.h>
516
517// Windows' tmpfile(3) requires administrator rights because
518// it creates temporary files in the root directory.
519static FILE* win32_tmpfile() {
520 char temp_path[PATH_MAX];
521 DWORD nchars = GetTempPath(sizeof(temp_path), temp_path);
522 if (nchars == 0 || nchars >= sizeof(temp_path)) {
523 fprintf(stderr, "GetTempPath failed, error %ld\n", GetLastError());
524 return nullptr;
525 }
526
527 char filename[PATH_MAX];
528 if (GetTempFileName(temp_path, "fastboot", 0, filename) == 0) {
529 fprintf(stderr, "GetTempFileName failed, error %ld\n", GetLastError());
530 return nullptr;
531 }
532
533 return fopen(filename, "w+bTD");
534}
535
536#define tmpfile win32_tmpfile
537
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000538static std::string make_temporary_directory() {
539 fprintf(stderr, "make_temporary_directory not supported under Windows, sorry!");
540 return "";
541}
542
543#else
544
545static std::string make_temporary_directory() {
546 const char *tmpdir = getenv("TMPDIR");
547 if (tmpdir == nullptr) {
548 tmpdir = P_tmpdir;
549 }
550 std::string result = std::string(tmpdir) + "/fastboot_userdata_XXXXXX";
551 if (mkdtemp(&result[0]) == NULL) {
552 fprintf(stderr, "Unable to create temporary directory: %s\n",
553 strerror(errno));
554 return "";
555 }
556 return result;
557}
558
Elliott Hughesa26fbee2015-06-03 15:22:11 -0700559#endif
560
Paul Crowley8f7f56e2015-11-27 09:29:37 +0000561static std::string create_fbemarker_tmpdir() {
562 std::string dir = make_temporary_directory();
563 if (dir.empty()) {
564 fprintf(stderr, "Unable to create local temp directory for FBE marker\n");
565 return "";
566 }
567 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
568 int fd = open(marker_file.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
569 if (fd == -1) {
570 fprintf(stderr, "Unable to create FBE marker file %s locally: %d, %s\n",
571 marker_file.c_str(), errno, strerror(errno));
572 return "";
573 }
574 close(fd);
575 return dir;
576}
577
578static void delete_fbemarker_tmpdir(const std::string& dir) {
579 std::string marker_file = dir + "/" + convert_fbe_marker_filename;
580 if (unlink(marker_file.c_str()) == -1) {
581 fprintf(stderr, "Unable to delete FBE marker file %s locally: %d, %s\n",
582 marker_file.c_str(), errno, strerror(errno));
583 return;
584 }
585 if (rmdir(dir.c_str()) == -1) {
586 fprintf(stderr, "Unable to delete FBE marker directory %s locally: %d, %s\n",
587 dir.c_str(), errno, strerror(errno));
588 return;
589 }
590}
591
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700592static int unzip_to_file(ZipArchiveHandle zip, char* entry_name) {
593 FILE* fp = tmpfile();
Elliott Hughesfc797672015-04-07 20:12:50 -0700594 if (fp == nullptr) {
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700595 fprintf(stderr, "failed to create temporary file for '%s': %s\n",
596 entry_name, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -0700597 return -1;
598 }
599
Yusuke Sato07447542015-06-25 14:39:19 -0700600 ZipString zip_entry_name(entry_name);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700601 ZipEntry zip_entry;
602 if (FindEntry(zip, zip_entry_name, &zip_entry) != 0) {
603 fprintf(stderr, "archive does not contain '%s'\n", entry_name);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700604 fclose(fp);
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000605 return -1;
606 }
607
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700608 int fd = fileno(fp);
609 int error = ExtractEntryToFile(zip, &zip_entry, fd);
610 if (error != 0) {
611 fprintf(stderr, "failed to extract '%s': %s\n", entry_name, ErrorCodeString(error));
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700612 fclose(fp);
Elliott Hughesa82c89d2015-03-19 11:44:32 -0700613 return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700614 }
615
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000616 lseek(fd, 0, SEEK_SET);
Greg Kaiserdc9b62b2016-08-11 11:34:16 -0700617 // TODO: We're leaking 'fp' here.
Rom Lemarchand622810c2013-06-28 09:54:59 -0700618 return fd;
619}
620
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800621static char *strip(char *s)
622{
623 int n;
624 while(*s && isspace(*s)) s++;
625 n = strlen(s);
626 while(n-- > 0) {
627 if(!isspace(s[n])) break;
628 s[n] = 0;
629 }
630 return s;
631}
632
633#define MAX_OPTIONS 32
634static int setup_requirement_line(char *name)
635{
636 char *val[MAX_OPTIONS];
Elliott Hughesfc797672015-04-07 20:12:50 -0700637 char *prod = nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800638 unsigned n, count;
639 char *x;
640 int invert = 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800641
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800642 if (!strncmp(name, "reject ", 7)) {
643 name += 7;
644 invert = 1;
645 } else if (!strncmp(name, "require ", 8)) {
646 name += 8;
647 invert = 0;
Wink Savilleb98762f2011-04-04 17:54:59 -0700648 } else if (!strncmp(name, "require-for-product:", 20)) {
649 // Get the product and point name past it
650 prod = name + 20;
651 name = strchr(name, ' ');
652 if (!name) return -1;
653 *name = 0;
654 name += 1;
655 invert = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800656 }
657
658 x = strchr(name, '=');
659 if (x == 0) return 0;
660 *x = 0;
661 val[0] = x + 1;
662
663 for(count = 1; count < MAX_OPTIONS; count++) {
664 x = strchr(val[count - 1],'|');
665 if (x == 0) break;
666 *x = 0;
667 val[count] = x + 1;
668 }
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800669
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800670 name = strip(name);
671 for(n = 0; n < count; n++) val[n] = strip(val[n]);
Tsu Chiang Chuangee520552011-02-25 18:38:53 -0800672
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800673 name = strip(name);
674 if (name == 0) return -1;
675
Elliott Hughes253c18d2015-03-18 22:47:09 -0700676 const char* var = name;
677 // Work around an unfortunate name mismatch.
678 if (!strcmp(name,"board")) var = "product";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800679
Elliott Hughes253c18d2015-03-18 22:47:09 -0700680 const char** out = reinterpret_cast<const char**>(malloc(sizeof(char*) * count));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800681 if (out == 0) return -1;
682
683 for(n = 0; n < count; n++) {
684 out[n] = strdup(strip(val[n]));
Elliott Hughes14e28d32013-10-29 14:12:46 -0700685 if (out[n] == 0) {
686 for(size_t i = 0; i < n; ++i) {
687 free((char*) out[i]);
688 }
689 free(out);
690 return -1;
691 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800692 }
693
Elliott Hughes253c18d2015-03-18 22:47:09 -0700694 fb_queue_require(prod, var, invert, n, out);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800695 return 0;
696}
697
Elliott Hughesfc797672015-04-07 20:12:50 -0700698static void setup_requirements(char* data, int64_t sz) {
699 char* s = data;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800700 while (sz-- > 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700701 if (*s == '\n') {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800702 *s++ = 0;
703 if (setup_requirement_line(data)) {
704 die("out of memory");
705 }
706 data = s;
707 } else {
708 s++;
709 }
710 }
711}
712
Elliott Hughesfc797672015-04-07 20:12:50 -0700713static void queue_info_dump() {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800714 fb_queue_notice("--------------------------------------------");
715 fb_queue_display("version-bootloader", "Bootloader Version...");
716 fb_queue_display("version-baseband", "Baseband Version.....");
717 fb_queue_display("serialno", "Serial Number........");
718 fb_queue_notice("--------------------------------------------");
719}
720
Rom Lemarchand622810c2013-06-28 09:54:59 -0700721static struct sparse_file **load_sparse_files(int fd, int max_size)
Colin Crossf8387882012-05-24 17:18:41 -0700722{
Mohamad Ayyash80cc1f62015-03-31 12:09:29 -0700723 struct sparse_file* s = sparse_file_import_auto(fd, false, true);
Colin Crossf8387882012-05-24 17:18:41 -0700724 if (!s) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700725 die("cannot sparse read file\n");
Colin Crossf8387882012-05-24 17:18:41 -0700726 }
727
Elliott Hughesfc797672015-04-07 20:12:50 -0700728 int files = sparse_file_resparse(s, max_size, nullptr, 0);
Colin Crossf8387882012-05-24 17:18:41 -0700729 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700730 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700731 }
732
Elliott Hughes253c18d2015-03-18 22:47:09 -0700733 sparse_file** out_s = reinterpret_cast<sparse_file**>(calloc(sizeof(struct sparse_file *), files + 1));
Colin Crossf8387882012-05-24 17:18:41 -0700734 if (!out_s) {
735 die("Failed to allocate sparse file array\n");
736 }
737
738 files = sparse_file_resparse(s, max_size, out_s, files);
739 if (files < 0) {
Rom Lemarchand622810c2013-06-28 09:54:59 -0700740 die("Failed to resparse\n");
Colin Crossf8387882012-05-24 17:18:41 -0700741 }
742
743 return out_s;
744}
745
David Pursell0b156632015-10-30 11:22:01 -0700746static int64_t get_target_sparse_limit(Transport* transport) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700747 std::string max_download_size;
David Pursell0b156632015-10-30 11:22:01 -0700748 if (!fb_getvar(transport, "max-download-size", &max_download_size) ||
749 max_download_size.empty()) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800750 fprintf(stderr, "target didn't report max-download-size\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700751 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700752 }
753
Elliott Hughes77c0e662015-11-02 15:51:12 -0800754 // Some bootloaders (angler, for example) send spurious whitespace too.
755 max_download_size = android::base::Trim(max_download_size);
756
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700757 uint64_t limit;
758 if (!android::base::ParseUint(max_download_size.c_str(), &limit)) {
Elliott Hughes3ab05862015-11-02 09:01:53 -0800759 fprintf(stderr, "couldn't parse max-download-size '%s'\n", max_download_size.c_str());
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700760 return 0;
761 }
762 if (limit > 0) {
763 fprintf(stderr, "target reported max download size of %" PRId64 " bytes\n", limit);
764 }
Colin Crossf8387882012-05-24 17:18:41 -0700765 return limit;
766}
767
David Pursell0b156632015-10-30 11:22:01 -0700768static int64_t get_sparse_limit(Transport* transport, int64_t size) {
Colin Crossf8387882012-05-24 17:18:41 -0700769 int64_t limit;
770
771 if (sparse_limit == 0) {
772 return 0;
773 } else if (sparse_limit > 0) {
774 limit = sparse_limit;
775 } else {
776 if (target_sparse_limit == -1) {
David Pursell0b156632015-10-30 11:22:01 -0700777 target_sparse_limit = get_target_sparse_limit(transport);
Colin Crossf8387882012-05-24 17:18:41 -0700778 }
779 if (target_sparse_limit > 0) {
780 limit = target_sparse_limit;
781 } else {
Colin Cross0bbfb392012-07-24 18:05:21 -0700782 return 0;
Colin Crossf8387882012-05-24 17:18:41 -0700783 }
784 }
785
786 if (size > limit) {
787 return limit;
788 }
789
790 return 0;
791}
792
Elliott Hughes2fd45a92015-10-30 11:49:47 -0700793// Until we get lazy inode table init working in make_ext4fs, we need to
794// erase partitions of type ext4 before flashing a filesystem so no stale
795// inodes are left lying around. Otherwise, e2fsck gets very upset.
David Pursell0b156632015-10-30 11:22:01 -0700796static bool needs_erase(Transport* transport, const char* partition) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800797 std::string partition_type;
David Pursell0b156632015-10-30 11:22:01 -0700798 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Elliott Hughes8ab9a322015-11-02 14:05:57 -0800799 return false;
800 }
801 return partition_type == "ext4";
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700802}
803
Elliott Hughes53ec4952016-05-11 12:39:27 -0700804static bool load_buf_fd(Transport* transport, int fd, struct fastboot_buffer* buf) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700805 int64_t sz = get_file_size(fd);
806 if (sz == -1) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700807 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700808 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700809
Elliott Hughesfc797672015-04-07 20:12:50 -0700810 lseek64(fd, 0, SEEK_SET);
David Pursell0b156632015-10-30 11:22:01 -0700811 int64_t limit = get_sparse_limit(transport, sz);
Colin Crossf8387882012-05-24 17:18:41 -0700812 if (limit) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700813 sparse_file** s = load_sparse_files(fd, limit);
814 if (s == nullptr) {
Elliott Hughes53ec4952016-05-11 12:39:27 -0700815 return false;
Colin Crossf8387882012-05-24 17:18:41 -0700816 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700817 buf->type = FB_BUFFER_SPARSE;
818 buf->data = s;
Colin Crossf8387882012-05-24 17:18:41 -0700819 } else {
Elliott Hughesfc797672015-04-07 20:12:50 -0700820 void* data = load_fd(fd, &sz);
821 if (data == nullptr) return -1;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700822 buf->type = FB_BUFFER;
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700823 buf->data = data;
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000824 buf->sz = sz;
Colin Crossf8387882012-05-24 17:18:41 -0700825 }
Rom Lemarchand622810c2013-06-28 09:54:59 -0700826
Elliott Hughes53ec4952016-05-11 12:39:27 -0700827 return true;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700828}
829
Elliott Hughes53ec4952016-05-11 12:39:27 -0700830static bool load_buf(Transport* transport, const char* fname, struct fastboot_buffer* buf) {
831 int fd = open(fname, O_RDONLY | O_BINARY);
832 if (fd == -1) {
833 return false;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700834 }
David Pursell0b156632015-10-30 11:22:01 -0700835 return load_buf_fd(transport, fd, buf);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700836}
837
838static void flash_buf(const char *pname, struct fastboot_buffer *buf)
839{
Elliott Hughes253c18d2015-03-18 22:47:09 -0700840 sparse_file** s;
Rom Lemarchand622810c2013-06-28 09:54:59 -0700841
842 switch (buf->type) {
Josh Gao9da9ac52016-01-19 14:50:18 -0800843 case FB_BUFFER_SPARSE: {
844 std::vector<std::pair<sparse_file*, int64_t>> sparse_files;
Elliott Hughes253c18d2015-03-18 22:47:09 -0700845 s = reinterpret_cast<sparse_file**>(buf->data);
Rom Lemarchand622810c2013-06-28 09:54:59 -0700846 while (*s) {
Elliott Hughesfc797672015-04-07 20:12:50 -0700847 int64_t sz = sparse_file_len(*s, true, false);
Josh Gao9da9ac52016-01-19 14:50:18 -0800848 sparse_files.emplace_back(*s, sz);
849 ++s;
850 }
851
852 for (size_t i = 0; i < sparse_files.size(); ++i) {
853 const auto& pair = sparse_files[i];
854 fb_queue_flash_sparse(pname, pair.first, pair.second, i + 1, sparse_files.size());
Rom Lemarchand622810c2013-06-28 09:54:59 -0700855 }
856 break;
Josh Gao9da9ac52016-01-19 14:50:18 -0800857 }
858
Rom Lemarchand622810c2013-06-28 09:54:59 -0700859 case FB_BUFFER:
860 fb_queue_flash(pname, buf->data, buf->sz);
861 break;
862 default:
863 die("unknown buffer type: %d", buf->type);
864 }
865}
866
Daniel Rosenberg80919472016-06-30 19:25:31 -0700867static std::string get_current_slot(Transport* transport)
868{
869 std::string current_slot;
870 if (fb_getvar(transport, "current-slot", &current_slot)) {
871 if (current_slot == "_a") return "a"; // Legacy support
872 if (current_slot == "_b") return "b"; // Legacy support
873 return current_slot;
874 }
875 return "";
876}
877
878// Legacy support
879static std::vector<std::string> get_suffixes_obsolete(Transport* transport) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700880 std::vector<std::string> suffixes;
881 std::string suffix_list;
David Pursell0b156632015-10-30 11:22:01 -0700882 if (!fb_getvar(transport, "slot-suffixes", &suffix_list)) {
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700883 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700884 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700885 suffixes = android::base::Split(suffix_list, ",");
886 // Unfortunately some devices will return an error message in the
887 // guise of a valid value. If we only see only one suffix, it's probably
888 // not real.
889 if (suffixes.size() == 1) {
890 suffixes.clear();
891 }
892 return suffixes;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700893}
894
Daniel Rosenberg80919472016-06-30 19:25:31 -0700895// Legacy support
896static bool supports_AB_obsolete(Transport* transport) {
897 return !get_suffixes_obsolete(transport).empty();
898}
899
900static int get_slot_count(Transport* transport) {
901 std::string var;
902 int count;
903 if (!fb_getvar(transport, "slot-count", &var)) {
904 if (supports_AB_obsolete(transport)) return 2; // Legacy support
905 }
906 if (!android::base::ParseInt(var.c_str(), &count)) return 0;
907 return count;
908}
909
Alex Lightbb9b8a52016-06-29 09:26:44 -0700910static bool supports_AB(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700911 return get_slot_count(transport) >= 2;
Alex Lightbb9b8a52016-06-29 09:26:44 -0700912}
913
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700914// Given a current slot, this returns what the 'other' slot is.
Daniel Rosenberg80919472016-06-30 19:25:31 -0700915static std::string get_other_slot(const std::string& current_slot, int count) {
916 if (count == 0) return "";
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700917
Daniel Rosenberg80919472016-06-30 19:25:31 -0700918 char next = (current_slot[0] - 'a' + 1)%count + 'a';
919 return std::string(1, next);
920}
921
922static std::string get_other_slot(Transport* transport, const std::string& current_slot) {
923 return get_other_slot(current_slot, get_slot_count(transport));
924}
925
926static std::string get_other_slot(Transport* transport, int count) {
927 return get_other_slot(get_current_slot(transport), count);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700928}
929
Alex Lightbb9b8a52016-06-29 09:26:44 -0700930static std::string get_other_slot(Transport* transport) {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700931 return get_other_slot(get_current_slot(transport), get_slot_count(transport));
Alex Lightbb9b8a52016-06-29 09:26:44 -0700932}
933
Daniel Rosenberg80919472016-06-30 19:25:31 -0700934static std::string verify_slot(Transport* transport, const std::string& slot_name, bool allow_all) {
935 std::string slot = slot_name;
936 if (slot == "_a") slot = "a"; // Legacy support
937 if (slot == "_b") slot = "b"; // Legacy support
938 if (slot == "all") {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800939 if (allow_all) {
940 return "all";
941 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700942 int count = get_slot_count(transport);
943 if (count > 0) {
944 return "a";
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800945 } else {
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800946 die("No known slots.");
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800947 }
948 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700949 }
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800950
Daniel Rosenberg80919472016-06-30 19:25:31 -0700951 int count = get_slot_count(transport);
952 if (count == 0) die("Device does not support slots.\n");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800953
Daniel Rosenberg80919472016-06-30 19:25:31 -0700954 if (slot == "other") {
955 std::string other = get_other_slot(transport, count);
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700956 if (other == "") {
957 die("No known slots.");
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800958 }
Daniel Rosenbergad3d3c12016-06-27 23:03:48 -0700959 return other;
Daniel Rosenbergc1743ba2016-01-05 16:54:40 -0800960 }
961
Daniel Rosenberg80919472016-06-30 19:25:31 -0700962 if (slot.size() == 1 && (slot[0]-'a' >= 0 && slot[0]-'a' < count)) return slot;
963
964 fprintf(stderr, "Slot %s does not exist. supported slots are:\n", slot.c_str());
965 for (int i=0; i<count; i++) {
966 fprintf(stderr, "%c\n", (char)(i + 'a'));
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700967 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700968
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700969 exit(1);
970}
971
Daniel Rosenberg80919472016-06-30 19:25:31 -0700972static std::string verify_slot(Transport* transport, const std::string& slot) {
Daniel Rosenberg9b432052015-11-25 15:17:10 -0800973 return verify_slot(transport, slot, true);
974}
975
Daniel Rosenberg80919472016-06-30 19:25:31 -0700976static void do_for_partition(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -0700977 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800978 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700979 std::string current_slot;
980
Daniel Rosenberg80919472016-06-30 19:25:31 -0700981 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
Daniel Rosenberga7974792015-11-11 16:13:13 -0800982 /* If has-slot is not supported, the answer is no. */
983 has_slot = "no";
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700984 }
Daniel Rosenberga7974792015-11-11 16:13:13 -0800985 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700986 if (slot == "") {
987 current_slot = get_current_slot(transport);
988 if (current_slot == "") {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700989 die("Failed to identify current slot.\n");
990 }
Daniel Rosenberg80919472016-06-30 19:25:31 -0700991 func(part + "_" + current_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700992 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700993 func(part + '_' + slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700994 }
995 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -0700996 if (force_slot && slot != "") {
David Pursell0b156632015-10-30 11:22:01 -0700997 fprintf(stderr, "Warning: %s does not support slots, and slot %s was requested.\n",
Daniel Rosenberg80919472016-06-30 19:25:31 -0700998 part.c_str(), slot.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700999 }
1000 func(part);
1001 }
1002}
1003
David Pursell0b156632015-10-30 11:22:01 -07001004/* This function will find the real partition name given a base name, and a slot. If slot is NULL or
1005 * empty, it will use the current slot. If slot is "all", it will return a list of all possible
1006 * partition names. If force_slot is true, it will fail if a slot is specified, and the given
1007 * partition does not support slots.
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001008 */
Daniel Rosenberg80919472016-06-30 19:25:31 -07001009static void do_for_partitions(Transport* transport, const std::string& part, const std::string& slot,
Chih-Hung Hsieh8f7b9e32016-07-27 16:25:51 -07001010 const std::function<void(const std::string&)>& func, bool force_slot) {
Daniel Rosenberga7974792015-11-11 16:13:13 -08001011 std::string has_slot;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001012
Daniel Rosenberg80919472016-06-30 19:25:31 -07001013 if (slot == "all") {
1014 if (!fb_getvar(transport, "has-slot:" + part, &has_slot)) {
1015 die("Could not check if partition %s has slot.", part.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001016 }
Daniel Rosenberga7974792015-11-11 16:13:13 -08001017 if (has_slot == "yes") {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001018 for (int i=0; i < get_slot_count(transport); i++) {
1019 do_for_partition(transport, part, std::string(1, (char)(i + 'a')), func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001020 }
1021 } else {
David Pursell0b156632015-10-30 11:22:01 -07001022 do_for_partition(transport, part, "", func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001023 }
1024 } else {
David Pursell0b156632015-10-30 11:22:01 -07001025 do_for_partition(transport, part, slot, func, force_slot);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001026 }
1027}
1028
David Pursell0b156632015-10-30 11:22:01 -07001029static void do_flash(Transport* transport, const char* pname, const char* fname) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001030 struct fastboot_buffer buf;
1031
Elliott Hughes53ec4952016-05-11 12:39:27 -07001032 if (!load_buf(transport, fname, &buf)) {
1033 die("cannot load '%s': %s", fname, strerror(errno));
Rom Lemarchand622810c2013-06-28 09:54:59 -07001034 }
1035 flash_buf(pname, &buf);
Colin Crossf8387882012-05-24 17:18:41 -07001036}
1037
Elliott Hughesfc797672015-04-07 20:12:50 -07001038static void do_update_signature(ZipArchiveHandle zip, char* fn) {
1039 int64_t sz;
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001040 void* data = unzip_file(zip, fn, &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001041 if (data == nullptr) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001042 fb_queue_download("signature", data, sz);
1043 fb_queue_command("signature", "installing signature");
1044}
1045
Daniel Rosenberg13454092016-06-27 19:43:11 -07001046// Sets slot_override as the active slot. If slot_override is blank,
1047// set current slot as active instead. This clears slot-unbootable.
Alex Lightbb9b8a52016-06-29 09:26:44 -07001048static void set_active(Transport* transport, const std::string& slot_override) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001049 std::string separator = "";
Alex Lightbb9b8a52016-06-29 09:26:44 -07001050 if (!supports_AB(transport)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001051 if (supports_AB_obsolete(transport)) {
1052 separator = "_"; // Legacy support
1053 } else {
1054 return;
1055 }
1056 }
1057 if (slot_override != "") {
1058 fb_set_active((separator + slot_override).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001059 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001060 std::string current_slot = get_current_slot(transport);
1061 if (current_slot != "") {
1062 fb_set_active((separator + current_slot).c_str());
Daniel Rosenberg13454092016-06-27 19:43:11 -07001063 }
1064 }
1065}
1066
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001067static void do_update(Transport* transport, const char* filename, const std::string& slot_override, bool erase_first, bool skip_secondary) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001068 queue_info_dump();
1069
Wink Savilleb98762f2011-04-04 17:54:59 -07001070 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1071
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001072 ZipArchiveHandle zip;
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001073 int error = OpenArchive(filename, &zip);
1074 if (error != 0) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001075 CloseArchive(zip);
Elliott Hughesa82c89d2015-03-19 11:44:32 -07001076 die("failed to open zip file '%s': %s", filename, ErrorCodeString(error));
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001077 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001078
Elliott Hughesfc797672015-04-07 20:12:50 -07001079 int64_t sz;
Elliott Hughes253c18d2015-03-18 22:47:09 -07001080 void* data = unzip_file(zip, "android-info.txt", &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001081 if (data == nullptr) {
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001082 CloseArchive(zip);
Elliott Hughes7c6d8842015-03-19 10:30:53 -07001083 die("update package '%s' has no android-info.txt", filename);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001084 }
1085
Elliott Hughes253c18d2015-03-18 22:47:09 -07001086 setup_requirements(reinterpret_cast<char*>(data), sz);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001087
Alex Lightbb9b8a52016-06-29 09:26:44 -07001088 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001089 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001090 if (slot_override != "") {
1091 secondary = get_other_slot(transport, slot_override);
1092 } else {
1093 secondary = get_other_slot(transport);
1094 }
1095 if (secondary == "") {
1096 if (supports_AB(transport)) {
1097 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1098 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001099 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001100 }
1101 }
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001102 for (size_t i = 0; i < arraysize(images); ++i) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001103 const char* slot = slot_override.c_str();
1104 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001105 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001106 slot = secondary.c_str();
1107 } else {
1108 continue;
1109 }
1110 }
1111
Elliott Hughes253c18d2015-03-18 22:47:09 -07001112 int fd = unzip_to_file(zip, images[i].img_name);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001113 if (fd == -1) {
1114 if (images[i].is_optional) {
Rom Lemarchand622810c2013-06-28 09:54:59 -07001115 continue;
Elliott Hughesacdbe922015-06-02 21:37:05 -07001116 }
Narayan Kamathf6e9ffb2015-05-11 16:59:46 +01001117 CloseArchive(zip);
Elliott Hughesacdbe922015-06-02 21:37:05 -07001118 exit(1); // unzip_to_file already explained why.
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001119 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001120 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001121 if (!load_buf_fd(transport, fd, &buf)) {
1122 die("cannot load %s from flash: %s", images[i].img_name, strerror(errno));
1123 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001124
1125 auto update = [&](const std::string &partition) {
1126 do_update_signature(zip, images[i].sig_name);
David Pursell0b156632015-10-30 11:22:01 -07001127 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001128 fb_queue_erase(partition.c_str());
1129 }
1130 flash_buf(partition.c_str(), &buf);
1131 /* not closing the fd here since the sparse code keeps the fd around
1132 * but hasn't mmaped data yet. The tmpfile will get cleaned up when the
1133 * program exits.
1134 */
1135 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001136 do_for_partitions(transport, images[i].part_name, slot, update, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001137 }
Elliott Hughesd30ad8a2015-03-18 23:12:44 -07001138
1139 CloseArchive(zip);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001140 if (slot_override == "all") {
1141 set_active(transport, "a");
1142 } else {
1143 set_active(transport, slot_override);
1144 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001145}
1146
Alex Lightbb9b8a52016-06-29 09:26:44 -07001147static void do_send_signature(const std::string& fn) {
1148 std::size_t extension_loc = fn.find(".img");
1149 if (extension_loc == std::string::npos) return;
Elliott Hughesfc797672015-04-07 20:12:50 -07001150
Alex Lightbb9b8a52016-06-29 09:26:44 -07001151 std::string fs_sig = fn.substr(0, extension_loc) + ".sig";
Elliott Hughesfc797672015-04-07 20:12:50 -07001152
1153 int64_t sz;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001154 void* data = load_file(fs_sig.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001155 if (data == nullptr) return;
Elliott Hughes2810d002016-04-25 14:31:18 -07001156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001157 fb_queue_download("signature", data, sz);
1158 fb_queue_command("signature", "installing signature");
1159}
1160
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001161static void do_flashall(Transport* transport, const std::string& slot_override, int erase_first, bool skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001162 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001163 queue_info_dump();
1164
Wink Savilleb98762f2011-04-04 17:54:59 -07001165 fb_queue_query_save("product", cur_product, sizeof(cur_product));
1166
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001167 fname = find_item("info", product);
Elliott Hughes2810d002016-04-25 14:31:18 -07001168 if (fname.empty()) die("cannot find android-info.txt");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001169
Elliott Hughesfc797672015-04-07 20:12:50 -07001170 int64_t sz;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001171 void* data = load_file(fname.c_str(), &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001172 if (data == nullptr) die("could not load android-info.txt: %s", strerror(errno));
Elliott Hughes253c18d2015-03-18 22:47:09 -07001173
1174 setup_requirements(reinterpret_cast<char*>(data), sz);
1175
Alex Lightbb9b8a52016-06-29 09:26:44 -07001176 std::string secondary;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001177 if (!skip_secondary) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001178 if (slot_override != "") {
1179 secondary = get_other_slot(transport, slot_override);
1180 } else {
1181 secondary = get_other_slot(transport);
1182 }
1183 if (secondary == "") {
1184 if (supports_AB(transport)) {
1185 fprintf(stderr, "Warning: Could not determine slot for secondary images. Ignoring.\n");
1186 }
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001187 skip_secondary = true;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001188 }
1189 }
Elliott Hughes253c18d2015-03-18 22:47:09 -07001190
Elliott Hughes749ae2d2016-06-28 14:48:45 -07001191 for (size_t i = 0; i < arraysize(images); i++) {
Alex Lightbb9b8a52016-06-29 09:26:44 -07001192 const char* slot = NULL;
1193 if (images[i].is_secondary) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001194 if (!skip_secondary) slot = secondary.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001195 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001196 slot = slot_override.c_str();
Alex Lightbb9b8a52016-06-29 09:26:44 -07001197 }
1198 if (!slot) continue;
1199 fname = find_item_given_name(images[i].img_name, product);
Elliott Hughes253c18d2015-03-18 22:47:09 -07001200 fastboot_buffer buf;
Elliott Hughes53ec4952016-05-11 12:39:27 -07001201 if (!load_buf(transport, fname.c_str(), &buf)) {
1202 if (images[i].is_optional) continue;
Alex Lightbb9b8a52016-06-29 09:26:44 -07001203 die("could not load '%s': %s\n", images[i].img_name, strerror(errno));
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001204 }
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001205
1206 auto flashall = [&](const std::string &partition) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001207 do_send_signature(fname.c_str());
David Pursell0b156632015-10-30 11:22:01 -07001208 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001209 fb_queue_erase(partition.c_str());
1210 }
1211 flash_buf(partition.c_str(), &buf);
1212 };
Alex Lightbb9b8a52016-06-29 09:26:44 -07001213 do_for_partitions(transport, images[i].part_name, slot, flashall, false);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001214 }
Daniel Rosenberg13454092016-06-27 19:43:11 -07001215
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001216 if (slot_override == "all") {
1217 set_active(transport, "a");
1218 } else {
1219 set_active(transport, slot_override);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001220 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001221}
1222
1223#define skip(n) do { argc -= (n); argv += (n); } while (0)
JP Abgrall2d13d142011-03-01 23:35:07 -08001224#define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001225
Elliott Hughes45be42e2015-09-02 20:15:48 -07001226static int do_bypass_unlock_command(int argc, char **argv)
Patrick Tjin51e8b032015-09-01 08:15:23 -07001227{
Patrick Tjin51e8b032015-09-01 08:15:23 -07001228 if (argc <= 2) return 0;
1229 skip(2);
1230
1231 /*
1232 * Process unlock_bootloader, we have to load the message file
1233 * and send that to the remote device.
1234 */
1235 require(1);
Elliott Hughes259ad4a2015-09-02 20:33:44 -07001236
1237 int64_t sz;
1238 void* data = load_file(*argv, &sz);
1239 if (data == nullptr) die("could not load '%s': %s", *argv, strerror(errno));
Patrick Tjin51e8b032015-09-01 08:15:23 -07001240 fb_queue_download("unlock_message", data, sz);
1241 fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
1242 skip(1);
1243 return 0;
1244}
1245
Elliott Hughes45be42e2015-09-02 20:15:48 -07001246static int do_oem_command(int argc, char **argv)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001247{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001248 char command[256];
1249 if (argc <= 1) return 0;
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001250
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001251 command[0] = 0;
1252 while(1) {
1253 strcat(command,*argv);
1254 skip(1);
1255 if(argc == 0) break;
1256 strcat(command," ");
1257 }
1258
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001259 fb_queue_command(command,"");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001260 return 0;
1261}
1262
Colin Crossf8387882012-05-24 17:18:41 -07001263static int64_t parse_num(const char *arg)
1264{
1265 char *endptr;
1266 unsigned long long num;
1267
1268 num = strtoull(arg, &endptr, 0);
1269 if (endptr == arg) {
1270 return -1;
1271 }
1272
1273 if (*endptr == 'k' || *endptr == 'K') {
1274 if (num >= (-1ULL) / 1024) {
1275 return -1;
1276 }
1277 num *= 1024LL;
1278 endptr++;
1279 } else if (*endptr == 'm' || *endptr == 'M') {
1280 if (num >= (-1ULL) / (1024 * 1024)) {
1281 return -1;
1282 }
1283 num *= 1024LL * 1024LL;
1284 endptr++;
1285 } else if (*endptr == 'g' || *endptr == 'G') {
1286 if (num >= (-1ULL) / (1024 * 1024 * 1024)) {
1287 return -1;
1288 }
1289 num *= 1024LL * 1024LL * 1024LL;
1290 endptr++;
1291 }
1292
1293 if (*endptr != '\0') {
1294 return -1;
1295 }
1296
1297 if (num > INT64_MAX) {
1298 return -1;
1299 }
1300
1301 return num;
1302}
1303
David Pursell0b156632015-10-30 11:22:01 -07001304static void fb_perform_format(Transport* transport,
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001305 const char* partition, int skip_if_not_supported,
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001306 const char* type_override, const char* size_override,
1307 const std::string& initial_dir) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001308 std::string partition_type, partition_size;
1309
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001310 struct fastboot_buffer buf;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001311 const char* errMsg = nullptr;
1312 const struct fs_generator* gen = nullptr;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001313 int fd;
1314
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001315 unsigned int limit = INT_MAX;
1316 if (target_sparse_limit > 0 && target_sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001317 limit = target_sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001318 }
1319 if (sparse_limit > 0 && sparse_limit < limit) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001320 limit = sparse_limit;
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001321 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001322
David Pursell0b156632015-10-30 11:22:01 -07001323 if (!fb_getvar(transport, std::string("partition-type:") + partition, &partition_type)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001324 errMsg = "Can't determine partition type.\n";
1325 goto failed;
1326 }
JP Abgrall7e859742014-05-06 15:14:15 -07001327 if (type_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001328 if (partition_type != type_override) {
1329 fprintf(stderr, "Warning: %s type is %s, but %s was requested for formatting.\n",
1330 partition, partition_type.c_str(), type_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001331 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001332 partition_type = type_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001333 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001334
David Pursell0b156632015-10-30 11:22:01 -07001335 if (!fb_getvar(transport, std::string("partition-size:") + partition, &partition_size)) {
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001336 errMsg = "Unable to get partition size\n";
1337 goto failed;
1338 }
JP Abgrall7e859742014-05-06 15:14:15 -07001339 if (size_override) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001340 if (partition_size != size_override) {
1341 fprintf(stderr, "Warning: %s size is %s, but %s was requested for formatting.\n",
1342 partition, partition_size.c_str(), size_override);
JP Abgrall7e859742014-05-06 15:14:15 -07001343 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001344 partition_size = size_override;
JP Abgrall7e859742014-05-06 15:14:15 -07001345 }
Elliott Hughesa2db2612015-11-12 07:28:39 -08001346 // Some bootloaders (angler, for example), send spurious leading whitespace.
1347 partition_size = android::base::Trim(partition_size);
1348 // Some bootloaders (hammerhead, for example) use implicit hex.
1349 // This code used to use strtol with base 16.
1350 if (!android::base::StartsWith(partition_size, "0x")) partition_size = "0x" + partition_size;
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001351
Elliott Hughes8ab9a322015-11-02 14:05:57 -08001352 gen = fs_get_generator(partition_type);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001353 if (!gen) {
1354 if (skip_if_not_supported) {
1355 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001356 fprintf(stderr, "File system type %s not supported.\n", partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001357 return;
1358 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001359 fprintf(stderr, "Formatting is not supported for file system with type '%s'.\n",
1360 partition_type.c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001361 return;
1362 }
1363
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001364 int64_t size;
1365 if (!android::base::ParseInt(partition_size.c_str(), &size)) {
1366 fprintf(stderr, "Couldn't parse partition size '%s'.\n", partition_size.c_str());
1367 return;
1368 }
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001369
1370 fd = fileno(tmpfile());
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001371 if (fs_generator_generate(gen, fd, size, initial_dir)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001372 fprintf(stderr, "Cannot generate image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001373 close(fd);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001374 return;
1375 }
1376
Elliott Hughes53ec4952016-05-11 12:39:27 -07001377 if (!load_buf_fd(transport, fd, &buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001378 fprintf(stderr, "Cannot read image: %s\n", strerror(errno));
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001379 close(fd);
1380 return;
1381 }
1382 flash_buf(partition, &buf);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001383 return;
1384
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001385failed:
1386 if (skip_if_not_supported) {
1387 fprintf(stderr, "Erase successful, but not automatically formatting.\n");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001388 if (errMsg) fprintf(stderr, "%s", errMsg);
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001389 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001390 fprintf(stderr, "FAILED (%s)\n", fb_get_error().c_str());
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -07001391}
1392
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001393int main(int argc, char **argv)
1394{
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001395 bool wants_wipe = false;
1396 bool wants_reboot = false;
1397 bool wants_reboot_bootloader = false;
Mitchell Wills31dce302016-09-26 10:26:21 -07001398 bool skip_reboot = false;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001399 bool wants_set_active = false;
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001400 bool skip_secondary = false;
Elliott Hughesfc797672015-04-07 20:12:50 -07001401 bool erase_first = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001402 bool set_fbe_marker = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001403 void *data;
Elliott Hughesfc797672015-04-07 20:12:50 -07001404 int64_t sz;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001405 int longindex;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001406 std::string slot_override;
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001407 std::string next_active;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001408
JP Abgrall7b8970c2013-03-07 17:06:41 -08001409 const struct option longopts[] = {
1410 {"base", required_argument, 0, 'b'},
1411 {"kernel_offset", required_argument, 0, 'k'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001412 {"kernel-offset", required_argument, 0, 'k'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001413 {"page_size", required_argument, 0, 'n'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001414 {"page-size", required_argument, 0, 'n'},
JP Abgrall7b8970c2013-03-07 17:06:41 -08001415 {"ramdisk_offset", required_argument, 0, 'r'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001416 {"ramdisk-offset", required_argument, 0, 'r'},
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001417 {"tags_offset", required_argument, 0, 't'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001418 {"tags-offset", required_argument, 0, 't'},
Elliott Hughes379646b2015-06-02 13:50:00 -07001419 {"help", no_argument, 0, 'h'},
1420 {"unbuffered", no_argument, 0, 0},
1421 {"version", no_argument, 0, 0},
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001422 {"slot", required_argument, 0, 0},
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001423 {"set_active", optional_argument, 0, 'a'},
Daniel Rosenberg7aa38bc2015-11-11 17:29:37 -08001424 {"set-active", optional_argument, 0, 'a'},
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001425 {"skip-secondary", no_argument, 0, 0},
Mitchell Wills31dce302016-09-26 10:26:21 -07001426 {"skip-reboot", no_argument, 0, 0},
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001427#if !defined(_WIN32)
1428 {"wipe-and-use-fbe", no_argument, 0, 0},
1429#endif
JP Abgrall7b8970c2013-03-07 17:06:41 -08001430 {0, 0, 0, 0}
1431 };
Colin Cross8879f982012-05-22 17:53:34 -07001432
1433 serial = getenv("ANDROID_SERIAL");
1434
1435 while (1) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001436 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 -07001437 if (c < 0) {
1438 break;
1439 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001440 /* Alphabetical cases */
Colin Cross8879f982012-05-22 17:53:34 -07001441 switch (c) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001442 case 'a':
1443 wants_set_active = true;
1444 if (optarg)
1445 next_active = optarg;
1446 break;
Colin Cross8879f982012-05-22 17:53:34 -07001447 case 'b':
1448 base_addr = strtoul(optarg, 0, 16);
1449 break;
Colin Cross8879f982012-05-22 17:53:34 -07001450 case 'c':
1451 cmdline = optarg;
1452 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001453 case 'h':
1454 usage();
1455 return 1;
Colin Cross8879f982012-05-22 17:53:34 -07001456 case 'i': {
Elliott Hughesfc797672015-04-07 20:12:50 -07001457 char *endptr = nullptr;
Colin Cross8879f982012-05-22 17:53:34 -07001458 unsigned long val;
1459
1460 val = strtoul(optarg, &endptr, 0);
1461 if (!endptr || *endptr != '\0' || (val & ~0xffff))
1462 die("invalid vendor id '%s'", optarg);
1463 vendor_id = (unsigned short)val;
1464 break;
1465 }
JP Abgrall7b8970c2013-03-07 17:06:41 -08001466 case 'k':
1467 kernel_offset = strtoul(optarg, 0, 16);
1468 break;
1469 case 'l':
1470 long_listing = 1;
1471 break;
1472 case 'n':
Elliott Hughesfc797672015-04-07 20:12:50 -07001473 page_size = (unsigned)strtoul(optarg, nullptr, 0);
JP Abgrall7b8970c2013-03-07 17:06:41 -08001474 if (!page_size) die("invalid page size");
1475 break;
1476 case 'p':
1477 product = optarg;
1478 break;
1479 case 'r':
1480 ramdisk_offset = strtoul(optarg, 0, 16);
1481 break;
Mohamad Ayyash29fd7b12014-01-14 18:27:25 -08001482 case 't':
1483 tags_offset = strtoul(optarg, 0, 16);
1484 break;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001485 case 's':
1486 serial = optarg;
1487 break;
1488 case 'S':
1489 sparse_limit = parse_num(optarg);
1490 if (sparse_limit < 0) {
1491 die("invalid sparse limit");
1492 }
1493 break;
1494 case 'u':
Elliott Hughesfc797672015-04-07 20:12:50 -07001495 erase_first = false;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001496 break;
1497 case 'w':
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001498 wants_wipe = true;
JP Abgrall7b8970c2013-03-07 17:06:41 -08001499 break;
Colin Cross8879f982012-05-22 17:53:34 -07001500 case '?':
1501 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001502 case 0:
1503 if (strcmp("unbuffered", longopts[longindex].name) == 0) {
Elliott Hughesfc797672015-04-07 20:12:50 -07001504 setvbuf(stdout, nullptr, _IONBF, 0);
1505 setvbuf(stderr, nullptr, _IONBF, 0);
Elliott Hughes379646b2015-06-02 13:50:00 -07001506 } else if (strcmp("version", longopts[longindex].name) == 0) {
1507 fprintf(stdout, "fastboot version %s\n", FASTBOOT_REVISION);
1508 return 0;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001509 } else if (strcmp("slot", longopts[longindex].name) == 0) {
1510 slot_override = std::string(optarg);
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001511 } else if (strcmp("skip-secondary", longopts[longindex].name) == 0 ) {
1512 skip_secondary = true;
Mitchell Wills31dce302016-09-26 10:26:21 -07001513 } else if (strcmp("skip-reboot", longopts[longindex].name) == 0 ) {
1514 skip_reboot = true;
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001515#if !defined(_WIN32)
1516 } else if (strcmp("wipe-and-use-fbe", longopts[longindex].name) == 0) {
1517 wants_wipe = true;
1518 set_fbe_marker = true;
1519#endif
1520 } else {
1521 fprintf(stderr, "Internal error in options processing for %s\n",
1522 longopts[longindex].name);
1523 return 1;
Florian Bäuerle27ded482014-11-24 11:29:34 +01001524 }
1525 break;
Colin Cross8879f982012-05-22 17:53:34 -07001526 default:
1527 abort();
1528 }
1529 }
1530
1531 argc -= optind;
1532 argv += optind;
1533
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001534 if (argc == 0 && !wants_wipe && !wants_set_active) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001535 usage();
Brian Carlstromeb31c0b2010-04-23 12:38:51 -07001536 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001537 }
1538
Colin Cross8fb6e062012-07-24 16:36:41 -07001539 if (argc > 0 && !strcmp(*argv, "devices")) {
Colin Cross8879f982012-05-22 17:53:34 -07001540 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001541 list_devices();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001542 return 0;
1543 }
1544
Colin Crossc7b75dc2012-08-29 18:17:06 -07001545 if (argc > 0 && !strcmp(*argv, "help")) {
1546 usage();
1547 return 0;
1548 }
1549
David Pursell0b156632015-10-30 11:22:01 -07001550 Transport* transport = open_device();
David Pursell2ec418a2016-01-20 08:32:08 -08001551 if (transport == nullptr) {
1552 return 1;
1553 }
1554
Daniel Rosenberg80919472016-06-30 19:25:31 -07001555 if (!supports_AB(transport) && supports_AB_obsolete(transport)) {
1556 fprintf(stderr, "Warning: Device A/B support is outdated. Bootloader update required.\n");
1557 }
1558 if (slot_override != "") slot_override = verify_slot(transport, slot_override);
1559 if (next_active != "") next_active = verify_slot(transport, next_active, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001560
1561 if (wants_set_active) {
1562 if (next_active == "") {
1563 if (slot_override == "") {
Daniel Rosenberg13454092016-06-27 19:43:11 -07001564 std::string current_slot;
1565 if (fb_getvar(transport, "current-slot", &current_slot)) {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001566 next_active = verify_slot(transport, current_slot, false);
Daniel Rosenberg13454092016-06-27 19:43:11 -07001567 } else {
1568 wants_set_active = false;
1569 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001570 } else {
Daniel Rosenberg80919472016-06-30 19:25:31 -07001571 next_active = verify_slot(transport, slot_override, false);
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001572 }
1573 }
1574 }
Elliott Hughes31dbed72009-10-07 15:38:53 -07001575
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001576 while (argc > 0) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001577 if (!strcmp(*argv, "getvar")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001578 require(2);
1579 fb_queue_display(argv[1], argv[1]);
1580 skip(2);
1581 } else if(!strcmp(*argv, "erase")) {
1582 require(2);
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001583
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001584 auto erase = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001585 std::string partition_type;
1586 if (fb_getvar(transport, std::string("partition-type:") + argv[1], &partition_type) &&
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001587 fs_get_generator(partition_type) != nullptr) {
1588 fprintf(stderr, "******** Did you mean to fastboot format this %s partition?\n",
1589 partition_type.c_str());
1590 }
Ken Sumrall5ee5d382012-09-29 14:46:25 -07001591
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001592 fb_queue_erase(partition.c_str());
1593 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001594 do_for_partitions(transport, argv[1], slot_override, erase, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001595 skip(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001596 } else if(!strncmp(*argv, "format", strlen("format"))) {
1597 char *overrides;
Elliott Hughesfc797672015-04-07 20:12:50 -07001598 char *type_override = nullptr;
1599 char *size_override = nullptr;
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001600 require(2);
JP Abgrall7e859742014-05-06 15:14:15 -07001601 /*
1602 * Parsing for: "format[:[type][:[size]]]"
1603 * Some valid things:
1604 * - select ontly the size, and leave default fs type:
1605 * format::0x4000000 userdata
1606 * - default fs type and size:
1607 * format userdata
1608 * format:: userdata
1609 */
1610 overrides = strchr(*argv, ':');
1611 if (overrides) {
1612 overrides++;
1613 size_override = strchr(overrides, ':');
1614 if (size_override) {
1615 size_override[0] = '\0';
1616 size_override++;
1617 }
1618 type_override = overrides;
1619 }
Elliott Hughesfc797672015-04-07 20:12:50 -07001620 if (type_override && !type_override[0]) type_override = nullptr;
1621 if (size_override && !size_override[0]) size_override = nullptr;
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001622
1623 auto format = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001624 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001625 fb_queue_erase(partition.c_str());
1626 }
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001627 fb_perform_format(transport, partition.c_str(), 0,
1628 type_override, size_override, "");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001629 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001630 do_for_partitions(transport, argv[1], slot_override, format, true);
Anatol Pomazauc8ba5362011-12-15 17:50:18 -08001631 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001632 } else if(!strcmp(*argv, "signature")) {
1633 require(2);
1634 data = load_file(argv[1], &sz);
Elliott Hughesfc797672015-04-07 20:12:50 -07001635 if (data == nullptr) die("could not load '%s': %s", argv[1], strerror(errno));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001636 if (sz != 256) die("signature must be 256 bytes");
1637 fb_queue_download("signature", data, sz);
1638 fb_queue_command("signature", "installing signature");
1639 skip(2);
1640 } else if(!strcmp(*argv, "reboot")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001641 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001642 skip(1);
Elliott Hughesca85df02015-02-25 10:02:00 -08001643 if (argc > 0) {
1644 if (!strcmp(*argv, "bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001645 wants_reboot = false;
1646 wants_reboot_bootloader = true;
Elliott Hughesca85df02015-02-25 10:02:00 -08001647 skip(1);
1648 }
1649 }
1650 require(0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001651 } else if(!strcmp(*argv, "reboot-bootloader")) {
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001652 wants_reboot_bootloader = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001653 skip(1);
1654 } else if (!strcmp(*argv, "continue")) {
1655 fb_queue_command("continue", "resuming boot");
1656 skip(1);
1657 } else if(!strcmp(*argv, "boot")) {
1658 char *kname = 0;
1659 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001660 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001661 skip(1);
1662 if (argc > 0) {
1663 kname = argv[0];
1664 skip(1);
1665 }
1666 if (argc > 0) {
1667 rname = argv[0];
1668 skip(1);
1669 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001670 if (argc > 0) {
1671 sname = argv[0];
1672 skip(1);
1673 }
1674 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001675 if (data == 0) return 1;
1676 fb_queue_download("boot.img", data, sz);
1677 fb_queue_command("boot", "booting");
1678 } else if(!strcmp(*argv, "flash")) {
Elliott Hughes2810d002016-04-25 14:31:18 -07001679 char* pname = argv[1];
1680 std::string fname;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001681 require(2);
1682 if (argc > 2) {
1683 fname = argv[2];
1684 skip(3);
1685 } else {
1686 fname = find_item(pname, product);
1687 skip(2);
1688 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001689 if (fname.empty()) die("cannot determine image filename for '%s'", pname);
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001690
1691 auto flash = [&](const std::string &partition) {
David Pursell0b156632015-10-30 11:22:01 -07001692 if (erase_first && needs_erase(transport, partition.c_str())) {
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001693 fb_queue_erase(partition.c_str());
1694 }
Elliott Hughes2810d002016-04-25 14:31:18 -07001695 do_flash(transport, partition.c_str(), fname.c_str());
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001696 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001697 do_for_partitions(transport, pname, slot_override, flash, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001698 } else if(!strcmp(*argv, "flash:raw")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001699 char *kname = argv[2];
1700 char *rname = 0;
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001701 char *sname = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001702 require(3);
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001703 skip(3);
1704 if (argc > 0) {
1705 rname = argv[0];
1706 skip(1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001707 }
Jeremy Compostellaa42adff2014-07-17 17:17:54 +02001708 if (argc > 0) {
1709 sname = argv[0];
1710 skip(1);
1711 }
1712 data = load_bootable_image(kname, rname, sname, &sz, cmdline);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001713 if (data == 0) die("cannot load bootable image");
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -07001714 auto flashraw = [&](const std::string &partition) {
1715 fb_queue_flash(partition.c_str(), data, sz);
1716 };
Daniel Rosenberg80919472016-06-30 19:25:31 -07001717 do_for_partitions(transport, argv[1], slot_override, flashraw, true);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001718 } else if(!strcmp(*argv, "flashall")) {
1719 skip(1);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001720 if (slot_override == "all") {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001721 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1722 do_flashall(transport, slot_override, erase_first, true);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001723 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001724 do_flashall(transport, slot_override, erase_first, skip_secondary);
Alex Lightbb9b8a52016-06-29 09:26:44 -07001725 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001726 wants_reboot = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001727 } else if(!strcmp(*argv, "update")) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001728 bool slot_all = (slot_override == "all");
1729 if (slot_all) {
1730 fprintf(stderr, "Warning: slot set to 'all'. Secondary slots will not be flashed.\n");
1731 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001732 if (argc > 1) {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001733 do_update(transport, argv[1], slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001734 skip(2);
1735 } else {
Daniel Rosenberg92b44762016-07-13 20:03:25 -07001736 do_update(transport, "update.zip", slot_override, erase_first, skip_secondary || slot_all);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001737 skip(1);
1738 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001739 wants_reboot = true;
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001740 } else if(!strcmp(*argv, "set_active")) {
1741 require(2);
Daniel Rosenberg80919472016-06-30 19:25:31 -07001742 std::string slot = verify_slot(transport, std::string(argv[1]), false);
Daniel Rosenberg9b432052015-11-25 15:17:10 -08001743 fb_set_active(slot.c_str());
1744 skip(2);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001745 } else if(!strcmp(*argv, "oem")) {
1746 argc = do_oem_command(argc, argv);
Patrick Tjin51e8b032015-09-01 08:15:23 -07001747 } else if(!strcmp(*argv, "flashing")) {
1748 if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
1749 !strcmp(*(argv+1), "lock") ||
1750 !strcmp(*(argv+1), "unlock_critical") ||
1751 !strcmp(*(argv+1), "lock_critical") ||
1752 !strcmp(*(argv+1), "get_unlock_ability") ||
1753 !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
1754 !strcmp(*(argv+1), "lock_bootloader"))) {
1755 argc = do_oem_command(argc, argv);
1756 } else
1757 if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
1758 argc = do_bypass_unlock_command(argc, argv);
Badhri Jagan Sridharanbf110952015-05-15 16:43:47 -07001759 } else {
1760 usage();
1761 return 1;
1762 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001763 } else {
1764 usage();
Tsu Chiang Chuangee520552011-02-25 18:38:53 -08001765 return 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001766 }
1767 }
1768
1769 if (wants_wipe) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001770 fprintf(stderr, "wiping userdata...\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001771 fb_queue_erase("userdata");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001772 if (set_fbe_marker) {
1773 fprintf(stderr, "setting FBE marker...\n");
1774 std::string initial_userdata_dir = create_fbemarker_tmpdir();
1775 if (initial_userdata_dir.empty()) {
1776 return 1;
1777 }
1778 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, initial_userdata_dir);
1779 delete_fbemarker_tmpdir(initial_userdata_dir);
1780 } else {
1781 fb_perform_format(transport, "userdata", 1, nullptr, nullptr, "");
1782 }
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001783
1784 std::string cache_type;
David Pursell0b156632015-10-30 11:22:01 -07001785 if (fb_getvar(transport, "partition-type:cache", &cache_type) && !cache_type.empty()) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001786 fprintf(stderr, "wiping cache...\n");
1787 fb_queue_erase("cache");
Paul Crowley8f7f56e2015-11-27 09:29:37 +00001788 fb_perform_format(transport, "cache", 1, nullptr, nullptr, "");
Elliott Hughes2fd45a92015-10-30 11:49:47 -07001789 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001790 }
Daniel Rosenberg0d088562015-11-11 16:15:30 -08001791 if (wants_set_active) {
1792 fb_set_active(next_active.c_str());
1793 }
Mitchell Wills31dce302016-09-26 10:26:21 -07001794 if (wants_reboot && !skip_reboot) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001795 fb_queue_reboot();
Mark Wachslerec25e7b2014-06-24 11:04:54 -04001796 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001797 } else if (wants_reboot_bootloader) {
1798 fb_queue_command("reboot-bootloader", "rebooting into bootloader");
Mark Wachsler157b0012013-10-02 09:35:38 -04001799 fb_queue_wait_for_disconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001800 }
1801
David Pursell0b156632015-10-30 11:22:01 -07001802 return fb_execute_queue(transport) ? EXIT_FAILURE : EXIT_SUCCESS;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001803}